不知道算不算是存储引擎,当时是因为要处理很多种类型数据的读写,所以觉得底层的存储细节太繁琐了,干脆自己写了一个文件存储接口屏蔽这些繁琐的细节,而可以更专心地考虑上层的逻辑设计。因为年代久远,代码风格不太好,略显稚嫩了,请大家多多包涵。
- #ifndef STORE_ENGINE_H_INCLUDED
- #define STORE_ENGINE_H_INCLUDED
- typedef void * database_handle;
- typedef void * database_iterator;
- database_handle database_open(const char *filepath);
-
- int database_register_type(database_handle, const char *tpname);
- database_iterator database_iterator_init (database_handle, const char *tpname);
- database_iterator database_iterator_head (database_iterator);
- database_iterator database_iterator_next (database_iterator);
- database_iterator database_iterator_prev (database_iterator);
- database_iterator database_iterator_tail (database_iterator);
- #define database_write_ptr(handle, tpname, ptr) \
- database_write(handle, tpname, ptr, sizeof *(ptr))
- int database_write (database_handle, const char *tpname, void *ptr, int size);
- int database_read (database_handle, database_iterator , void *ptr, int *size);
- int database_delete (database_handle, database_iterator);
- int database_close(database_handle);
- void show_handle(FILE *fp, database_handle handle);
- void show_database(const char *filepath);
- #endif // STORE_ENGINE_H_INCLUDED
- #include "c.h"
- #include "list.h"
- #include "memmgr2.h"
- #include "store_engine.h"
- #define DISABLE_MSG
- #define DEBUG1
- #include "debug.h"
- #define PAGE_SIZE_MASK 12
- #define PAGE_SIZE (1 << PAGE_SIZE_MASK)
- #define MIN_BLKSZ_MASK 5
- #define MIN_BLOCK_SIZE (1 << MIN_BLKSZ_MASK)
- #define MAX_BLOCK_SIZE PAGE_SIZE
- #define MAX_NAME_LEN 32
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- struct type_object {
- char tpname[MAX_NAME_LEN];
- };
- typedef struct type_object tp_obj_t;
- #define tpobj_size (sizeof(tp_obj_t))
- struct page_object {
- int szid;
- int pgid;
- };
- typedef struct page_object pg_obj_t;
- #define pgobj_size (sizeof(pg_obj_t))
- struct data_object {
- int tpid;
- int size;
- };
- typedef struct data_object dat_obj_t;
- #define datobj_size (sizeof(dat_obj_t))
- #define TPOBJ_ARRLEN 8
- struct engine_object {
- int pgcnt;
- struct type_object tpobj[TPOBJ_ARRLEN];
- };
- typedef struct engine_object eng_obj_t;
- #define engobj_size (sizeof(eng_obj_t))
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- #define FREOBJ_ARRLEN 8
- struct engine{
- struct engine_object object;
- FILE *fpos;
- int tpcnt;
- int datcnt;
- struct list_head pghead;
- struct list_head freelist[FREOBJ_ARRLEN];
- struct type *tp[TPOBJ_ARRLEN];
- };
- typedef struct engine engine_t;
- struct type {
- struct type_object object;
- struct engine *engine;
- struct list_head dathead;
- char *tpname;
- int tpid;
- int datcnt;
- };
- typedef struct type type_t;
- struct page {
- struct page_object object;
- struct engine *engine;
- struct list_head list;
- struct data **data_array;
- int blksize;
- int blkcnt;
- int datcnt;
- };
- typedef struct page page_t;
- struct data {
- struct data_object object;
- struct type *type;
- struct page *page;
- struct list_head list;
- long seek;
- int index;
- };
- typedef struct data data_t;
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- static inline int size_to_index(int size)
- {
- static char array[] = {
- 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
- 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
- 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
- 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
- 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
- };
- int k = ((size + MIN_BLOCK_SIZE - 1) >> MIN_BLKSZ_MASK) - 1;
- return *(array + k);
- }
- static inline int index_to_size(int index)
- {
- return 32 << index;
- }
- static inline int page_blk_cnt(int szid)
- {
- return ((PAGE_SIZE >> MIN_BLKSZ_MASK) >> szid) - 1;
- }
- static int page_blk_size(int szid)
- {
- return index_to_size(szid);
- }
- static inline int blk_offset(int szid, int index)
- {
- return sizeof(pg_obj_t) + page_blk_size(szid) * index;
- }
- static inline int dat_offset(int szid, int index)
- {
- return blk_offset(szid, index) + sizeof(dat_obj_t);
- }
- static inline int phy_page_start(int pgid)
- {
- return pgid << PAGE_SIZE_MASK;
- }
- static inline char *mem_blk_start(char *pgbuf, int szid, int index)
- {
- return pgbuf + blk_offset(szid, index);
- }
- static inline char *mem_dat_start(char *pgbuf, int szid, int index)
- {
- return pgbuf + dat_offset(szid, index);
- }
- static inline long phy_dat_start(int pgid, int szid, int index)
- {
- return phy_page_start(pgid) + dat_offset(szid, index);
- }
- static inline long phy_blk_start(int pgid, int szid, int index)
- {
- return phy_page_start(pgid) + blk_offset(szid, index);
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////
- #define fcntl_read_pointer(fp, seek, ptr) \
- fcntl_read(fp, seek, ptr, sizeof *(ptr))
- #define fcntl_write_pointer(fp, seek, ptr) \
- fcntl_write(fp, seek, ptr, sizeof *(ptr))
-
- static void fcntl_read(FILE *fp, long seek, void *ptr, int size)
- {
- rewind(fp);
- Bzero(ptr, size);
- fseek(fp, seek, SEEK_SET);
- fread(ptr, size, 1, fp);
- }
- static void fcntl_write(FILE *fp, long seek, void *ptr, int size)
- {
- rewind(fp);
- fseek(fp, seek, SEEK_SET);
- fwrite(ptr, size, 1, fp);
- }
- static FILE * file_create(const char *filepath)
- {
- return_val_if_fail(filepath != NULL,
- NULL, "Filepath null");
- FILE *fp = fopen(filepath, "wb+");
- return_val_if_fail(fp != NULL, NULL,
- "Create file failed: %s", filepath);
- return fp;
- }
- static struct engine * engine_new(void)
- {
- struct engine *engine = NEWP(engine);
- return_val_if_fail(engine != NULL, NULL,
- "Engine alloc failed");
- LIST_HEAD_INIT(engine, pghead);
- int i;
- for (i = 0; i < FREOBJ_ARRLEN; i++)
- init_list_head(engine->freelist + i);
- return engine;
- }
- static struct engine * database_create(const char *filepath)
- {
- FILE *fpos = file_create(filepath);
- return_val_if_fail(fpos != NULL, NULL,
- "Create file failed: %s", filepath);
- struct engine *engine = engine_new();
- return_val_if_fail(engine != NULL, NULL,
- "Engine alloc failed");
- engine->fpos = fpos;
- return engine;
- }
- static struct type *type_new(void)
- {
- struct type *tp = NEWP(tp);
- return_val_if_fail(tp != NULL,
- NULL, "Type alloc failed");
- LIST_HEAD_INIT(tp, dathead);
- return tp;
- }
- static int database_type_initialize(struct engine *engine)
- {
- return_val_if_fail(engine != NULL, -1,
- "Engine pointer null");
- int i;
- struct type_object *tpobj = engine->object.tpobj;
- for (i = 0; i < TPOBJ_ARRLEN; i++) {
- if (strlen(tpobj[i].tpname) <= 0) break;
- struct type *tp = type_new();
- return_val_if_fail(tp != NULL, -1, "Type alloc failed");
- tp->tpid = engine->tpcnt;
- engine->tp[tp->tpid] = tp;
- engine->tpcnt++;
- tp->engine = engine;
- tp->tpname = tp->object.tpname;
- strcpy(tp->tpname, tpobj[i].tpname);
- }
- return 0;
- }
- static struct data *data_new(void)
- {
- struct data * dat = NEWP(dat);
- return_val_if_fail(dat != NULL,
- NULL, "Data alloc failed");
- LIST_HEAD_INIT(dat, list);
- return dat;
- }
- static int database_data_initialize(struct page *page, char *pgbuf)
- {
- return_val_if_fail(page != NULL, -1);
- return_val_if_fail(pgbuf != NULL, -1);
- int i;
- struct engine * engine = page->engine;
- int szid = page->object.szid;
- for (i = 0; i < page->blkcnt; i++) {
- struct data_object * datobj =
- (dat_obj_t *)mem_blk_start(
- pgbuf, szid, i
- );
- struct data * data = data_new();
- return_val_if_fail(data != NULL,
- -1, "Data alloc failed");
- data->object.tpid = datobj->tpid;
- data->object.size = datobj->size;
- data->page = page;
- data->index = i;
- data->seek = phy_dat_start(
- page->object.pgid, page->object.szid, i
- );
- if (datobj->size == 0) {
- list_add_tail(&data->list, engine->freelist + szid);
- continue;
- }
- int tpid = datobj->tpid;
- return_val_if_fail(tpid >=0 && tpid < TPOBJ_ARRLEN,
- -1, "type id invalid: %d", tpid);
- data->type = engine->tp[tpid];
- list_add_tail(&data->list, &data->type->dathead);
- data->type->datcnt++;
- page->datcnt++;
- engine->datcnt++;
- page->data_array[i] = data;
- }
- return 0;
- }
- static struct page * page_new(int szidx)
- {
- int blkcnt = page_blk_cnt(szidx);
- int alloc_size = blkcnt * sizeof(struct data *);
- alloc_size += sizeof(struct page);
- struct page *page = NEW(alloc_size);
- return_val_if_fail(page != NULL,
- NULL, "Alloc page failed");
- page->data_array = (struct data**)(page + 1);
- return page;
- }
- static int database_page_initialize(struct engine *engine)
- {
- return_val_if_fail(engine != NULL, -1,
- "Engine pointer null");
- int i;
- for (i = 1; i <= engine->object.pgcnt; i++) {
- char pgbuf[PAGE_SIZE];
- fcntl_read(engine->fpos, phy_page_start(i), pgbuf, PAGE_SIZE);
- struct page_object *pgobj = (pg_obj_t *)pgbuf;
- int szid = pgobj->szid;
- int pgid = pgobj->pgid;
- return_val_if_fail(pgid == i, -1,
- "Invalid param: not match");
- int blkcnt = page_blk_cnt(szid);
- struct page * page = page_new(szid);
- return_val_if_fail(page != NULL, -1,
- "Page alloc failed");
- page->engine = engine;
- page->blkcnt = blkcnt;
- page->object.szid = pgobj->szid;
- page->object.pgid = pgobj->pgid;
- page->blksize = index_to_size(szid);
- list_add_tail(&page->list, &engine->pghead);
- return_val_if_fail(page->object.pgid == i,
- -1, "Invalid Param");
- database_data_initialize(page, pgbuf);
- // Show_Value(page->datcnt, d);
- }
-
- return 0;
- }
- static struct engine * database_initialize(FILE *fp)
- {
- return_val_if_fail(fp != NULL,
- NULL, "File pointer null");
- struct engine * engine = engine_new();
- return_val_if_fail(engine != NULL,
- NULL, "Engine alloc failed");
- engine->fpos = fp;
- fcntl_read_pointer(fp, 0, &engine->object);
- int ret = database_type_initialize(engine);
- debug_if_fail(ret != -1) {
- FREE(engine);
- return NULL;
- }
- database_page_initialize(engine);
- return engine;
- }
- static struct engine * _database_open(const char *filepath)
- {
- FILE *fp = fopen(filepath, "rb+");
- if (fp == NULL)
- return database_create(filepath);
- return database_initialize(fp);
- }
- static int tpname_to_tpid(struct engine *engine, const char *tpname)
- {
- return_val_if_fail(engine != NULL, -1);
- return_val_if_fail(tpname != NULL, -1);
- return_val_if_fail(strlen(tpname) < MAX_NAME_LEN,
- -1, "Type name too long: %s", tpname);
- int i;
- for (i = 0; i < engine->tpcnt; i++) {
- return_val_if_fail(engine->tp[i] != NULL, -1);
- if (strcmp(engine->tp[i]->tpname, tpname) == 0)
- return i;
- }
- return -1;
- }
- static struct type * tpname_to_tpptr(struct engine *engine, const char *tpname)
- {
- return_val_if_fail(engine != NULL, NULL);
- return_val_if_fail(tpname != NULL, NULL);
- return_val_if_fail(strlen(tpname) < MAX_NAME_LEN,
- NULL, "Type name too long: %s", tpname);
- int tpid = tpname_to_tpid(engine, tpname);
- return tpid != -1 ? engine->tp[tpid] : NULL;
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////
- static inline struct data * list_entry_data(struct list_head *list)
- {
- return list ? list_entry(list, struct data *, list) : NULL;
- }
- static inline struct page * list_entry_page(struct list_head *list)
- {
- return list ? list_entry(list, struct page *, list) : NULL;
- }
- static inline struct data * entry_data_head(struct list_head *head)
- {
- if (head == NULL || list_empty_careful(head)) return NULL;
- return list_entry_data(head->next);
- }
- static inline struct data * entry_data_tail(struct list_head *head)
- {
- if (head == NULL || list_empty_careful(head)) return NULL;
- return list_entry_data(head->prev);
- }
- static inline struct page * entry_page_tail(struct list_head *head)
- {
- if (head == NULL || list_empty_careful(head)) return NULL;
- return list_entry_page(head->prev);
- }
- static inline struct page *entry_page_head(struct list_head *head)
- {
- if (head == NULL || list_empty_careful(head)) return NULL;
- return list_entry_page(head->next);
- }
- static inline struct data *_entry_data_next(struct list_head *list)
- {
- return list_entry_data(list->next);
- }
- static inline struct data *entry_data_next(struct data *data)
- {
- return _entry_data_next(&data->list);
- }
- static inline struct data *_entry_data_prev(struct list_head *list)
- {
- return list_entry_data(list->prev);
- }
- static inline struct data *entry_data_prev(struct data *data)
- {
- return _entry_data_prev(&data->list);
- }
- static inline struct page *_entry_page_next(struct list_head *list)
- {
- return list_entry_page(list->next);
- }
- static inline struct page *entry_page_next(struct page *page)
- {
- return _entry_page_next(&page->list);
- }
- static inline struct page *_entry_page_prev(struct list_head *list)
- {
- return list_entry_page(list->prev);
- }
- static inline struct page *entry_page_prev(struct page *page)
- {
- return _entry_page_prev(&page->list);
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////
- struct data * _database_iterator_init(struct engine * engine, const char *tpname)
- {
- int tpid = tpname_to_tpid(engine, tpname);
- if (tpid == -1) return NULL;
- return entry_data_head(&engine->tp[tpid]->dathead);
- }
- struct data * _database_iterator_head(struct data *iter)
- {
- return_val_if_fail(iter != NULL, NULL, "Param iter null");
- return entry_data_head(&iter->type->dathead);
- }
- struct data * _database_iterator_next(struct data *iter)
- {
- return_val_if_fail(iter != NULL, NULL, "Param iter null");
- return_val_if_fail(iter->type != NULL, NULL);
- struct list_head *head = &iter->type->dathead;
- if (iter->list.next == head) return NULL;
- return entry_data_next(iter);
- }
- struct data *_database_iterator_prev(struct data *iter)
- {
- return_val_if_fail(iter != NULL, NULL, "Param iter null");
- return_val_if_fail(iter->type != NULL, NULL);
- struct list_head *head = &iter->type->dathead;
- if (iter->list.prev == head) return NULL;
- return entry_data_prev(iter);
- }
- struct data * _database_iterator_tail(struct data *iter)
- {
- return_val_if_fail(iter != NULL, NULL, "Param iter null");
- return entry_data_tail(&iter->type->dathead);
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////
- static int _database_register_type(struct engine *engine, const char *tpname)
- {
- return_val_if_fail(strlen(tpname) < MAX_NAME_LEN,
- -1, "Type name too long: %s", tpname);
-
- /*
- * return 0 means succedd even exist same type
- */
- int tpid = tpname_to_tpid(engine, tpname);
- if (tpid != -1) return 0;
-
- return_val_if_fail(engine->tpcnt < TPOBJ_ARRLEN,
- -1, "Type count reach upper limit");
-
- tpid = engine->tpcnt;
- engine->tpcnt++;
- struct type *tp = type_new();
- return_val_if_fail(tp != NULL, -1,
- "Type alloc failed");
- tp->tpid = tpid;
- tp->engine = engine;
- strcpy(tp->object.tpname, tpname);
- tp->tpname = tp->object.tpname;
- engine->tp[tpid] = tp;
- strcpy(engine->object.tpobj[tpid].tpname, tpname);
- fcntl_write_pointer(engine->fpos, 0, &engine->object);
- return 0;
- }
- static struct data * _get_free_space(struct engine *engine, int szidx)
- {
- struct list_head * head = engine->freelist + szidx;
- struct data *dat = entry_data_head(head);
- if (dat == NULL) return dat;
- list_del_init(&dat->list);
- return dat;
- }
- static int database_add_new_page(struct engine *engine, int szid)
- {
- return_val_if_fail(engine != NULL,
- -1, "Engine null pointer");
- return_val_if_fail(szid >= 0 &&
- szid < FREOBJ_ARRLEN, -1);
- struct page *page = page_new(szid);
- return_val_if_fail(page != NULL,
- -1, "Alloc page failed");
- list_add_tail(&page->list, &engine->pghead);
- engine->object.pgcnt++;
- page->object.szid = szid;
- page->object.pgid = engine->object.pgcnt;
- page->engine = engine;
- page->blksize = index_to_size(szid);
- page->blkcnt = page_blk_cnt(szid);
- int i;
- for (i = 0; i < page->blkcnt; i++) {
- struct data *data = data_new();
- page->data_array[i] = data;
- page->data_array[i]->page = page;
- page->data_array[i]->index = i;
- int pgid = page->object.pgid;
- int szid = page->object.szid;
- page->data_array[i]->seek = phy_dat_start(pgid, szid, i);
- list_add_tail(&data->list, engine->freelist + szid);
- }
- long pgseek = phy_page_start(page->object.pgid);
- fcntl_write_pointer(engine->fpos, pgseek, &page->object);
- fcntl_write_pointer(engine->fpos, 0, &engine->object);
- return 0;
- }
- static struct data * get_free_space(struct engine *engine, int totalsize)
- {
- return_val_if_fail(engine != NULL,
- NULL, "Engine null pointer");
- return_val_if_fail(totalsize > 0, NULL,
- "Invalid totalsize: %d", totalsize);
- int szidx = size_to_index(totalsize);
- return_val_if_fail(szidx >= 0 &&
- szidx <FREOBJ_ARRLEN, NULL);
- struct data *dat = _get_free_space(engine, szidx);
- if (dat != NULL) return dat;
- database_add_new_page(engine, szidx);
- dat = _get_free_space(engine, szidx);
- return_val_if_fail(dat != NULL, NULL,
- "Get free space failed");
- return dat;
- }
- static int _database_write(struct engine *engine, const char *tpname, void *ptr, int size)
- {
- struct type *tp = tpname_to_tpptr(engine, tpname);
- return_val_if_fail(tp != NULL,
- -1, "Type not exist: %s", tpname);
- int totalsize = size + datobj_size;
- struct data * data = get_free_space(engine, totalsize);
- return_val_if_fail(data != NULL, -1,
- "Get free space failed: %d", totalsize);
- data->type = tp;
- list_add_tail(&data->list, &tp->dathead);
- data->type->datcnt++;
- data->page->datcnt++;
- engine->datcnt++;
-
- data->object.tpid = tp->tpid;
- data->object.size = size;
- long blkseek = data->seek - sizeof(dat_obj_t);
- fcntl_write_pointer(engine->fpos, blkseek, &data->object);
- fcntl_write(engine->fpos, data->seek, ptr, size);
- return 0;
- }
- static int _database_read(struct engine *engine, struct data * dat, void *ptr, int *psize)
- {
- int realsize = dat->object.size;
- if (realsize > *psize) {
- *psize = realsize;
- return_val_if_fail(realsize <= *psize,
- -1, "Buffer pointer size %d < %d",
- *psize, realsize);
- assert(0);
- }
- fcntl_read(engine->fpos, dat->seek, ptr, realsize);
- return 0;
- }
- static int _database_delete(struct engine *engine, struct data *dat)
- {
- dat->object.tpid = 0;
- dat->object.size = 0;
-
- long blkseek = dat->seek - datobj_size;
- fcntl_write_pointer(engine->fpos, blkseek, &dat->object);
-
- list_del_init(&dat->list);
- dat->type->datcnt--;
- dat->page->datcnt--;
- engine->datcnt--;
-
- dat->type = NULL;
-
- int szid = dat->page->object.szid;
- struct list_head *head = engine->freelist + szid;
- list_add_tail(&dat->list, head);
-
- return 0;
- }
- static int _database_close(struct engine *engine)
- {
- fcntl_write_pointer(engine->fpos, 0, &engine->object);
- fclose(engine->fpos);
- return 0;
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////
- database_handle database_open(const char *filepath)
- {
- return_val_if_fail(filepath != NULL, NULL, "Filepath Null");
- atexit(memory_leaking_check);
- return (database_handle)_database_open(filepath);
- }
- int database_close(database_handle handle)
- {
- return_val_if_fail(handle != NULL, -1, "Database handle null");
- return _database_close((engine_t *)handle);
- }
- int database_register_type(database_handle handle, const char *tpname)
- {
- return_val_if_fail(handle != NULL, -1, "Database handle null");
- return_val_if_fail(tpname != NULL, -1, "Typename null");
- int ret = _database_register_type((engine_t *)handle, tpname);
- msg(stdout, "\ndatabase register type: %s %s!!\n",
- tpname, ret == 0 ? "success" : "failed");
- return ret;
- }
- database_iterator database_iterator_init(database_handle handle, const char *tpname)
- {
- return_val_if_fail(handle != NULL, NULL, "Database handle null");
- return_val_if_fail(tpname != NULL, NULL, "Typename null");
- return (database_iterator)_database_iterator_init((engine_t *)handle, tpname);
- }
- database_iterator database_iterator_head(database_iterator iterator)
- {
- return_val_if_fail(iterator != NULL, NULL, "Database iterator null");
- return (database_iterator)_database_iterator_head((data_t *)iterator);
- }
- database_iterator database_iterator_next(database_iterator iterator)
- {
- return_val_if_fail(iterator != NULL, NULL, "Database iterator null");
- return (database_iterator)_database_iterator_next((data_t *)iterator);
- }
- database_iterator database_iterator_prev(database_iterator iterator)
- {
- return_val_if_fail(iterator != NULL, NULL, "Database iterator null");
- return (database_iterator)_database_iterator_prev((data_t *)iterator);
- }
- database_iterator database_iterator_tail(database_iterator iterator)
- {
- return_val_if_fail(iterator != NULL, NULL, "Database iterator null");
- return (database_iterator)_database_iterator_tail((data_t *)iterator);
- }
- int database_write(database_handle handle, const char *tpname, void *ptr, int size)
- {
- return_val_if_fail(handle != NULL, -1, "Database handle null");
- return_val_if_fail(tpname != NULL, -1, "Typename null");
- return_val_if_fail(ptr != NULL, -1, "Buffer pointer null");
- return_val_if_fail(size > 0, -1, "Buffer size invalid: %d", size);
- return _database_write((engine_t *)handle, tpname, ptr, size);
- }
- int database_read(database_handle handle, database_iterator iterator, void *ptr, int *psize)
- {
- return_val_if_fail(handle != NULL, -1, "Database handle null");
- return_val_if_fail(iterator != NULL, -1, "Database iterator null");
- return_val_if_fail(ptr != NULL, -1, "Buffer pointer null");
- return_val_if_fail(psize != NULL && *psize > 0, -1, "Buffer size invalid: %d", *psize);
- return _database_read((engine_t *)handle, (data_t *)iterator, ptr, psize);
- }
- int database_delete(database_handle handle, database_iterator iterator)
- {
- return_val_if_fail(handle != NULL, -1, "Database handle null");
- return_val_if_fail(iterator != NULL, -1, "Database iterator null");
- return _database_delete((engine_t *)handle, (data_t *)iterator);
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////
- static void show_data(FILE *fp, struct engine *engine)
- {
- return_if_fail(fp != NULL);
- return_if_fail(engine != NULL);
- struct page *page;
- int i;
- list_for_each_entry(page, &engine->pghead, list){
- for (i = 0; i < page->blkcnt; i++) {
- struct data *data = page->data_array[i];
- if (data != NULL) {
- msg(fp, "\n\nshow data: %p", data);
- msg(fp, "\ntype id: %d", data->object.tpid);
- msg(fp, "\ndata size: %d", data->object.size);
- msg(fp, "\ntype pointer: %p", data->type);
- msg(fp, "\npage pointer: %p", data->page);
- msg(fp, "\ndata seek: %ld", data->seek);
- }
- }
- }
- msg(fp, "\n");
- }
- static void show_type(FILE *fp, struct engine *engine)
- {
- return_if_fail(fp != NULL);
- return_if_fail(engine != NULL);
- int i;
- for (i = 0; i < TPOBJ_ARRLEN; i++) {
- if (engine->tp[i] == NULL) break;
- struct type *type = engine->tp[i];
- msg(fp, "\n\nshow type %d: %p", i, type);
- msg(fp, "\ntype name: %s", type->object.tpname);
- return_if_fail(type->tpname == type->object.tpname);
- msg(fp, "\nengine: %p", type->engine);
- msg(fp, "\ntype id: %d", type->tpid);
- msg(fp, "\ndata count: %d", type->datcnt);
- struct data *data;
- int k = 1;
- list_for_each_entry(data, &type->dathead, list) {
- if (k++ % 5 == 1)
- msg(fp, "\ndata list: ");
- msg(fp, "%p\t", data);
- }
- }
- msg(fp, "\n");
- }
- static void show_page(FILE *fp, struct engine *engine)
- {
- return_if_fail(fp != NULL);
- return_if_fail(engine != NULL);
- struct page *page;
- list_for_each_entry(page, &engine->pghead, list) {
- msg(fp, "\nshow page %d: %p", page->object.pgid, page);
- msg(fp, "\npage size idx: %d", page->object.szid);
- msg(fp, "\nengine: %p", page->engine);
- msg(fp, "\nbulk size: %d", page->blksize);
- msg(fp, "\nbulk count: %d", page->blkcnt);
- msg(fp, "\ndata count: %d", page->datcnt);
- int i, k = 1;
- for (i = 0; i < page->blkcnt; i++) {
- if (page->data_array[i] != NULL) {
- if (k++ % 5 == 1)
- msg(fp, "\nshow data: ");
- msg(fp, "%p\t", page->data_array[i]);
- }
- }
- }
- msg(fp, "\n");
- }
- static void show_engine(FILE *fp, struct engine *engine)
- {
- return_if_fail(fp != NULL);
- return_if_fail(engine != NULL);
-
- msg(fp, "engine object: %p", engine);
- msg(fp, "\npage count: %d", engine->object.pgcnt);
- int i, k = 1;
- for(i = 0; i < TPOBJ_ARRLEN; i++)
- msg(fp, "\ntype name %d: %s", i, engine->object.tpobj[i].tpname);
-
- struct page *page;
- list_for_each_entry(page, &engine->pghead, list){
- if (k++ % 5 == 1)
- msg(fp, "\nengine page:");
- msg(fp, "%p\t", page);
- }
-
- for (i = 0; i < FREOBJ_ARRLEN; i++) {
- struct data *data;
- k = 1;
- int j = 0;
- list_for_each_entry(data, engine->freelist + i, list) {
- if (k++ % 5 == 1)
- msg(fp, "\nengine freelist %d - %d\t: ", i, j++);
- msg(fp, "%p\t", data);
- }
- }
-
- k = 1;
- for (i = 0; i < TPOBJ_ARRLEN; i++) {
- if (k++ % 5 == 1)
- msg(fp, "\nengine type: ");
- msg(fp, "%p\t", engine->tp[i]);
- }
-
- msg(fp, "\nengine fpos: %p", engine->fpos);
- msg(fp, "\nengine type count: %d", engine->tpcnt);
- msg(fp, "\nengine data count: %d", engine->datcnt);
- msg(fp, "\n");
- }
- void show_handle(FILE *fp, database_handle handle)
- {
- msg(fp,"\n\n\n");
- show_engine(fp, (struct engine *)handle);
- show_type(fp, (struct engine *)handle);
- show_page(fp, (struct engine *)handle);
- show_data(fp, (struct engine *)handle);
- }
- void show_pagebuf(char *pgbuf)
- {
- int *p = (int*)pgbuf;
- char *end = pgbuf + PAGE_SIZE;
- int k = 1;
- printf("\n\n");
- while ((char*)p - end < 0) {
- printf("%x\t", *p++);
- if (k++ % 8 == 0) printf("\n");
- }
- printf("\n\n");
- }
- void show_database(const char *filepath)
- {
- FILE *fp = fopen(filepath, "rb");
- return_if_fail(fp != NULL, "database open failed");
- struct engine_object engine;
- fcntl_read_pointer(fp, 0, &engine);
- msg(stdout, "\npgcnt : %d", engine.pgcnt);
- int i;
- struct type_object *tpobj = engine.tpobj;
- for (i = 0; strlen(tpobj[i].tpname) > 0; i++) {
- msg(stdout, "\ntype %d: %s", i, tpobj[i].tpname);
- }
- msg(stdout, "\n\n");
- char pgbuf[PAGE_SIZE];
- for (i = 1; i <= engine.pgcnt; i++) {
- memset(pgbuf, 0, PAGE_SIZE);
- fcntl_read(fp, phy_page_start(i), pgbuf, PAGE_SIZE);
- // show_pagebuf(pgbuf);
- struct page_object *pgobj = (pg_obj_t *)pgbuf;
- msg(stdout, "\npage : szid = %d, pgid = %d", pgobj->szid, pgobj->pgid);
- int j;
- for (j = 0; j < page_blk_cnt(pgobj->szid); j++) {
- struct data_object *datobj = (dat_obj_t *)mem_blk_start(pgbuf, pgobj->szid, j);
- msg(stdout, "\ndata %d : tpid = %x, size = %x", j, datobj->tpid, datobj->size);
- }
- }
-
- msg(stdout, "\n\n");
- fclose(fp);
- }
阅读(636) | 评论(0) | 转发(0) |