📄 db.h
字号:
/** * Copyright (C) 2005-2007 Christoph Rupp (chris@crupp.de). * All rights reserved. See file LICENSE for licence and copyright * information. * * internal macros and headers * */#ifndef HAM_DB_H__#define HAM_DB_H__#ifdef __cplusplusextern "C" {#endif#include "endian.h"#include "backend.h"#include "cache.h"#include "page.h"#include "os.h"#include "freelist.h"#include "extkeys.h"#include "error.h"#include "txn.h"#include "mem.h"#include "device.h"#include "env.h"#define OFFSET_OF(type, member) ((size_t) &((type *)0)->member)/* * This is the minimum chunk size; all chunks (pages and blobs) are aligned * to this size. */#define DB_CHUNKSIZE 64/* * the maximum number of indices (if this file is an environment with * multiple indices) */#define DB_MAX_INDICES 16 /* 16*32 = 512 byte wasted */#include "packstart.h"/* * the persistent database header */typedef HAM_PACK_0 HAM_PACK_1 struct{ /* magic cookie - always "ham\0" */ ham_u8_t _magic[4]; /* version information - major, minor, rev, reserved */ ham_u8_t _version[4]; /* serial number */ ham_u32_t _serialno; /* size of the page */ ham_u32_t _pagesize; /* padding for SUN Sparc */ ham_u32_t _reserved2; /* maximum number of _indexdata arrays - always set to * DB_MAX_INDICES (not yet needed, but stored for later use) */ ham_u16_t _max_db; /* padding for SUN Sparc */ ham_u16_t _reserved3; /* private data of the index backend */ ham_u8_t _indexdata[DB_MAX_INDICES][32]; /* start of the freelist - the freelist spans the rest of the page. * don't add members after this field! */ ham_u8_t _freelist_start;} db_header_t;#include "packstop.h"/* * set the 'magic' field of a file header */#define db_set_magic(db, a,b,c,d) { db_get_header(db)->_magic[0]=a; \ db_get_header(db)->_magic[1]=b; \ db_get_header(db)->_magic[2]=c; \ db_get_header(db)->_magic[3]=d; }/* * get byte #i of the 'magic'-header */#define db_get_magic(db, i) (db_get_header(db)->_magic[i])/* * set the version of a file header */#define db_set_version(db,a,b,c,d) { db_get_header(db)->_version[0]=a; \ db_get_header(db)->_version[1]=b; \ db_get_header(db)->_version[2]=c; \ db_get_header(db)->_version[3]=d; }/* * get byte #i of the 'version'-header */#define db_get_version(db, i) (db_get_header(db)->_version[i])/* * get the serial number */#define db_get_serialno(db) (ham_db2h32(db_get_header(db)->_serialno))/* * set the serial number */#define db_set_serialno(db, n) db_get_header(db)->_serialno=ham_h2db32(n)/* * get the key size */#define db_get_keysize(db) be_get_keysize(db_get_backend(db))/* * get the page size */#define db_get_pagesize(db) (ham_db2h32(db_get_header(db)->_pagesize))/* * set the page size */#define db_set_pagesize(db, ps) db_get_header(db)->_pagesize=ham_h2db32(ps)/** * get the size of the usable persistent payload of a page */#define db_get_usable_pagesize(db) (db_get_pagesize(db)-(sizeof(ham_u32_t)*3))/* * get the maximum number of databases for this file */#define db_get_indexdata_size(db) ham_db2h16(db_get_header(db)->_max_db)/* * set the maximum number of databases for this file */#define db_set_indexdata_size(db,s) db_get_header(db)->_max_db=ham_h2db16(s)/* * get the private data of the backend; interpretation of the * data is up to the backend */#define db_get_indexdata(db) &db_get_header(db)->_indexdata[ \ db_get_indexdata_offset(db)][0]/* * get the private data of the backend; interpretation of the * data is up to the backend */#define db_get_indexdata_at(db, i) &db_get_header(db)->_indexdata[i][0]/* * get the currently active transaction */#define db_get_txn(db) (db_get_env(db) \ ? env_get_txn(db_get_env(db)) \ : (db)->_txn)/* * set the currently active transaction */#define db_set_txn(db, txn) do { if (db_get_env(db)) \ env_set_txn(db_get_env(db), txn); \ else (db)->_txn=txn; } while(0)/* * get the cache for extended keys */#define db_get_extkey_cache(db) (db_get_env(db) \ ? env_get_extkey_cache(db_get_env(db)) \ : (db)->_extkey_cache)/* * set the cache for extended keys */#define db_set_extkey_cache(db, c) ham_assert(db_get_env(db)==0, ("")); \ (db)->_extkey_cache=c/* * the database structure */struct ham_db_t{ /* the current transaction ID */ ham_u64_t _txn_id; /* the last error code */ ham_status_t _error; /* a custom error handler */ ham_errhandler_fun _errh; /* the backend pointer - btree, hashtable etc */ ham_backend_t *_backend; /* the memory allocator */ mem_allocator_t *_allocator; /* the device (either a file or an in-memory-db) */ ham_device_t *_device; /* the cache */ ham_cache_t *_cache; /* the size of the last allocated data pointer for records */ ham_size_t _rec_allocsize; /* the last allocated data pointer for records */ void *_rec_allocdata; /* the size of the last allocated data pointer for keys */ ham_size_t _key_allocsize; /* the last allocated data pointer for keys */ void *_key_allocdata; /* the prefix-comparison function */ ham_prefix_compare_func_t _prefixcompfoo; /* the comparison function */ ham_compare_func_t _compfoo; /* the file header page */ ham_page_t *_hdrpage; /* the active txn */ ham_txn_t *_txn; /* the cache for extended keys */ extkey_cache_t *_extkey_cache; /* the database flags - a combination of the persistent flags * and runtime flags */ ham_u32_t _rt_flags; /* the offset of this database in the environment _indexdata */ ham_u16_t _indexdata_offset; /* the environment of this database - can be NULL */ ham_env_t *_env; /* the next database in a linked list of databases */ ham_db_t *_next;};/* * get the header page */#define db_get_header_page(db) (db_get_env(db) \ ? env_get_header_page(db_get_env(db)) \ : (db)->_hdrpage)/* * set the header page - not allowed when we have an environment! */#define db_set_header_page(db, h) ham_assert(db_get_env(db)==0, ("")); \ (db)->_hdrpage=(h)/* * get the current transaction ID */#define db_get_txn_id(db) (db_get_env(db) \ ? env_get_txn_id(db_get_env(db)) \ : (db)->_txn_id)/* * set the current transaction ID */#define db_set_txn_id(db, id) do { if (db_get_env(db)) \ env_set_txn_id(db_get_env(db), id); \ else (db)->_txn_id=id; } while(0)/* * get the last error code */#define db_get_error(db) (db)->_error/* * set the last error code */#define db_set_error(db, e) (db)->_error=e/* * get the backend pointer */#define db_get_backend(db) (db)->_backend/* * set the backend pointer */#define db_set_backend(db, be) (db)->_backend=be
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -