xref: /titanic_44/usr/src/lib/libsqlite/src/btree.h (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1 
2 #pragma ident	"%Z%%M%	%I%	%E% SMI"
3 
4 /*
5 ** 2001 September 15
6 **
7 ** The author disclaims copyright to this source code.  In place of
8 ** a legal notice, here is a blessing:
9 **
10 **    May you do good and not evil.
11 **    May you find forgiveness for yourself and forgive others.
12 **    May you share freely, never taking more than you give.
13 **
14 *************************************************************************
15 ** This header file defines the interface that the sqlite B-Tree file
16 ** subsystem.  See comments in the source code for a detailed description
17 ** of what each interface routine does.
18 **
19 ** @(#) $Id: btree.h,v 1.36 2004/02/10 02:57:59 drh Exp $
20 */
21 #ifndef _BTREE_H_
22 #define _BTREE_H_
23 
24 /*
25 ** Forward declarations of structure
26 */
27 typedef struct Btree Btree;
28 typedef struct BtCursor BtCursor;
29 typedef struct BtOps BtOps;
30 typedef struct BtCursorOps BtCursorOps;
31 
32 
33 /*
34 ** An instance of the following structure contains pointers to all
35 ** methods against an open BTree.  Alternative BTree implementations
36 ** (examples: file based versus in-memory) can be created by substituting
37 ** different methods.  Users of the BTree cannot tell the difference.
38 **
39 ** In C++ we could do this by defining a virtual base class and then
40 ** creating subclasses for each different implementation.  But this is
41 ** C not C++ so we have to be a little more explicit.
42 */
43 struct BtOps {
44     int (*Close)(Btree*);
45     int (*SetCacheSize)(Btree*, int);
46     int (*SetSafetyLevel)(Btree*, int);
47     int (*BeginTrans)(Btree*);
48     int (*Commit)(Btree*);
49     int (*Rollback)(Btree*);
50     int (*BeginCkpt)(Btree*);
51     int (*CommitCkpt)(Btree*);
52     int (*RollbackCkpt)(Btree*);
53     int (*CreateTable)(Btree*, int*);
54     int (*CreateIndex)(Btree*, int*);
55     int (*DropTable)(Btree*, int);
56     int (*ClearTable)(Btree*, int);
57     int (*Cursor)(Btree*, int iTable, int wrFlag, BtCursor **ppCur);
58     int (*GetMeta)(Btree*, int*);
59     int (*UpdateMeta)(Btree*, int*);
60     char *(*IntegrityCheck)(Btree*, int*, int);
61     const char *(*GetFilename)(Btree*);
62     int (*Copyfile)(Btree*,Btree*);
63     struct Pager *(*Pager)(Btree*);
64 #ifdef SQLITE_TEST
65     int (*PageDump)(Btree*, int, int);
66 #endif
67 };
68 
69 /*
70 ** An instance of this structure defines all of the methods that can
71 ** be executed against a cursor.
72 */
73 struct BtCursorOps {
74     int (*Moveto)(BtCursor*, const void *pKey, int nKey, int *pRes);
75     int (*Delete)(BtCursor*);
76     int (*Insert)(BtCursor*, const void *pKey, int nKey,
77                              const void *pData, int nData);
78     int (*First)(BtCursor*, int *pRes);
79     int (*Last)(BtCursor*, int *pRes);
80     int (*Next)(BtCursor*, int *pRes);
81     int (*Previous)(BtCursor*, int *pRes);
82     int (*KeySize)(BtCursor*, int *pSize);
83     int (*Key)(BtCursor*, int offset, int amt, char *zBuf);
84     int (*KeyCompare)(BtCursor*, const void *pKey, int nKey,
85                                  int nIgnore, int *pRes);
86     int (*DataSize)(BtCursor*, int *pSize);
87     int (*Data)(BtCursor*, int offset, int amt, char *zBuf);
88     int (*CloseCursor)(BtCursor*);
89 #ifdef SQLITE_TEST
90     int (*CursorDump)(BtCursor*, int*);
91 #endif
92 };
93 
94 /*
95 ** The number of 4-byte "meta" values contained on the first page of each
96 ** database file.
97 */
98 #define SQLITE_N_BTREE_META 10
99 
100 int sqliteBtreeOpen(const char *zFilename, int mode, int nPg, Btree **ppBtree);
101 int sqliteRbtreeOpen(const char *zFilename, int mode, int nPg, Btree **ppBtree);
102 
103 #define btOps(pBt) (*((BtOps **)(pBt)))
104 #define btCOps(pCur) (*((BtCursorOps **)(pCur)))
105 
106 #define sqliteBtreeClose(pBt)              (btOps(pBt)->Close(pBt))
107 #define sqliteBtreeSetCacheSize(pBt, sz)   (btOps(pBt)->SetCacheSize(pBt, sz))
108 #define sqliteBtreeSetSafetyLevel(pBt, sl) (btOps(pBt)->SetSafetyLevel(pBt, sl))
109 #define sqliteBtreeBeginTrans(pBt)         (btOps(pBt)->BeginTrans(pBt))
110 #define sqliteBtreeCommit(pBt)             (btOps(pBt)->Commit(pBt))
111 #define sqliteBtreeRollback(pBt)           (btOps(pBt)->Rollback(pBt))
112 #define sqliteBtreeBeginCkpt(pBt)          (btOps(pBt)->BeginCkpt(pBt))
113 #define sqliteBtreeCommitCkpt(pBt)         (btOps(pBt)->CommitCkpt(pBt))
114 #define sqliteBtreeRollbackCkpt(pBt)       (btOps(pBt)->RollbackCkpt(pBt))
115 #define sqliteBtreeCreateTable(pBt,piTable)\
116                 (btOps(pBt)->CreateTable(pBt,piTable))
117 #define sqliteBtreeCreateIndex(pBt, piIndex)\
118                 (btOps(pBt)->CreateIndex(pBt, piIndex))
119 #define sqliteBtreeDropTable(pBt, iTable) (btOps(pBt)->DropTable(pBt, iTable))
120 #define sqliteBtreeClearTable(pBt, iTable)\
121                 (btOps(pBt)->ClearTable(pBt, iTable))
122 #define sqliteBtreeCursor(pBt, iTable, wrFlag, ppCur)\
123                 (btOps(pBt)->Cursor(pBt, iTable, wrFlag, ppCur))
124 #define sqliteBtreeMoveto(pCur, pKey, nKey, pRes)\
125                 (btCOps(pCur)->Moveto(pCur, pKey, nKey, pRes))
126 #define sqliteBtreeDelete(pCur)           (btCOps(pCur)->Delete(pCur))
127 #define sqliteBtreeInsert(pCur, pKey, nKey, pData, nData) \
128                 (btCOps(pCur)->Insert(pCur, pKey, nKey, pData, nData))
129 #define sqliteBtreeFirst(pCur, pRes)      (btCOps(pCur)->First(pCur, pRes))
130 #define sqliteBtreeLast(pCur, pRes)       (btCOps(pCur)->Last(pCur, pRes))
131 #define sqliteBtreeNext(pCur, pRes)       (btCOps(pCur)->Next(pCur, pRes))
132 #define sqliteBtreePrevious(pCur, pRes)   (btCOps(pCur)->Previous(pCur, pRes))
133 #define sqliteBtreeKeySize(pCur, pSize)   (btCOps(pCur)->KeySize(pCur, pSize) )
134 #define sqliteBtreeKey(pCur, offset, amt, zBuf)\
135                 (btCOps(pCur)->Key(pCur, offset, amt, zBuf))
136 #define sqliteBtreeKeyCompare(pCur, pKey, nKey, nIgnore, pRes)\
137                 (btCOps(pCur)->KeyCompare(pCur, pKey, nKey, nIgnore, pRes))
138 #define sqliteBtreeDataSize(pCur, pSize)  (btCOps(pCur)->DataSize(pCur, pSize))
139 #define sqliteBtreeData(pCur, offset, amt, zBuf)\
140                 (btCOps(pCur)->Data(pCur, offset, amt, zBuf))
141 #define sqliteBtreeCloseCursor(pCur)      (btCOps(pCur)->CloseCursor(pCur))
142 #define sqliteBtreeGetMeta(pBt, aMeta)    (btOps(pBt)->GetMeta(pBt, aMeta))
143 #define sqliteBtreeUpdateMeta(pBt, aMeta) (btOps(pBt)->UpdateMeta(pBt, aMeta))
144 #define sqliteBtreeIntegrityCheck(pBt, aRoot, nRoot)\
145                 (btOps(pBt)->IntegrityCheck(pBt, aRoot, nRoot))
146 #define sqliteBtreeGetFilename(pBt)       (btOps(pBt)->GetFilename(pBt))
147 #define sqliteBtreeCopyFile(pBt1, pBt2)   (btOps(pBt1)->Copyfile(pBt1, pBt2))
148 #define sqliteBtreePager(pBt)             (btOps(pBt)->Pager(pBt))
149 
150 #ifdef SQLITE_TEST
151 #define sqliteBtreePageDump(pBt, pgno, recursive)\
152                 (btOps(pBt)->PageDump(pBt, pgno, recursive))
153 #define sqliteBtreeCursorDump(pCur, aResult)\
154                 (btCOps(pCur)->CursorDump(pCur, aResult))
155 int btree_native_byte_order;
156 #endif /* SQLITE_TEST */
157 
158 
159 #endif /* _BTREE_H_ */
160