1 #include "ntdb-source.h"
2 #include "tap-interface.h"
3 #include <ccan/ilog/ilog.h>
9 static ntdb_off_t
ntdb_offset(struct ntdb_context
*ntdb
, NTDB_DATA key
)
12 struct ntdb_used_record urec
;
15 off
= find_and_lock(ntdb
, key
, F_RDLCK
, &h
, &urec
, NULL
);
16 if (NTDB_OFF_IS_ERR(off
))
18 ntdb_unlock_hash(ntdb
, h
.h
, F_RDLCK
);
22 int main(int argc
, char *argv
[])
24 unsigned int i
, j
, moves
;
25 struct ntdb_context
*ntdb
;
26 unsigned char *buffer
;
27 ntdb_off_t oldoff
= 0, newoff
;
28 int flags
[] = { NTDB_INTERNAL
, NTDB_DEFAULT
, NTDB_NOMMAP
,
29 NTDB_INTERNAL
|NTDB_CONVERT
, NTDB_CONVERT
,
30 NTDB_NOMMAP
|NTDB_CONVERT
};
31 NTDB_DATA key
= ntdb_mkdata("key", 3);
34 buffer
= malloc(MAX_SIZE
);
35 for (i
= 0; i
< MAX_SIZE
; i
++)
38 plan_tests(sizeof(flags
) / sizeof(flags
[0])
39 * ((3 + MAX_SIZE
/SIZE_STEP
* 5) * 2 + 7)
42 /* Using ntdb_store. */
43 for (i
= 0; i
< sizeof(flags
) / sizeof(flags
[0]); i
++) {
44 ntdb
= ntdb_open("run-append.ntdb", flags
[i
]|MAYBE_NOSYNC
,
45 O_RDWR
|O_CREAT
|O_TRUNC
, 0600, &tap_log_attr
);
51 for (j
= 0; j
< MAX_SIZE
; j
+= SIZE_STEP
) {
54 ok1(ntdb_store(ntdb
, key
, data
, NTDB_REPLACE
) == 0);
55 ok1(ntdb_check(ntdb
, NULL
, NULL
) == 0);
56 ok1(ntdb_fetch(ntdb
, key
, &data
) == NTDB_SUCCESS
);
58 ok1(memcmp(data
.dptr
, buffer
, data
.dsize
) == 0);
60 newoff
= ntdb_offset(ntdb
, key
);
65 ok1(!ntdb
->file
|| (ntdb
->file
->allrecord_lock
.count
== 0
66 && ntdb
->file
->num_lockrecs
== 0));
67 /* We should increase by 50% each time... */
68 ok(moves
<= ilog64(j
/ SIZE_STEP
)*2,
69 "Moved %u times", moves
);
73 /* Using ntdb_append. */
74 for (i
= 0; i
< sizeof(flags
) / sizeof(flags
[0]); i
++) {
76 ntdb
= ntdb_open("run-append.ntdb", flags
[i
]|MAYBE_NOSYNC
,
77 O_RDWR
|O_CREAT
|O_TRUNC
, 0600, &tap_log_attr
);
83 for (j
= 0; j
< MAX_SIZE
; j
+= SIZE_STEP
) {
84 data
.dptr
= buffer
+ prev_len
;
85 data
.dsize
= j
- prev_len
;
86 ok1(ntdb_append(ntdb
, key
, data
) == 0);
87 ok1(ntdb_check(ntdb
, NULL
, NULL
) == 0);
88 ok1(ntdb_fetch(ntdb
, key
, &data
) == NTDB_SUCCESS
);
90 ok1(memcmp(data
.dptr
, buffer
, data
.dsize
) == 0);
92 prev_len
= data
.dsize
;
93 newoff
= ntdb_offset(ntdb
, key
);
98 ok1(!ntdb
->file
|| (ntdb
->file
->allrecord_lock
.count
== 0
99 && ntdb
->file
->num_lockrecs
== 0));
100 /* We should increase by 50% each time... */
101 ok(moves
<= ilog64(j
/ SIZE_STEP
)*2,
102 "Moved %u times", moves
);
106 for (i
= 0; i
< sizeof(flags
) / sizeof(flags
[0]); i
++) {
107 ntdb
= ntdb_open("run-append.ntdb", flags
[i
]|MAYBE_NOSYNC
,
108 O_RDWR
|O_CREAT
|O_TRUNC
, 0600, &tap_log_attr
);
113 /* Huge initial store. */
115 data
.dsize
= MAX_SIZE
;
116 ok1(ntdb_append(ntdb
, key
, data
) == 0);
117 ok1(ntdb_check(ntdb
, NULL
, NULL
) == 0);
118 ok1(ntdb_fetch(ntdb
, key
, &data
) == NTDB_SUCCESS
);
119 ok1(data
.dsize
== MAX_SIZE
);
120 ok1(memcmp(data
.dptr
, buffer
, data
.dsize
) == 0);
122 ok1(!ntdb
->file
|| (ntdb
->file
->allrecord_lock
.count
== 0
123 && ntdb
->file
->num_lockrecs
== 0));
127 ok1(tap_log_messages
== 0);
129 return exit_status();