]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-15-append.c
e1b85b4880fc0bf45427e0368eeab18c52540e94
[ccan] / ccan / tdb2 / test / run-15-append.c
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>
11 #include "logging.h"
12
13 #define MAX_SIZE 13100
14 #define SIZE_STEP 131
15
16 static tdb_off_t tdb_offset(struct tdb_context *tdb, struct tdb_data key)
17 {
18         tdb_off_t off;
19         struct tdb_used_record rec;
20         struct hash_info h;
21
22         off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
23         if (TDB_OFF_IS_ERR(off))
24                 return 0;
25         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
26         return off;
27 }
28
29 int main(int argc, char *argv[])
30 {
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 };
39         struct tdb_data data;
40
41         buffer = malloc(MAX_SIZE);
42         for (i = 0; i < MAX_SIZE; i++)
43                 buffer[i] = i;
44
45         plan_tests(sizeof(flags) / sizeof(flags[0])
46                    * ((3 + MAX_SIZE/SIZE_STEP * 5) * 2 + 7)
47                    + 1);
48
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);
53                 ok1(tdb);
54                 if (!tdb)
55                         continue;
56
57                 moves = 0;
58                 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
59                         data.dptr = buffer;
60                         data.dsize = j;
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);
64                         ok1(data.dsize == j);
65                         ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
66                         free(data.dptr);
67                         newoff = tdb_offset(tdb, key);
68                         if (newoff != oldoff)
69                                 moves++;
70                         oldoff = newoff;
71                 }
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);
75                 tdb_close(tdb);
76         }
77
78         /* Using tdb_append. */
79         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
80                 size_t prev_len = 0;
81                 tdb = tdb_open("run-append.tdb", flags[i],
82                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
83                 ok1(tdb);
84                 if (!tdb)
85                         continue;
86
87                 moves = 0;
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);
94                         ok1(data.dsize == j);
95                         ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
96                         free(data.dptr);
97                         prev_len = data.dsize;
98                         newoff = tdb_offset(tdb, key);
99                         if (newoff != oldoff)
100                                 moves++;
101                         oldoff = newoff;
102                 }
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);
106                 tdb_close(tdb);
107         }
108
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);
112                 ok1(tdb);
113                 if (!tdb)
114                         continue;
115
116                 /* Huge initial store. */
117                 data.dptr = buffer;
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);
124                 free(data.dptr);
125                 ok1(tdb->allrecord_lock.count == 0 && tdb->num_lockrecs == 0);
126                 tdb_close(tdb);
127         }
128
129         ok1(tap_log_messages == 0);
130         free(buffer);
131         return exit_status();
132 }