1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/free.c>
3 #include <ccan/tdb2/lock.c>
4 #include <ccan/tdb2/io.c>
5 #include <ccan/tdb2/hash.c>
6 #include <ccan/tdb2/check.c>
7 #include <ccan/tdb2/transaction.c>
8 #include <ccan/tap/tap.h>
9 #include <ccan/ilog/ilog.h>
12 #define MAX_SIZE 13100
15 static tdb_off_t tdb_offset(struct tdb_context *tdb, struct tdb_data key)
18 struct tdb_used_record rec;
21 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
22 if (TDB_OFF_IS_ERR(off))
24 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
28 int main(int argc, char *argv[])
30 unsigned int i, j, moves;
31 struct tdb_context *tdb;
32 unsigned char *buffer;
33 tdb_off_t oldoff = 0, newoff;
34 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
35 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
36 TDB_NOMMAP|TDB_CONVERT };
37 struct tdb_data key = { (unsigned char *)"key", 3 };
40 buffer = malloc(MAX_SIZE);
41 for (i = 0; i < MAX_SIZE; i++)
44 plan_tests(sizeof(flags) / sizeof(flags[0])
45 * ((3 + MAX_SIZE/SIZE_STEP * 5) * 2 + 7)
48 /* Using tdb_store. */
49 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
50 tdb = tdb_open("run-append.tdb", flags[i],
51 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
57 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
60 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
61 ok1(tdb_check(tdb, NULL, NULL) == 0);
62 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
64 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
66 newoff = tdb_offset(tdb, key);
71 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
72 /* We should increase by 50% each time... */
73 ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
77 /* Using tdb_append. */
78 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
80 tdb = tdb_open("run-append.tdb", flags[i],
81 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
87 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
88 data.dptr = buffer + prev_len;
89 data.dsize = j - prev_len;
90 ok1(tdb_append(tdb, key, data) == 0);
91 ok1(tdb_check(tdb, NULL, NULL) == 0);
92 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
94 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
96 prev_len = data.dsize;
97 newoff = tdb_offset(tdb, key);
102 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
103 /* We should increase by 50% each time... */
104 ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
108 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
109 tdb = tdb_open("run-append.tdb", flags[i],
110 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
115 /* Huge initial store. */
117 data.dsize = MAX_SIZE;
118 ok1(tdb_append(tdb, key, data) == 0);
119 ok1(tdb_check(tdb, NULL, NULL) == 0);
120 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
121 ok1(data.dsize == MAX_SIZE);
122 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
124 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
128 ok1(tap_log_messages == 0);
130 return exit_status();