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>
13 #define MAX_SIZE 13100
16 static tdb_off_t
tdb_offset(struct tdb_context
*tdb
, struct tdb_data key
)
19 struct tdb_used_record rec
;
22 off
= find_and_lock(tdb
, key
, F_RDLCK
, &h
, &rec
, NULL
);
23 if (TDB_OFF_IS_ERR(off
))
25 tdb_unlock_hashes(tdb
, h
.hlock_start
, h
.hlock_range
, F_RDLCK
);
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);
41 buffer
= malloc(MAX_SIZE
);
42 for (i
= 0; i
< MAX_SIZE
; i
++)
45 plan_tests(sizeof(flags
) / sizeof(flags
[0])
46 * ((3 + MAX_SIZE
/SIZE_STEP
* 5) * 2 + 7)
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
);
58 for (j
= 0; j
< MAX_SIZE
; j
+= SIZE_STEP
) {
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
);
65 ok1(memcmp(data
.dptr
, buffer
, data
.dsize
) == 0);
67 newoff
= tdb_offset(tdb
, key
);
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
);
79 /* Using tdb_append. */
80 for (i
= 0; i
< sizeof(flags
) / sizeof(flags
[0]); i
++) {
82 tdb
= tdb_open("run-append.tdb", flags
[i
],
83 O_RDWR
|O_CREAT
|O_TRUNC
, 0600, &tap_log_attr
);
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
);
96 ok1(memcmp(data
.dptr
, buffer
, data
.dsize
) == 0);
98 prev_len
= data
.dsize
;
99 newoff
= tdb_offset(tdb
, key
);
100 if (newoff
!= oldoff
)
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
);
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
);
118 /* Huge initial store. */
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);
127 ok1(!tdb
->file
|| (tdb
->file
->allrecord_lock
.count
== 0
128 && tdb
->file
->num_lockrecs
== 0));
132 ok1(tap_log_messages
== 0);
134 return exit_status();