X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb%2Ftest%2Frun-transaction-expand.c;h=26426b2f1fb2092c0b4fa03ffd291445aba02d54;hp=463cb1069c93c8b8655b2dff9529c3dcefeca1b5;hb=c46e5c375d5f5e3187034687d241ab6da6eb6205;hpb=75281499320b9a4368aaa281ae282e55c47ac488 diff --git a/ccan/tdb/test/run-transaction-expand.c b/ccan/tdb/test/run-transaction-expand.c index 463cb106..26426b2f 100644 --- a/ccan/tdb/test/run-transaction-expand.c +++ b/ccan/tdb/test/run-transaction-expand.c @@ -41,13 +41,10 @@ static void write_record(struct tdb_context *tdb, size_t extra_len, key.dsize = strlen("hi"); key.dptr = (void *)"hi"; - data->dptr = realloc(data->dptr, data->dsize + extra_len); - memset(data->dptr + data->dsize, 'x', extra_len); data->dsize += extra_len; tdb_transaction_start(tdb); tdb_store(tdb, key, *data, TDB_REPLACE); tdb_transaction_commit(tdb); - diag("TDB size = %zu", (size_t)tdb->map_size); } int main(int argc, char *argv[]) @@ -55,22 +52,53 @@ int main(int argc, char *argv[]) struct tdb_context *tdb; size_t i; TDB_DATA data; + struct tdb_record rec; + tdb_off_t off; - plan_tests(2); + plan_tests(4); tdb = tdb_open_ex("run-transaction-expand.tdb", 1024, TDB_CLEAR_IF_FIRST, O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL); ok1(tdb); data.dsize = 0; - data.dptr = NULL; + data.dptr = calloc(1000, getpagesize()); /* Simulate a slowly growing record. */ for (i = 0; i < 1000; i++) write_record(tdb, getpagesize(), &data); - /* We should only be about 3 times larger than largest record. */ - ok1(tdb->map_size < 3 * i * getpagesize()); + tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &off); + tdb_read(tdb, off, &rec, sizeof(rec), DOCONV()); + diag("TDB size = %zu, recovery = %u-%u", + (size_t)tdb->map_size, off, off + sizeof(rec) + rec.rec_len); + + /* We should only be about 5 times larger than largest record. */ + ok1(tdb->map_size < 6 * i * getpagesize()); + tdb_close(tdb); + + tdb = tdb_open_ex("run-transaction-expand.tdb", + 1024, TDB_CLEAR_IF_FIRST, + O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL); + ok1(tdb); + + data.dsize = 0; + + /* Simulate a slowly growing record, repacking to keep + * recovery area at end. */ + for (i = 0; i < 1000; i++) { + write_record(tdb, getpagesize(), &data); + if (i % 10 == 0) + tdb_repack(tdb); + } + + tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &off); + tdb_read(tdb, off, &rec, sizeof(rec), DOCONV()); + diag("TDB size = %zu, recovery = %u-%u", + (size_t)tdb->map_size, off, off + sizeof(rec) + rec.rec_len); + + /* We should only be about 4 times larger than largest record. */ + ok1(tdb->map_size < 5 * i * getpagesize()); tdb_close(tdb); free(data.dptr);