1 #include "tdb2-source.h"
2 #include "tap-interface.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 urec;
15 off = find_and_lock(tdb, key, F_RDLCK, &h, &urec, 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,
69 "Moved %u times", moves);
73 /* Using tdb_append. */
74 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
76 tdb = tdb_open("run-append.tdb", flags[i],
77 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
83 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
84 data.dptr = buffer + prev_len;
85 data.dsize = j - prev_len;
86 ok1(tdb_append(tdb, key, data) == 0);
87 ok1(tdb_check(tdb, NULL, NULL) == 0);
88 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
90 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
92 prev_len = data.dsize;
93 newoff = tdb_offset(tdb, key);
98 ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
99 && tdb->file->num_lockrecs == 0));
100 /* We should increase by 50% each time... */
101 ok(moves <= ilog64(j / SIZE_STEP)*2,
102 "Moved %u times", moves);
106 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
107 tdb = tdb_open("run-append.tdb", flags[i],
108 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
113 /* Huge initial store. */
115 data.dsize = MAX_SIZE;
116 ok1(tdb_append(tdb, key, data) == 0);
117 ok1(tdb_check(tdb, NULL, NULL) == 0);
118 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
119 ok1(data.dsize == MAX_SIZE);
120 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
122 ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
123 && tdb->file->num_lockrecs == 0));
127 ok1(tap_log_messages == 0);
129 return exit_status();