add .gitignore
[tedtools.git] / tbtree.h
index fa11321..28d8837 100644 (file)
--- a/tbtree.h
+++ b/tbtree.h
 
 
 #include <sys/types.h>
+#include "tools.h" 
 
-/* C-utils */
-#ifndef offsetof
-#define offsetof(type, field)   ((int) &((type *)0)->field)
-#endif   /* offsetof */
-
-#define TYPEALIGN(ALIGNVAL,LEN)  \
-        (((long) (LEN) + (ALIGNVAL-1)) & ~((long) (ALIGNVAL-1)))
-
-#define PTRALIGN(LEN)     TYPEALIGN(sizeof(void*), (LEN))
-
+#define HASHSIZE(LEN)  ( (LEN)<<1 )
 /* end utils */
 
 
@@ -71,30 +63,33 @@ typedef struct {
 #define TBTPOINTERSIZE(db)     PTRALIGN( (db->keylen) ? TBTPOINTERHRDSZ + db->keylen : sizeof(TBTPointer) )
 #define ISINFPOINTER(db, page, ptr)  ( (page)->isleaf==0 && (page)->rightlink == 0 && (char*)(ptr) == (page)->data + ((page)->npointer-1) * TBTPOINTERSIZE(db) ) 
 
+/* can changed up to 65536 */
 #define TBTREEPAGESIZE 8192
 #define TBTPAGEHDRSZ   (2*sizeof(u_int32_t))
 
 typedef struct {
        u_int32_t       rightlink;
        u_int32_t       
-               freespace:13, /* correlate to BTREEPAGESIZE */
-               npointer:10,
-               isleaf:1,
-               unused: 8;
+               freespace:16, /* correlate to TBTREEPAGESIZE */
+               npointer:15,
+               isleaf:1;
        char    data[TBTREEPAGESIZE-TBTPAGEHDRSZ];      
 } TBTPage;
 
-typedef struct {
+typedef struct TBTMemPage {
        u_int32_t       pagenumber;
-       struct timeval last;
        u_int32_t
                issynced:1,
                iscached:1,
                islocked:1,
                unused:29;
+       struct TBTMemPage *prev;
+       struct TBTMemPage *next;
+       struct TBTMemPage *link;
        TBTPage page;
 } TBTMemPage;
 
+#define TBTMEMPAGEHDRSZ (sizeof(u_int32_t)*2 + sizeof(TBTMemPage*)*3 + TBTPAGEHDRSZ)
 typedef struct {
        u_int16_t       length;
        char            *value;
@@ -113,13 +108,15 @@ typedef struct {
                readonly:1,
                keylen:11,
                strategy:2,
-               unused:18;
+               unused:2,
+               pointersize:16;
 
        /* cache page subsystem */
        u_int32_t       npage;
        u_int32_t       curpage;
        TBTMemPage      **Cache;
-       TBTMemPage      **TimeCache;
+       TBTMemPage      *TimeCache;
+       TBTMemPage      *TimeCacheLast;
        u_int32_t       lastpagenumber;
 
        /* stat subsystem */
@@ -140,6 +137,8 @@ int TBTSync(TBTree *db) ;
 int TBTFind(TBTree *db, TBTValue *key, TBTValue *value);
 int TBTInsert(TBTree *db, TBTValue *key, TBTValue *value);
 int TBTDelete(TBTree *db, TBTValue *key);
+
+/* debug function, assume key is int4 or string and values are strings */
 void dumpTree(TBTree *db, u_int32_t pagenumber, int follow);
 
 typedef struct {
@@ -152,4 +151,7 @@ int TBTIterate(TBTree *db, TBTIterator *iterator, TBTValue *key, TBTValue *value
 void TBTFreeIterator(TBTree *db, TBTIterator *iterator);
 
 
+int TBTGetFirst(TBTree *db, TBTValue *key, TBTValue *value);
+int TBTGetLast(TBTree *db, TBTValue *key, TBTValue *value);
+
 #endif