Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Updates to code to read log files. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
aabb6dacf659c3c79fa20e73e96151df |
User & Date: | dan 2013-10-24 19:59:05.046 |
Context
2013-10-24
| ||
23:54 | Add tests for the pbReal output of sqlite4_num_from_text check-in: fd1c1a7d4f user: peterreid tags: trunk | |
19:59 | Updates to code to read log files. check-in: aabb6dacf6 user: dan tags: trunk | |
2013-10-23
| ||
22:54 | Prevent unnecessary rounding before multiplying check-in: 68dfaa3491 user: peterreid tags: trunk | |
Changes
Changes to lsm-test/lsmtest_tdb.c.
︙ | ︙ | |||
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 | }; BtDb *p = 0; bt_db *pBt = 0; int rc; sqlite4_env *pEnv = sqlite4_env_default(); if( bClear && zFilename && zFilename[0] ){ unlink(zFilename); } rc = sqlite4BtNew(pEnv, sizeof(BtDb), &pBt); if( rc==SQLITE4_OK ){ p = (BtDb*)sqlite4BtExtra(pBt); p->base.pMethods = &SqlMethods; p->pBt = pBt; | > > > | 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 | }; BtDb *p = 0; bt_db *pBt = 0; int rc; sqlite4_env *pEnv = sqlite4_env_default(); if( bClear && zFilename && zFilename[0] ){ char *zLog = sqlite3_mprintf("%s-wal", zFilename); unlink(zFilename); unlink(zLog); sqlite3_free(zLog); } rc = sqlite4BtNew(pEnv, sizeof(BtDb), &pBt); if( rc==SQLITE4_OK ){ p = (BtDb*)sqlite4BtExtra(pBt); p->base.pMethods = &SqlMethods; p->pBt = pBt; |
︙ | ︙ |
Changes to src/bt_log.c.
︙ | ︙ | |||
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | ** byte offset 0 and the other at offset <sector-size> (the start of ** the second disk sector in the file, according to the xSectorSize ** VFS method). */ struct BtWalHdr { u32 iMagic; /* Magic number (BT_WAL_MAGIC) */ u32 iVersion; /* File format version */ u32 nSector; /* Sector size when header written */ u32 iSalt1; /* Initial frame cksum-0 value */ u32 iSalt2; /* Initial frame cksum-1 value */ u32 iFirstFrame; /* First frame of log (numbered from 1) */ u32 aCksum[2]; /* Checksum of all prior fields */ }; /* ** WAL Frame header. All fields are stored in big-endian order. */ struct BtFrameHdr { u32 pgno; /* Page number of this frame */ u32 ctrl; /* Next frame pointer and commit bit */ u32 aCksum[2]; /* Frame checksum */ }; /* ** Shared memory header. Shared memory begins with two copies of ** this structure. All fields are stored in machine byte-order. */ struct BtShmHdr { u32 aLog[6]; /* First/last frames for each log region */ | > > > > | 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | ** byte offset 0 and the other at offset <sector-size> (the start of ** the second disk sector in the file, according to the xSectorSize ** VFS method). */ struct BtWalHdr { u32 iMagic; /* Magic number (BT_WAL_MAGIC) */ u32 iVersion; /* File format version */ u32 iCnt; /* 0, 1 or 2 */ u32 nSector; /* Sector size when header written */ u32 nPgsz; /* Database page size in bytes */ u32 iSalt1; /* Initial frame cksum-0 value */ u32 iSalt2; /* Initial frame cksum-1 value */ u32 iFirstFrame; /* First frame of log (numbered from 1) */ u32 aCksum[2]; /* Checksum of all prior fields */ }; /* ** WAL Frame header. All fields are stored in big-endian order. */ struct BtFrameHdr { u32 pgno; /* Page number of this frame */ u32 ctrl; /* Next frame pointer and commit bit */ u32 aCksum[2]; /* Frame checksum */ }; #define BT_FRAME_COMMIT 0x80000000 /* ** Shared memory header. Shared memory begins with two copies of ** this structure. All fields are stored in machine byte-order. */ struct BtShmHdr { u32 aLog[6]; /* First/last frames for each log region */ |
︙ | ︙ | |||
236 237 238 239 240 241 242 243 244 245 246 247 248 | } static void btLogZeroSnapshot(BtLog *pLog){ bt_env *pVfs = pLog->pLock->pVfs; memset(&pLog->snapshot, 0, sizeof(BtShmHdr)); pLog->snapshot.nSector = pVfs->xSectorSize(pLog->pFd); } /* ** Run log recovery. In other words, read the log file from disk and ** initialize the shared-memory accordingly. */ static int btLogRecover(BtLog *pLog){ | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 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 415 416 417 418 419 420 421 422 423 424 425 426 427 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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | } static void btLogZeroSnapshot(BtLog *pLog){ bt_env *pVfs = pLog->pLock->pVfs; memset(&pLog->snapshot, 0, sizeof(BtShmHdr)); pLog->snapshot.nSector = pVfs->xSectorSize(pLog->pFd); } /* ** Return the offset of frame iFrame within the log file. */ static i64 btLogFrameOffset(BtLog *pLog, int pgsz, u32 iFrame){ return (i64)pLog->snapshot.nSector*2 + (i64)(iFrame-1) * (i64)(pgsz + sizeof(BtFrameHdr)); } static int btLogWriteData(BtLog *pLog, i64 iOff, u8 *aData, int nData){ bt_env *pVfs = pLog->pLock->pVfs; return pVfs->xWrite(pLog->pFd, iOff, aData, nData); } static int btLogReadData(BtLog *pLog, i64 iOff, u8 *aData, int nData){ bt_env *pVfs = pLog->pLock->pVfs; return pVfs->xRead(pLog->pFd, iOff, aData, nData); } static int btLogReadHeader(BtLog *pLog, int iOff, BtWalHdr *pHdr){ int rc = btLogReadData(pLog, (i64)iOff, (u8*)pHdr, sizeof(BtWalHdr)); if( rc==SQLITE4_OK ){ u32 aCksum[2]; btLogChecksum(1, (u8*)pHdr, offsetof(BtWalHdr, aCksum), 0, aCksum); if( pHdr->iMagic!=BT_WAL_MAGIC || aCksum[0]!=pHdr->aCksum[0] || aCksum[1]!=pHdr->aCksum[1] ){ rc = SQLITE4_NOTFOUND; } } return rc; } /* ** This function is used as part of recovery. It reads the contents of ** the log file from disk and invokes the xFrame callback for each valid ** frame in the file. */ static int btLogTraverse( BtLog *pLog, /* Log module handle */ BtWalHdr *pHdr, /* Log header read from file */ int(*xFrame)(BtLog*, void*, u32, BtFrameHdr*), /* Frame callback */ void *pCtx /* Passed as second argument to xFrame */ ){ sqlite4_env *pEnv = pLog->pLock->pEnv; const int pgsz = pHdr->nPgsz; u32 iFrame = pHdr->iFirstFrame; u32 aCksum[2]; BtFrameHdr fhdr; /* Frame header */ u8 *aBuf; /* Buffer for frame data */ int rc = SQLITE4_OK; aCksum[0] = pHdr->iSalt1; aCksum[1] = pHdr->iSalt2; aBuf = sqlite4_malloc(pEnv, pgsz); if( aBuf==0 ){ rc = SQLITE4_NOMEM; } while( rc==SQLITE4_OK ){ i64 iOff; iOff = btLogFrameOffset(pLog, pgsz, iFrame); rc = btLogReadData(pLog, iOff, (u8*)&fhdr, sizeof(BtFrameHdr)); if( rc==SQLITE4_OK ){ rc = btLogReadData(pLog, iOff+sizeof(BtFrameHdr), aBuf, pgsz); } if( rc==SQLITE4_OK ){ btLogChecksum(1, (u8*)&fhdr, offsetof(BtFrameHdr,aCksum), aCksum, aCksum); btLogChecksum(1, aBuf, pgsz, aCksum, aCksum); if( aCksum[0]!=fhdr.aCksum[0] || aCksum[1]!=fhdr.aCksum[1] ) break; } if( rc==SQLITE4_OK ){ rc = xFrame(pLog, pCtx, iFrame, &fhdr); } iFrame++; } return rc; } /* ** Locate the iHash'th hash table in shared memory. Return it. */ static int btLogFindHash( BtLog *pLog, /* Log handle */ int iHash, /* Hash table (numbered from 0) to find */ ht_slot **paHash, /* OUT: Pointer to hash slots */ u32 **paPgno, /* OUT: Pointer to page number array */ u32 *piZero /* OUT: Frame associated with *paPgno[0] */ ){ int rc; rc = btLogMapShm(pLog, iHash); if( rc==SQLITE4_OK ){ u8 *aChunk = pLog->apShm[iHash]; u32 *aPgno; u32 iZero; *paHash = (ht_slot*)&aChunk[HASHTABLE_OFFSET_1]; if( iHash==0 ){ aPgno = (u32*)&aChunk[sizeof(BtShm)]; iZero = 1; }else{ aPgno = (u32*)aChunk; iZero = 1 + HASHTABLE_NFRAME_ONE + (HASHTABLE_NFRAME * (iHash-1)); } *paPgno = aPgno; *piZero = iZero; } return rc; } /* ** Return the index of the hash table that contains the entry for frame ** iFrame. */ static int btLogFrameHash(BtLog *pLog, u32 iFrame){ if( iFrame<=HASHTABLE_NFRAME_ONE ) return 0; return 1 + ((iFrame - HASHTABLE_NFRAME_ONE - 1) / HASHTABLE_NFRAME); } /* ** Return a hash key for page number pgno. */ static int btLogHashKey(BtLog *pLog, u32 pgno){ assert( pgno>=1 ); return ((pgno * HASHTABLE_KEY_MUL) % HASHTABLE_NSLOT); } static int btLogHashNext(BtLog *pLog, int iSlot){ return ((iSlot + 1) % HASHTABLE_NSLOT); } /* ** Add an entry mapping database page pgno to log frame iFrame to the ** the shared hash table. Return SQLITE4_OK if successful, or an SQLite4 ** error code if an error occurs. */ static int btLogHashInsert(BtLog *pLog, u32 pgno, u32 iFrame){ int iHash; /* Index of hash table to update */ int rc = SQLITE4_OK; /* Return code */ ht_slot *aHash; /* Hash slots */ u32 *aPgno; /* Page array for updated hash table */ u32 iZero; /* Zero-offset of updated hash table */ assert( iFrame>=1 && pgno>=1 ); /* Find the required hash table */ iHash = btLogFrameHash(pLog, iFrame); rc = btLogFindHash(pLog, iHash, &aHash, &aPgno, &iZero); /* Update the hash table */ if( rc==SQLITE4_OK ){ int iSlot; int nCollide = HASHTABLE_NSLOT*2; aPgno[iFrame-iZero] = pgno; for(iSlot=btLogHashKey(pLog,pgno); ; iSlot=btLogHashNext(pLog, iSlot)){ if( aHash[iSlot]==0 ){ aHash[iSlot] = (iFrame-iZero+1); break; } if( (nCollide--)==0 ) return btErrorBkpt(SQLITE4_CORRUPT); } } return rc; } /* ** Remove everything following frame iFrame from the iHash'th hash table. */ static int btLogHashRollback(BtLog *pLog, int iHash, u32 iFrame){ const int nPgno = (iHash==0 ? HASHTABLE_NFRAME_ONE : HASHTABLE_NFRAME); ht_slot *aHash; /* Hash slots */ u32 *aPgno; /* Page array for updated hash table */ u32 iZero; /* Zero-offset of updated hash table */ int rc; rc = btLogFindHash(pLog, iHash, &aHash, &aPgno, &iZero); if( rc==SQLITE4_OK ){ int i; ht_slot iMax; iMax = (iFrame - iZero) + 1; for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i]>iMax ) aHash[i] = 0; } memset(&aPgno[iMax], 0, (nPgno-iMax)*sizeof(u32)); } return rc; } /* ** Return true if log is completely empty (as it is if a file zero bytes ** in size has been opened or created). */ static int btLogIsEmpty(BtLog *pLog){ return (pLog->snapshot.aLog[4]==0); } static int btLogRecoverFrame( BtLog *pLog, /* Log module handle */ void *pCtx, /* woints to type u32 - pgno of last commit*/ u32 iFrame, /* Frame number */ BtFrameHdr *pHdr /* Frame header */ ){ btLogHashInsert(pLog, pHdr->pgno, iFrame); if( pHdr->ctrl & BT_FRAME_COMMIT ){ *(u32*)pCtx = iFrame; memcpy(pLog->snapshot.aFrameCksum, pHdr->aCksum, sizeof(pHdr->aCksum)); } #if 0 fprintf(stderr, "recovered frame=%d pgno=%d\n", iFrame, pHdr->pgno); fflush(stderr); #endif return 0; } /* ** Run log recovery. In other words, read the log file from disk and ** initialize the shared-memory accordingly. */ static int btLogRecover(BtLog *pLog){ bt_env *pVfs = pLog->pLock->pVfs; i64 nByte = 0; /* Size of log file on disk */ int rc; /* Return code */ BtWalHdr *pHdr = 0; /* Read a log file header from the start of the file. */ rc = pVfs->xSize(pLog->pFd, &nByte); if( rc==SQLITE4_OK && nByte>0 ){ BtWalHdr hdr1; rc = btLogReadHeader(pLog, 0, &hdr1); if( rc==SQLITE4_OK ){ BtWalHdr hdr2; rc = btLogReadHeader(pLog, hdr1.nSector, &hdr2); if( rc==SQLITE4_NOTFOUND ){ pHdr = &hdr1; }else if( rc==SQLITE4_OK ){ int aGreater[3] = {1, 2, 0}; pHdr = ((hdr2.iCnt==aGreater[hdr1.iCnt]) ? &hdr2 : &hdr1); } }else if( rc==SQLITE4_NOTFOUND ){ int iOff; for(iOff=256; iOff<=65536 && rc==SQLITE4_NOTFOUND; iOff=iOff*2){ rc = btLogReadHeader(pLog, iOff, &hdr1); } if( rc==SQLITE4_OK ){ pHdr = &hdr1; } } if( rc==SQLITE4_NOTFOUND ) rc = SQLITE4_OK; } /* If a header was successfully read from the file, scan the log file ** and populate the shared-memory hash tables. */ if( pHdr ){ u32 iCommit = 0; rc = btLogTraverse(pLog, pHdr, btLogRecoverFrame, (void*)&iCommit); if( rc==SQLITE4_OK && iCommit>0 ){ pLog->snapshot.aLog[4] = 1; pLog->snapshot.aLog[5] = iCommit; rc = btLogHashRollback(pLog, btLogFrameHash(pLog, iCommit), iCommit); } } return rc; } /* ** Open the log file for pager pPager. If successful, return the BtLog* ** handle via output variable *ppLog. If parameter bRecover is true, then ** also run database recovery before returning. In this case, the caller ** has already obtained the required locks. |
︙ | ︙ | |||
313 314 315 316 317 318 319 | sqlite4_free(pEnv, pLog->apShm); sqlite4_free(pEnv, pLog); } return rc; } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 | sqlite4_free(pEnv, pLog->apShm); sqlite4_free(pEnv, pLog); } return rc; } static int btLogWriteHeader(BtLog *pLog, int iHdr, BtWalHdr *pHdr){ int rc; /* Return code */ i64 iOff; /* File offset to write to */ assert( iHdr==0 || iHdr==1 ); /* Calculate a checksum for the header */ btLogChecksum(1, (u8*)pHdr, offsetof(BtWalHdr, aCksum), 0, pHdr->aCksum); /* Write the object to disk */ iOff = iHdr * pLog->snapshot.nSector; rc = btLogWriteData(pLog, iOff, (u8*)pHdr, sizeof(BtWalHdr)); return rc; } static int btLogHashSearch(BtLog *pLog, int iHash, u32 pgno, u32 *piFrame){ ht_slot *aHash; u32 *aPgno; u32 iZero; int rc; rc = btLogFindHash(pLog, iHash, &aHash, &aPgno, &iZero); |
︙ | ︙ | |||
425 426 427 428 429 430 431 | } } return rc; } | < < < < < < < < < | 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | } } return rc; } /* ** Attempt to read data for page pgno from the log file. If successful, ** the data is written into buffer aData[] (which must be at least as ** large as a database page). In this case SQLITE4_OK is returned. ** ** If the log does not contain any version of page pgno, SQLITE4_NOTFOUND ** is returned and the contents of buffer aData[] are not modified. |
︙ | ︙ | |||
472 473 474 475 476 477 478 | fflush(stderr); #endif } return rc; } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < > > > > | | 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 | fflush(stderr); #endif } return rc; } /* ** Write a frame to the log file. */ int sqlite4BtLogWrite(BtLog *pLog, u32 pgno, u8 *aData, int bCommit){ const int pgsz = sqlite4BtPagerPagesize((BtPager*)(pLog->pLock)); int rc = SQLITE4_OK; u32 iFrame; /* Write this frame (numbered from 1) */ BtFrameHdr frame; /* Header for new frame */ u32 *a; /* Pointer to cksum of previous frame */ i64 iOff; /* Offset of log file to write to */ /* Handle a special case - if the log file is completely empty then ** this writer must write the first header into the WAL file. */ if( btLogIsEmpty(pLog) ){ BtWalHdr hdr; memset(&hdr, 0, sizeof(BtWalHdr)); hdr.iMagic = BT_WAL_MAGIC; hdr.iVersion = BT_WAL_VERSION; hdr.nSector = pLog->snapshot.nSector; hdr.nPgsz = pgsz; hdr.iSalt1 = 22; hdr.iSalt2 = 23; hdr.iFirstFrame = 1; rc = btLogWriteHeader(pLog, 0, &hdr); if( rc!=SQLITE4_OK ) return rc; pLog->snapshot.aFrameCksum[0] = hdr.iSalt1; pLog->snapshot.aFrameCksum[1] = hdr.iSalt2; } /* Figure out where exactly to write the new data */ iFrame = pLog->snapshot.aLog[5] + 1; iOff = btLogFrameOffset(pLog, pgsz, iFrame); /* Populate the frame header object. */ memset(&frame, 0, sizeof(frame)); frame.pgno = pgno; frame.ctrl = (bCommit ? BT_FRAME_COMMIT : 0); a = pLog->snapshot.aFrameCksum; btLogChecksum(1, (u8*)&frame, offsetof(BtFrameHdr, aCksum), a, frame.aCksum); btLogChecksum(1, aData, pgsz, frame.aCksum, frame.aCksum); #if 0 fprintf(stderr, "writing page %d at log offset %d (frame %d)\n", (int)pgno, (int)iOff, (int)iFrame); fflush(stderr); |
︙ | ︙ |