Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Fix enough of bt_pager.c that it may be used for testing the b-tree built on top of it. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
21096194824131c6b79d129da5896e4b |
User & Date: | dan 2013-09-18 15:46:38.719 |
Context
2013-09-19
| ||
15:13 | Add b-tree design notes to www/ directory. check-in: 8c828db5a6 user: dan tags: trunk | |
2013-09-18
| ||
15:46 | Fix enough of bt_pager.c that it may be used for testing the b-tree built on top of it. check-in: 2109619482 user: dan tags: trunk | |
2013-09-17
| ||
20:26 | Add new file bt_pager.c. check-in: c8eb0a0cf4 user: dan tags: trunk | |
Changes
Changes to main.mk.
︙ | ︙ | |||
69 70 71 72 73 74 75 | LIBOBJ+= vdbe.o parse.o \ alter.o analyze.o attach.o auth.o \ build.o \ callback.o complete.o ctime.o date.o delete.o env.o expr.o \ fault.o fkey.o fts5.o fts5func.o \ func.o global.o hash.o \ icu.o insert.o kv.o kvlsm.o kvmem.o legacy.o \ | < > > | 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | LIBOBJ+= vdbe.o parse.o \ alter.o analyze.o attach.o auth.o \ build.o \ callback.o complete.o ctime.o date.o delete.o env.o expr.o \ fault.o fkey.o fts5.o fts5func.o \ func.o global.o hash.o \ icu.o insert.o kv.o kvlsm.o kvmem.o legacy.o \ lsm_ckpt.o lsm_file.o lsm_log.o lsm_main.o lsm_mem.o lsm_mutex.o \ lsm_shared.o lsm_str.o lsm_sorted.o lsm_tree.o \ lsm_unix.o lsm_varint.o \ main.o malloc.o math.o mem.o mem0.o mem2.o mem3.o mem5.o \ mutex.o mutex_noop.o mutex_unix.o mutex_w32.o \ opcodes.o os.o \ pragma.o prepare.o printf.o \ random.o resolve.o rowset.o rtree.o select.o status.o \ tokenize.o trigger.o \ update.o util.o varint.o \ vdbeapi.o vdbeaux.o vdbecodec.o vdbecursor.o \ vdbemem.o vdbetrace.o \ walker.o where.o utf.o LIBOBJ += bt_unix.o bt_pager.o bt_main.o # All of the source code files. # SRC = \ $(TOP)/src/alter.c \ $(TOP)/src/analyze.c \ $(TOP)/src/attach.c \ |
︙ | ︙ |
Changes to src/bt_main.c.
︙ | ︙ | |||
8 9 10 11 12 13 14 | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** */ | | > | | | 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** */ #include "btInt.h" struct bt_db { sqlite4_env *pEnv; /* SQLite environment */ BtPager *pPager; /* Underlying page-based database */ }; int sqlite4BtNew(sqlite4_env *pEnv, int nExtra, bt_db **ppDb){ return SQLITE4_OK; } int sqlite4BtClose(bt_db *db){ return SQLITE4_OK; } |
︙ | ︙ | |||
89 90 91 92 93 94 95 | return 0; } int sqlite4BtCsrKey(bt_cursor *pCsr, const void **ppK, int *pnK){ return SQLITE4_OK; } | | > > > > > > | 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | return 0; } int sqlite4BtCsrKey(bt_cursor *pCsr, const void **ppK, int *pnK){ return SQLITE4_OK; } int sqlite4BtCsrData( bt_cursor *pCsr, /* Cursor handle */ int iOffset, /* Offset of requested data */ int nByte, /* Bytes requested (or -ve for all avail.) */ const void **ppV, /* OUT: Pointer to data buffer */ int *pnV /* OUT: Size of data buffer in bytes */ ){ return SQLITE4_OK; } int sqlite4BtReplace(bt_db *db, const void *pK, int nK, const void *pV, int nV){ return SQLITE4_OK; } |
︙ | ︙ |
Changes to src/bt_pager.c.
︙ | ︙ | |||
8 9 10 11 12 13 14 15 16 17 18 19 20 21 | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* */ #include "btInt.h" /* By default pages are 1024 bytes in size. */ #define BT_DEFAULT_PGSZ 1024 typedef struct BtPageHash BtPageHash; typedef struct BtDbhdr BtDbhdr; | > > > | 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* */ #include "btInt.h" #include <string.h> #include <assert.h> /* By default pages are 1024 bytes in size. */ #define BT_DEFAULT_PGSZ 1024 typedef struct BtPageHash BtPageHash; typedef struct BtDbhdr BtDbhdr; |
︙ | ︙ | |||
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | BtPage *pNextDirty; /* Next page in BtPager.pDirty list */ }; /* ** Candidate values for BtPage.flags */ #define BT_PAGE_DIRTY 0x0001 /* Set for pages in BtPager.pDirty list */ /* ** Pager object. */ struct BtPager { sqlite4_env *pEnv; /* SQLite environment */ bt_env *pVfs; /* Bt environment */ int iTransactionLevel; /* Current transaction level (see bt.h) */ char *zFile; /* Database file name */ int nFile; /* Length of string zFile in bytes */ bt_file *pFd; /* Database file */ BtPageHash hash; /* Hash table */ BtPage *pDirty; /* List of all dirty pages */ | > > > > > > > > > > < < < < < | < < < < < | 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | BtPage *pNextDirty; /* Next page in BtPager.pDirty list */ }; /* ** Candidate values for BtPage.flags */ #define BT_PAGE_DIRTY 0x0001 /* Set for pages in BtPager.pDirty list */ /* ** Database header. */ struct BtDbhdr { u32 pgsz; /* Page size in bytes */ u32 nPg; /* Number of pages in database */ u32 cookie; /* User cookie value */ }; /* ** Pager object. */ struct BtPager { sqlite4_env *pEnv; /* SQLite environment */ bt_env *pVfs; /* Bt environment */ int iTransactionLevel; /* Current transaction level (see bt.h) */ char *zFile; /* Database file name */ int nFile; /* Length of string zFile in bytes */ bt_file *pFd; /* Database file */ BtPageHash hash; /* Hash table */ BtPage *pDirty; /* List of all dirty pages */ BtDbhdr dbhdr; }; /************************************************************************** ** Interface to BtPageHash object. */ /* ** Return the hash key for page number pgno in a hash table with nHash ** buckets. */ static int hashkey(int nHash, u32 pgno){ return (pgno % nHash); } /* ** Add page pPg to the hash table. */ static int btHashAdd(BtPager *p, BtPage *pPg){ int h; /* If required, increase the number of buckets in the hash table. */ if( p->hash.nEntry>=p->hash.nHash/2 ){ int i; int nNew = (p->hash.nHash ? p->hash.nHash*2 : 256); BtPage **aNew; |
︙ | ︙ | |||
119 120 121 122 123 124 125 126 127 128 129 130 131 132 | /* Add the new entry to the hash table. */ assert( pPg->pNextHash==0 ); h = hashkey(p->hash.nHash, pPg->pgno); pPg->pNextHash = p->hash.aHash[h]; p->hash.aHash[h] = pPg; p->hash.nEntry++; } /* ** Remove page pPg from the hash table. */ static void btHashRemove(BtPager *p, BtPage *pPg){ BtPage **pp; | > > | 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | /* Add the new entry to the hash table. */ assert( pPg->pNextHash==0 ); h = hashkey(p->hash.nHash, pPg->pgno); pPg->pNextHash = p->hash.aHash[h]; p->hash.aHash[h] = pPg; p->hash.nEntry++; return SQLITE4_OK; } /* ** Remove page pPg from the hash table. */ static void btHashRemove(BtPager *p, BtPage *pPg){ BtPage **pp; |
︙ | ︙ | |||
165 166 167 168 169 170 171 | int sqlite4BtPagerNew(sqlite4_env *pEnv, int nExtra, BtPager **pp){ BtPager *p; int nByte; nByte = sizeof(BtPager) + nExtra; p = (BtPager*)sqlite4_malloc(pEnv, nByte); if( !p ) return SQLITE4_NOMEM; | | | 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | int sqlite4BtPagerNew(sqlite4_env *pEnv, int nExtra, BtPager **pp){ BtPager *p; int nByte; nByte = sizeof(BtPager) + nExtra; p = (BtPager*)sqlite4_malloc(pEnv, nByte); if( !p ) return SQLITE4_NOMEM; memset(p, 0, nByte); p->pEnv = pEnv; p->pVfs = sqlite4BtEnvDefault(); *pp = p; return SQLITE4_OK; } |
︙ | ︙ | |||
203 204 205 206 207 208 209 210 211 212 213 | ** This function may only be called once for each BtPager object. If it ** fails, the BtPager is rendered unusable (and must be closed by the ** caller using BtPagerClose()). ** ** If successful, SQLITE4_OK is returned. Otherwise, an SQLite error code. */ int sqlite4BtPagerOpen(BtPager *p, const char *zFilename){ int flags = 0; /* Flags to pass to xOpen() */ sqlite4_env *pEnv = p->pEnv; bt_env *pVfs = p->pVfs; | > | | 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | ** This function may only be called once for each BtPager object. If it ** fails, the BtPager is rendered unusable (and must be closed by the ** caller using BtPagerClose()). ** ** If successful, SQLITE4_OK is returned. Otherwise, an SQLite error code. */ int sqlite4BtPagerOpen(BtPager *p, const char *zFilename){ int rc; /* Return code */ int flags = 0; /* Flags to pass to xOpen() */ sqlite4_env *pEnv = p->pEnv; bt_env *pVfs = p->pVfs; assert( p->pFd==0 && p->zFile==0 ); rc = pVfs->xFullpath(pEnv, pVfs, zFilename, &p->zFile); if( rc==SQLITE4_OK ){ p->nFile = strlen(p->zFile); rc = pVfs->xOpen(pEnv, pVfs, zFilename, flags, &p->pFd); } |
︙ | ︙ | |||
240 241 242 243 244 245 246 247 248 249 250 251 252 253 | memset(&p->dbhdr, 0, sizeof(p->dbhdr)); p->dbhdr.pgsz = BT_DEFAULT_PGSZ; } return rc; } static int btCommitTransaction(BtPager *p){ int rc = SQLITE4_OK; BtPage *pPg; BtPage *pNext; assert( p->iTransactionLevel>=2 ); for(pPg=p->pDirty; pPg; pPg=pNext){ | > > > | 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | memset(&p->dbhdr, 0, sizeof(p->dbhdr)); p->dbhdr.pgsz = BT_DEFAULT_PGSZ; } return rc; } /* ** Commit the current write transaction to disk. */ static int btCommitTransaction(BtPager *p){ int rc = SQLITE4_OK; BtPage *pPg; BtPage *pNext; assert( p->iTransactionLevel>=2 ); for(pPg=p->pDirty; pPg; pPg=pNext){ |
︙ | ︙ | |||
261 262 263 264 265 266 267 268 269 270 271 272 273 274 | } p->pDirty = 0; if( rc==SQLITE4_OK ){ rc = p->pVfs->xWrite(p->pFd, 0, (void*)&p->dbhdr, sizeof(BtDbhdr)); } return rc; } /* ** Transactions. These methods are more or less the same as their ** counterparts in bt.h. */ | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | } p->pDirty = 0; if( rc==SQLITE4_OK ){ rc = p->pVfs->xWrite(p->pFd, 0, (void*)&p->dbhdr, sizeof(BtDbhdr)); } return rc; } static int btLoadPageData(BtPager *p, BtPage *pPg){ i64 iOff = (i64)p->dbhdr.pgsz * (i64)(pPg->pgno-1); int rc = p->pVfs->xRead(p->pFd, iOff, pPg->aData, p->dbhdr.pgsz); return rc; } static int btAllocatePage(BtPager *p, BtPage **ppPg){ int rc; /* Return code */ BtPage *pRet; u8 *aData; pRet = (BtPage*)sqlite4_malloc(p->pEnv, sizeof(BtPage)); aData = (u8*)sqlite4_malloc(p->pEnv, p->dbhdr.pgsz); if( pRet && aData ){ memset(pRet, 0, sizeof(BtPage)); pRet->aData = aData; pRet->pPager = p; rc = SQLITE4_OK; }else{ sqlite4_free(p->pEnv, pRet); sqlite4_free(p->pEnv, aData); rc = SQLITE4_NOMEM; pRet = 0; } *ppPg = pRet; return rc; } static void btFreePage(BtPager *p, BtPage *pPg){ if( pPg ){ sqlite4_free(p->pEnv, pPg->aData); sqlite4_free(p->pEnv, pPg); } } /* ** Roll back the current write transaction. */ static int btRollbackTransaction(BtPager *p){ int rc = SQLITE4_OK; BtPage *pPg; BtPage *pNext; assert( p->iTransactionLevel>=2 ); /* Load the old db header from disk */ rc = p->pVfs->xRead(p->pFd, 0, &p->dbhdr, sizeof(p->dbhdr)); /* Loop through all dirty pages in memory. Discard those with nRef==0. ** Reload data from disk for any others. */ for(pPg=p->pDirty; pPg; pPg=pNext){ pNext = pPg->pNextDirty; pPg->flags &= ~(BT_PAGE_DIRTY); pPg->pNextDirty = 0; if( pPg->nRef==0 ){ btHashRemove(p, pPg); btFreePage(p, pPg); }else if( rc==SQLITE4_OK && (pPg->pgno<=p->dbhdr.nPg) ){ rc = btLoadPageData(p, pPg); } } p->pDirty = 0; return rc; } /* ** Transactions. These methods are more or less the same as their ** counterparts in bt.h. */ |
︙ | ︙ | |||
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | } p->iTransactionLevel = iLevel; } return rc; } int sqlite4BtPagerRollback(BtPager *p, int iLevel){ assert( p->pFd ); if( p->iTransactionLevel>=iLevel ){ } } int sqlite4BtPagerRevert(BtPager *p, int iLevel){ assert( p->pFd ); rc = sqlite4BtPagerRollback(p, iLevel); if( rc==SQLITE4_OK && iLevel>=2 && p->iTransactionLevel==iLevel ){ /* Rollback (but do not close) transaction iLevel */ } } /* ** Return the current transaction level. */ int sqlite4BtPagerTransactionLevel(BtPager *p){ return p->iTransactionLevel; | > > > > > > > > > > > | 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | } p->iTransactionLevel = iLevel; } return rc; } int sqlite4BtPagerRollback(BtPager *p, int iLevel){ int rc; assert( p->pFd ); if( p->iTransactionLevel>=iLevel ){ assert( iLevel<=1 ); /* TODO: Fix this! */ rc = btRollbackTransaction(p); p->iTransactionLevel = iLevel; } return rc; } int sqlite4BtPagerRevert(BtPager *p, int iLevel){ int rc; assert( 0 ); /* TODO: Fix this */ assert( p->pFd ); rc = sqlite4BtPagerRollback(p, iLevel); if( rc==SQLITE4_OK && iLevel>=2 && p->iTransactionLevel==iLevel ){ /* Rollback (but do not close) transaction iLevel */ } return rc; } /* ** Return the current transaction level. */ int sqlite4BtPagerTransactionLevel(BtPager *p){ return p->iTransactionLevel; |
︙ | ︙ | |||
340 341 342 343 344 345 346 | */ int sqlite4BtPagerRootpgno(BtPager *p, u32 *piRoot){ assert( p->iTransactionLevel>=1 && p->pFd ); *piRoot = 2; return SQLITE4_OK; } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | */ int sqlite4BtPagerRootpgno(BtPager *p, u32 *piRoot){ assert( p->iTransactionLevel>=1 && p->pFd ); *piRoot = 2; return SQLITE4_OK; } /* ** Read, write and trim existing database pages. */ int sqlite4BtPageGet(BtPager *p, u32 pgno, BtPage **ppPg){ int rc = SQLITE4_OK; /* Return code */ BtPage *pRet; /* Returned page handle */ /* Search the cache for an existing page. */ pRet = btHashSearch(p, pgno); /* If the page is not in the cache, load it from disk */ if( pRet==0 ){ rc = btAllocatePage(p, &pRet); if( rc==SQLITE4_OK ){ pRet->pgno = pgno; if( pgno<=p->dbhdr.nPg ){ rc = btLoadPageData(p, pRet); }else{ memset(pRet->aData, 0, p->dbhdr.pgsz); } if( rc==SQLITE4_OK ){ rc = btHashAdd(p, pRet); } |
︙ | ︙ | |||
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | /* ** Read the schema cookie value. Requires an open read-transaction. */ int sqlite4BtPagerSetCookie(BtPager *p, u32 iVal){ assert( p->iTransactionLevel>=2 ); p->dbhdr.cookie = iVal; } /* ** Set the schema cookie value. Requires an open write-transaction. */ int sqlite4BtPagerGetCookie(BtPager *p, u32 *piVal){ assert( p->iTransactionLevel>=1 ); *piVal = p->dbhdr.cookie; } | > > | 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 | /* ** Read the schema cookie value. Requires an open read-transaction. */ int sqlite4BtPagerSetCookie(BtPager *p, u32 iVal){ assert( p->iTransactionLevel>=2 ); p->dbhdr.cookie = iVal; return SQLITE4_OK; } /* ** Set the schema cookie value. Requires an open write-transaction. */ int sqlite4BtPagerGetCookie(BtPager *p, u32 *piVal){ assert( p->iTransactionLevel>=1 ); *piVal = p->dbhdr.cookie; return SQLITE4_OK; } |