1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/open.c>
3 #include <ccan/tdb2/free.c>
4 #include <ccan/tdb2/lock.c>
5 #include <ccan/tdb2/io.c>
6 #include <ccan/tdb2/hash.c>
7 #include <ccan/tdb2/check.c>
8 #include <ccan/tdb2/transaction.c>
9 #include <ccan/tap/tap.h>
10 #include <ccan/ilog/ilog.h>
13 #define MAX_SIZE 13100
16 static tdb_off_t tdb_offset(struct tdb_context *tdb, struct tdb_data key)
19 struct tdb_used_record rec;
22 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
23 if (TDB_OFF_IS_ERR(off))
25 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
29 int main(int argc, char *argv[])
31 unsigned int i, j, moves;
32 struct tdb_context *tdb;
33 unsigned char *buffer;
34 tdb_off_t oldoff = 0, newoff;
35 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
36 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
37 TDB_NOMMAP|TDB_CONVERT };
38 struct tdb_data key = { (unsigned char *)"key", 3 };
41 buffer = malloc(MAX_SIZE);
42 for (i = 0; i < MAX_SIZE; i++)
45 plan_tests(sizeof(flags) / sizeof(flags[0])
46 * ((3 + MAX_SIZE/SIZE_STEP * 5) * 2 + 7)
49 /* Using tdb_store. */
50 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
51 tdb = tdb_open("run-append.tdb", flags[i],
52 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
58 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
61 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
62 ok1(tdb_check(tdb, NULL, NULL) == 0);
63 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
65 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
67 newoff = tdb_offset(tdb, key);
72 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
73 /* We should increase by 50% each time... */
74 ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
78 /* Using tdb_append. */
79 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
81 tdb = tdb_open("run-append.tdb", flags[i],
82 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
88 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
89 data.dptr = buffer + prev_len;
90 data.dsize = j - prev_len;
91 ok1(tdb_append(tdb, key, data) == 0);
92 ok1(tdb_check(tdb, NULL, NULL) == 0);
93 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
95 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
97 prev_len = data.dsize;
98 newoff = tdb_offset(tdb, key);
103 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
104 /* We should increase by 50% each time... */
105 ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
109 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
110 tdb = tdb_open("run-append.tdb", flags[i],
111 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
116 /* Huge initial store. */
118 data.dsize = MAX_SIZE;
119 ok1(tdb_append(tdb, key, data) == 0);
120 ok1(tdb_check(tdb, NULL, NULL) == 0);
121 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
122 ok1(data.dsize == MAX_SIZE);
123 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
125 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
129 ok1(tap_log_messages == 0);
131 return exit_status();