wasCSharpSQLite – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Diagnostics; |
||
3 | |||
4 | using i16 = System.Int16; |
||
5 | using i64 = System.Int64; |
||
6 | using u8 = System.Byte; |
||
7 | using u16 = System.UInt16; |
||
8 | using u32 = System.UInt32; |
||
9 | using u64 = System.UInt64; |
||
10 | |||
11 | using sqlite3_int64 = System.Int64; |
||
12 | using Pgno = System.UInt32; |
||
13 | |||
14 | namespace Community.CsharpSqlite |
||
15 | { |
||
16 | using DbPage = Sqlite3.PgHdr; |
||
17 | |||
18 | public partial class Sqlite3 |
||
19 | { |
||
20 | /* |
||
21 | ** 2004 April 6 |
||
22 | ** |
||
23 | ** The author disclaims copyright to this source code. In place of |
||
24 | ** a legal notice, here is a blessing: |
||
25 | ** |
||
26 | ** May you do good and not evil. |
||
27 | ** May you find forgiveness for yourself and forgive others. |
||
28 | ** May you share freely, never taking more than you give. |
||
29 | ** |
||
30 | ************************************************************************* |
||
31 | ** |
||
32 | ** This file implements a external (disk-based) database using BTrees. |
||
33 | ** For a detailed discussion of BTrees, refer to |
||
34 | ** |
||
35 | ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: |
||
36 | ** "Sorting And Searching", pages 473-480. Addison-Wesley |
||
37 | ** Publishing Company, Reading, Massachusetts. |
||
38 | ** |
||
39 | ** The basic idea is that each page of the file contains N database |
||
40 | ** entries and N+1 pointers to subpages. |
||
41 | ** |
||
42 | ** ---------------------------------------------------------------- |
||
43 | ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) | |
||
44 | ** ---------------------------------------------------------------- |
||
45 | ** |
||
46 | ** All of the keys on the page that Ptr(0) points to have values less |
||
47 | ** than Key(0). All of the keys on page Ptr(1) and its subpages have |
||
48 | ** values greater than Key(0) and less than Key(1). All of the keys |
||
49 | ** on Ptr(N) and its subpages have values greater than Key(N-1). And |
||
50 | ** so forth. |
||
51 | ** |
||
52 | ** Finding a particular key requires reading O(log(M)) pages from the |
||
53 | ** disk where M is the number of entries in the tree. |
||
54 | ** |
||
55 | ** In this implementation, a single file can hold one or more separate |
||
56 | ** BTrees. Each BTree is identified by the index of its root page. The |
||
57 | ** key and data for any entry are combined to form the "payload". A |
||
58 | ** fixed amount of payload can be carried directly on the database |
||
59 | ** page. If the payload is larger than the preset amount then surplus |
||
60 | ** bytes are stored on overflow pages. The payload for an entry |
||
61 | ** and the preceding pointer are combined to form a "Cell". Each |
||
62 | ** page has a small header which contains the Ptr(N) pointer and other |
||
63 | ** information such as the size of key and data. |
||
64 | ** |
||
65 | ** FORMAT DETAILS |
||
66 | ** |
||
67 | ** The file is divided into pages. The first page is called page 1, |
||
68 | ** the second is page 2, and so forth. A page number of zero indicates |
||
69 | ** "no such page". The page size can be any power of 2 between 512 and 65536. |
||
70 | ** Each page can be either a btree page, a freelist page, an overflow |
||
71 | ** page, or a pointer-map page. |
||
72 | ** |
||
73 | ** The first page is always a btree page. The first 100 bytes of the first |
||
74 | ** page contain a special header (the "file header") that describes the file. |
||
75 | ** The format of the file header is as follows: |
||
76 | ** |
||
77 | ** OFFSET SIZE DESCRIPTION |
||
78 | ** 0 16 Header string: "SQLite format 3\000" |
||
79 | ** 16 2 Page size in bytes. |
||
80 | ** 18 1 File format write version |
||
81 | ** 19 1 File format read version |
||
82 | ** 20 1 Bytes of unused space at the end of each page |
||
83 | ** 21 1 Max embedded payload fraction |
||
84 | ** 22 1 Min embedded payload fraction |
||
85 | ** 23 1 Min leaf payload fraction |
||
86 | ** 24 4 File change counter |
||
87 | ** 28 4 Reserved for future use |
||
88 | ** 32 4 First freelist page |
||
89 | ** 36 4 Number of freelist pages in the file |
||
90 | ** 40 60 15 4-byte meta values passed to higher layers |
||
91 | ** |
||
92 | ** 40 4 Schema cookie |
||
93 | ** 44 4 File format of schema layer |
||
94 | ** 48 4 Size of page cache |
||
95 | ** 52 4 Largest root-page (auto/incr_vacuum) |
||
96 | ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be |
||
97 | ** 60 4 User version |
||
98 | ** 64 4 Incremental vacuum mode |
||
99 | ** 68 4 unused |
||
100 | ** 72 4 unused |
||
101 | ** 76 4 unused |
||
102 | ** |
||
103 | ** All of the integer values are big-endian (most significant byte first). |
||
104 | ** |
||
105 | ** The file change counter is incremented when the database is changed |
||
106 | ** This counter allows other processes to know when the file has changed |
||
107 | ** and thus when they need to flush their cache. |
||
108 | ** |
||
109 | ** The max embedded payload fraction is the amount of the total usable |
||
110 | ** space in a page that can be consumed by a single cell for standard |
||
111 | ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default |
||
112 | ** is to limit the maximum cell size so that at least 4 cells will fit |
||
113 | ** on one page. Thus the default max embedded payload fraction is 64. |
||
114 | ** |
||
115 | ** If the payload for a cell is larger than the max payload, then extra |
||
116 | ** payload is spilled to overflow pages. Once an overflow page is allocated, |
||
117 | ** as many bytes as possible are moved into the overflow pages without letting |
||
118 | ** the cell size drop below the min embedded payload fraction. |
||
119 | ** |
||
120 | ** The min leaf payload fraction is like the min embedded payload fraction |
||
121 | ** except that it applies to leaf nodes in a LEAFDATA tree. The maximum |
||
122 | ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it |
||
123 | ** not specified in the header. |
||
124 | ** |
||
125 | ** Each btree pages is divided into three sections: The header, the |
||
126 | ** cell pointer array, and the cell content area. Page 1 also has a 100-byte |
||
127 | ** file header that occurs before the page header. |
||
128 | ** |
||
129 | ** |----------------| |
||
130 | ** | file header | 100 bytes. Page 1 only. |
||
131 | ** |----------------| |
||
132 | ** | page header | 8 bytes for leaves. 12 bytes for interior nodes |
||
133 | ** |----------------| |
||
134 | ** | cell pointer | | 2 bytes per cell. Sorted order. |
||
135 | ** | array | | Grows downward |
||
136 | ** | | v |
||
137 | ** |----------------| |
||
138 | ** | unallocated | |
||
139 | ** | space | |
||
140 | ** |----------------| ^ Grows upwards |
||
141 | ** | cell content | | Arbitrary order interspersed with freeblocks. |
||
142 | ** | area | | and free space fragments. |
||
143 | ** |----------------| |
||
144 | ** |
||
145 | ** The page headers looks like this: |
||
146 | ** |
||
147 | ** OFFSET SIZE DESCRIPTION |
||
148 | ** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf |
||
149 | ** 1 2 byte offset to the first freeblock |
||
150 | ** 3 2 number of cells on this page |
||
151 | ** 5 2 first byte of the cell content area |
||
152 | ** 7 1 number of fragmented free bytes |
||
153 | ** 8 4 Right child (the Ptr(N) value). Omitted on leaves. |
||
154 | ** |
||
155 | ** The flags define the format of this btree page. The leaf flag means that |
||
156 | ** this page has no children. The zerodata flag means that this page carries |
||
157 | ** only keys and no data. The intkey flag means that the key is a integer |
||
158 | ** which is stored in the key size entry of the cell header rather than in |
||
159 | ** the payload area. |
||
160 | ** |
||
161 | ** The cell pointer array begins on the first byte after the page header. |
||
162 | ** The cell pointer array contains zero or more 2-byte numbers which are |
||
163 | ** offsets from the beginning of the page to the cell content in the cell |
||
164 | ** content area. The cell pointers occur in sorted order. The system strives |
||
165 | ** to keep free space after the last cell pointer so that new cells can |
||
166 | ** be easily added without having to defragment the page. |
||
167 | ** |
||
168 | ** Cell content is stored at the very end of the page and grows toward the |
||
169 | ** beginning of the page. |
||
170 | ** |
||
171 | ** Unused space within the cell content area is collected into a linked list of |
||
172 | ** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset |
||
173 | ** to the first freeblock is given in the header. Freeblocks occur in |
||
174 | ** increasing order. Because a freeblock must be at least 4 bytes in size, |
||
175 | ** any group of 3 or fewer unused bytes in the cell content area cannot |
||
176 | ** exist on the freeblock chain. A group of 3 or fewer free bytes is called |
||
177 | ** a fragment. The total number of bytes in all fragments is recorded. |
||
178 | ** in the page header at offset 7. |
||
179 | ** |
||
180 | ** SIZE DESCRIPTION |
||
181 | ** 2 Byte offset of the next freeblock |
||
182 | ** 2 Bytes in this freeblock |
||
183 | ** |
||
184 | ** Cells are of variable length. Cells are stored in the cell content area at |
||
185 | ** the end of the page. Pointers to the cells are in the cell pointer array |
||
186 | ** that immediately follows the page header. Cells is not necessarily |
||
187 | ** contiguous or in order, but cell pointers are contiguous and in order. |
||
188 | ** |
||
189 | ** Cell content makes use of variable length integers. A variable |
||
190 | ** length integer is 1 to 9 bytes where the lower 7 bits of each |
||
191 | ** byte are used. The integer consists of all bytes that have bit 8 set and |
||
192 | ** the first byte with bit 8 clear. The most significant byte of the integer |
||
193 | ** appears first. A variable-length integer may not be more than 9 bytes long. |
||
194 | ** As a special case, all 8 bytes of the 9th byte are used as data. This |
||
195 | ** allows a 64-bit integer to be encoded in 9 bytes. |
||
196 | ** |
||
197 | ** 0x00 becomes 0x00000000 |
||
198 | ** 0x7f becomes 0x0000007f |
||
199 | ** 0x81 0x00 becomes 0x00000080 |
||
200 | ** 0x82 0x00 becomes 0x00000100 |
||
201 | ** 0x80 0x7f becomes 0x0000007f |
||
202 | ** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678 |
||
203 | ** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081 |
||
204 | ** |
||
205 | ** Variable length integers are used for rowids and to hold the number of |
||
206 | ** bytes of key and data in a btree cell. |
||
207 | ** |
||
208 | ** The content of a cell looks like this: |
||
209 | ** |
||
210 | ** SIZE DESCRIPTION |
||
211 | ** 4 Page number of the left child. Omitted if leaf flag is set. |
||
212 | ** var Number of bytes of data. Omitted if the zerodata flag is set. |
||
213 | ** var Number of bytes of key. Or the key itself if intkey flag is set. |
||
214 | ** * Payload |
||
215 | ** 4 First page of the overflow chain. Omitted if no overflow |
||
216 | ** |
||
217 | ** Overflow pages form a linked list. Each page except the last is completely |
||
218 | ** filled with data (pagesize - 4 bytes). The last page can have as little |
||
219 | ** as 1 byte of data. |
||
220 | ** |
||
221 | ** SIZE DESCRIPTION |
||
222 | ** 4 Page number of next overflow page |
||
223 | ** * Data |
||
224 | ** |
||
225 | ** Freelist pages come in two subtypes: trunk pages and leaf pages. The |
||
226 | ** file header points to the first in a linked list of trunk page. Each trunk |
||
227 | ** page points to multiple leaf pages. The content of a leaf page is |
||
228 | ** unspecified. A trunk page looks like this: |
||
229 | ** |
||
230 | ** SIZE DESCRIPTION |
||
231 | ** 4 Page number of next trunk page |
||
232 | ** 4 Number of leaf pointers on this page |
||
233 | ** * zero or more pages numbers of leaves |
||
234 | ************************************************************************* |
||
235 | ** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart |
||
236 | ** C#-SQLite is an independent reimplementation of the SQLite software library |
||
237 | ** |
||
238 | ** SQLITE_SOURCE_ID: 2011-05-19 13:26:54 ed1da510a239ea767a01dc332b667119fa3c908e |
||
239 | ** |
||
240 | ************************************************************************* |
||
241 | */ |
||
242 | //#include "sqliteInt.h" |
||
243 | |||
244 | /* The following value is the maximum cell size assuming a maximum page |
||
245 | ** size give above. |
||
246 | */ |
||
247 | //#define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8)) |
||
248 | static int MX_CELL_SIZE( BtShared pBt ) |
||
249 | { |
||
250 | return (int)( pBt.pageSize - 8 ); |
||
251 | } |
||
252 | |||
253 | /* The maximum number of cells on a single page of the database. This |
||
254 | ** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself |
||
255 | ** plus 2 bytes for the index to the cell in the page header). Such |
||
256 | ** small cells will be rare, but they are possible. |
||
257 | */ |
||
258 | //#define MX_CELL(pBt) ((pBt.pageSize-8)/6) |
||
259 | static int MX_CELL( BtShared pBt ) |
||
260 | { |
||
261 | return ( (int)( pBt.pageSize - 8 ) / 6 ); |
||
262 | } |
||
263 | |||
264 | /* Forward declarations */ |
||
265 | //typedef struct MemPage MemPage; |
||
266 | //typedef struct BtLock BtLock; |
||
267 | |||
268 | /* |
||
269 | ** This is a magic string that appears at the beginning of every |
||
270 | ** SQLite database in order to identify the file as a real database. |
||
271 | ** |
||
272 | ** You can change this value at compile-time by specifying a |
||
273 | ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The |
||
274 | ** header must be exactly 16 bytes including the zero-terminator so |
||
275 | ** the string itself should be 15 characters long. If you change |
||
276 | ** the header, then your custom library will not be able to read |
||
277 | ** databases generated by the standard tools and the standard tools |
||
278 | ** will not be able to read databases created by your custom library. |
||
279 | */ |
||
280 | #if !SQLITE_FILE_HEADER //* 123456789 123456 */ |
||
281 | const string SQLITE_FILE_HEADER = "SQLite format 3\0"; |
||
282 | #endif |
||
283 | |||
284 | /* |
||
285 | ** Page type flags. An ORed combination of these flags appear as the |
||
286 | ** first byte of on-disk image of every BTree page. |
||
287 | */ |
||
288 | const byte PTF_INTKEY = 0x01; |
||
289 | const byte PTF_ZERODATA = 0x02; |
||
290 | const byte PTF_LEAFDATA = 0x04; |
||
291 | const byte PTF_LEAF = 0x08; |
||
292 | |||
293 | /* |
||
294 | ** As each page of the file is loaded into memory, an instance of the following |
||
295 | ** structure is appended and initialized to zero. This structure stores |
||
296 | ** information about the page that is decoded from the raw file page. |
||
297 | ** |
||
298 | ** The pParent field points back to the parent page. This allows us to |
||
299 | ** walk up the BTree from any leaf to the root. Care must be taken to |
||
300 | ** unref() the parent page pointer when this page is no longer referenced. |
||
301 | ** The pageDestructor() routine handles that chore. |
||
302 | ** |
||
303 | ** Access to all fields of this structure is controlled by the mutex |
||
304 | ** stored in MemPage.pBt.mutex. |
||
305 | */ |
||
306 | public struct _OvflCell |
||
307 | { /* Cells that will not fit on aData[] */ |
||
308 | public u8[] pCell; /* Pointers to the body of the overflow cell */ |
||
309 | public u16 idx; /* Insert this cell before idx-th non-overflow cell */ |
||
310 | public _OvflCell Copy() |
||
311 | { |
||
312 | _OvflCell cp = new _OvflCell(); |
||
313 | if ( pCell != null ) |
||
314 | { |
||
315 | cp.pCell = sqlite3Malloc( pCell.Length ); |
||
316 | Buffer.BlockCopy( pCell, 0, cp.pCell, 0, pCell.Length ); |
||
317 | } |
||
318 | cp.idx = idx; |
||
319 | return cp; |
||
320 | } |
||
321 | }; |
||
322 | public class MemPage |
||
323 | { |
||
324 | public u8 isInit; /* True if previously initialized. MUST BE FIRST! */ |
||
325 | public u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ |
||
326 | public u8 intKey; /* True if u8key flag is set */ |
||
327 | public u8 leaf; /* 1 if leaf flag is set */ |
||
328 | public u8 hasData; /* True if this page stores data */ |
||
329 | public u8 hdrOffset; /* 100 for page 1. 0 otherwise */ |
||
330 | public u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ |
||
331 | public u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */ |
||
332 | public u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */ |
||
333 | public u16 cellOffset; /* Index in aData of first cell pou16er */ |
||
334 | public u16 nFree; /* Number of free bytes on the page */ |
||
335 | public u16 nCell; /* Number of cells on this page, local and ovfl */ |
||
336 | public u16 maskPage; /* Mask for page offset */ |
||
337 | public _OvflCell[] aOvfl = new _OvflCell[5]; |
||
338 | public BtShared pBt; /* Pointer to BtShared that this page is part of */ |
||
339 | public byte[] aData; /* Pointer to disk image of the page data */ |
||
340 | public DbPage pDbPage; /* Pager page handle */ |
||
341 | public Pgno pgno; /* Page number for this page */ |
||
342 | |||
343 | //public byte[] aData |
||
344 | //{ |
||
345 | // get |
||
346 | // { |
||
347 | // Debug.Assert( pgno != 1 || pDbPage.pData == _aData ); |
||
348 | // return _aData; |
||
349 | // } |
||
350 | // set |
||
351 | // { |
||
352 | // _aData = value; |
||
353 | // Debug.Assert( pgno != 1 || pDbPage.pData == _aData ); |
||
354 | // } |
||
355 | //} |
||
356 | |||
357 | public MemPage Copy() |
||
358 | { |
||
359 | MemPage cp = (MemPage)MemberwiseClone(); |
||
360 | if ( aOvfl != null ) |
||
361 | { |
||
362 | cp.aOvfl = new _OvflCell[aOvfl.Length]; |
||
363 | for ( int i = 0; i < aOvfl.Length; i++ ) |
||
364 | cp.aOvfl[i] = aOvfl[i].Copy(); |
||
365 | } |
||
366 | if ( aData != null ) |
||
367 | { |
||
368 | cp.aData = sqlite3Malloc( aData.Length ); |
||
369 | Buffer.BlockCopy( aData, 0, cp.aData, 0, aData.Length ); |
||
370 | } |
||
371 | return cp; |
||
372 | } |
||
373 | }; |
||
374 | |||
375 | /* |
||
376 | ** The in-memory image of a disk page has the auxiliary information appended |
||
377 | ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold |
||
378 | ** that extra information. |
||
379 | */ |
||
380 | const int EXTRA_SIZE = 0;// No used in C#, since we use create a class; was MemPage.Length; |
||
381 | |||
382 | /* |
||
383 | ** A linked list of the following structures is stored at BtShared.pLock. |
||
384 | ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor |
||
385 | ** is opened on the table with root page BtShared.iTable. Locks are removed |
||
386 | ** from this list when a transaction is committed or rolled back, or when |
||
387 | ** a btree handle is closed. |
||
388 | */ |
||
389 | public class BtLock |
||
390 | { |
||
391 | Btree pBtree; /* Btree handle holding this lock */ |
||
392 | Pgno iTable; /* Root page of table */ |
||
393 | u8 eLock; /* READ_LOCK or WRITE_LOCK */ |
||
394 | BtLock pNext; /* Next in BtShared.pLock list */ |
||
395 | }; |
||
396 | |||
397 | /* Candidate values for BtLock.eLock */ |
||
398 | //#define READ_LOCK 1 |
||
399 | //#define WRITE_LOCK 2 |
||
400 | const int READ_LOCK = 1; |
||
401 | const int WRITE_LOCK = 2; |
||
402 | |||
403 | /* A Btree handle |
||
404 | ** |
||
405 | ** A database connection contains a pointer to an instance of |
||
406 | ** this object for every database file that it has open. This structure |
||
407 | ** is opaque to the database connection. The database connection cannot |
||
408 | ** see the internals of this structure and only deals with pointers to |
||
409 | ** this structure. |
||
410 | ** |
||
411 | ** For some database files, the same underlying database cache might be |
||
412 | ** shared between multiple connections. In that case, each connection |
||
413 | ** has it own instance of this object. But each instance of this object |
||
414 | ** points to the same BtShared object. The database cache and the |
||
415 | ** schema associated with the database file are all contained within |
||
416 | ** the BtShared object. |
||
417 | ** |
||
418 | ** All fields in this structure are accessed under sqlite3.mutex. |
||
419 | ** The pBt pointer itself may not be changed while there exists cursors |
||
420 | ** in the referenced BtShared that point back to this Btree since those |
||
421 | ** cursors have to go through this Btree to find their BtShared and |
||
422 | ** they often do so without holding sqlite3.mutex. |
||
423 | */ |
||
424 | public class Btree |
||
425 | { |
||
426 | public sqlite3 db; /* The database connection holding this Btree */ |
||
427 | public BtShared pBt; /* Sharable content of this Btree */ |
||
428 | public u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */ |
||
429 | public bool sharable; /* True if we can share pBt with another db */ |
||
430 | public bool locked; /* True if db currently has pBt locked */ |
||
431 | public int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */ |
||
432 | public int nBackup; /* Number of backup operations reading this btree */ |
||
433 | public Btree pNext; /* List of other sharable Btrees from the same db */ |
||
434 | public Btree pPrev; /* Back pointer of the same list */ |
||
435 | #if !SQLITE_OMIT_SHARED_CACHE |
||
436 | BtLock lock; /* Object used to lock page 1 */ |
||
437 | #endif |
||
438 | }; |
||
439 | |||
440 | /* |
||
441 | ** Btree.inTrans may take one of the following values. |
||
442 | ** |
||
443 | ** If the shared-data extension is enabled, there may be multiple users |
||
444 | ** of the Btree structure. At most one of these may open a write transaction, |
||
445 | ** but any number may have active read transactions. |
||
446 | */ |
||
447 | const byte TRANS_NONE = 0; |
||
448 | const byte TRANS_READ = 1; |
||
449 | const byte TRANS_WRITE = 2; |
||
450 | |||
451 | /* |
||
452 | ** An instance of this object represents a single database file. |
||
453 | ** |
||
454 | ** A single database file can be in use as the same time by two |
||
455 | ** or more database connections. When two or more connections are |
||
456 | ** sharing the same database file, each connection has it own |
||
457 | ** private Btree object for the file and each of those Btrees points |
||
458 | ** to this one BtShared object. BtShared.nRef is the number of |
||
459 | ** connections currently sharing this database file. |
||
460 | ** |
||
461 | ** Fields in this structure are accessed under the BtShared.mutex |
||
462 | ** mutex, except for nRef and pNext which are accessed under the |
||
463 | ** global SQLITE_MUTEX_STATIC_MASTER mutex. The pPager field |
||
464 | ** may not be modified once it is initially set as long as nRef>0. |
||
465 | ** The pSchema field may be set once under BtShared.mutex and |
||
466 | ** thereafter is unchanged as long as nRef>0. |
||
467 | ** |
||
468 | ** isPending: |
||
469 | ** |
||
470 | ** If a BtShared client fails to obtain a write-lock on a database |
||
471 | ** table (because there exists one or more read-locks on the table), |
||
472 | ** the shared-cache enters 'pending-lock' state and isPending is |
||
473 | ** set to true. |
||
474 | ** |
||
475 | ** The shared-cache leaves the 'pending lock' state when either of |
||
476 | ** the following occur: |
||
477 | ** |
||
478 | ** 1) The current writer (BtShared.pWriter) concludes its transaction, OR |
||
479 | ** 2) The number of locks held by other connections drops to zero. |
||
480 | ** |
||
481 | ** while in the 'pending-lock' state, no connection may start a new |
||
482 | ** transaction. |
||
483 | ** |
||
484 | ** This feature is included to help prevent writer-starvation. |
||
485 | */ |
||
486 | public class BtShared |
||
487 | { |
||
488 | public Pager pPager; /* The page cache */ |
||
489 | public sqlite3 db; /* Database connection currently using this Btree */ |
||
490 | public BtCursor pCursor; /* A list of all open cursors */ |
||
491 | public MemPage pPage1; /* First page of the database */ |
||
492 | public bool readOnly; /* True if the underlying file is readonly */ |
||
493 | public bool pageSizeFixed; /* True if the page size can no longer be changed */ |
||
494 | public bool secureDelete; /* True if secure_delete is enabled */ |
||
495 | public bool initiallyEmpty; /* Database is empty at start of transaction */ |
||
496 | public u8 openFlags; /* Flags to sqlite3BtreeOpen() */ |
||
497 | #if !SQLITE_OMIT_AUTOVACUUM |
||
498 | public bool autoVacuum; /* True if auto-vacuum is enabled */ |
||
499 | public bool incrVacuum; /* True if incr-vacuum is enabled */ |
||
500 | #endif |
||
501 | public u8 inTransaction; /* Transaction state */ |
||
502 | public bool doNotUseWAL; /* If true, do not open write-ahead-log file */ |
||
503 | public u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
||
504 | public u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */ |
||
505 | public u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */ |
||
506 | public u16 minLeaf; /* Minimum local payload in a LEAFDATA table */ |
||
507 | public u32 pageSize; /* Total number of bytes on a page */ |
||
508 | public u32 usableSize; /* Number of usable bytes on each page */ |
||
509 | public int nTransaction; /* Number of open transactions (read + write) */ |
||
510 | public Pgno nPage; /* Number of pages in the database */ |
||
511 | public Schema pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ |
||
512 | public dxFreeSchema xFreeSchema;/* Destructor for BtShared.pSchema */ |
||
513 | public sqlite3_mutex mutex; /* Non-recursive mutex required to access this object */ |
||
514 | public Bitvec pHasContent; /* Set of pages moved to free-list this transaction */ |
||
515 | #if !SQLITE_OMIT_SHARED_CACHE |
||
516 | public int nRef; /* Number of references to this structure */ |
||
517 | public BtShared pNext; /* Next on a list of sharable BtShared structs */ |
||
518 | public BtLock pLock; /* List of locks held on this shared-btree struct */ |
||
519 | public Btree pWriter; /* Btree with currently open write transaction */ |
||
520 | public u8 isExclusive; /* True if pWriter has an EXCLUSIVE lock on the db */ |
||
521 | public u8 isPending; /* If waiting for read-locks to clear */ |
||
522 | #endif |
||
523 | public byte[] pTmpSpace; /* BtShared.pageSize bytes of space for tmp use */ |
||
524 | }; |
||
525 | |||
526 | /* |
||
527 | ** An instance of the following structure is used to hold information |
||
528 | ** about a cell. The parseCellPtr() function fills in this structure |
||
529 | ** based on information extract from the raw disk page. |
||
530 | */ |
||
531 | //typedef struct CellInfo CellInfo; |
||
532 | public struct CellInfo |
||
533 | { |
||
534 | public int iCell; /* Offset to start of cell content -- Needed for C# */ |
||
535 | public byte[] pCell; /* Pointer to the start of cell content */ |
||
536 | public i64 nKey; /* The key for INTKEY tables, or number of bytes in key */ |
||
537 | public u32 nData; /* Number of bytes of data */ |
||
538 | public u32 nPayload; /* Total amount of payload */ |
||
539 | public u16 nHeader; /* Size of the cell content header in bytes */ |
||
540 | public u16 nLocal; /* Amount of payload held locally */ |
||
541 | public u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */ |
||
542 | public u16 nSize; /* Size of the cell content on the main b-tree page */ |
||
543 | public bool Equals( CellInfo ci ) |
||
544 | { |
||
545 | if ( ci.iCell >= ci.pCell.Length || iCell >= this.pCell.Length ) |
||
546 | return false; |
||
547 | if ( ci.pCell[ci.iCell] != this.pCell[iCell] ) |
||
548 | return false; |
||
549 | if ( ci.nKey != this.nKey || ci.nData != this.nData || ci.nPayload != this.nPayload ) |
||
550 | return false; |
||
551 | if ( ci.nHeader != this.nHeader || ci.nLocal != this.nLocal ) |
||
552 | return false; |
||
553 | if ( ci.iOverflow != this.iOverflow || ci.nSize != this.nSize ) |
||
554 | return false; |
||
555 | return true; |
||
556 | } |
||
557 | }; |
||
558 | |||
559 | /* |
||
560 | ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than |
||
561 | ** this will be declared corrupt. This value is calculated based on a |
||
562 | ** maximum database size of 2^31 pages a minimum fanout of 2 for a |
||
563 | ** root-node and 3 for all other internal nodes. |
||
564 | ** |
||
565 | ** If a tree that appears to be taller than this is encountered, it is |
||
566 | ** assumed that the database is corrupt. |
||
567 | */ |
||
568 | //#define BTCURSOR_MAX_DEPTH 20 |
||
569 | const int BTCURSOR_MAX_DEPTH = 20; |
||
570 | |||
571 | /* |
||
572 | ** A cursor is a pointer to a particular entry within a particular |
||
573 | ** b-tree within a database file. |
||
574 | ** |
||
575 | ** The entry is identified by its MemPage and the index in |
||
576 | ** MemPage.aCell[] of the entry. |
||
577 | ** |
||
578 | ** A single database file can shared by two more database connections, |
||
579 | ** but cursors cannot be shared. Each cursor is associated with a |
||
580 | ** particular database connection identified BtCursor.pBtree.db. |
||
581 | ** |
||
582 | ** Fields in this structure are accessed under the BtShared.mutex |
||
583 | ** found at self.pBt.mutex. |
||
584 | */ |
||
585 | public class BtCursor |
||
586 | { |
||
587 | public Btree pBtree; /* The Btree to which this cursor belongs */ |
||
588 | public BtShared pBt; /* The BtShared this cursor points to */ |
||
589 | public BtCursor pNext; |
||
590 | public BtCursor pPrev; /* Forms a linked list of all cursors */ |
||
591 | public KeyInfo pKeyInfo; /* Argument passed to comparison function */ |
||
592 | public Pgno pgnoRoot; /* The root page of this tree */ |
||
593 | public sqlite3_int64 cachedRowid; /* Next rowid cache. 0 means not valid */ |
||
594 | public CellInfo info = new CellInfo(); /* A parse of the cell we are pointing at */ |
||
595 | public byte[] pKey; /* Saved key that was cursor's last known position */ |
||
596 | public i64 nKey; /* Size of pKey, or last integer key */ |
||
597 | public int skipNext; /* Prev() is noop if negative. Next() is noop if positive */ |
||
598 | public u8 wrFlag; /* True if writable */ |
||
599 | public u8 atLast; /* VdbeCursor pointing to the last entry */ |
||
600 | public bool validNKey; /* True if info.nKey is valid */ |
||
601 | public int eState; /* One of the CURSOR_XXX constants (see below) */ |
||
602 | #if !SQLITE_OMIT_INCRBLOB |
||
603 | public Pgno[] aOverflow; /* Cache of overflow page locations */ |
||
604 | public bool isIncrblobHandle; /* True if this cursor is an incr. io handle */ |
||
605 | #endif |
||
606 | public i16 iPage; /* Index of current page in apPage */ |
||
607 | public u16[] aiIdx = new u16[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */ |
||
608 | public MemPage[] apPage = new MemPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */ |
||
609 | |||
610 | public void Clear() |
||
611 | { |
||
612 | pNext = null; |
||
613 | pPrev = null; |
||
614 | pKeyInfo = null; |
||
615 | pgnoRoot = 0; |
||
616 | cachedRowid = 0; |
||
617 | info = new CellInfo(); |
||
618 | wrFlag = 0; |
||
619 | atLast = 0; |
||
620 | validNKey = false; |
||
621 | eState = 0; |
||
622 | pKey = null; |
||
623 | nKey = 0; |
||
624 | skipNext = 0; |
||
625 | #if !SQLITE_OMIT_INCRBLOB |
||
626 | isIncrblobHandle=false; |
||
627 | aOverflow= null; |
||
628 | #endif |
||
629 | iPage = 0; |
||
630 | } |
||
631 | public BtCursor Copy() |
||
632 | { |
||
633 | BtCursor cp = (BtCursor)MemberwiseClone(); |
||
634 | return cp; |
||
635 | } |
||
636 | }; |
||
637 | |||
638 | /* |
||
639 | ** Potential values for BtCursor.eState. |
||
640 | ** |
||
641 | ** CURSOR_VALID: |
||
642 | ** VdbeCursor points to a valid entry. getPayload() etc. may be called. |
||
643 | ** |
||
644 | ** CURSOR_INVALID: |
||
645 | ** VdbeCursor does not point to a valid entry. This can happen (for example) |
||
646 | ** because the table is empty or because BtreeCursorFirst() has not been |
||
647 | ** called. |
||
648 | ** |
||
649 | ** CURSOR_REQUIRESEEK: |
||
650 | ** The table that this cursor was opened on still exists, but has been |
||
651 | ** modified since the cursor was last used. The cursor position is saved |
||
652 | ** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in |
||
653 | ** this state, restoreCursorPosition() can be called to attempt to |
||
654 | ** seek the cursor to the saved position. |
||
655 | ** |
||
656 | ** CURSOR_FAULT: |
||
657 | ** A unrecoverable error (an I/O error or a malloc failure) has occurred |
||
658 | ** on a different connection that shares the BtShared cache with this |
||
659 | ** cursor. The error has left the cache in an inconsistent state. |
||
660 | ** Do nothing else with this cursor. Any attempt to use the cursor |
||
661 | ** should return the error code stored in BtCursor.skip |
||
662 | */ |
||
663 | const int CURSOR_INVALID = 0; |
||
664 | const int CURSOR_VALID = 1; |
||
665 | const int CURSOR_REQUIRESEEK = 2; |
||
666 | const int CURSOR_FAULT = 3; |
||
667 | |||
668 | /* |
||
669 | ** The database page the PENDING_BYTE occupies. This page is never used. |
||
670 | */ |
||
671 | //# define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt) |
||
672 | // TODO -- Convert PENDING_BYTE_PAGE to inline |
||
673 | static u32 PENDING_BYTE_PAGE( BtShared pBt ) |
||
674 | { |
||
675 | return (u32)PAGER_MJ_PGNO( pBt.pPager ); |
||
676 | } |
||
677 | |||
678 | /* |
||
679 | ** These macros define the location of the pointer-map entry for a |
||
680 | ** database page. The first argument to each is the number of usable |
||
681 | ** bytes on each page of the database (often 1024). The second is the |
||
682 | ** page number to look up in the pointer map. |
||
683 | ** |
||
684 | ** PTRMAP_PAGENO returns the database page number of the pointer-map |
||
685 | ** page that stores the required pointer. PTRMAP_PTROFFSET returns |
||
686 | ** the offset of the requested map entry. |
||
687 | ** |
||
688 | ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page, |
||
689 | ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be |
||
690 | ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements |
||
691 | ** this test. |
||
692 | */ |
||
693 | //#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno) |
||
694 | static Pgno PTRMAP_PAGENO( BtShared pBt, Pgno pgno ) |
||
695 | { |
||
696 | return ptrmapPageno( pBt, pgno ); |
||
697 | } |
||
698 | //#define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1)) |
||
699 | static u32 PTRMAP_PTROFFSET( u32 pgptrmap, u32 pgno ) |
||
700 | { |
||
701 | return ( 5 * ( pgno - pgptrmap - 1 ) ); |
||
702 | } |
||
703 | //#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno)) |
||
704 | static bool PTRMAP_ISPAGE( BtShared pBt, u32 pgno ) |
||
705 | { |
||
706 | return ( PTRMAP_PAGENO( ( pBt ), ( pgno ) ) == ( pgno ) ); |
||
707 | } |
||
708 | /* |
||
709 | ** The pointer map is a lookup table that identifies the parent page for |
||
710 | ** each child page in the database file. The parent page is the page that |
||
711 | ** contains a pointer to the child. Every page in the database contains |
||
712 | ** 0 or 1 parent pages. (In this context 'database page' refers |
||
713 | ** to any page that is not part of the pointer map itself.) Each pointer map |
||
714 | ** entry consists of a single byte 'type' and a 4 byte parent page number. |
||
715 | ** The PTRMAP_XXX identifiers below are the valid types. |
||
716 | ** |
||
717 | ** The purpose of the pointer map is to facility moving pages from one |
||
718 | ** position in the file to another as part of autovacuum. When a page |
||
719 | ** is moved, the pointer in its parent must be updated to point to the |
||
720 | ** new location. The pointer map is used to locate the parent page quickly. |
||
721 | ** |
||
722 | ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not |
||
723 | ** used in this case. |
||
724 | ** |
||
725 | ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number |
||
726 | ** is not used in this case. |
||
727 | ** |
||
728 | ** PTRMAP_OVERFLOW1: The database page is the first page in a list of |
||
729 | ** overflow pages. The page number identifies the page that |
||
730 | ** contains the cell with a pointer to this overflow page. |
||
731 | ** |
||
732 | ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of |
||
733 | ** overflow pages. The page-number identifies the previous |
||
734 | ** page in the overflow page list. |
||
735 | ** |
||
736 | ** PTRMAP_BTREE: The database page is a non-root btree page. The page number |
||
737 | ** identifies the parent page in the btree. |
||
738 | */ |
||
739 | //#define PTRMAP_ROOTPAGE 1 |
||
740 | //#define PTRMAP_FREEPAGE 2 |
||
741 | //#define PTRMAP_OVERFLOW1 3 |
||
742 | //#define PTRMAP_OVERFLOW2 4 |
||
743 | //#define PTRMAP_BTREE 5 |
||
744 | const int PTRMAP_ROOTPAGE = 1; |
||
745 | const int PTRMAP_FREEPAGE = 2; |
||
746 | const int PTRMAP_OVERFLOW1 = 3; |
||
747 | const int PTRMAP_OVERFLOW2 = 4; |
||
748 | const int PTRMAP_BTREE = 5; |
||
749 | |||
750 | /* A bunch of Debug.Assert() statements to check the transaction state variables |
||
751 | ** of handle p (type Btree*) are internally consistent. |
||
752 | */ |
||
753 | #if DEBUG |
||
754 | //#define btreeIntegrity(p) \ |
||
755 | // Debug.Assert( p.pBt.inTransaction!=TRANS_NONE || p.pBt.nTransaction==0 ); \ |
||
756 | // Debug.Assert( p.pBt.inTransaction>=p.inTrans ); |
||
757 | static void btreeIntegrity( Btree p ) |
||
758 | { |
||
759 | Debug.Assert( p.pBt.inTransaction != TRANS_NONE || p.pBt.nTransaction == 0 ); |
||
760 | Debug.Assert( p.pBt.inTransaction >= p.inTrans ); |
||
761 | } |
||
762 | #else |
||
763 | static void btreeIntegrity(Btree p) { } |
||
764 | #endif |
||
765 | |||
766 | /* |
||
767 | ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine |
||
768 | ** if the database supports auto-vacuum or not. Because it is used |
||
769 | ** within an expression that is an argument to another macro |
||
770 | ** (sqliteMallocRaw), it is not possible to use conditional compilation. |
||
771 | ** So, this macro is defined instead. |
||
772 | */ |
||
773 | #if !SQLITE_OMIT_AUTOVACUUM |
||
774 | //#define ISAUTOVACUUM (pBt.autoVacuum) |
||
775 | #else |
||
776 | //#define ISAUTOVACUUM 0 |
||
777 | public static bool ISAUTOVACUUM =false; |
||
778 | #endif |
||
779 | |||
780 | |||
781 | /* |
||
782 | ** This structure is passed around through all the sanity checking routines |
||
783 | ** in order to keep track of some global state information. |
||
784 | */ |
||
785 | //typedef struct IntegrityCk IntegrityCk; |
||
786 | public class IntegrityCk |
||
787 | { |
||
788 | public BtShared pBt; /* The tree being checked out */ |
||
789 | public Pager pPager; /* The associated pager. Also accessible by pBt.pPager */ |
||
790 | public Pgno nPage; /* Number of pages in the database */ |
||
791 | public int[] anRef; /* Number of times each page is referenced */ |
||
792 | public int mxErr; /* Stop accumulating errors when this reaches zero */ |
||
793 | public int nErr; /* Number of messages written to zErrMsg so far */ |
||
794 | //public int mallocFailed; /* A memory allocation error has occurred */ |
||
795 | public StrAccum errMsg = new StrAccum( 100 ); /* Accumulate the error message text here */ |
||
796 | }; |
||
797 | |||
798 | /* |
||
799 | ** Read or write a two- and four-byte big-endian integer values. |
||
800 | */ |
||
801 | //#define get2byte(x) ((x)[0]<<8 | (x)[1]) |
||
802 | static int get2byte( byte[] p, int offset ) |
||
803 | { |
||
804 | return p[offset + 0] << 8 | p[offset + 1]; |
||
805 | } |
||
806 | |||
807 | //#define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v)) |
||
808 | static void put2byte( byte[] pData, int Offset, u32 v ) |
||
809 | { |
||
810 | pData[Offset + 0] = (byte)( v >> 8 ); |
||
811 | pData[Offset + 1] = (byte)v; |
||
812 | } |
||
813 | static void put2byte( byte[] pData, int Offset, int v ) |
||
814 | { |
||
815 | pData[Offset + 0] = (byte)( v >> 8 ); |
||
816 | pData[Offset + 1] = (byte)v; |
||
817 | } |
||
818 | |||
819 | //#define get4byte sqlite3Get4byte |
||
820 | //#define put4byte sqlite3Put4byte |
||
821 | |||
822 | } |
||
823 | } |