X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Ftest%2Frun-03-coalesce.c;h=93b817284467c03361859065d8016bc6206899b9;hp=4def490ffdb281c9e8e830512d90f51a38bce4eb;hb=98c754ffe65bc335f66161d6cc8705d4ea2710ec;hpb=5e30abc662990449444769c71cf98ca788db4117 diff --git a/ccan/tdb2/test/run-03-coalesce.c b/ccan/tdb2/test/run-03-coalesce.c index 4def490f..93b81728 100644 --- a/ccan/tdb2/test/run-03-coalesce.c +++ b/ccan/tdb2/test/run-03-coalesce.c @@ -1,10 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include +#include "tdb2-source.h" #include #include "logging.h" #include "layout.h" @@ -12,134 +6,157 @@ static tdb_len_t free_record_length(struct tdb_context *tdb, tdb_off_t off) { struct tdb_free_record f; + enum TDB_ERROR ecode; - if (tdb_read_convert(tdb, off, &f, sizeof(f)) != 0) - return TDB_OFF_ERR; + ecode = tdb_read_convert(tdb, off, &f, sizeof(f)); + if (ecode != TDB_SUCCESS) + return ecode; if (frec_magic(&f) != TDB_FREE_MAGIC) - return TDB_OFF_ERR; + return TDB_ERR_CORRUPT; return frec_len(&f); } int main(int argc, char *argv[]) { - tdb_off_t b_off; + tdb_off_t b_off, test; struct tdb_context *tdb; struct tdb_layout *layout; struct tdb_data data, key; tdb_len_t len; /* FIXME: Test TDB_CONVERT */ + /* FIXME: Test lock order fail. */ - plan_tests(38); - data.dptr = (void *)"world"; - data.dsize = 5; - key.dptr = (void *)"hello"; - key.dsize = 5; + plan_tests(42); + data = tdb_mkdata("world", 5); + key = tdb_mkdata("hello", 5); /* No coalescing can be done due to EOF */ - layout = new_tdb_layout(NULL); - tdb_layout_add_freelist(layout); + layout = new_tdb_layout("run-03-coalesce.tdb"); + tdb_layout_add_freetable(layout); len = 1024; tdb_layout_add_free(layout, len, 0); - tdb = tdb_layout_get(layout); + tdb = tdb_layout_get(layout, &tap_log_attr); ok1(tdb_check(tdb, NULL, NULL) == 0); ok1(free_record_length(tdb, layout->elem[1].base.off) == len); /* Figure out which bucket free entry is. */ - b_off = bucket_off(tdb->flist_off, size_to_bucket(len)); + b_off = bucket_off(tdb->tdb2.ftable_off, size_to_bucket(len)); /* Lock and fail to coalesce. */ ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0); - ok1(coalesce(tdb, layout->elem[1].base.off, b_off, len) == 0); + test = layout->elem[1].base.off; + ok1(coalesce(tdb, layout->elem[1].base.off, b_off, len, &test) + == 0); tdb_unlock_free_bucket(tdb, b_off); ok1(free_record_length(tdb, layout->elem[1].base.off) == len); + ok1(test == layout->elem[1].base.off); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); + tdb_layout_free(layout); /* No coalescing can be done due to used record */ - layout = new_tdb_layout(NULL); - tdb_layout_add_freelist(layout); + layout = new_tdb_layout("run-03-coalesce.tdb"); + tdb_layout_add_freetable(layout); tdb_layout_add_free(layout, 1024, 0); tdb_layout_add_used(layout, key, data, 6); - tdb = tdb_layout_get(layout); + tdb = tdb_layout_get(layout, &tap_log_attr); ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024); ok1(tdb_check(tdb, NULL, NULL) == 0); /* Figure out which bucket free entry is. */ - b_off = bucket_off(tdb->flist_off, size_to_bucket(1024)); + b_off = bucket_off(tdb->tdb2.ftable_off, size_to_bucket(1024)); /* Lock and fail to coalesce. */ ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0); - ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 0); + test = layout->elem[1].base.off; + ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024, &test) + == 0); tdb_unlock_free_bucket(tdb, b_off); ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024); + ok1(test == layout->elem[1].base.off); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); + tdb_layout_free(layout); /* Coalescing can be done due to two free records, then EOF */ - layout = new_tdb_layout(NULL); - tdb_layout_add_freelist(layout); + layout = new_tdb_layout("run-03-coalesce.tdb"); + tdb_layout_add_freetable(layout); tdb_layout_add_free(layout, 1024, 0); tdb_layout_add_free(layout, 2048, 0); - tdb = tdb_layout_get(layout); + tdb = tdb_layout_get(layout, &tap_log_attr); ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024); ok1(free_record_length(tdb, layout->elem[2].base.off) == 2048); ok1(tdb_check(tdb, NULL, NULL) == 0); /* Figure out which bucket (first) free entry is. */ - b_off = bucket_off(tdb->flist_off, size_to_bucket(1024)); + b_off = bucket_off(tdb->tdb2.ftable_off, size_to_bucket(1024)); /* Lock and coalesce. */ ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0); - ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1); - ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0); + test = layout->elem[2].base.off; + ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024, &test) + == 1024 + sizeof(struct tdb_used_record) + 2048); + /* Should tell us it's erased this one... */ + ok1(test == TDB_ERR_NOEXIST); + ok1(tdb->file->allrecord_lock.count == 0 && tdb->file->num_lockrecs == 0); ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024 + sizeof(struct tdb_used_record) + 2048); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); + tdb_layout_free(layout); /* Coalescing can be done due to two free records, then data */ - layout = new_tdb_layout(NULL); - tdb_layout_add_freelist(layout); + layout = new_tdb_layout("run-03-coalesce.tdb"); + tdb_layout_add_freetable(layout); tdb_layout_add_free(layout, 1024, 0); tdb_layout_add_free(layout, 512, 0); tdb_layout_add_used(layout, key, data, 6); - tdb = tdb_layout_get(layout); + tdb = tdb_layout_get(layout, &tap_log_attr); ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024); ok1(free_record_length(tdb, layout->elem[2].base.off) == 512); ok1(tdb_check(tdb, NULL, NULL) == 0); /* Figure out which bucket free entry is. */ - b_off = bucket_off(tdb->flist_off, size_to_bucket(1024)); + b_off = bucket_off(tdb->tdb2.ftable_off, size_to_bucket(1024)); /* Lock and coalesce. */ ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0); - ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1); - ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0); + test = layout->elem[2].base.off; + ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024, &test) + == 1024 + sizeof(struct tdb_used_record) + 512); + ok1(tdb->file->allrecord_lock.count == 0 && tdb->file->num_lockrecs == 0); ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024 + sizeof(struct tdb_used_record) + 512); + ok1(test == TDB_ERR_NOEXIST); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); + tdb_layout_free(layout); /* Coalescing can be done due to three free records, then EOF */ - layout = new_tdb_layout(NULL); - tdb_layout_add_freelist(layout); + layout = new_tdb_layout("run-03-coalesce.tdb"); + tdb_layout_add_freetable(layout); tdb_layout_add_free(layout, 1024, 0); tdb_layout_add_free(layout, 512, 0); tdb_layout_add_free(layout, 256, 0); - tdb = tdb_layout_get(layout); + tdb = tdb_layout_get(layout, &tap_log_attr); ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024); ok1(free_record_length(tdb, layout->elem[2].base.off) == 512); ok1(free_record_length(tdb, layout->elem[3].base.off) == 256); ok1(tdb_check(tdb, NULL, NULL) == 0); /* Figure out which bucket free entry is. */ - b_off = bucket_off(tdb->flist_off, size_to_bucket(1024)); + b_off = bucket_off(tdb->tdb2.ftable_off, size_to_bucket(1024)); /* Lock and coalesce. */ ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0); - ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024) == 1); - ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0); + test = layout->elem[2].base.off; + ok1(coalesce(tdb, layout->elem[1].base.off, b_off, 1024, &test) + == 1024 + sizeof(struct tdb_used_record) + 512 + + sizeof(struct tdb_used_record) + 256); + ok1(tdb->file->allrecord_lock.count == 0 + && tdb->file->num_lockrecs == 0); ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024 + sizeof(struct tdb_used_record) + 512 + sizeof(struct tdb_used_record) + 256); ok1(tdb_check(tdb, NULL, NULL) == 0); tdb_close(tdb); + tdb_layout_free(layout); ok1(tap_log_messages == 0); return exit_status();