s3:net_util: add some const to sockaddr_storage
[Samba/gebeck_regimport.git] / lib / tdb2 / test / run-15-append.c
blobd2f9ec65989157a105af1c9d3197cd51bbdeae7e
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"
13 #define MAX_SIZE 13100
14 #define SIZE_STEP 131
16 static tdb_off_t tdb_offset(struct tdb_context *tdb, struct tdb_data key)
18 tdb_off_t off;
19 struct tdb_used_record rec;
20 struct hash_info h;
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;
29 int main(int argc, char *argv[])
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 = tdb_mkdata("key", 3);
39 struct tdb_data data;
41 buffer = malloc(MAX_SIZE);
42 for (i = 0; i < MAX_SIZE; i++)
43 buffer[i] = i;
45 plan_tests(sizeof(flags) / sizeof(flags[0])
46 * ((3 + MAX_SIZE/SIZE_STEP * 5) * 2 + 7)
47 + 1);
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;
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;
72 ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
73 && tdb->file->num_lockrecs == 0));
74 /* We should increase by 50% each time... */
75 ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
76 tdb_close(tdb);
79 /* Using tdb_append. */
80 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
81 size_t prev_len = 0;
82 tdb = tdb_open("run-append.tdb", flags[i],
83 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
84 ok1(tdb);
85 if (!tdb)
86 continue;
88 moves = 0;
89 for (j = 0; j < MAX_SIZE; j += SIZE_STEP) {
90 data.dptr = buffer + prev_len;
91 data.dsize = j - prev_len;
92 ok1(tdb_append(tdb, key, data) == 0);
93 ok1(tdb_check(tdb, NULL, NULL) == 0);
94 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
95 ok1(data.dsize == j);
96 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
97 free(data.dptr);
98 prev_len = data.dsize;
99 newoff = tdb_offset(tdb, key);
100 if (newoff != oldoff)
101 moves++;
102 oldoff = newoff;
104 ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
105 && tdb->file->num_lockrecs == 0));
106 /* We should increase by 50% each time... */
107 ok(moves <= ilog64(j / SIZE_STEP)*2, "Moved %u times", moves);
108 tdb_close(tdb);
111 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
112 tdb = tdb_open("run-append.tdb", flags[i],
113 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
114 ok1(tdb);
115 if (!tdb)
116 continue;
118 /* Huge initial store. */
119 data.dptr = buffer;
120 data.dsize = MAX_SIZE;
121 ok1(tdb_append(tdb, key, data) == 0);
122 ok1(tdb_check(tdb, NULL, NULL) == 0);
123 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
124 ok1(data.dsize == MAX_SIZE);
125 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
126 free(data.dptr);
127 ok1(!tdb->file || (tdb->file->allrecord_lock.count == 0
128 && tdb->file->num_lockrecs == 0));
129 tdb_close(tdb);
132 ok1(tap_log_messages == 0);
133 free(buffer);
134 return exit_status();