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/tap/tap.h>
8 #include <ccan/ilog/ilog.h>
11 #define MAX_SIZE 13100
14 static tdb_off_t tdb_offset(struct tdb_context *tdb, struct tdb_data key)
17 struct tdb_used_record rec;
20 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
21 if (unlikely(off == TDB_OFF_ERR))
23 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
27 int main(int argc, char *argv[])
29 unsigned int i, j, moves;
30 struct tdb_context *tdb;
31 unsigned char *buffer;
32 tdb_off_t oldoff = 0, newoff;
33 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
34 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
35 TDB_NOMMAP|TDB_CONVERT };
36 struct tdb_data key = { (unsigned char *)"key", 3 };
39 buffer = malloc(MAX_SIZE);
40 for (i = 0; i < MAX_SIZE; i++)
43 plan_tests(sizeof(flags) / sizeof(flags[0])
44 * ((3 + MAX_SIZE/SIZE_STEP * 4) * 2 + 6)
47 /* Using tdb_store. */
48 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
49 tdb = tdb_open("run-append.tdb", flags[i],
50 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
56 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
59 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
60 ok1(tdb_check(tdb, NULL, NULL) == 0);
61 data = tdb_fetch(tdb, key);
63 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
65 newoff = tdb_offset(tdb, key);
70 ok1(!tdb_has_locks(tdb));
71 /* We should increase by 50% each time... */
72 ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
76 /* Using tdb_append. */
77 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
79 tdb = tdb_open("run-append.tdb", flags[i],
80 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
86 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
87 data.dptr = buffer + prev_len;
88 data.dsize = j - prev_len;
89 ok1(tdb_append(tdb, key, data) == 0);
90 ok1(tdb_check(tdb, NULL, NULL) == 0);
91 data = tdb_fetch(tdb, key);
93 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
95 prev_len = data.dsize;
96 newoff = tdb_offset(tdb, key);
101 ok1(!tdb_has_locks(tdb));
102 /* We should increase by 50% each time... */
103 ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
107 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
108 tdb = tdb_open("run-append.tdb", flags[i],
109 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
114 /* Huge initial store. */
116 data.dsize = MAX_SIZE;
117 ok1(tdb_append(tdb, key, data) == 0);
118 ok1(tdb_check(tdb, NULL, NULL) == 0);
119 data = tdb_fetch(tdb, key);
120 ok1(data.dsize == MAX_SIZE);
121 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
123 ok1(!tdb_has_locks(tdb));
127 ok1(tap_log_messages == 0);
128 return exit_status();