]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-15-append.c
Merge branch 'tdb2'
[ccan] / ccan / tdb2 / test / run-15-append.c
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/tdb2/transaction.c>
8 #include <ccan/tap/tap.h>
9 #include <ccan/ilog/ilog.h>
10 #include "logging.h"
11
12 #define MAX_SIZE 13100
13 #define SIZE_STEP 131
14
15 static tdb_off_t tdb_offset(struct tdb_context *tdb, struct tdb_data key)
16 {
17         tdb_off_t off;
18         struct tdb_used_record rec;
19         struct hash_info h;
20
21         off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
22         if (unlikely(off == TDB_OFF_ERR))
23                 return 0;
24         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
25         return off;
26 }
27
28 int main(int argc, char *argv[])
29 {
30         unsigned int i, j, moves;
31         struct tdb_context *tdb;
32         unsigned char *buffer;
33         tdb_off_t oldoff = 0, newoff;
34         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
35                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
36                         TDB_NOMMAP|TDB_CONVERT };
37         struct tdb_data key = { (unsigned char *)"key", 3 };
38         struct tdb_data data;
39
40         buffer = malloc(MAX_SIZE);
41         for (i = 0; i < MAX_SIZE; i++)
42                 buffer[i] = i;
43
44         plan_tests(sizeof(flags) / sizeof(flags[0])
45                    * ((3 + MAX_SIZE/SIZE_STEP * 4) * 2 + 6)
46                    + 1);
47
48         /* Using tdb_store. */
49         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
50                 tdb = tdb_open("run-append.tdb", flags[i],
51                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
52                 ok1(tdb);
53                 if (!tdb)
54                         continue;
55
56                 moves = 0;
57                 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
58                         data.dptr = buffer;
59                         data.dsize = j;
60                         ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
61                         ok1(tdb_check(tdb, NULL, NULL) == 0);
62                         data = tdb_fetch(tdb, key);
63                         ok1(data.dsize == j);
64                         ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
65                         free(data.dptr);
66                         newoff = tdb_offset(tdb, key);
67                         if (newoff != oldoff)
68                                 moves++;
69                         oldoff = newoff;
70                 }
71                 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
72                 /* We should increase by 50% each time... */
73                 ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
74                 tdb_close(tdb);
75         }
76
77         /* Using tdb_append. */
78         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
79                 size_t prev_len = 0;
80                 tdb = tdb_open("run-append.tdb", flags[i],
81                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
82                 ok1(tdb);
83                 if (!tdb)
84                         continue;
85
86                 moves = 0;
87                 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
88                         data.dptr = buffer + prev_len;
89                         data.dsize = j - prev_len;
90                         ok1(tdb_append(tdb, key, data) == 0);
91                         ok1(tdb_check(tdb, NULL, NULL) == 0);
92                         data = tdb_fetch(tdb, key);
93                         ok1(data.dsize == j);
94                         ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
95                         free(data.dptr);
96                         prev_len = data.dsize;
97                         newoff = tdb_offset(tdb, key);
98                         if (newoff != oldoff)
99                                 moves++;
100                         oldoff = newoff;
101                 }
102                 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
103                 /* We should increase by 50% each time... */
104                 ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
105                 tdb_close(tdb);
106         }
107
108         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
109                 tdb = tdb_open("run-append.tdb", flags[i],
110                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
111                 ok1(tdb);
112                 if (!tdb)
113                         continue;
114
115                 /* Huge initial store. */
116                 data.dptr = buffer;
117                 data.dsize = MAX_SIZE;
118                 ok1(tdb_append(tdb, key, data) == 0);
119                 ok1(tdb_check(tdb, NULL, NULL) == 0);
120                 data = tdb_fetch(tdb, key);
121                 ok1(data.dsize == MAX_SIZE);
122                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
123                 free(data.dptr);
124                 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
125                 tdb_close(tdb);
126         }
127
128         ok1(tap_log_messages == 0);
129         return exit_status();
130 }