1 #include "tdb2-source.h"
2 #include <ccan/tap/tap.h>
3 #include <ccan/ilog/ilog.h>
9 static tdb_off_t
tdb_offset(struct tdb_context
*tdb
, struct tdb_data key
)
12 struct tdb_used_record rec
;
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
),
21 off
= find_and_lock(tdb
, key
, F_RDLCK
, &h
, &rec
, NULL
);
22 if (TDB_OFF_IS_ERR(off
))
24 tdb_unlock_hashes(tdb
, h
.hlock_start
, h
.hlock_range
, F_RDLCK
);
28 int main(int argc
, char *argv
[])
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);
45 buffer
= malloc(MAX_SIZE
);
46 for (i
= 0; i
< MAX_SIZE
; i
++)
49 plan_tests(sizeof(flags
) / sizeof(flags
[0])
50 * ((3 + MAX_SIZE
/SIZE_STEP
* 5) * 2 + 7)
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
);
62 for (j
= 0; j
< MAX_SIZE
; j
+= SIZE_STEP
) {
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
);
69 ok1(memcmp(data
.dptr
, buffer
, data
.dsize
) == 0);
71 newoff
= tdb_offset(tdb
, key
);
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
);
83 /* We should increase by 50% each time... */
84 ok(moves
<= ilog64(j
/ SIZE_STEP
)*2,
85 "Moved %u times", moves
);
90 /* Using tdb_append. */
91 for (i
= 0; i
< sizeof(flags
) / sizeof(flags
[0]); i
++) {
93 tdb
= tdb_open("run-append.tdb", flags
[i
],
94 O_RDWR
|O_CREAT
|O_TRUNC
, 0600, &tap_log_attr
);
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);
109 prev_len
= data
.dsize
;
110 newoff
= tdb_offset(tdb
, key
);
111 if (newoff
!= oldoff
)
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
);
122 /* We should increase by 50% each time... */
123 ok(moves
<= ilog64(j
/ SIZE_STEP
)*2,
124 "Moved %u times", moves
);
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
);
136 /* Huge initial store. */
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);
145 ok1(!tdb
->file
|| (tdb
->file
->allrecord_lock
.count
== 0
146 && tdb
->file
->num_lockrecs
== 0));
150 ok1(tap_log_messages
== 0);
152 return exit_status();