]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/test/run-15-append.c
ttxml: removed cruft from tests
[ccan] / ccan / tdb2 / test / run-15-append.c
1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include <ccan/ilog/ilog.h>
4 #include "logging.h"
5
6 #define MAX_SIZE 13100
7 #define SIZE_STEP 131
8
9 static tdb_off_t tdb_offset(struct tdb_context *tdb, struct tdb_data key)
10 {
11         tdb_off_t off;
12         struct tdb_used_record rec;
13         struct hash_info h;
14
15         if (tdb_get_flags(tdb) & TDB_VERSION1) {
16                 struct tdb1_record rec;
17                 return tdb1_find(tdb, key, tdb_hash(tdb, key.dptr, key.dsize),
18                                  &rec);
19         }
20
21         off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
22         if (TDB_OFF_IS_ERR(off))
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                         TDB_INTERNAL|TDB_VERSION1, TDB_VERSION1,
38                         TDB_NOMMAP|TDB_VERSION1,
39                         TDB_INTERNAL|TDB_CONVERT|TDB_VERSION1,
40                         TDB_CONVERT|TDB_VERSION1,
41                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
42         struct tdb_data key = tdb_mkdata("key", 3);
43         struct tdb_data data;
44
45         buffer = malloc(MAX_SIZE);
46         for (i = 0; i < MAX_SIZE; i++)
47                 buffer[i] = i;
48
49         plan_tests(sizeof(flags) / sizeof(flags[0])
50                    * ((3 + MAX_SIZE/SIZE_STEP * 5) * 2 + 7)
51                    + 1);
52
53         /* Using tdb_store. */
54         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
55                 tdb = tdb_open("run-append.tdb", flags[i],
56                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
57                 ok1(tdb);
58                 if (!tdb)
59                         continue;
60
61                 moves = 0;
62                 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
63                         data.dptr = buffer;
64                         data.dsize = j;
65                         ok1(tdb_store(tdb, key, data, TDB_REPLACE) == 0);
66                         ok1(tdb_check(tdb, NULL, NULL) == 0);
67                         ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
68                         ok1(data.dsize == j);
69                         ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
70                         free(data.dptr);
71                         newoff = tdb_offset(tdb, key);
72                         if (newoff != oldoff)
73                                 moves++;
74                         oldoff = newoff;
75                 }
76                 ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
77                                    && tdb->file->num_lockrecs == 0));
78                 if (flags[i] & TDB_VERSION1) {
79                         /* TDB1 simply over-size by 25%. */
80                         ok(moves <= ilog64(j / SIZE_STEP)*4,
81                            "Moved %u times", moves);
82                 } else {
83                         /* We should increase by 50% each time... */
84                         ok(moves <= ilog64(j / SIZE_STEP)*2,
85                            "Moved %u times", moves);
86                 }
87                 tdb_close(tdb);
88         }
89
90         /* Using tdb_append. */
91         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
92                 size_t prev_len = 0;
93                 tdb = tdb_open("run-append.tdb", flags[i],
94                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
95                 ok1(tdb);
96                 if (!tdb)
97                         continue;
98
99                 moves = 0;
100                 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
101                         data.dptr = buffer + prev_len;
102                         data.dsize = j - prev_len;
103                         ok1(tdb_append(tdb, key, data) == 0);
104                         ok1(tdb_check(tdb, NULL, NULL) == 0);
105                         ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
106                         ok1(data.dsize == j);
107                         ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
108                         free(data.dptr);
109                         prev_len = data.dsize;
110                         newoff = tdb_offset(tdb, key);
111                         if (newoff != oldoff)
112                                 moves++;
113                         oldoff = newoff;
114                 }
115                 ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
116                                    && tdb->file->num_lockrecs == 0));
117                 if (flags[i] & TDB_VERSION1) {
118                         /* TDB1 simply over-size by 25%. */
119                         ok(moves <= ilog64(j / SIZE_STEP)*4,
120                            "Moved %u times", moves);
121                 } else {
122                         /* We should increase by 50% each time... */
123                         ok(moves <= ilog64(j / SIZE_STEP)*2,
124                            "Moved %u times", moves);
125                 }
126                 tdb_close(tdb);
127         }
128
129         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
130                 tdb = tdb_open("run-append.tdb", flags[i],
131                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
132                 ok1(tdb);
133                 if (!tdb)
134                         continue;
135
136                 /* Huge initial store. */
137                 data.dptr = buffer;
138                 data.dsize = MAX_SIZE;
139                 ok1(tdb_append(tdb, key, data) == 0);
140                 ok1(tdb_check(tdb, NULL, NULL) == 0);
141                 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
142                 ok1(data.dsize == MAX_SIZE);
143                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
144                 free(data.dptr);
145                 ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
146                                    && tdb->file->num_lockrecs == 0));
147                 tdb_close(tdb);
148         }
149
150         ok1(tap_log_messages == 0);
151         free(buffer);
152         return exit_status();
153 }