1 #include "ntdb-source.h"
2 #include "tap-interface.h"
3 #include <ccan/ilog/ilog.h>
5 #include "helprun-external-agent.h"
10 static ntdb_off_t ntdb_offset(struct ntdb_context *ntdb, NTDB_DATA key)
13 struct ntdb_used_record urec;
16 off = find_and_lock(ntdb, key, F_RDLCK, &h, &urec, NULL);
17 if (NTDB_OFF_IS_ERR(off))
19 ntdb_unlock_hash(ntdb, h.h, F_RDLCK);
23 int main(int argc, char *argv[])
25 unsigned int i, j, moves;
26 struct ntdb_context *ntdb;
27 unsigned char *buffer;
28 ntdb_off_t oldoff = 0, newoff;
29 int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
30 NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
31 NTDB_NOMMAP|NTDB_CONVERT };
32 NTDB_DATA key = ntdb_mkdata("key", 3);
35 buffer = malloc(MAX_SIZE);
36 for (i = 0; i < MAX_SIZE; i++)
39 plan_tests(sizeof(flags) / sizeof(flags[0])
40 * ((3 + MAX_SIZE/SIZE_STEP * 5) * 2 + 7)
43 /* Using ntdb_store. */
44 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
45 ntdb = ntdb_open("run-append.ntdb", flags[i]|MAYBE_NOSYNC,
46 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
52 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
55 ok1(ntdb_store(ntdb, key, data, NTDB_REPLACE) == 0);
56 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
57 ok1(ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS);
59 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
61 newoff = ntdb_offset(ntdb, key);
66 ok1(!ntdb->file || (ntdb->file->allrecord_lock.count == 0
67 && ntdb->file->num_lockrecs == 0));
68 /* We should increase by 50% each time... */
69 ok(moves <= ilog64(j / SIZE_STEP)*2,
70 "Moved %u times", moves);
74 /* Using ntdb_append. */
75 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
77 ntdb = ntdb_open("run-append.ntdb", flags[i]|MAYBE_NOSYNC,
78 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
84 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
85 data.dptr = buffer + prev_len;
86 data.dsize = j - prev_len;
87 ok1(ntdb_append(ntdb, key, data) == 0);
88 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
89 ok1(ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS);
91 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
93 prev_len = data.dsize;
94 newoff = ntdb_offset(ntdb, key);
99 ok1(!ntdb->file || (ntdb->file->allrecord_lock.count == 0
100 && ntdb->file->num_lockrecs == 0));
101 /* We should increase by 50% each time... */
102 ok(moves <= ilog64(j / SIZE_STEP)*2,
103 "Moved %u times", moves);
107 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
108 ntdb = ntdb_open("run-append.ntdb", flags[i]|MAYBE_NOSYNC,
109 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
114 /* Huge initial store. */
116 data.dsize = MAX_SIZE;
117 ok1(ntdb_append(ntdb, key, data) == 0);
118 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
119 ok1(ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS);
120 ok1(data.dsize == MAX_SIZE);
121 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
123 ok1(!ntdb->file || (ntdb->file->allrecord_lock.count == 0
124 && ntdb->file->num_lockrecs == 0));
128 ok1(tap_log_messages == 0);
130 return exit_status();