/* ** This file contains tests related to recovery following application ** and system crashes (power failures) while writing to the database. */ #include "lsmtest.h" /* ** Structure used by testCksumDatabase() to accumulate checksum values in. */ typedef struct Cksum Cksum; struct Cksum { int nRow; int cksum1; int cksum2; }; /* ** tdb_scan() callback used by testCksumDatabase() */ static void scanCksumDb( void *pCtx, void *pKey, int nKey, void *pVal, int nVal ){ Cksum *p = (Cksum *)pCtx; int i; p->nRow++; for(i=0; icksum1 += ((u8 *)pKey)[i]; p->cksum2 += p->cksum1; } for(i=0; icksum1 += ((u8 *)pVal)[i]; p->cksum2 += p->cksum1; } } /* ** tdb_scan() callback used by testCountDatabase() */ static void scanCountDb( void *pCtx, void *pKey, int nKey, void *pVal, int nVal ){ Cksum *p = (Cksum *)pCtx; p->nRow++; unused_parameter(pKey); unused_parameter(nKey); unused_parameter(pVal); unused_parameter(nVal); } /* ** Iterate through the entire contents of database pDb. Write a checksum ** string based on the db contents into buffer zOut before returning. A ** checksum string is at most 29 (TEST_CKSUM_BYTES) bytes in size: ** ** * 32-bit integer (10 bytes) ** * 1 space (1 byte) ** * 32-bit hex (8 bytes) ** * 1 space (1 byte) ** * 32-bit hex (8 bytes) ** * nul-terminator (1 byte) ** ** The number of entries in the database is returned. */ int testCksumDatabase( TestDb *pDb, /* Database handle */ char *zOut /* Buffer to write checksum to */ ){ Cksum cksum; memset(&cksum, 0, sizeof(Cksum)); tdb_scan(pDb, (void *)&cksum, 0, 0, 0, 0, 0, scanCksumDb); sprintf(zOut, "%d %x %x", cksum.nRow, (u32)cksum.cksum1, (u32)cksum.cksum2 ); assert( strlen(zOut)0 ); */ if( testrc==0 ) testrc = lsm_checkpoint(db, 0); } tdb_close(pDb); /* Check that the database content is still correct */ testCompareCksumLsmdb(DBNAME, bCompress, testCksumArrayGet(pCksumDb, nRow), 0, pRc); } testCksumArrayFree(pCksumDb); testDatasourceFree(pData); } /* ** This test verifies that if a system crash occurs while committing a ** transaction to the log file, no earlier transactions are lost or damaged. */ static void crash_test2(int bCompress, int *pRc){ const char *DBNAME = "testdb.lsm"; const DatasourceDefn defn = {TEST_DATASOURCE_RANDOM, 12, 16, 1000, 1000}; const int nIter = 200; const int nInsert = 20; int i; int iDot = 0; Datasource *pData; CksumDb *pCksumDb; TestDb *pDb; /* Allocate datasource. And calculate the expected checksums. */ pData = testDatasourceNew(&defn); pCksumDb = testCksumArrayNew(pData, 100, 100+nInsert, 1); /* Setup and save the initial database. */ testSetupSavedLsmdb("", DBNAME, pData, 100, pRc); for(i=0; izTest) ){ p->x(p->bCompress, pRc); testCaseFinish(*pRc); } } }