1 #if defined(__i386__) || defined(__x86_64__)
10 #define entityIdIndex indecies[entityIdIdx]
11 #define entityNmIndex indecies[entityNmIdx]
12 #define freeStoreIndex indecies[freeStoreIdx]
13 #define addrStoreIndex indecies[addrStoreIdx]
14 #define freeSpaceIndex indecies[cache.freeIdx]
15 #define addrSpaceIndex indecies[cache.addrIdx]
17 #define noThrow std::nothrow
19 #define likely(x) (__builtin_constant_p(x) ? !!(x) : __builtin_expect(!!(x), 1))
20 #define unlikely(x) (__builtin_constant_p(x) ? !!(x) : __builtin_expect(!!(x), 0))
23 #define lengthof(x) ((int)(sizeof(x)/sizeof(x[0])))
27 inline void *operator new(size_t n) { void *vp = malloc(n); bzero(vp,n); return vp; }
28 inline void operator delete(void *t) { free(t); }
29 inline void operator delete(void *t,size_t n) { free(t); }
30 inline void *operator new[](size_t n) { void *vp = malloc(n); bzero(vp,n); return vp; }
31 inline void operator delete[](void *t) { free(t); }
32 inline void operator delete[](void *t,size_t n) { free(t); }
40 #include <linux/futex.h>
41 #include <sys/syscall.h>
49 class nm##Obj : public Db::Obj { public:
52 class nm##Loc : public Db::ObjectLoc { public: \
53 nm##Obj *operator ->() { return (nm##Obj *)addr(); } \
54 nm##Loc(Db::Entity *ep) : Db::ObjectLoc(ep) {} \
55 nm##Loc(Db::Entity &e) : Db::ObjectLoc(&e) {}
58 #define basic_def(ty,n) class t_##n { public: ty v; t_##n() {} \
59 t_##n(const ty &i) : v(i) {} \
60 t_##n(const t_##n &i) : v(i.v) {} \
61 t_##n &operator =(const t_##n &i) { v = i.v; return *this; } \
62 ty &operator =(const ty &i) { return v = i; } \
63 ty *addr() { return &v; } int size() { return sizeof(v); } \
67 #define array_def(ty,n,l) class t_##n { public: ty v[l]; t_##n() {} \
68 t_##n(const t_##n &i) { memcpy(&v,&i.v,sizeof(v)); } \
69 t_##n(ty *i) { memcpy(&v,i,sizeof(v)); } \
70 t_##n(ty(*i)[l]) { memcpy(&v,i,sizeof(v)); } \
71 ty *operator =(const ty *i) { memcpy(&v,i,sizeof(v)); return &v[0]; } \
72 ty *addr() { return &v[0]; } int size() { return sizeof(v); } \
75 // variable array definitions
76 #define varray_def(ty,n) \
77 class t_##n { public: char *v; int l; t_##n() {} \
78 t_##n(const char *i, int sz) { v = (char *)i; l = sz; } \
79 t_##n(const unsigned char *i, int sz) { v = (char *)i; l = sz; } \
80 ty *addr() { return (ty *)v; } int size() { return l; } \
83 // string array definitions
84 #define sarray_def(ty,n) \
85 class t_##n { public: char *v; int l; t_##n() {} \
86 t_##n(const char *i, int sz) { v = (char *)i; l = sz; } \
87 t_##n(const unsigned char *i, int sz) { v = (char *)i; l = sz; } \
88 t_##n(const char *i) { t_##n(i,strlen(i)+1); } \
89 t_##n(const unsigned char *i) { t_##n(i,strlen(v)+1); } \
90 ty *addr() { return (ty *)v; } int size() { return l; } \
94 #define basic_ref(ty,n) \
95 ty *_##n() { return (*this)->v_##n.addr(); } \
96 ty n() { return *_##n(); } \
97 void n(ty i) { _wr(); *_##n() = i; } \
98 int size_##n() { return (*this)->v_##n.size(); } \
101 #define array_ref(ty,n,l) \
102 ty *_##n() { return (*this)->v_##n.addr(); } \
103 ty (&n())[l] { return *(ty (*)[l])_##n(); } \
104 void n(const ty *i,int m) { _wr(); if( m > 0 ) memcpy(n(),i,m); } \
105 void n(const ty *i) { n(i,(*this)->v_##n.size()); } \
106 int size_##n() { return (*this)->v_##n.size(); } \
108 //variable array type ref
109 #define varray_ref(ty,n) \
110 ty *_##n() { return (ty *)addr((*this)->v_##n); } \
111 ty *_##n(int sz) { size((*this)->v_##n, sz); \
112 return sz > 0 ? (ty *)addr_wr((*this)->v_##n) : 0; } \
113 ty (&n())[] { return *(ty (*)[])_##n(); } \
114 int n(const ty *v, int sz) { ty *vp=_##n(sz); \
115 if( vp && sz > 0 ) memcpy(vp, v, sz); return 0; } \
116 int size_##n() { return (*this)->v_##n.size(); } \
118 //string array type ref
119 #define sarray_ref(ty,n) \
120 ty *_##n() { return (ty *)addr((*this)->v_##n); } \
121 ty *_##n(int sz) { size((*this)->v_##n, sz); \
122 return sz > 0 ? (ty *)addr_wr((*this)->v_##n) : 0; } \
123 ty (&n())[] { return *(ty (*)[])_##n(); } \
124 int n(const ty *v, int sz) { ty *vp=_##n(sz); \
125 if( vp && sz > 0 ) memcpy(vp, v, sz); return 0; } \
126 int n(const char *v) { return n((ty *)v,strlen(v)+1); } \
127 int n(const unsigned char *v) { return n((const char *)v); } \
128 int size_##n() { return (*this)->v_##n.size(); } \
133 #define DEBUG_TIMESTAMPS
134 #define DBBUG_ERR 0x00000001
135 #define DBBUG_FAIL 0x00000002
137 //#define CHK 1 ? 0 :
154 int root_magic; // info_magic label
155 int root_info_size; // root_info blob size
159 ioAddr root_info_addr;
160 ioAddr last_info_addr;
161 transId transaction_id; // current transaction
162 ioAddr file_size; // current file size
163 pageId freePages; // free page table page list
164 int indeciesUsed; // number of active indecies
165 int pageTableUsed; // number of active pages
170 int shm_init, no_shm;
172 static void *get_mem8_t(int id);
173 static void *new_mem8_t(int size, int &id);
174 static int del_mem8_t(const void *vp, int id);
175 static void *get_shm8_t(int id);
176 static void *new_shm8_t(int size, int &id);
177 static int del_shm8_t(const void *vp, int id);
178 void *(*get_mem)(int id);
179 void *(*new_mem)(int size, int &id);
180 int (*del_mem)(const void *vp, int id);
183 uint8_t *get_uint8_t(int id, int pg=-1);
184 uint8_t *new_uint8_t(int size, int &id, int pg=-1);
185 int del_uint8_t(const void *vp, int id=-1, int pg=-1);
188 typedef int (*CmprFn)(char *,char *);
189 static int cmprFrSt(char *a, char *b);
190 static int cmprAdSt(char *a, char *b);
191 static int cmprFrSp(char *a, char *b);
192 static int cmprAdSp(char *a, char *b);
193 static int cmprOIds(char *a, char *b);
194 static int cmprStr(char *a, char *b);
195 static int cmprKey(char *a, char *b);
196 static int cmprLast(char *a, char *b);
197 static CmprFn cmprFns[];
198 typedef void (*errCallback)(Db *db, int v);
199 static const pageId NIL=-1, DDONE=-2;
217 idxId = 0, nmSz = 32,
219 keyLT=-2, keyLE=-1, keyEQ=0, keyGE=1, keyGT=2,
228 static void zincr(volatile int &v) { /* atomic(++v) */
229 asm ( " lock incl %1\n" : "+m" (v) :: );
231 static void zdecr(volatile int &v) { /* atomic(--v) */
232 asm ( " lock decl %1\n" : "+m" (v) :: );
234 static char tdecr(volatile int &v) {
235 char ret; /* ret = atomic(--loc >= 0 ? 1 : 0) */
236 asm ( " lock decl %1\n setge %0\n" : "=r" (ret), "+m" (v) :: );
239 static char tincr(volatile int &v) {
240 char ret; /* ret = atomic(++loc > 0 ? 1 : 0) */
241 asm ( " lock incl %1\n setg %0\n" : "=r" (ret), "+m" (v) :: );
244 static int zcmpxchg(int old, int val, volatile int &v) {
246 asm volatile( " lock\n cmpxchgl %2,%1\n"
247 : "+a" (ret), "+m" (v) : "r" (val) : "memory" );
250 static int zxchg(int val, volatile int &v) {
251 asm volatile( " xchgl %0,%1\n"
252 : "+r" (val), "+m" (v) :: "memory" );
255 static int zadd(int n, volatile int &v) {
257 do { val = (old=v)+n; mod = zcmpxchg(old,val,v);
258 } while( mod != old );
261 static void zmfence() {
262 asm volatile ( " mfence\n" ::: "memory" );
270 #define ZLOCK_INIT zzlock_t()
274 int zfutex(int op, int val, timespec *time=0) {
275 return syscall(SYS_futex,&loc,op,val,time,0,0);
280 int zwake(int nwakeups);
281 int zwait(int val, timespec *ts=0);
282 int zwait() { return zwait(loc); }
283 zloc_t() : loc(-1) {}
287 class zlock_t : zloc_t {
291 void *vp; asm ("movq %%fs:%c1,%q0" : "=r" (vp) : "i" (16));
293 void *vp; asm ("mov %%fs:%c1,%q0" : "=r" (vp) : "i" (16));
298 friend class zblock_t;
299 friend class zrwlock_t;
301 int zunlock(int nwakeups=1);
305 int v, ret = unlikely( (v=zcmpxchg(-1,0,loc)) >= 0 ) ? zlock(v) : 0;
310 if( unlikely(loc < 0) ) { return zemsg1(); }
312 int v, ret = unlikely( (v=zcmpxchg(0,-1,loc)) != 0 ) ? zunlock() : 0;
315 zlock_t() { owner = 0; }
319 class zblock_t : zlock_t {
321 void block() { loc = 0; zwait(0); }
322 void unblock() { loc = -1; zwake(INT_MAX); }
327 class zrwlock_t : zloc_t {
332 void enter() { zincr(loc); if( unlikely( lk.loc >= 0 ) ) zenter(); }
333 void leave() { if( unlikely( !tdecr(loc) ) ) zleave(); }
336 int locked() { return loc >= 0 ? 0 : lk.loc >= 0 ? 1 : -1; }
337 int blocked() { return lk.loc >= 0 ? 1 : 0; }
343 #define ZLOCK_INIT { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER }
347 pthread_mutex_t zlock;
349 void lock() { pthread_mutex_lock(&zlock); }
350 void unlock() { pthread_mutex_unlock(&zlock); }
351 zlock_t() { pthread_mutex_init(&zlock, 0); }
352 ~zlock_t() { pthread_mutex_destroy(&zlock); }
356 pthread_mutex_t zblock;
360 pthread_mutex_init(&zblock, 0);
361 pthread_cond_init(&cond, 0);
364 pthread_mutex_destroy(&zblock);
365 pthread_cond_destroy(&cond);
368 pthread_mutex_lock(&zblock);
369 pthread_cond_wait(&cond, &zblock);
370 pthread_mutex_unlock(&zblock);
372 void unblock() { pthread_cond_broadcast(&cond); }
375 class zrwlock_t : zlock_t {
376 volatile int blocking, users;
379 void wait() { pthread_cond_wait(&cond, &zlock); }
380 void wake() { pthread_cond_signal(&cond); }
382 void enter() { lock();
383 while( blocking ) { unlock(); lk.lock(); lk.unlock(); lock(); }
386 void leave() { lock(); if( !--users && blocking ) wake(); unlock(); }
387 void write_enter() { lk.lock(); blocking = 1;
388 lock(); while( users ) wait(); unlock();
390 void write_leave() { blocking = 0; lk.unlock(); }
391 int count() { return users; }
392 int locked() { return users ? 0 : blocking ? 1 : -1; }
393 int blocked() { return blocking; }
395 zrwlock_t() { pthread_cond_init(&cond, 0); users = 0; blocking = 0; }
396 ~zrwlock_t() { pthread_cond_destroy(&cond); }
404 locked(zlock_t &l) : lk(l) { lk.lock(); }
405 ~locked() { lk.unlock(); }
411 read_locked(zrwlock_t &l) : rwlk(l) { rwlk.enter(); }
412 ~read_locked() { rwlk.leave(); }
415 class write_blocked {
418 write_blocked(zrwlock_t &l) : rwlk(l) { rwlk.write_enter(); }
419 ~write_blocked() { rwlk.write_leave(); }
425 db_magic=0x00624474, // tDb
426 idx_magic=0x00786469, // idx
427 info_magic=0x6f666e69, // info
428 root_magic=0x746f6f72, // root
429 page_magic=0x6770, // pg
430 entity_magic = 0x6d65, // em
432 root_info_extra_pages = 2,
433 idxNil=0, idxBin=1, idxStr=2,
434 ktyBin=0, ktyInd=1, ktyDir=2,
435 opDelete=-1, opFind=0, opInsert=1,
436 pg_unknown=0, pg_root_info=1, pg_free=2,
437 pg_entity=0x0100, pg_index=0x1000,
438 max_entity_type = pg_index-pg_entity-1,
439 max_index_type = 0x10000-pg_index-1,
440 min_heap_allocation = 32,
441 entityIdIdx = 0, entityNmIdx = 1,
442 freeStoreIdx = 2, addrStoreIdx = 3,
443 freeSpaceIdx = 4, addrSpaceIdx = 5,
445 fl_wr=1, fl_rd=2, fl_new=4, fl_free=8,
447 defaultStoreBlockSize = 8192,
448 defaultPageTableHunkSize = 8192,
449 defaultIndexTableHunkSize = 4096,
450 defaultBinaryBlockSize = 16384,
451 defaultStringBlockSize = 4096,
452 defaultEntityPageSize = 65536,
457 errCallback err_callback;
459 transId active_transaction;
463 static const char *errMsgs[];
464 static void dmsg(int msk, const char *msg,...);
465 int _err_(int v,const char *fn,int ln);
466 int _fail_(int v,const char *fn,int ln);
467 #define err_(v) _err_(v,__func__,__LINE__)
468 #define fail_(v) _fail_(v,__func__,__LINE__)
470 static void dmsg(int msk, const char *msg,...) {}
471 int _err_(int v) { error(v); return v; }
472 int _fail_(int v) { return v; }
473 #define err_(v) _err_(v)
474 #define fail_(v) _fail_(v)
477 #define Err(v) return err_(v)
478 #define Fail(v) return fail_(v)
480 #define if_ret(fn) do{ int _ret; \
481 if(unlikely((_ret=(fn))<0)) return _ret; \
483 #define if_err(fn) do{ int _ret; \
484 if(unlikely((_ret=(fn))<0)) Err(_ret); \
486 #define if_fail(fn) do{ int _ret; \
487 if(unlikely((_ret=(fn))<0)) Fail(_ret); \
493 int owner, last_owner;
494 int info_key, info_id;
498 zlock_t infoLk; // lock dbinfo up to here
499 zrwlock_t dbRwLk; // global lock
500 zrwlock_t pgTblLk;// pageTable realloc
501 zlock_t pgAlLk; // new page pagesUsed/pagesAllocated
502 zlock_t pgLdLk; // pageLoad
503 zlock_t blkAlLk; // blockAllocate/Free
504 zlock_t objAlLk; // objectAllocate/Free
505 zrwlock_t rw_locks[max_entity_type];
507 DbInfo(int pid, int key, int id);
509 int new_info(int key);
510 int get_info(int key);
515 int attach_rw(int zrw) { return zrw ? attach_wr() : attach_rd(); }
522 unsigned short magic;
527 Page *get_Page(pageId pid) volatile { return pageTable[pid]; }
528 void set_Page(pageId pid, Page *pp) { pageTable[pid] = pp; }
529 //Page *get_page(pageId pid) { blocked by(pgTblk); return get_Page(pid); }
530 Page *get_page(pageId pid); // locked pageTable access
532 static pageId getPageId(unsigned char *&bp) {
533 int i = sizeof(pageId); pageId id;
534 for( id = *bp++; --i > 0; id |= *bp++ ) id <<= 8;
537 static void putPageId(unsigned char *&bp, pageId id) {
538 int i = sizeof(pageId) * 8;
539 while( (i -= 8) >= 0 ) *bp++ = id >> i;
541 static pageId readPageId(char *cp) {
542 unsigned char *bp = (unsigned char *)cp;
543 return getPageId(bp);
545 static void writePageId(char *cp, pageId id) {
546 unsigned char *bp = (unsigned char *)cp;
550 class keyBlock : public pagePrefix {
551 char rightLink[sizeof(pageId)];
553 int right_link() { return readPageId(&rightLink[0]); }
554 void right_link(pageId id) { writePageId(&rightLink[0],id); }
557 static int defaultBlockSizes[];
559 class IndexTypeInfo {
562 short type; /* type of index */
564 char name[nmSz]; /* index string identifier */
567 class IndexBaseType : public IndexTypeInfo {
569 IndexBaseType(int typ);
572 class IndexRecdInfo {
574 int idx; /* index in db->indecies[] */
575 int keySz, dataSz; /* sizeof key/data fields in bytes */
576 pageId rootPageId; /* index root page ID */
577 pageId rightHandSide; /* the right hand side of the tree for this index */
578 pageId freeBlocks; /* free index page list */
579 unsigned int blockSize; /* size of new index blocks */
581 long count; /* index population count */
584 class IndexBaseRecd : public IndexRecdInfo {
586 IndexBaseRecd(int typ, int zidx, int ksz, int dsz);
589 class IndexBaseStorage;
590 class IndexBaseInfo : public IndexTypeInfo, public IndexRecdInfo {
592 operator IndexBaseStorage *() { return (IndexBaseStorage *)this; }
595 class IndexBaseStorage : public IndexBaseInfo {
597 IndexBaseStorage(int typ, int zidx, int ksz, int dsz);
598 IndexBaseStorage() {}
599 ~IndexBaseStorage() {}
603 IndexBaseStorage *st;
605 Db *db; /* owner db */
607 pgRef lastAccess, lastFind; /* last operational access/find location */
608 pgRef lastInsert, lastDelete; /* last operational insert/delete location */
609 pgRef lastNext; /* last operational next location */
610 int kdSz; /* keySz + dataSz */
611 int lastOp; /* last operation, delete=-1/find=0/insert=1 */
612 int cInsCount; /* number of consecutive insertions */
613 int cFindCount; /* number of consecutive finds */
614 int cDelCount; /* number of consecutive deletions */
617 virtual int keyMap(pageId s, int(IndexBase::*fn)(pageId id)) = 0;
618 virtual int keyCopy(pageId s, IndexBase *ib) = 0;
619 int blockAllocate(pageId &pid, keyBlock *&bp);
620 int blockAllocate(pageId &pid, keyBlock *&bp, Page *&pp, char *&cp) {
621 pp = 0; cp = 0; if_err( blockAllocate(pid, bp) );
622 pp = db->get_page(pid); cp = (char *)(bp + 1);
625 int blockFree(pageId pid);
626 int blockRelease(pageId pid);
627 int blockLoad(pageId pid);
628 int deleteFreeBlocks();
630 void chkLastInsert();
631 void chkLastDelete();
632 void chkLastFind(pgRef &last);
635 int _err_(int v,const char *fn,int ln) { return db->_err_(v,fn,ln); }
636 int _fail_(int v,const char *fn,int ln) { return db->_fail_(v,fn,ln); }
638 int _err_(int v) { return db->_err_(v); }
639 int _fail_(int v) { return db->_fail_(v); }
641 virtual int Locate(int op,void *key,CmprFn cmpr,void *rtnKey,void *rtnData) = 0;
642 virtual int Find(void *key,void *rtnData) = 0;
643 virtual int Insert(void *key,void *data) = 0;
644 virtual int Delete(void *key) = 0;
645 virtual int First(void *rtnKey,void *rtnData) = 0;
646 virtual int Last(void *rtnKey,void *rtnData) = 0;
647 virtual int Modify(void *key,void *recd) = 0;
648 virtual int Next(pgRef &loc,void *rtnKey,void *rtnData) = 0;
649 int First(pgRef &loc,void *rtnKey,void *rtnData) {
650 if_fail( First(rtnKey, rtnData) );
654 int Next(void *rtnKey,void *rtnData) {
655 if( lastNext.id < 0 ) Fail(errInvalid);
656 return Next(lastNext,rtnKey,rtnData);
658 long Count() { return st->count; }
662 int NextLoc(pgRef &loc) { loc = lastNext; return 0; }
663 IndexBase(Db *zdb, int typ, int zidx, int ksz, int dsz);
664 IndexBase(Db *zdb, IndexBaseStorage &d);
665 virtual ~IndexBase();
668 int indeciesAllocated, indecies_sz;
670 class IndexBinaryStorage;
671 class IndexBinaryInfo {
673 operator IndexBinaryStorage *() { return (IndexBinaryStorage *)this; }
677 class IndexBinaryStorage : public IndexBinaryInfo {
679 IndexBinaryStorage(int cmprId) { this->cmprId = cmprId; }
680 IndexBinaryStorage() {}
681 ~IndexBinaryStorage() {}
684 class IndexBinary : public IndexBase {
685 IndexBinaryStorage *bst;
687 CmprFn compare; /* the key compare function type */
688 char *akey; /* pointer to key argument */
689 int keyInterior; /* last insert interior/exterior */
690 int idf; /* interior delete flag */
691 char *iky, *tky; /* search/promoted temp key storage */
694 int keyMap(pageId s, int(IndexBase::*fn)(pageId id));
695 int keyCopy(pageId s, IndexBase *ib);
696 int keyBlockUnderflow(int &t,keyBlock *lbb,pageId p,keyBlock *pbb,int pi);
697 void makeKey(char *cp,char *key,int l,char *recd,int n);
698 void setLastKey(pageId s,pageId u,int k);
699 int keyLocate(pgRef &last,pageId s, int op,void *ky, CmprFn cmpr);
700 int chkNext(pgRef &loc, char *&kp);
701 int keyNext(pgRef &loc, char *kp);
702 int chkFind(pgRef &loc, char *key);
703 int keyFind(pgRef &loc,void *ky, pageId s);
704 int chkInsert(void *key,void *data);
705 int keyInsert(pageId s, pageId &t);
706 int chkDelete(pgRef &loc, void *kp);
707 int keyDelete(int &t,void *kp,pageId s,pageId p,keyBlock *pbb,int pi);
708 int keyFirst(pgRef &loc, pageId s);
709 int keyLast(pgRef &loc, pageId s);
711 int refLocate(pgRef &loc, int op,void *key, CmprFn cmpr);
712 int Locate(int op,void *key,CmprFn cmpr,void *rtnKey,void *rtnData);
713 int refFind(pgRef &loc, void *key);
714 int Find(void *key,void *rtnData);
715 int Insert(void *key,void *data);
716 int Delete(void *key);
717 int First(void *rtnKey,void *rtnData);
718 int Last(void *rtnKey,void *rtnData);
719 int Modify(void *key,void *recd);
720 int Next(pgRef &loc,void *rtnKey,void *rtnData);
721 int Next(void *rtnKey,void *rtnData) {
722 return IndexBase::Next(rtnKey,rtnData);
724 void wr_key(void *kp, char *bp, int sz) {
725 switch( st->key_type ) {
726 case ktyBin: memcpy(bp,kp,sz); break;
728 case ktyInd: ((Key *)kp)->wr_key(bp); break;
731 char *ikey() { return iky; }
732 char *tkey() { return tky; }
734 IndexBinary(Db *zdb, int zidx, int ksz, int dsz, CmprFn cmpr);
735 IndexBinary(Db *zdb, IndexBaseStorage *b, IndexBinaryStorage *d);
736 IndexBinary(IndexBase *ib, IndexBaseStorage *b, IndexBinaryStorage *d);
739 friend class IndexBinary;
741 class IndexStringStorage;
742 class IndexStringInfo {
743 char dummy; // compiler needs this for some reason
745 operator IndexStringStorage *() { return (IndexStringStorage *)this; }
748 class IndexStringStorage : public IndexStringInfo {
750 IndexStringStorage() {}
751 ~IndexStringStorage() {}
754 class IndexString : public IndexBase {
755 IndexStringStorage *sst;
757 static int ustrcmp(unsigned char *a, unsigned char *b) {
758 return strncmp((char *)a,(char *)b,keysz);
760 static void ustrcpy(unsigned char *a, unsigned char *b) {
761 strncpy((char *)a,(char *)b,keysz);
763 static void umemmove(unsigned char *&a, unsigned char *b, int n) {
764 memmove(a,b,n); a += n;
766 static int kpress(unsigned char *kp, unsigned char *lp, unsigned char *cp);
767 int split(int n, int i, pageId s, pageId &l, pageId r);
769 int keyMap(pageId s, int(IndexBase::*fn)(pageId id));
770 int keyCopy(pageId s, IndexBase *ib);
771 int chkInsert(void *key,void *data);
772 int keyInsert(pageId &t, pageId s);
773 int keyFirst(pageId s);
774 int keyLast(pageId s);
775 int keyLocate(pgRef &last,pageId s,int &t, int op,
776 unsigned char *ky,CmprFn cmpr, unsigned char *rky);
777 int chkFind(pgRef &loc, char *key, unsigned char *lkey, unsigned char *lky=0);
778 int keyFind(pgRef &loc, unsigned char *ky);
779 int keyNext(pgRef &loc, unsigned char *rky);
780 int keyUnderflow(pageId s, pageId &t, int k);
781 int keyOverflow(pageId s, pageId &t, int k, int o);
782 int keyRemap(pageId s, pageId &t, int k, int o);
783 int keyDelete(pageId s, pageId &t);
785 unsigned char lastAccKey[keysz], lastFndKey[keysz];
786 unsigned char lastInsKey[keysz], lastDelKey[keysz];
787 unsigned char lastNxtKey[keysz];
788 unsigned char *tky, *dky; // dataSz+keysz+1
789 unsigned char *tbfr; // 3*allocated
790 unsigned char akey[keysz]; // key in use
791 int idf; /* interior delete flag */
793 int refLocate(pgRef &loc, int op, void *key, CmprFn cmpr, unsigned char *rkey);
794 int Locate(int op,void *key,CmprFn cmpr,void *rtnKey,void *rtnData);
795 int refFind(pgRef &loc, void *key);
796 int Find(void *key,void *rtnData);
797 int Insert(void *key,void *data);
798 int Delete(void *key);
799 int First(void *rtnKey,void *rtnData);
800 int Last(void *rtnKey,void *rtnData);
801 int Modify(void *key,void *recd);
802 int Next(pgRef &loc,void *rtnKey,void *rtnData);
803 int Next(void *rtnKey,void *rtnData) {
804 return IndexBase::Next(rtnKey,rtnData);
808 IndexString(Db *zdb, int zidx, int dsz);
809 IndexString(Db *zdb, IndexBaseStorage *b, IndexStringStorage *d);
810 IndexString(IndexBase *ib, IndexBaseStorage *b, IndexStringStorage *d);
813 friend class IndexString;
815 class IndexBinaryData : public IndexBaseInfo, public IndexBinaryInfo {};
816 class IndexStringData : public IndexBaseInfo, public IndexStringInfo {};
823 IndexInfo *index_info; /* image for index storage */
827 unsigned short magic;
832 class freeStoreRecord {
838 class addrStoreRecord {
844 class freeSpaceRecord {
851 class addrSpaceRecord {
863 int cacheFlush(Db *db);
864 int Get(Db *db,int &size, pgRef &ref);
865 int Load(Db *db, pageId id, int ofs, int sz);
866 void init() { loc.id = NIL; loc.offset = 0; avail = 0; }
867 void init(pageId id, int ofs, int sz) {
868 loc.id = id; loc.offset = ofs; avail = sz;
871 printf("loc: %d/%d avl: %d\n", loc.id,loc.offset,avail);
873 int freeIdx, addrIdx;
874 int init_idx(Db *db, const char *nm);
877 int cacheFlush() { return alloc_cache.cacheFlush(this); }
878 void cacheDelete(AllocCache &cache);
879 int cache_all_flush();
884 unsigned int allocated;
885 unsigned short flags;
896 PageStorage() { init(); }
898 int chk_flags(int fl) { return flags & fl; }
899 int set_flags(int fl) { return flags |= fl; }
900 int clr_flags(int fl) { return flags &= ~fl; }
910 // PageStorage access
911 int iused() { return st->used-sizeof(keyBlock); }
912 void iused(int v) { st->used = v+sizeof(keyBlock); }
913 int iallocated() { return st->allocated-sizeof(keyBlock); }
914 void iallocated(int v) { st->allocated = v+sizeof(keyBlock); }
915 PageStorage *operator ->() { return st; }
917 void reset_to(Page *pp);
918 Page(PageStorage &d) { st = &d; init(); }
922 int pageTableAllocated, page_table_sz;
926 int pageTableHunkSize;
927 int indexTableHunkSize;
936 int cfnAllocated, cfnUsed;
937 undoData() : cfn(0), cfnAllocated(0), cfnUsed(0) {}
938 ~undoData() { delete [] cfn; cfnAllocated = cfnUsed = 0; }
943 ioAddr file_position;
945 char *bfr, *lmt, *inp;
949 int write_bfr(char *dp, int sz);
955 inline static unsigned int on_bits(unsigned int n) {
956 n = (n & 0x55555555) + ((n >> 1) & 0x55555555);
957 n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
958 n = (n & 0x0f0f0f0f) + ((n >> 4) & 0x0f0f0f0f);
959 n += n >> 8; n += n >> 16; //ok, fldsz > 5 bits
964 inline static unsigned int low_bit(unsigned int n) {
968 // bit number of lowest on bit
969 inline static unsigned int low_bit_no(unsigned int n) {
970 return on_bits(low_bit(n) - 1);
973 // highest on bit, and all lower bits set
974 inline static unsigned int high_bit_mask(unsigned int n) {
975 n |= n >> 1; n |= n >> 2;
976 n |= n >> 4; n |= n >> 8;
982 inline static unsigned int high_bit(unsigned int n) {
983 unsigned m = high_bit_mask(n);
987 // bit number of highest on bit
988 inline static unsigned int high_bit_no(unsigned int n) {
989 return on_bits(high_bit_mask(n)) - 1;
993 static inline int cpu_aligned() {
995 asm volatile( "pushf\n" "pop %0\n" : "=rm" (flags) );
996 return (flags>>18) & 1;
1002 uint64_t clip(int64_t v) { return v<0 ? 0 : (uint64_t)v>vmx ? vmx : v; }
1004 enum { alignBits = 8, };
1006 static void init(int v=-1) { aligned = v >= 0 ? v : cpu_aligned(); }
1007 void put(uint64_t v, int n);
1008 void putc(uint64_t v, int n) { put(clip(v), n); }
1009 void iput(int v) { put(v, 8*sizeof(int)); }
1010 void lput(int64_t v) { put(v, 8*sizeof(int64_t)); }
1011 uint64_t get(int n);
1012 int iget() { return get(8*sizeof(int)); }
1013 int64_t lget() { return get(8*sizeof(int64_t)); }
1014 int pos() { return idx; }
1015 void seek(int i) { idx = i; }
1016 void init(uint8_t *bp) { bits = bp; idx = 0; vmx = 0; }
1017 uint8_t *bfr() { return this->bits; }
1018 void align() { idx = (idx+alignBits-1) & ~(alignBits-1); }
1019 void *addr() { return &bits[idx/8]; }
1020 void set_max(uint64_t v) { vmx = v; }
1022 pack() : bits(0) { idx = 0; }
1023 pack(uint8_t *bp) { init(bp); }
1033 static int64_t bit_size(int len, int w);
1034 static int64_t byte_size(int len, int w);
1035 static void build(uint8_t *kp, uint8_t *bp, int w, int len) {
1036 mediaKey key(kp, bp, w, len);
1038 static int64_t count(void *kp) {
1039 mediaKey *mkp = (mediaKey *)kp;
1040 return be64toh(mkp->cnt);
1042 static int64_t set_count(void *kp, int64_t v) {
1043 mediaKey *mkp = (mediaKey *)kp;
1044 return mkp->cnt = htobe64(v);
1046 static int64_t incr_count(void *kp, int64_t dv) {
1047 return set_count(kp, count(kp) + dv);
1049 static int64_t count1(uint8_t *kp);
1050 mediaKey(uint8_t *kp, uint8_t *bp, int w, int len);
1056 int w, len, dsz, psz, spos;
1057 int64_t cnt, *dat, **dp;
1058 void get_fields(int n, int k);
1059 int dsize(int n) { int m = (n+1)/2; return m>1 ? m+dsize(m) : 1; }
1062 void load(uint8_t *kp);
1064 mediaLoad(uint8_t *bp);
1070 int w, len, dsz, psz, spos;
1071 int64_t acnt, bcnt, *adat, *bdat, **adp, **bdp;
1073 uint64_t sqr(int64_t v) { return v*v; }
1074 int dsize(int n) { int m = (n+1)/2; return m>1 ? m+dsize(m) : 1; }
1075 uint64_t chk_fields(int m, int k);
1076 int cmpr_fields(int m, int k);
1079 uint64_t chk(uint8_t *kp, uint64_t lmt=~0);
1080 int cmpr(uint8_t *kp, uint64_t lmt=~0);
1082 mediaCmpr(uint8_t *bp);
1089 typedef IndexBase *Index;
1090 int new_entity(Entity &entity, const char *nm, int sz);
1091 int get_entity(Entity &entity, const char *nm);
1092 int del_entity(Entity &entity);
1094 class Obj { /* per object storage base class */
1102 int size() { return len; }
1103 void init() { len = -1; loc.id = NIL; loc.offset = 0; }
1104 void del(Entity *entity) { len = -1; entity->deallocate(loc); }
1106 typedef varObj Obj::*vRef;
1112 Obj *addr(pgRef &loc) {
1114 return loc.id < 0 || entity->db->addrRead(loc,op) ? 0 : (Obj *)op;
1116 Obj *addr_wr(pgRef &loc) {
1118 return loc.id < 0 || entity->db->addrWrite(loc,op) ? 0 : (Obj *)op;
1120 void _wr() { Page &pg = *entity->db->get_page(obj.id); pg->set_flags(fl_wr); }
1121 Obj *addr() { return addr(obj); }
1122 Obj *addr_wr() { return addr_wr(obj); }
1123 void *addr(varObj &vobj) { return addr(vobj.loc); }
1124 void *addr_wr(varObj &vobj) { return addr_wr(vobj.loc); }
1125 Obj *operator ->() { return (Obj *)addr(); }
1126 ObjectLoc(Entity *ep) : entity(ep) { obj.id = NIL; obj.offset = 0; }
1129 virtual int allocate(int sz=0) { return entity->allocate(*this,sz); }
1130 virtual int construct() { return entity->construct(*this); }
1131 virtual int destruct() { return entity->destruct(*this); }
1132 virtual int deallocate() { return entity->deallocate(*this); }
1133 virtual int insertCascade() { return 0; }
1134 virtual int insertProhibit() { return 0; }
1135 virtual int deleteCascade() { return 0; }
1136 virtual int deleteProhibit() { return 0; }
1137 virtual int modifyCascade() { return 0; }
1138 virtual int modifyProhibit() { return 0; }
1139 virtual int copy(ObjectLoc &dobj);
1140 int id() { ObjectLoc &oloc = *this; return oloc->id; }
1141 const int *_id() { ObjectLoc &oloc = *this; return &oloc->id; }
1142 int _id_size() { return sizeof(int); }
1143 int size(varObj &vobj) { return vobj.len; }
1144 int size(varObj &vobj, int sz);
1145 Index index(int i) { return entity->index(i); }
1149 int FindId(int id) { return index(idxId)->Find(&id,&obj); }
1150 int LocateId(int op, int id) { return index(idxId)->Locate(op,&id,0,0,&obj); }
1151 int FirstId() { return index(idxId)->First(0,&obj); }
1152 int LastId() { return index(idxId)->Last(0,&obj); }
1153 int NextId() { return index(idxId)->Next(0,&obj); }
1154 int FirstId(pgRef &loc) { return index(idxId)->First(loc,0,&obj); }
1155 int NextId(pgRef &loc) { return index(idxId)->Next(loc,0,&obj); }
1156 int NextLocId(pgRef &loc) { return index(idxId)->NextLoc(loc); }
1158 static int cmpr_char(const char *ap, int asz, const char *bp, int bsz);
1159 static int cmpr_uchar(const unsigned char *ap, int asz, const unsigned char *bp, int bsz);
1160 static int cmpr_short(const short *ap, int asz, const short *bp, int bsz);
1161 static int cmpr_ushort(const unsigned short *ap, int asz, const unsigned short *bp, int bsz);
1162 static int cmpr_int(const int *ap, int asz, const int *bp, int bsz);
1163 static int cmpr_uint(const unsigned int *ap, int asz, const unsigned int *bp, int bsz);
1164 static int cmpr_long(const long *ap, int asz, const long *bp, int bsz);
1165 static int cmpr_ulong(const unsigned long *ap, int asz, const unsigned long *bp, int bsz);
1166 static int cmpr_float(const float *ap, int asz, const float *bp, int bsz);
1167 static int cmpr_double(const double *ap, int asz, const double *bp, int bsz);
1169 static int cmpr_media(const unsigned char *ap, int asz, const unsigned char *bp, int bsz);
1173 int _err_(int v,const char *fn,int ln) { return entity->db->_err_(v,fn,ln); }
1174 int _fail_(int v,const char *fn,int ln) { return entity->db->_fail_(v,fn,ln); }
1176 int _err_(int v) { return entity->db->_err_(v); }
1177 int _fail_(int v) { return entity->db->_fail_(v); }
1180 int last(Index idx,ObjectLoc &last_loc);
1181 int last(const char *nm,int (ObjectLoc::*ip)());
1182 unsigned int last(const char *nm,unsigned int (ObjectLoc::*ip)());
1190 virtual int wr_key(char *cp) = 0;
1191 Key(Index i, ObjectLoc &l, CmprFn c) : loc(l), idx(i), cmpr(c) {}
1192 Key(const char *nm, ObjectLoc &l, CmprFn c) : loc(l), cmpr(c) {
1193 idx = loc.entity->index(nm);
1195 operator void *() { return (void *)this; }
1197 int _err_(int v,const char *fn,int ln) { return loc._err_(v,fn,ln); }
1198 int _fail_(int v,const char *fn,int ln) { return loc._fail_(v,fn,ln); }
1200 int _err_(int v) { return loc._err_(v); }
1201 int _fail_(int v) { return loc._fail_(v); }
1205 class rKey : public Key {
1207 rKey(Index i, ObjectLoc &l, CmprFn c) : Key(i,l,c) {}
1208 rKey(const char *nm, ObjectLoc &l, CmprFn c) : Key(nm,l,c) {}
1209 virtual int wr_key(char *cp=0) { return -1; }
1210 int NextLoc(pgRef &pos) { return idx->NextLoc(pos); }
1211 int First(); int First(pgRef &pos);
1212 int Next(); int Next(pgRef &pos);
1214 int Locate(int op=keyGE);
1217 class iKey : protected rKey {
1219 iKey(Index i, ObjectLoc &l, CmprFn c) : rKey(i,l,c) {}
1220 iKey(const char *nm, ObjectLoc &l, CmprFn c) : rKey(nm,l,c) {}
1221 int NextLoc(pgRef &pos) { return idx->NextLoc(pos); }
1223 int Locate(int op=keyGE);
1227 class EntityObj : public Obj { /* entity storage */
1229 char name[nmSz]; /* string identifier */
1230 AllocCache alloc_cache; /* entity allocator cache */
1231 int maxId; /* highest ID value */
1232 int recdSz; /* record size in bytes */
1233 int count; /* number of records */
1234 int nidxs; /* index count */
1235 int indexs[1]; /* id/loc index */
1236 EntityObj(EntityObj &eobj, int eid);
1239 class EntityLoc : public ObjectLoc {
1241 EntityObj *operator ->() { return (EntityObj *)addr(); }
1242 EntityLoc(Entity *ep) : ObjectLoc(ep) {}
1245 EntityLoc &eloc = *this; _wr();
1246 return eloc->alloc_cache.cacheFlush(entity->db);
1251 typedef varObjRef *varObjs;
1256 varObjRef(varObjs &lp, vRef rp) : next(lp), ref(rp) {}
1259 static int ioCmpr(const void *a, const void *b, void *c);
1268 operator zrwlock_t&() { return *rw_lock; }
1271 int _err_(int v,const char *fn,int ln) { return db->_err_(v,fn,ln); }
1272 int _fail_(int v,const char *fn,int ln) { return db->_fail_(v,fn,ln); }
1274 int _err_(int v) { return db->_err_(v); }
1275 int _fail_(int v) { return db->_fail_(v); }
1277 Entity(Db *const db) : db(db), ent(this), vobjs(0) {}
1280 int allocate(ObjectLoc &loc,int sz=0);
1281 int construct_(ObjectLoc &loc, int id);
1282 int construct(ObjectLoc &loc) { return construct_(loc,ent->maxId); }
1283 int destruct_(ObjectLoc &loc, int id);
1284 int destruct(ObjectLoc &loc) { return destruct_(loc, loc->id); }
1285 int deallocate(pgRef &obj) { return db->deallocate(obj,ent->alloc_cache); }
1286 int deallocate(ObjectLoc &loc) { return deallocate(loc.obj); }
1287 int get_index(const char *nm, CmprFn cmpr=0);
1288 Index index(int i) { return db->indecies[ent->indexs[i]]; }
1289 Index index(const char *nm) {
1290 int idx = get_index(nm);
1291 return idx >= 0 ? index(idx) : 0;
1293 int MaxId() { return ent->maxId; }
1294 int Count() { return ent->count; }
1295 int add_index(int idx, int kty=ktyBin);
1296 int add_bindex(const char *nm,int keySz,int dataSz) {
1297 int idx = db->new_binary_index(nm,keySz,dataSz);
1298 if_err( idx ); if_err( add_index(idx,ktyBin) );
1301 int add_ind_index(const char *nm) {
1302 int idx = db->new_binary_index(nm,0,sizeof(int),Db::cmprKey);
1303 if_err( idx ); if_err( add_index(idx,ktyInd) );
1306 int add_dir_index(const char *nm,int keySz) {
1307 int idx = db->new_binary_index(nm,keySz,sizeof(int),Db::cmprKey);
1308 if_err( idx ); if_err( add_index(idx,ktyDir) );
1311 int add_str_index(const char *nm,int dataSz) {
1312 int idx = db->new_string_index(nm, dataSz);
1313 if_err( idx ); if_err( add_index(idx,ktyDir) );
1316 int del_index_(int idx);
1317 int del_index(int idx);
1318 int del_index(const char *nm);
1319 int new_entity(const char *nm, int sz) { return db->new_entity(*this,nm,sz); }
1320 int get_entity(const char *nm) { return db->get_entity(*this,nm); }
1321 int del_entity() { return db->del_entity(*this); }
1322 void add_vref(vRef rp) { vobjs = new varObjRef(vobjs,rp); }
1326 typedef ObjectList *Objects;
1327 static void finit(Objects objects);
1333 ObjectList(Objects op, ObjectLoc &o) : next(op), obj(&o) {}
1337 int new_entity_(Entity &entity, const char *nm, int sz);
1339 int findCmprFn(CmprFn fn);
1340 int pageLoad(pageId id, Page &pg);
1341 int addrRead_(pgRef &loc, char *&vp, int mpsz=0) {
1342 Page &pg = *get_page(loc.id); vp = 0;
1343 if( unlikely( !pg.addr || pg->chk_flags(fl_rd) ) ) {
1344 if_err( pageLoad(loc.id, pg) );
1346 vp = (char *)pg.addr+loc.offset+mpsz;
1349 int addrRead_(pgRef &loc, keyBlock *&vp, int mpsz=0) {
1350 return addrRead_(loc,*(char**)&vp, mpsz);
1352 int addrRead_(pgRef &loc, allocPrefix *&vp, int mpsz=0) {
1353 return addrRead_(loc,*(char**)&vp, mpsz);
1355 int addrRead_(pgRef &loc, pagePrefix *&vp, int mpsz=0) {
1356 return addrRead_(loc,*(char**)&vp, mpsz);
1358 int addrWrite_(pgRef &loc, char *&vp, int mpsz=0) {
1359 Page &pg = *get_page(loc.id); vp = 0;
1360 if( unlikely( !pg.addr || pg->chk_flags(fl_rd) ) ) {
1361 if_err( pageLoad(loc.id, pg) );
1363 pg->set_flags(fl_wr);
1364 vp = (char *)pg.addr+loc.offset+mpsz;
1367 int addrWrite_(pgRef &loc, keyBlock *&vp, int mpsz=0) {
1368 return addrWrite_(loc,*(char**)&vp, mpsz);
1370 int addrWrite_(pgRef &loc, allocPrefix *&vp, int mpsz=0) {
1371 return addrWrite_(loc,*(char**)&vp, mpsz);
1373 int addrWrite_(pgRef &loc, pagePrefix *&vp, int mpsz=0) {
1374 return addrWrite_(loc,*(char**)&vp, mpsz);
1376 int addrRead(pgRef &loc, char *&vp) {
1377 return addrRead_(loc, vp, sizeof(allocPrefix));
1379 int addrWrite(pgRef &loc, char *&vp) {
1380 return addrWrite_(loc, vp, sizeof(allocPrefix));
1383 int objectHeapInsert(int sz,int pg,int off,AllocCache &cache);
1384 int objectHeapDelete(int sz,int pg,int off,AllocCache &cache);
1385 int objectAllocate(int typ, int &size, pgRef &loc,AllocCache &cache);
1386 int objectFree(pgRef &loc,AllocCache &cache);
1387 int pgRefGet(int &size, pgRef &loc,AllocCache &cache);
1388 int pgRefNew(int &size, pgRef &lo,AllocCache &cache);
1389 int pgRefAllocate(int &size, pgRef &lo,AllocCache &cache);
1391 int storeInsert(long size, ioAddr io_addr);
1392 int storeDelete(long size, ioAddr io_addr);
1393 int storeGet(int &size, ioAddr &io_addr);
1394 int storeNew(int &size, ioAddr &io_addr);
1395 int storeAllocate(int &size, ioAddr &io_addr);
1396 int storeFree(int size, ioAddr io_addr);
1398 int icommit(int force);
1403 int iopen(int undo_save=1);
1408 void del_page(int id);
1409 int alloc_pageTable(int sz);
1410 void free_page_(int pid);
1411 void free_page(int pid) { locked by(db_info->pgAlLk); free_page_(pid); }
1412 pageId lower_page(int mid);
1413 int alloc_indecies(int n);
1415 void del_index(int idx);
1416 int new_index(IndexBase *&ibp, IndexBaseInfo *b, IndexBinaryInfo *d);
1417 int new_index(IndexBase *&ibp, IndexBaseInfo *b, IndexStringInfo *d);
1418 int indexRead(pageId pid, int df, keyBlock *&bp) {
1419 pgRef pg; pg.id = pid; pg.offset = 0;
1420 return !df ? addrRead_(pg,*(char**)&bp) : addrWrite_(pg,*(char**)&bp);
1422 int indexRead(pageId pid,int df,keyBlock *&bp, Page *&pp, char *&cp) {
1423 pp = 0; cp = 0; if_err( indexRead(pid, df, bp) );
1424 pp = get_page(pid); cp = (char *)(bp + 1);
1427 void pageDealloc(Page &pg, int mode=1);
1428 int pageRead(ioAddr io_adr, uint8_t *bp, int len);
1429 int pageWrite(Page &pg);
1430 int seek_data(ioAddr io_addr);
1431 int size_data(char *dp, int sz);
1432 int read_data(char *dp, int sz);
1433 int write_data(char *dp, int sz);
1434 int write_zeros(ioAddr io_addr);
1435 int write_padding(ioAddr io_addr);
1436 int readRootInfo(int(Db::*fn)(char *dp,int sz));
1437 int writeRootInfo(int(Db::*fn)(char *dp,int sz));
1438 ioAddr storeBlocks(ioAddr sz) { return (sz+storeBlockSize-1)/storeBlockSize; }
1439 ioAddr entityPages(ioAddr sz) { return (sz+entityPageSize-1)/entityPageSize; }
1440 int indeciesHunks(int sz) { return (sz+indexTableHunkSize-1)/indexTableHunkSize; }
1441 int pageTableHunks(int sz) { return (sz+pageTableHunkSize-1)/pageTableHunkSize; }
1442 int pagePrefixHunks(int sz) { return (sz+sizeof(pagePrefix)-1)/sizeof(pagePrefix); }
1448 int start_transaction(int undo_save=1);
1449 void enter() { db_info->dbRwLk.enter(); }
1450 void leave() { db_info->dbRwLk.leave(); }
1451 void write_enter() { db_info->dbRwLk.write_enter(); }
1452 void write_leave() { db_info->dbRwLk.write_leave(); }
1455 // 1:wr, 0:rd, -1:unlocked
1456 int is_locked() { return db_info->dbRwLk.locked(); }
1457 int is_blocked() { return db_info->dbRwLk.blocked(); }
1460 int open(int zfd, int zkey=-1);
1463 int attach(int zrw, int zfd, int zkey);
1464 int attach(int zrw=0) { return attach(fd, key, zrw); }
1467 int copy(Db *db, Objects objs);
1468 int new_binary_index(const char *nm, int ksz, int dsz, CmprFn cmpr=0);
1469 int new_string_index(const char *nm, int dsz);
1470 int get_index(const char *nm, CmprFn cmpr=0);
1471 long get_count(int r);
1472 int ins (int r, void *key, void *data);
1473 int del (int r, void *key);
1474 int find (int r, void *key, void *rtnData=0);
1475 int locate(int r, int op, void *key, CmprFn cmpr, void *rtnKey, void *rtnData=0);
1476 int locate(int r, int op, void *key, void *rtnKey, void *rtnData=0) {
1477 return locate(r,op,key,0,rtnKey,rtnData);
1479 int first (int r, void *key, void *rtnData=0);
1480 int last (int r, void *key, void *rtnData=0);
1481 int next (int r, void *key, void *rtnData=0);
1482 int nextloc(int r, pgRef &loc);
1483 int allocate(int typ, int size, pgRef &loc, AllocCache &cache);
1484 int reallocate(int size, pgRef &loc, AllocCache &cache);
1485 int deallocate(pgRef &loc, AllocCache &cache);
1486 int commit(int force=0);
1490 int transaction() { return !root_info ? -1 : root_info->transaction_id; }
1491 int64_t filesize() { return !root_info ? -1 : root_info->file_size; }
1492 int opened() { return fd>=0 ? 1 : 0; }
1493 void use_shm(int v) { no_shm = v ? 0 : 1; }
1494 int error() { return err_no; }
1496 void Error(int v,const char *msg);
1498 int load_indecies();
1508 void admp(); void achk(); void fchk();
1509 void edmp(AllocCache &cache);
1510 void bdmp(AllocCache &cache);