ccan/ntdb: demote to junkcode.
[ccan] / junkcode / rusty@rustcorp.com.au-ntdb / test / run-15-append.c
1 #include "ntdb-source.h"
2 #include "tap-interface.h"
3 #include <ccan/ilog/ilog.h>
4 #include "logging.h"
5 #include "helprun-external-agent.h"
6
7 #define MAX_SIZE 13100
8 #define SIZE_STEP 131
9
10 static ntdb_off_t ntdb_offset(struct ntdb_context *ntdb, NTDB_DATA key)
11 {
12         ntdb_off_t off;
13         struct ntdb_used_record urec;
14         struct hash_info h;
15
16         off = find_and_lock(ntdb, key, F_RDLCK, &h, &urec, NULL);
17         if (NTDB_OFF_IS_ERR(off))
18                 return 0;
19         ntdb_unlock_hash(ntdb, h.h, F_RDLCK);
20         return off;
21 }
22
23 int main(int argc, char *argv[])
24 {
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);
33         NTDB_DATA data;
34
35         buffer = malloc(MAX_SIZE);
36         for (i = 0; i < MAX_SIZE; i++)
37                 buffer[i] = i;
38
39         plan_tests(sizeof(flags) / sizeof(flags[0])
40                    * ((3 + MAX_SIZE/SIZE_STEP * 5) * 2 + 7)
41                    + 1);
42
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);
47                 ok1(ntdb);
48                 if (!ntdb)
49                         continue;
50
51                 moves = 0;
52                 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
53                         data.dptr = buffer;
54                         data.dsize = j;
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);
58                         ok1(data.dsize == j);
59                         ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
60                         free(data.dptr);
61                         newoff = ntdb_offset(ntdb, key);
62                         if (newoff != oldoff)
63                                 moves++;
64                         oldoff = newoff;
65                 }
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);
71                 ntdb_close(ntdb);
72         }
73
74         /* Using ntdb_append. */
75         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
76                 size_t prev_len = 0;
77                 ntdb = ntdb_open("run-append.ntdb", flags[i]|MAYBE_NOSYNC,
78                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
79                 ok1(ntdb);
80                 if (!ntdb)
81                         continue;
82
83                 moves = 0;
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);
90                         ok1(data.dsize == j);
91                         ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
92                         free(data.dptr);
93                         prev_len = data.dsize;
94                         newoff = ntdb_offset(ntdb, key);
95                         if (newoff != oldoff)
96                                 moves++;
97                         oldoff = newoff;
98                 }
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);
104                 ntdb_close(ntdb);
105         }
106
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);
110                 ok1(ntdb);
111                 if (!ntdb)
112                         continue;
113
114                 /* Huge initial store. */
115                 data.dptr = buffer;
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);
122                 free(data.dptr);
123                 ok1(!ntdb->file || (ntdb->file->allrecord_lock.count == 0
124                                    && ntdb->file->num_lockrecs == 0));
125                 ntdb_close(ntdb);
126         }
127
128         ok1(tap_log_messages == 0);
129         free(buffer);
130         return exit_status();
131 }