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