Line data Source code
1 : #include "../common/tdb_private.h"
2 : #include "../common/io.c"
3 : #include "../common/tdb.c"
4 : #include "../common/lock.c"
5 : #include "../common/freelist.c"
6 : #include "../common/traverse.c"
7 : #include "../common/transaction.c"
8 : #include "../common/error.c"
9 : #include "../common/open.c"
10 : #include "../common/check.c"
11 : #include "../common/hash.c"
12 : #include "../common/mutex.c"
13 : #include "tap-interface.h"
14 : #include <stdlib.h>
15 : #include "logging.h"
16 :
17 : static char keystr0[] = "x";
18 : static TDB_DATA key0 = { .dptr = (uint8_t *)keystr0,
19 : .dsize = sizeof(keystr0) };
20 : static char valuestr0[] = "y";
21 : static TDB_DATA value0 = { .dptr = (uint8_t *)valuestr0,
22 : .dsize = sizeof(valuestr0) };
23 :
24 : static char keystr1[] = "aaa";
25 : static TDB_DATA key1 = { .dptr = (uint8_t *)keystr1,
26 : .dsize = sizeof(keystr1) };
27 : static char valuestr1[] = "bbbbb";
28 : static TDB_DATA value1 = { .dptr = (uint8_t *)valuestr1,
29 : .dsize = sizeof(valuestr1) };
30 :
31 : static TDB_DATA *keys[] = { &key0, &key1 };
32 : static TDB_DATA *values[] = { &value0, &value1 };
33 :
34 4 : static bool tdb_data_same(TDB_DATA d1, TDB_DATA d2)
35 : {
36 4 : if (d1.dsize != d2.dsize) {
37 0 : return false;
38 : }
39 4 : return (memcmp(d1.dptr, d2.dptr, d1.dsize) == 0);
40 : }
41 :
42 : struct traverse_chain_state {
43 : size_t idx;
44 : bool ok;
45 : };
46 :
47 2 : static int traverse_chain_fn(struct tdb_context *tdb,
48 : TDB_DATA key,
49 : TDB_DATA data,
50 : void *private_data)
51 : {
52 2 : struct traverse_chain_state *state = private_data;
53 :
54 2 : state->ok &= tdb_data_same(key, *keys[state->idx]);
55 2 : state->ok &= tdb_data_same(data, *values[state->idx]);
56 2 : state->idx += 1;
57 :
58 2 : return 0;
59 : }
60 :
61 1 : int main(int argc, char *argv[])
62 : {
63 : struct tdb_context *tdb;
64 1 : struct traverse_chain_state state = { .ok = true };
65 : int ret;
66 :
67 : plan_tests(4);
68 :
69 1 : tdb = tdb_open_ex(
70 : "traverse_chain.tdb",
71 : 1,
72 : TDB_CLEAR_IF_FIRST,
73 : O_RDWR|O_CREAT,
74 : 0600,
75 : &taplogctx,
76 : NULL);
77 1 : ok1(tdb);
78 :
79 : /* add in reverse order, tdb_store adds to the front of the list */
80 1 : ret = tdb_store(tdb, key1, value1, TDB_INSERT);
81 1 : ok1(ret == 0);
82 1 : ret = tdb_store(tdb, key0, value0, TDB_INSERT);
83 1 : ok1(ret == 0);
84 :
85 1 : ret = tdb_traverse_key_chain(tdb, key0, traverse_chain_fn, &state);
86 1 : ok1(ret == 2);
87 1 : ok1(state.ok);
88 :
89 1 : unlink(tdb_name(tdb));
90 :
91 1 : tdb_close(tdb);
92 :
93 1 : return exit_status();
94 : }
|