1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include <ccan/ilog/ilog.h>
9 static tdb_off_t tdb_offset(struct tdb_context *tdb, struct tdb_data key)
12 struct tdb_used_record rec;
15 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
16 if (TDB_OFF_IS_ERR(off))
18 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
22 int main(int argc, char *argv[])
24 unsigned int i, j, moves;
25 struct tdb_context *tdb;
26 unsigned char *buffer;
27 tdb_off_t oldoff = 0, newoff;
28 int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
29 TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
30 TDB_NOMMAP|TDB_CONVERT };
31 struct tdb_data key = tdb_mkdata("key", 3);
34 buffer = malloc(MAX_SIZE);
35 for (i = 0; i < MAX_SIZE; i++)
38 plan_tests(sizeof(flags) / sizeof(flags[0])
39 * ((3 + MAX_SIZE/SIZE_STEP * 5) * 2 + 7)
42 /* Using tdb_store. */
43 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
44 tdb = tdb_open("run-append.tdb", flags[i],
45 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
51 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
54 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
55 ok1(tdb_check(tdb, NULL, NULL) == 0);
56 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
58 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
60 newoff = tdb_offset(tdb, key);
65 ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
66 && tdb->file->num_lockrecs == 0));
67 /* We should increase by 50% each time... */
68 ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
72 /* Using tdb_append. */
73 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
75 tdb = tdb_open("run-append.tdb", flags[i],
76 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
82 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
83 data.dptr = buffer + prev_len;
84 data.dsize = j - prev_len;
85 ok1(tdb_append(tdb, key, data) == 0);
86 ok1(tdb_check(tdb, NULL, NULL) == 0);
87 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
89 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
91 prev_len = data.dsize;
92 newoff = tdb_offset(tdb, key);
97 ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
98 && tdb->file->num_lockrecs == 0));
99 /* We should increase by 50% each time... */
100 ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
104 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
105 tdb = tdb_open("run-append.tdb", flags[i],
106 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
111 /* Huge initial store. */
113 data.dsize = MAX_SIZE;
114 ok1(tdb_append(tdb, key, data) == 0);
115 ok1(tdb_check(tdb, NULL, NULL) == 0);
116 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
117 ok1(data.dsize == MAX_SIZE);
118 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
120 ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
121 && tdb->file->num_lockrecs == 0));
125 ok1(tap_log_messages == 0);
127 return exit_status();