9 #define entityIdIndex indecies[entityIdIdx]
10 #define entityNmIndex indecies[entityNmIdx]
11 #define freeStoreIndex indecies[freeStoreIdx]
12 #define addrStoreIndex indecies[addrStoreIdx]
13 #define freeSpaceIndex indecies[cache.freeIdx]
14 #define addrSpaceIndex indecies[cache.addrIdx]
16 #define noThrow std::nothrow
18 #define likely(x) (__builtin_constant_p(x) ? !!(x) : __builtin_expect(!!(x), 1))
19 #define unlikely(x) (__builtin_constant_p(x) ? !!(x) : __builtin_expect(!!(x), 0))
22 #define lengthof(x) ((int)(sizeof(x)/sizeof(x[0])))
26 inline void *operator new(size_t n) { void *vp = malloc(n); bzero(vp,n); return vp; }
27 inline void operator delete(void *t) { free(t); }
28 inline void operator delete(void *t,size_t n) { free(t); }
29 inline void *operator new[](size_t n) { void *vp = malloc(n); bzero(vp,n); return vp; }
30 inline void operator delete[](void *t) { free(t); }
31 inline void operator delete[](void *t,size_t n) { free(t); }
39 #include <linux/futex.h>
40 #include <sys/syscall.h>
48 class nm##Obj : public Db::Obj { public:
51 class nm##Loc : public Db::ObjectLoc { public: \
52 nm##Obj *operator ->() { return (nm##Obj *)addr(); } \
53 nm##Loc(Db::Entity *ep) : Db::ObjectLoc(ep) {} \
54 nm##Loc(Db::Entity &e) : Db::ObjectLoc(&e) {}
57 #define basic_def(ty,n) class t_##n { public: ty v; t_##n() {} \
58 t_##n(const ty &i) : v(i) {} \
59 t_##n(const t_##n &i) : v(i.v) {} \
60 t_##n &operator =(const t_##n &i) { v = i.v; return *this; } \
61 ty &operator =(const ty &i) { return v = i; } \
62 ty *addr() { return &v; } int size() { return sizeof(v); } \
66 #define array_def(ty,n,l) class t_##n { public: ty v[l]; t_##n() {} \
67 t_##n(const t_##n &i) { memcpy(&v,&i.v,sizeof(v)); } \
68 t_##n(ty *i) { memcpy(&v,i,sizeof(v)); } \
69 t_##n(ty(*i)[l]) { memcpy(&v,i,sizeof(v)); } \
70 ty *operator =(const ty *i) { memcpy(&v,i,sizeof(v)); return &v[0]; } \
71 ty *addr() { return &v[0]; } int size() { return sizeof(v); } \
74 // variable array definitions
75 #define varray_def(ty,n) \
76 class t_##n { public: char *v; int l; t_##n() {} \
77 t_##n(const char *i, int sz) { v = (char *)i; l = sz; } \
78 t_##n(const unsigned char *i, int sz) { v = (char *)i; l = sz; } \
79 ty *addr() { return (ty *)v; } int size() { return l; } \
82 // string array definitions
83 #define sarray_def(ty,n) \
84 class t_##n { public: char *v; int l; t_##n() {} \
85 t_##n(const char *i, int sz) { v = (char *)i; l = sz; } \
86 t_##n(const unsigned char *i, int sz) { v = (char *)i; l = sz; } \
87 t_##n(const char *i) { t_##n(i,strlen(i)+1); } \
88 t_##n(const unsigned char *i) { t_##n(i,strlen(v)+1); } \
89 ty *addr() { return (ty *)v; } int size() { return l; } \
93 #define basic_ref(ty,n) \
94 ty *_##n() { return (*this)->v_##n.addr(); } \
95 ty n() { return *_##n(); } \
96 void n(ty i) { _wr(); *_##n() = i; } \
97 int size_##n() { return (*this)->v_##n.size(); } \
100 #define array_ref(ty,n,l) \
101 ty *_##n() { return (*this)->v_##n.addr(); } \
102 ty (&n())[l] { return *(ty (*)[l])_##n(); } \
103 void n(const ty *i,int m) { _wr(); if( m > 0 ) memcpy(n(),i,m); } \
104 void n(const ty *i) { n(i,(*this)->v_##n.size()); } \
105 int size_##n() { return (*this)->v_##n.size(); } \
107 //variable array type ref
108 #define varray_ref(ty,n) \
109 ty *_##n() { return (ty *)addr((*this)->v_##n); } \
110 ty *_##n(int sz) { size((*this)->v_##n, sz); \
111 return sz > 0 ? (ty *)addr_wr((*this)->v_##n) : 0; } \
112 ty (&n())[] { return *(ty (*)[])_##n(); } \
113 int n(const ty *v, int sz) { ty *vp=_##n(sz); \
114 if( vp && sz > 0 ) memcpy(vp, v, sz); return 0; } \
115 int size_##n() { return (*this)->v_##n.size(); } \
117 //string array type ref
118 #define sarray_ref(ty,n) \
119 ty *_##n() { return (ty *)addr((*this)->v_##n); } \
120 ty *_##n(int sz) { size((*this)->v_##n, sz); \
121 return sz > 0 ? (ty *)addr_wr((*this)->v_##n) : 0; } \
122 ty (&n())[] { return *(ty (*)[])_##n(); } \
123 int n(const ty *v, int sz) { ty *vp=_##n(sz); \
124 if( vp && sz > 0 ) memcpy(vp, v, sz); return 0; } \
125 int n(const char *v) { return n((ty *)v,strlen(v)+1); } \
126 int n(const unsigned char *v) { return n((const char *)v); } \
127 int size_##n() { return (*this)->v_##n.size(); } \
132 #define DEBUG_TIMESTAMPS
133 #define DBBUG_ERR 0x00000001
134 #define DBBUG_FAIL 0x00000002
136 //#define CHK 1 ? 0 :
153 int root_magic; // info_magic label
154 int root_info_size; // root_info blob size
158 ioAddr root_info_addr;
159 ioAddr last_info_addr;
160 transId transaction_id; // current transaction
161 ioAddr file_size; // current file size
162 pageId freePages; // free page table page list
163 int indeciesUsed; // number of active indecies
164 int pageTableUsed; // number of active pages
169 int shm_init, no_shm;
171 static void *get_mem8_t(int id);
172 static void *new_mem8_t(int size, int &id);
173 static int del_mem8_t(const void *vp, int id);
174 static void *get_shm8_t(int id);
175 static void *new_shm8_t(int size, int &id);
176 static int del_shm8_t(const void *vp, int id);
177 void *(*get_mem)(int id);
178 void *(*new_mem)(int size, int &id);
179 int (*del_mem)(const void *vp, int id);
182 uint8_t *get_uint8_t(int id, int pg=-1);
183 uint8_t *new_uint8_t(int size, int &id, int pg=-1);
184 int del_uint8_t(const void *vp, int id=-1, int pg=-1);
187 typedef int (*CmprFn)(char *,char *);
188 static int cmprFrSt(char *a, char *b);
189 static int cmprAdSt(char *a, char *b);
190 static int cmprFrSp(char *a, char *b);
191 static int cmprAdSp(char *a, char *b);
192 static int cmprOIds(char *a, char *b);
193 static int cmprStr(char *a, char *b);
194 static int cmprKey(char *a, char *b);
195 static int cmprLast(char *a, char *b);
196 static CmprFn cmprFns[];
197 typedef void (*errCallback)(Db *db, int v);
198 static const pageId NIL=-1, DDONE=-2;
216 idxId = 0, nmSz = 32,
218 keyLT=-2, keyLE=-1, keyEQ=0, keyGE=1, keyGT=2,
227 static void zincr(volatile int &v) { /* atomic(++v) */
228 asm ( " lock incl %1\n" : "+m" (v) :: );
230 static void zdecr(volatile int &v) { /* atomic(--v) */
231 asm ( " lock decl %1\n" : "+m" (v) :: );
233 static char tdecr(volatile int &v) {
234 char ret; /* ret = atomic(--loc >= 0 ? 1 : 0) */
235 asm ( " lock decl %1\n setge %0\n" : "=r" (ret), "+m" (v) :: );
238 static char tincr(volatile int &v) {
239 char ret; /* ret = atomic(++loc > 0 ? 1 : 0) */
240 asm ( " lock incl %1\n setg %0\n" : "=r" (ret), "+m" (v) :: );
243 static int zcmpxchg(int old, int val, volatile int &v) {
245 asm volatile( " lock\n cmpxchgl %2,%1\n"
246 : "+a" (ret), "+m" (v) : "r" (val) : "memory" );
249 static int zxchg(int val, volatile int &v) {
250 asm volatile( " xchgl %0,%1\n"
251 : "+r" (val), "+m" (v) :: "memory" );
254 static int zadd(int n, volatile int &v) {
256 do { val = (old=v)+n; mod = zcmpxchg(old,val,v);
257 } while( mod != old );
260 static void zmfence() {
261 asm volatile ( " mfence\n" ::: "memory" );
269 #define ZLOCK_INIT zzlock_t()
273 int zfutex(int op, int val, timespec *time=0) {
274 return syscall(SYS_futex,&loc,op,val,time,0,0);
279 int zwake(int nwakeups);
280 int zwait(int val, timespec *ts=0);
281 int zwait() { return zwait(loc); }
282 zloc_t() : loc(-1) {}
286 class zlock_t : zloc_t {
290 void *vp; asm ("movq %%fs:%c1,%q0" : "=r" (vp) : "i" (16));
292 void *vp; asm ("mov %%fs:%c1,%q0" : "=r" (vp) : "i" (16));
297 friend class zblock_t;
298 friend class zrwlock_t;
300 int zunlock(int nwakeups=1);
304 int v, ret = unlikely( (v=zcmpxchg(-1,0,loc)) >= 0 ) ? zlock(v) : 0;
309 if( unlikely(loc < 0) ) { return zemsg1(); }
311 int v, ret = unlikely( (v=zcmpxchg(0,-1,loc)) != 0 ) ? zunlock() : 0;
314 zlock_t() { owner = 0; }
318 class zblock_t : zlock_t {
320 void block() { loc = 0; zwait(0); }
321 void unblock() { loc = -1; zwake(INT_MAX); }
326 class zrwlock_t : zloc_t {
331 void enter() { zincr(loc); if( unlikely( lk.loc >= 0 ) ) zenter(); }
332 void leave() { if( unlikely( !tdecr(loc) ) ) zleave(); }
335 int locked() { return loc >= 0 ? 0 : lk.loc >= 0 ? 1 : -1; }
336 int blocked() { return lk.loc >= 0 ? 1 : 0; }
342 #define ZLOCK_INIT { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER }
346 pthread_mutex_t zlock;
348 void lock() { pthread_mutex_lock(&zlock); }
349 void unlock() { pthread_mutex_unlock(&zlock); }
350 zlock_t() { pthread_mutex_init(&zlock, 0); }
351 ~zlock_t() { pthread_mutex_destroy(&zlock); }
355 pthread_mutex_t zblock;
359 pthread_mutex_init(&zblock, 0);
360 pthread_cond_init(&cond, 0);
363 pthread_mutex_destroy(&zblock);
364 pthread_cond_destroy(&cond);
367 pthread_mutex_lock(&zblock);
368 pthread_cond_wait(&cond, &zblock);
369 pthread_mutex_unlock(&zblock);
371 void unblock() { pthread_cond_broadcast(&cond); }
374 class zrwlock_t : zlock_t {
375 volatile int blocking, users;
378 void wait() { pthread_cond_wait(&cond, &zlock); }
379 void wake() { pthread_cond_signal(&cond); }
381 void enter() { lock();
382 while( blocking ) { unlock(); lk.lock(); lk.unlock(); lock(); }
385 void leave() { lock(); if( !--users && blocking ) wake(); unlock(); }
386 void write_enter() { lk.lock(); blocking = 1;
387 lock(); while( users ) wait(); unlock();
389 void write_leave() { blocking = 0; lk.unlock(); }
390 int count() { return users; }
391 int locked() { return users ? 0 : blocking ? 1 : -1; }
392 int blocked() { return blocking; }
394 zrwlock_t() { pthread_cond_init(&cond, 0); users = 0; blocking = 0; }
395 ~zrwlock_t() { pthread_cond_destroy(&cond); }
403 locked(zlock_t &l) : lk(l) { lk.lock(); }
404 ~locked() { lk.unlock(); }
410 read_locked(zrwlock_t &l) : rwlk(l) { rwlk.enter(); }
411 ~read_locked() { rwlk.leave(); }
414 class write_blocked {
417 write_blocked(zrwlock_t &l) : rwlk(l) { rwlk.write_enter(); }
418 ~write_blocked() { rwlk.write_leave(); }
424 db_magic=0x00624474, // tDb
425 idx_magic=0x00786469, // idx
426 info_magic=0x6f666e69, // info
427 root_magic=0x746f6f72, // root
428 page_magic=0x6770, // pg
429 entity_magic = 0x6d65, // em
431 root_info_extra_pages = 2,
432 idxNil=0, idxBin=1, idxStr=2,
433 ktyBin=0, ktyInd=1, ktyDir=2,
434 opDelete=-1, opFind=0, opInsert=1,
435 pg_unknown=0, pg_root_info=1, pg_free=2,
436 pg_entity=0x0100, pg_index=0x1000,
437 max_entity_type = pg_index-pg_entity-1,
438 max_index_type = 0x10000-pg_index-1,
439 min_heap_allocation = 32,
440 entityIdIdx = 0, entityNmIdx = 1,
441 freeStoreIdx = 2, addrStoreIdx = 3,
442 freeSpaceIdx = 4, addrSpaceIdx = 5,
444 fl_wr=1, fl_rd=2, fl_new=4, fl_free=8,
446 defaultStoreBlockSize = 8192,
447 defaultPageTableHunkSize = 8192,
448 defaultIndexTableHunkSize = 4096,
449 defaultBinaryBlockSize = 16384,
450 defaultStringBlockSize = 4096,
451 defaultEntityPageSize = 65536,
456 errCallback err_callback;
458 transId active_transaction;
462 static const char *errMsgs[];
463 static void dmsg(int msk, const char *msg,...);
464 int _err_(int v,const char *fn,int ln);
465 int _fail_(int v,const char *fn,int ln);
466 #define err_(v) _err_(v,__func__,__LINE__)
467 #define fail_(v) _fail_(v,__func__,__LINE__)
469 static void dmsg(int msk, const char *msg,...) {}
470 int _err_(int v) { error(v); return v; }
471 int _fail_(int v) { return v; }
472 #define err_(v) _err_(v)
473 #define fail_(v) _fail_(v)
476 #define Err(v) return err_(v)
477 #define Fail(v) return fail_(v)
479 #define if_ret(fn) do{ int _ret; \
480 if(unlikely((_ret=(fn))<0)) return _ret; \
482 #define if_err(fn) do{ int _ret; \
483 if(unlikely((_ret=(fn))<0)) Err(_ret); \
485 #define if_fail(fn) do{ int _ret; \
486 if(unlikely((_ret=(fn))<0)) Fail(_ret); \
492 int owner, last_owner;
493 int info_key, info_id;
497 zlock_t infoLk; // lock dbinfo up to here
498 zrwlock_t dbRwLk; // global lock
499 zrwlock_t pgTblLk;// pageTable realloc
500 zlock_t pgAlLk; // new page pagesUsed/pagesAllocated
501 zlock_t pgLdLk; // pageLoad
502 zlock_t blkAlLk; // blockAllocate/Free
503 zlock_t objAlLk; // objectAllocate/Free
504 zrwlock_t rw_locks[max_entity_type];
506 DbInfo(int pid, int key, int id);
508 int new_info(int key);
509 int get_info(int key);
514 int attach_rw(int zrw) { return zrw ? attach_wr() : attach_rd(); }
521 unsigned short magic;
526 Page *get_Page(pageId pid) volatile { return pageTable[pid]; }
527 void set_Page(pageId pid, Page *pp) { pageTable[pid] = pp; }
528 //Page *get_page(pageId pid) { blocked by(pgTblk); return get_Page(pid); }
529 Page *get_page(pageId pid); // locked pageTable access
531 static pageId getPageId(unsigned char *&bp) {
532 int i = sizeof(pageId); pageId id;
533 for( id = *bp++; --i > 0; id |= *bp++ ) id <<= 8;
536 static void putPageId(unsigned char *&bp, pageId id) {
537 int i = sizeof(pageId) * 8;
538 while( (i -= 8) >= 0 ) *bp++ = id >> i;
540 static pageId readPageId(char *cp) {
541 unsigned char *bp = (unsigned char *)cp;
542 return getPageId(bp);
544 static void writePageId(char *cp, pageId id) {
545 unsigned char *bp = (unsigned char *)cp;
549 class keyBlock : public pagePrefix {
550 char rightLink[sizeof(pageId)];
552 int right_link() { return readPageId(&rightLink[0]); }
553 void right_link(pageId id) { writePageId(&rightLink[0],id); }
556 static int defaultBlockSizes[];
558 class IndexTypeInfo {
561 short type; /* type of index */
563 char name[nmSz]; /* index string identifier */
566 class IndexBaseType : public IndexTypeInfo {
568 IndexBaseType(int typ);
571 class IndexRecdInfo {
573 int idx; /* index in db->indecies[] */
574 int keySz, dataSz; /* sizeof key/data fields in bytes */
575 pageId rootPageId; /* index root page ID */
576 pageId rightHandSide; /* the right hand side of the tree for this index */
577 pageId freeBlocks; /* free index page list */
578 unsigned int blockSize; /* size of new index blocks */
580 long count; /* index population count */
583 class IndexBaseRecd : public IndexRecdInfo {
585 IndexBaseRecd(int typ, int zidx, int ksz, int dsz);
588 class IndexBaseStorage;
589 class IndexBaseInfo : public IndexTypeInfo, public IndexRecdInfo {
591 operator IndexBaseStorage *() { return (IndexBaseStorage *)this; }
594 class IndexBaseStorage : public IndexBaseInfo {
596 IndexBaseStorage(int typ, int zidx, int ksz, int dsz);
597 IndexBaseStorage() {}
598 ~IndexBaseStorage() {}
602 IndexBaseStorage *st;
604 Db *db; /* owner db */
606 pgRef lastAccess, lastFind; /* last operational access/find location */
607 pgRef lastInsert, lastDelete; /* last operational insert/delete location */
608 pgRef lastNext; /* last operational next location */
609 int kdSz; /* keySz + dataSz */
610 int lastOp; /* last operation, delete=-1/find=0/insert=1 */
611 int cInsCount; /* number of consecutive insertions */
612 int cFindCount; /* number of consecutive finds */
613 int cDelCount; /* number of consecutive deletions */
616 virtual int keyMap(pageId s, int(IndexBase::*fn)(pageId id)) = 0;
617 virtual int keyCopy(pageId s, IndexBase *ib) = 0;
618 int blockAllocate(pageId &pid, keyBlock *&bp);
619 int blockAllocate(pageId &pid, keyBlock *&bp, Page *&pp, char *&cp) {
620 pp = 0; cp = 0; if_err( blockAllocate(pid, bp) );
621 pp = db->get_page(pid); cp = (char *)(bp + 1);
624 int blockFree(pageId pid);
625 int blockRelease(pageId pid);
626 int blockLoad(pageId pid);
627 int deleteFreeBlocks();
629 void chkLastInsert();
630 void chkLastDelete();
631 void chkLastFind(pgRef &last);
634 int _err_(int v,const char *fn,int ln) { return db->_err_(v,fn,ln); }
635 int _fail_(int v,const char *fn,int ln) { return db->_fail_(v,fn,ln); }
637 int _err_(int v) { return db->_err_(v); }
638 int _fail_(int v) { return db->_fail_(v); }
640 virtual int Locate(int op,void *key,CmprFn cmpr,void *rtnKey,void *rtnData) = 0;
641 virtual int Find(void *key,void *rtnData) = 0;
642 virtual int Insert(void *key,void *data) = 0;
643 virtual int Delete(void *key) = 0;
644 virtual int First(void *rtnKey,void *rtnData) = 0;
645 virtual int Last(void *rtnKey,void *rtnData) = 0;
646 virtual int Modify(void *key,void *recd) = 0;
647 virtual int Next(pgRef &loc,void *rtnKey,void *rtnData) = 0;
648 int First(pgRef &loc,void *rtnKey,void *rtnData) {
649 if_fail( First(rtnKey, rtnData) );
653 int Next(void *rtnKey,void *rtnData) {
654 if( lastNext.id < 0 ) Fail(errInvalid);
655 return Next(lastNext,rtnKey,rtnData);
657 long Count() { return st->count; }
661 int NextLoc(pgRef &loc) { loc = lastNext; return 0; }
662 IndexBase(Db *zdb, int typ, int zidx, int ksz, int dsz);
663 IndexBase(Db *zdb, IndexBaseStorage &d);
664 virtual ~IndexBase();
667 int indeciesAllocated, indecies_sz;
669 class IndexBinaryStorage;
670 class IndexBinaryInfo {
672 operator IndexBinaryStorage *() { return (IndexBinaryStorage *)this; }
676 class IndexBinaryStorage : public IndexBinaryInfo {
678 IndexBinaryStorage(int cmprId) { this->cmprId = cmprId; }
679 IndexBinaryStorage() {}
680 ~IndexBinaryStorage() {}
683 class IndexBinary : public IndexBase {
684 IndexBinaryStorage *bst;
686 CmprFn compare; /* the key compare function type */
687 char *akey; /* pointer to key argument */
688 int keyInterior; /* last insert interior/exterior */
689 int idf; /* interior delete flag */
690 char *iky, *tky; /* search/promoted temp key storage */
693 int keyMap(pageId s, int(IndexBase::*fn)(pageId id));
694 int keyCopy(pageId s, IndexBase *ib);
695 int keyBlockUnderflow(int &t,keyBlock *lbb,pageId p,keyBlock *pbb,int pi);
696 void makeKey(char *cp,char *key,int l,char *recd,int n);
697 void setLastKey(pageId s,pageId u,int k);
698 int keyLocate(pgRef &last,pageId s, int op,void *ky, CmprFn cmpr);
699 int chkNext(pgRef &loc, char *&kp);
700 int keyNext(pgRef &loc, char *kp);
701 int chkFind(pgRef &loc, char *key);
702 int keyFind(pgRef &loc,void *ky, pageId s);
703 int chkInsert(void *key,void *data);
704 int keyInsert(pageId s, pageId &t);
705 int chkDelete(pgRef &loc, void *kp);
706 int keyDelete(int &t,void *kp,pageId s,pageId p,keyBlock *pbb,int pi);
707 int keyFirst(pgRef &loc, pageId s);
708 int keyLast(pgRef &loc, pageId s);
710 int refLocate(pgRef &loc, int op,void *key, CmprFn cmpr);
711 int Locate(int op,void *key,CmprFn cmpr,void *rtnKey,void *rtnData);
712 int refFind(pgRef &loc, void *key);
713 int Find(void *key,void *rtnData);
714 int Insert(void *key,void *data);
715 int Delete(void *key);
716 int First(void *rtnKey,void *rtnData);
717 int Last(void *rtnKey,void *rtnData);
718 int Modify(void *key,void *recd);
719 int Next(pgRef &loc,void *rtnKey,void *rtnData);
720 int Next(void *rtnKey,void *rtnData) {
721 return IndexBase::Next(rtnKey,rtnData);
723 void wr_key(void *kp, char *bp, int sz) {
724 switch( st->key_type ) {
725 case ktyBin: memcpy(bp,kp,sz); break;
727 case ktyInd: ((Key *)kp)->wr_key(bp); break;
730 char *ikey() { return iky; }
731 char *tkey() { return tky; }
733 IndexBinary(Db *zdb, int zidx, int ksz, int dsz, CmprFn cmpr);
734 IndexBinary(Db *zdb, IndexBaseStorage *b, IndexBinaryStorage *d);
735 IndexBinary(IndexBase *ib, IndexBaseStorage *b, IndexBinaryStorage *d);
738 friend class IndexBinary;
740 class IndexStringStorage;
741 class IndexStringInfo {
742 char dummy; // compiler needs this for some reason
744 operator IndexStringStorage *() { return (IndexStringStorage *)this; }
747 class IndexStringStorage : public IndexStringInfo {
749 IndexStringStorage() {}
750 ~IndexStringStorage() {}
753 class IndexString : public IndexBase {
754 IndexStringStorage *sst;
756 static int ustrcmp(unsigned char *a, unsigned char *b) {
757 return strncmp((char *)a,(char *)b,keysz);
759 static void ustrcpy(unsigned char *a, unsigned char *b) {
760 strncpy((char *)a,(char *)b,keysz);
762 static void umemmove(unsigned char *&a, unsigned char *b, int n) {
763 memmove(a,b,n); a += n;
765 static int kpress(unsigned char *kp, unsigned char *lp, unsigned char *cp);
766 int split(int n, int i, pageId s, pageId &l, pageId r);
768 int keyMap(pageId s, int(IndexBase::*fn)(pageId id));
769 int keyCopy(pageId s, IndexBase *ib);
770 int chkInsert(void *key,void *data);
771 int keyInsert(pageId &t, pageId s);
772 int keyFirst(pageId s);
773 int keyLast(pageId s);
774 int keyLocate(pgRef &last,pageId s,int &t, int op,
775 unsigned char *ky,CmprFn cmpr, unsigned char *rky);
776 int chkFind(pgRef &loc, char *key, unsigned char *lkey, unsigned char *lky=0);
777 int keyFind(pgRef &loc, unsigned char *ky);
778 int keyNext(pgRef &loc, unsigned char *rky);
779 int keyUnderflow(pageId s, pageId &t, int k);
780 int keyOverflow(pageId s, pageId &t, int k, int o);
781 int keyRemap(pageId s, pageId &t, int k, int o);
782 int keyDelete(pageId s, pageId &t);
784 unsigned char lastAccKey[keysz], lastFndKey[keysz];
785 unsigned char lastInsKey[keysz], lastDelKey[keysz];
786 unsigned char lastNxtKey[keysz];
787 unsigned char *tky, *dky; // dataSz+keysz+1
788 unsigned char *tbfr; // 3*allocated
789 unsigned char akey[keysz]; // key in use
790 int idf; /* interior delete flag */
792 int refLocate(pgRef &loc, int op, void *key, CmprFn cmpr, unsigned char *rkey);
793 int Locate(int op,void *key,CmprFn cmpr,void *rtnKey,void *rtnData);
794 int refFind(pgRef &loc, void *key);
795 int Find(void *key,void *rtnData);
796 int Insert(void *key,void *data);
797 int Delete(void *key);
798 int First(void *rtnKey,void *rtnData);
799 int Last(void *rtnKey,void *rtnData);
800 int Modify(void *key,void *recd);
801 int Next(pgRef &loc,void *rtnKey,void *rtnData);
802 int Next(void *rtnKey,void *rtnData) {
803 return IndexBase::Next(rtnKey,rtnData);
807 IndexString(Db *zdb, int zidx, int dsz);
808 IndexString(Db *zdb, IndexBaseStorage *b, IndexStringStorage *d);
809 IndexString(IndexBase *ib, IndexBaseStorage *b, IndexStringStorage *d);
812 friend class IndexString;
814 class IndexBinaryData : public IndexBaseInfo, public IndexBinaryInfo {};
815 class IndexStringData : public IndexBaseInfo, public IndexStringInfo {};
822 IndexInfo *index_info; /* image for index storage */
826 unsigned short magic;
831 class freeStoreRecord {
837 class addrStoreRecord {
843 class freeSpaceRecord {
850 class addrSpaceRecord {
862 int cacheFlush(Db *db);
863 int Get(Db *db,int &size, pgRef &ref);
864 int Load(Db *db, pageId id, int ofs, int sz);
865 void init() { loc.id = NIL; loc.offset = 0; avail = 0; }
866 void init(pageId id, int ofs, int sz) {
867 loc.id = id; loc.offset = ofs; avail = sz;
870 printf("loc: %d/%d avl: %d\n", loc.id,loc.offset,avail);
872 int freeIdx, addrIdx;
873 int init_idx(Db *db, const char *nm);
876 int cacheFlush() { return alloc_cache.cacheFlush(this); }
877 void cacheDelete(AllocCache &cache);
878 int cache_all_flush();
883 unsigned int allocated;
884 unsigned short flags;
895 PageStorage() { init(); }
897 int chk_flags(int fl) { return flags & fl; }
898 int set_flags(int fl) { return flags |= fl; }
899 int clr_flags(int fl) { return flags &= ~fl; }
909 // PageStorage access
910 int iused() { return st->used-sizeof(keyBlock); }
911 void iused(int v) { st->used = v+sizeof(keyBlock); }
912 int iallocated() { return st->allocated-sizeof(keyBlock); }
913 void iallocated(int v) { st->allocated = v+sizeof(keyBlock); }
914 PageStorage *operator ->() { return st; }
916 void reset_to(Page *pp);
917 Page(PageStorage &d) { st = &d; init(); }
921 int pageTableAllocated, page_table_sz;
925 int pageTableHunkSize;
926 int indexTableHunkSize;
935 int cfnAllocated, cfnUsed;
936 undoData() : cfn(0), cfnAllocated(0), cfnUsed(0) {}
937 ~undoData() { delete [] cfn; cfnAllocated = cfnUsed = 0; }
942 ioAddr file_position;
944 char *bfr, *lmt, *inp;
948 int write_bfr(char *dp, int sz);
954 inline static unsigned int on_bits(unsigned int n) {
955 n = (n & 0x55555555) + ((n >> 1) & 0x55555555);
956 n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
957 n = (n & 0x0f0f0f0f) + ((n >> 4) & 0x0f0f0f0f);
958 n += n >> 8; n += n >> 16; //ok, fldsz > 5 bits
963 inline static unsigned int low_bit(unsigned int n) {
967 // bit number of lowest on bit
968 inline static unsigned int low_bit_no(unsigned int n) {
969 return on_bits(low_bit(n) - 1);
972 // highest on bit, and all lower bits set
973 inline static unsigned int high_bit_mask(unsigned int n) {
974 n |= n >> 1; n |= n >> 2;
975 n |= n >> 4; n |= n >> 8;
981 inline static unsigned int high_bit(unsigned int n) {
982 unsigned m = high_bit_mask(n);
986 // bit number of highest on bit
987 inline static unsigned int high_bit_no(unsigned int n) {
988 return on_bits(high_bit_mask(n)) - 1;
992 static inline int cpu_aligned() {
994 asm volatile( "pushf\n" "pop %0\n" : "=rm" (flags) );
995 return (flags>>18) & 1;
1001 uint64_t clip(int64_t v) { return v<0 ? 0 : (uint64_t)v>vmx ? vmx : v; }
1003 enum { alignBits = 8, };
1005 static void init(int v=-1) { aligned = v >= 0 ? v : cpu_aligned(); }
1006 void put(uint64_t v, int n);
1007 void putc(uint64_t v, int n) { put(clip(v), n); }
1008 void iput(int v) { put(v, 8*sizeof(int)); }
1009 void lput(int64_t v) { put(v, 8*sizeof(int64_t)); }
1010 uint64_t get(int n);
1011 int iget() { return get(8*sizeof(int)); }
1012 int64_t lget() { return get(8*sizeof(int64_t)); }
1013 int pos() { return idx; }
1014 void seek(int i) { idx = i; }
1015 void init(uint8_t *bp) { bits = bp; idx = 0; vmx = 0; }
1016 uint8_t *bfr() { return this->bits; }
1017 void align() { idx = (idx+alignBits-1) & ~(alignBits-1); }
1018 void *addr() { return &bits[idx/8]; }
1019 void set_max(uint64_t v) { vmx = v; }
1021 pack() : bits(0) { idx = 0; }
1022 pack(uint8_t *bp) { init(bp); }
1032 static int64_t bit_size(int len, int w);
1033 static int64_t byte_size(int len, int w);
1034 static void build(uint8_t *kp, uint8_t *bp, int w, int len) {
1035 mediaKey key(kp, bp, w, len);
1037 static int64_t count(void *kp) {
1038 mediaKey *mkp = (mediaKey *)kp;
1039 return be64toh(mkp->cnt);
1041 static int64_t set_count(void *kp, int64_t v) {
1042 mediaKey *mkp = (mediaKey *)kp;
1043 return mkp->cnt = htobe64(v);
1045 static int64_t incr_count(void *kp, int64_t dv) {
1046 return set_count(kp, count(kp) + dv);
1048 static int64_t count1(uint8_t *kp);
1049 mediaKey(uint8_t *kp, uint8_t *bp, int w, int len);
1055 int w, len, dsz, psz, spos;
1056 int64_t cnt, *dat, **dp;
1057 void get_fields(int n, int k);
1058 int dsize(int n) { int m = (n+1)/2; return m>1 ? m+dsize(m) : 1; }
1061 void load(uint8_t *kp);
1063 mediaLoad(uint8_t *bp);
1069 int w, len, dsz, psz, spos;
1070 int64_t acnt, bcnt, *adat, *bdat, **adp, **bdp;
1072 uint64_t sqr(int64_t v) { return v*v; }
1073 int dsize(int n) { int m = (n+1)/2; return m>1 ? m+dsize(m) : 1; }
1074 uint64_t chk_fields(int m, int k);
1075 int cmpr_fields(int m, int k);
1078 uint64_t chk(uint8_t *kp, uint64_t lmt=~0);
1079 int cmpr(uint8_t *kp, uint64_t lmt=~0);
1081 mediaCmpr(uint8_t *bp);
1088 typedef IndexBase *Index;
1089 int new_entity(Entity &entity, const char *nm, int sz);
1090 int get_entity(Entity &entity, const char *nm);
1091 int del_entity(Entity &entity);
1093 class Obj { /* per object storage base class */
1101 int size() { return len; }
1102 void init() { len = -1; loc.id = NIL; loc.offset = 0; }
1103 void del(Entity *entity) { len = -1; entity->deallocate(loc); }
1105 typedef varObj Obj::*vRef;
1111 Obj *addr(pgRef &loc) {
1113 return loc.id < 0 || entity->db->addrRead(loc,op) ? 0 : (Obj *)op;
1115 Obj *addr_wr(pgRef &loc) {
1117 return loc.id < 0 || entity->db->addrWrite(loc,op) ? 0 : (Obj *)op;
1119 void _wr() { Page &pg = *entity->db->get_page(obj.id); pg->set_flags(fl_wr); }
1120 Obj *addr() { return addr(obj); }
1121 Obj *addr_wr() { return addr_wr(obj); }
1122 void *addr(varObj &vobj) { return addr(vobj.loc); }
1123 void *addr_wr(varObj &vobj) { return addr_wr(vobj.loc); }
1124 Obj *operator ->() { return (Obj *)addr(); }
1125 ObjectLoc(Entity *ep) : entity(ep) { obj.id = NIL; obj.offset = 0; }
1128 virtual int allocate(int sz=0) { return entity->allocate(*this,sz); }
1129 virtual int construct() { return entity->construct(*this); }
1130 virtual int destruct() { return entity->destruct(*this); }
1131 virtual int deallocate() { return entity->deallocate(*this); }
1132 virtual int insertCascade() { return 0; }
1133 virtual int insertProhibit() { return 0; }
1134 virtual int deleteCascade() { return 0; }
1135 virtual int deleteProhibit() { return 0; }
1136 virtual int modifyCascade() { return 0; }
1137 virtual int modifyProhibit() { return 0; }
1138 virtual int copy(ObjectLoc &dobj);
1139 int id() { ObjectLoc &oloc = *this; return oloc->id; }
1140 const int *_id() { ObjectLoc &oloc = *this; return &oloc->id; }
1141 int _id_size() { return sizeof(int); }
1142 int size(varObj &vobj) { return vobj.len; }
1143 int size(varObj &vobj, int sz);
1144 Index index(int i) { return entity->index(i); }
1148 int FindId(int id) { return index(idxId)->Find(&id,&obj); }
1149 int LocateId(int op, int id) { return index(idxId)->Locate(op,&id,0,0,&obj); }
1150 int FirstId() { return index(idxId)->First(0,&obj); }
1151 int LastId() { return index(idxId)->Last(0,&obj); }
1152 int NextId() { return index(idxId)->Next(0,&obj); }
1153 int FirstId(pgRef &loc) { return index(idxId)->First(loc,0,&obj); }
1154 int NextId(pgRef &loc) { return index(idxId)->Next(loc,0,&obj); }
1155 int NextLocId(pgRef &loc) { return index(idxId)->NextLoc(loc); }
1157 static int cmpr_char(const char *ap, int asz, const char *bp, int bsz);
1158 static int cmpr_uchar(const unsigned char *ap, int asz, const unsigned char *bp, int bsz);
1159 static int cmpr_short(const short *ap, int asz, const short *bp, int bsz);
1160 static int cmpr_ushort(const unsigned short *ap, int asz, const unsigned short *bp, int bsz);
1161 static int cmpr_int(const int *ap, int asz, const int *bp, int bsz);
1162 static int cmpr_uint(const unsigned int *ap, int asz, const unsigned int *bp, int bsz);
1163 static int cmpr_long(const long *ap, int asz, const long *bp, int bsz);
1164 static int cmpr_ulong(const unsigned long *ap, int asz, const unsigned long *bp, int bsz);
1165 static int cmpr_float(const float *ap, int asz, const float *bp, int bsz);
1166 static int cmpr_double(const double *ap, int asz, const double *bp, int bsz);
1168 static int cmpr_media(const unsigned char *ap, int asz, const unsigned char *bp, int bsz);
1172 int _err_(int v,const char *fn,int ln) { return entity->db->_err_(v,fn,ln); }
1173 int _fail_(int v,const char *fn,int ln) { return entity->db->_fail_(v,fn,ln); }
1175 int _err_(int v) { return entity->db->_err_(v); }
1176 int _fail_(int v) { return entity->db->_fail_(v); }
1179 int last(Index idx,ObjectLoc &last_loc);
1180 int last(const char *nm,int (ObjectLoc::*ip)());
1181 unsigned int last(const char *nm,unsigned int (ObjectLoc::*ip)());
1189 virtual int wr_key(char *cp) = 0;
1190 Key(Index i, ObjectLoc &l, CmprFn c) : loc(l), idx(i), cmpr(c) {}
1191 Key(const char *nm, ObjectLoc &l, CmprFn c) : loc(l), cmpr(c) {
1192 idx = loc.entity->index(nm);
1194 operator void *() { return (void *)this; }
1196 int _err_(int v,const char *fn,int ln) { return loc._err_(v,fn,ln); }
1197 int _fail_(int v,const char *fn,int ln) { return loc._fail_(v,fn,ln); }
1199 int _err_(int v) { return loc._err_(v); }
1200 int _fail_(int v) { return loc._fail_(v); }
1204 class rKey : public Key {
1206 rKey(Index i, ObjectLoc &l, CmprFn c) : Key(i,l,c) {}
1207 rKey(const char *nm, ObjectLoc &l, CmprFn c) : Key(nm,l,c) {}
1208 virtual int wr_key(char *cp=0) { return -1; }
1209 int NextLoc(pgRef &pos) { return idx->NextLoc(pos); }
1210 int First(); int First(pgRef &pos);
1211 int Next(); int Next(pgRef &pos);
1213 int Locate(int op=keyGE);
1216 class iKey : protected rKey {
1218 iKey(Index i, ObjectLoc &l, CmprFn c) : rKey(i,l,c) {}
1219 iKey(const char *nm, ObjectLoc &l, CmprFn c) : rKey(nm,l,c) {}
1220 int NextLoc(pgRef &pos) { return idx->NextLoc(pos); }
1222 int Locate(int op=keyGE);
1226 class EntityObj : public Obj { /* entity storage */
1228 char name[nmSz]; /* string identifier */
1229 AllocCache alloc_cache; /* entity allocator cache */
1230 int maxId; /* highest ID value */
1231 int recdSz; /* record size in bytes */
1232 int count; /* number of records */
1233 int nidxs; /* index count */
1234 int indexs[1]; /* id/loc index */
1235 EntityObj(EntityObj &eobj, int eid);
1238 class EntityLoc : public ObjectLoc {
1240 EntityObj *operator ->() { return (EntityObj *)addr(); }
1241 EntityLoc(Entity *ep) : ObjectLoc(ep) {}
1244 EntityLoc &eloc = *this; _wr();
1245 return eloc->alloc_cache.cacheFlush(entity->db);
1250 typedef varObjRef *varObjs;
1255 varObjRef(varObjs &lp, vRef rp) : next(lp), ref(rp) {}
1258 static int ioCmpr(const void *a, const void *b, void *c);
1267 operator zrwlock_t&() { return *rw_lock; }
1270 int _err_(int v,const char *fn,int ln) { return db->_err_(v,fn,ln); }
1271 int _fail_(int v,const char *fn,int ln) { return db->_fail_(v,fn,ln); }
1273 int _err_(int v) { return db->_err_(v); }
1274 int _fail_(int v) { return db->_fail_(v); }
1276 Entity(Db *const db) : db(db), ent(this), vobjs(0) {}
1279 int allocate(ObjectLoc &loc,int sz=0);
1280 int construct_(ObjectLoc &loc, int id);
1281 int construct(ObjectLoc &loc) { return construct_(loc,ent->maxId); }
1282 int destruct_(ObjectLoc &loc, int id);
1283 int destruct(ObjectLoc &loc) { return destruct_(loc, loc->id); }
1284 int deallocate(pgRef &obj) { return db->deallocate(obj,ent->alloc_cache); }
1285 int deallocate(ObjectLoc &loc) { return deallocate(loc.obj); }
1286 int get_index(const char *nm, CmprFn cmpr=0);
1287 Index index(int i) { return db->indecies[ent->indexs[i]]; }
1288 Index index(const char *nm) {
1289 int idx = get_index(nm);
1290 return idx >= 0 ? index(idx) : 0;
1292 int MaxId() { return ent->maxId; }
1293 int Count() { return ent->count; }
1294 int add_index(int idx, int kty=ktyBin);
1295 int add_bindex(const char *nm,int keySz,int dataSz) {
1296 int idx = db->new_binary_index(nm,keySz,dataSz);
1297 if_err( idx ); if_err( add_index(idx,ktyBin) );
1300 int add_ind_index(const char *nm) {
1301 int idx = db->new_binary_index(nm,0,sizeof(int),Db::cmprKey);
1302 if_err( idx ); if_err( add_index(idx,ktyInd) );
1305 int add_dir_index(const char *nm,int keySz) {
1306 int idx = db->new_binary_index(nm,keySz,sizeof(int),Db::cmprKey);
1307 if_err( idx ); if_err( add_index(idx,ktyDir) );
1310 int add_str_index(const char *nm,int dataSz) {
1311 int idx = db->new_string_index(nm, dataSz);
1312 if_err( idx ); if_err( add_index(idx,ktyDir) );
1315 int del_index_(int idx);
1316 int del_index(int idx);
1317 int del_index(const char *nm);
1318 int new_entity(const char *nm, int sz) { return db->new_entity(*this,nm,sz); }
1319 int get_entity(const char *nm) { return db->get_entity(*this,nm); }
1320 int del_entity() { return db->del_entity(*this); }
1321 void add_vref(vRef rp) { vobjs = new varObjRef(vobjs,rp); }
1325 typedef ObjectList *Objects;
1326 static void finit(Objects objects);
1332 ObjectList(Objects op, ObjectLoc &o) : next(op), obj(&o) {}
1336 int new_entity_(Entity &entity, const char *nm, int sz);
1338 int findCmprFn(CmprFn fn);
1339 int pageLoad(pageId id, Page &pg);
1340 int addrRead_(pgRef &loc, char *&vp, int mpsz=0) {
1341 Page &pg = *get_page(loc.id); vp = 0;
1342 if( unlikely( !pg.addr || pg->chk_flags(fl_rd) ) ) {
1343 if_err( pageLoad(loc.id, pg) );
1345 vp = (char *)pg.addr+loc.offset+mpsz;
1348 int addrRead_(pgRef &loc, keyBlock *&vp, int mpsz=0) {
1349 return addrRead_(loc,*(char**)&vp, mpsz);
1351 int addrRead_(pgRef &loc, allocPrefix *&vp, int mpsz=0) {
1352 return addrRead_(loc,*(char**)&vp, mpsz);
1354 int addrRead_(pgRef &loc, pagePrefix *&vp, int mpsz=0) {
1355 return addrRead_(loc,*(char**)&vp, mpsz);
1357 int addrWrite_(pgRef &loc, char *&vp, int mpsz=0) {
1358 Page &pg = *get_page(loc.id); vp = 0;
1359 if( unlikely( !pg.addr || pg->chk_flags(fl_rd) ) ) {
1360 if_err( pageLoad(loc.id, pg) );
1362 pg->set_flags(fl_wr);
1363 vp = (char *)pg.addr+loc.offset+mpsz;
1366 int addrWrite_(pgRef &loc, keyBlock *&vp, int mpsz=0) {
1367 return addrWrite_(loc,*(char**)&vp, mpsz);
1369 int addrWrite_(pgRef &loc, allocPrefix *&vp, int mpsz=0) {
1370 return addrWrite_(loc,*(char**)&vp, mpsz);
1372 int addrWrite_(pgRef &loc, pagePrefix *&vp, int mpsz=0) {
1373 return addrWrite_(loc,*(char**)&vp, mpsz);
1375 int addrRead(pgRef &loc, char *&vp) {
1376 return addrRead_(loc, vp, sizeof(allocPrefix));
1378 int addrWrite(pgRef &loc, char *&vp) {
1379 return addrWrite_(loc, vp, sizeof(allocPrefix));
1382 int objectHeapInsert(int sz,int pg,int off,AllocCache &cache);
1383 int objectHeapDelete(int sz,int pg,int off,AllocCache &cache);
1384 int objectAllocate(int typ, int &size, pgRef &loc,AllocCache &cache);
1385 int objectFree(pgRef &loc,AllocCache &cache);
1386 int pgRefGet(int &size, pgRef &loc,AllocCache &cache);
1387 int pgRefNew(int &size, pgRef &lo,AllocCache &cache);
1388 int pgRefAllocate(int &size, pgRef &lo,AllocCache &cache);
1390 int storeInsert(long size, ioAddr io_addr);
1391 int storeDelete(long size, ioAddr io_addr);
1392 int storeGet(int &size, ioAddr &io_addr);
1393 int storeNew(int &size, ioAddr &io_addr);
1394 int storeAllocate(int &size, ioAddr &io_addr);
1395 int storeFree(int size, ioAddr io_addr);
1397 int icommit(int force);
1402 int iopen(int undo_save=1);
1407 void del_page(int id);
1408 int alloc_pageTable(int sz);
1409 void free_page_(int pid);
1410 void free_page(int pid) { locked by(db_info->pgAlLk); free_page_(pid); }
1411 pageId lower_page(int mid);
1412 int alloc_indecies(int n);
1414 void del_index(int idx);
1415 int new_index(IndexBase *&ibp, IndexBaseInfo *b, IndexBinaryInfo *d);
1416 int new_index(IndexBase *&ibp, IndexBaseInfo *b, IndexStringInfo *d);
1417 int indexRead(pageId pid, int df, keyBlock *&bp) {
1418 pgRef pg; pg.id = pid; pg.offset = 0;
1419 return !df ? addrRead_(pg,*(char**)&bp) : addrWrite_(pg,*(char**)&bp);
1421 int indexRead(pageId pid,int df,keyBlock *&bp, Page *&pp, char *&cp) {
1422 pp = 0; cp = 0; if_err( indexRead(pid, df, bp) );
1423 pp = get_page(pid); cp = (char *)(bp + 1);
1426 void pageDealloc(Page &pg, int mode=1);
1427 int pageRead(ioAddr io_adr, uint8_t *bp, int len);
1428 int pageWrite(Page &pg);
1429 int seek_data(ioAddr io_addr);
1430 int size_data(char *dp, int sz);
1431 int read_data(char *dp, int sz);
1432 int write_data(char *dp, int sz);
1433 int write_zeros(ioAddr io_addr);
1434 int write_padding(ioAddr io_addr);
1435 int readRootInfo(int(Db::*fn)(char *dp,int sz));
1436 int writeRootInfo(int(Db::*fn)(char *dp,int sz));
1437 ioAddr storeBlocks(ioAddr sz) { return (sz+storeBlockSize-1)/storeBlockSize; }
1438 ioAddr entityPages(ioAddr sz) { return (sz+entityPageSize-1)/entityPageSize; }
1439 int indeciesHunks(int sz) { return (sz+indexTableHunkSize-1)/indexTableHunkSize; }
1440 int pageTableHunks(int sz) { return (sz+pageTableHunkSize-1)/pageTableHunkSize; }
1441 int pagePrefixHunks(int sz) { return (sz+sizeof(pagePrefix)-1)/sizeof(pagePrefix); }
1447 int start_transaction(int undo_save=1);
1448 void enter() { db_info->dbRwLk.enter(); }
1449 void leave() { db_info->dbRwLk.leave(); }
1450 void write_enter() { db_info->dbRwLk.write_enter(); }
1451 void write_leave() { db_info->dbRwLk.write_leave(); }
1454 // 1:wr, 0:rd, -1:unlocked
1455 int is_locked() { return db_info->dbRwLk.locked(); }
1456 int is_blocked() { return db_info->dbRwLk.blocked(); }
1459 int open(int zfd, int zkey=-1);
1462 int attach(int zrw, int zfd, int zkey);
1463 int attach(int zrw=0) { return attach(fd, key, zrw); }
1466 int copy(Db *db, Objects objs);
1467 int new_binary_index(const char *nm, int ksz, int dsz, CmprFn cmpr=0);
1468 int new_string_index(const char *nm, int dsz);
1469 int get_index(const char *nm, CmprFn cmpr=0);
1470 long get_count(int r);
1471 int ins (int r, void *key, void *data);
1472 int del (int r, void *key);
1473 int find (int r, void *key, void *rtnData=0);
1474 int locate(int r, int op, void *key, CmprFn cmpr, void *rtnKey, void *rtnData=0);
1475 int locate(int r, int op, void *key, void *rtnKey, void *rtnData=0) {
1476 return locate(r,op,key,0,rtnKey,rtnData);
1478 int first (int r, void *key, void *rtnData=0);
1479 int last (int r, void *key, void *rtnData=0);
1480 int next (int r, void *key, void *rtnData=0);
1481 int nextloc(int r, pgRef &loc);
1482 int allocate(int typ, int size, pgRef &loc, AllocCache &cache);
1483 int reallocate(int size, pgRef &loc, AllocCache &cache);
1484 int deallocate(pgRef &loc, AllocCache &cache);
1485 int commit(int force=0);
1489 int transaction() { return !root_info ? -1 : root_info->transaction_id; }
1490 int64_t filesize() { return !root_info ? -1 : root_info->file_size; }
1491 int opened() { return fd>=0 ? 1 : 0; }
1492 void use_shm(int v) { no_shm = v ? 0 : 1; }
1493 int error() { return err_no; }
1495 void Error(int v,const char *msg);
1497 int load_indecies();
1507 void admp(); void achk(); void fchk();
1508 void edmp(AllocCache &cache);
1509 void bdmp(AllocCache &cache);