1 #include "private.h" // For NTDB_TOPLEVEL_HASH_BITS
2 #include <ccan/hash/hash.h>
7 #include "tap-interface.h"
10 /* We rig the hash so adjacent-numbered records always clash. */
11 static uint32_t clash(const void *key
, size_t len
, uint32_t seed
, void *priv
)
13 return *((const unsigned int *)key
) / 2;
16 /* We use the same seed which we saw a failure on. */
17 static uint32_t fixedhash(const void *key
, size_t len
, uint32_t seed
, void *p
)
19 return hash64_stable((const unsigned char *)key
, len
,
23 static bool store_records(struct ntdb_context
*ntdb
)
26 NTDB_DATA key
= { (unsigned char *)&i
, sizeof(i
) };
27 NTDB_DATA d
, data
= { (unsigned char *)&i
, sizeof(i
) };
29 for (i
= 0; i
< 1000; i
++) {
30 if (ntdb_store(ntdb
, key
, data
, NTDB_REPLACE
) != 0)
32 ntdb_fetch(ntdb
, key
, &d
);
33 if (!ntdb_deq(d
, data
))
40 static void test_val(struct ntdb_context
*ntdb
, uint64_t val
)
43 NTDB_DATA key
= { (unsigned char *)&v
, sizeof(v
) };
44 NTDB_DATA d
, data
= { (unsigned char *)&v
, sizeof(v
) };
46 /* Insert an entry, then delete it. */
48 /* Delete should fail. */
49 ok1(ntdb_delete(ntdb
, key
) == NTDB_ERR_NOEXIST
);
50 ok1(ntdb_check(ntdb
, NULL
, NULL
) == 0);
52 /* Insert should succeed. */
53 ok1(ntdb_store(ntdb
, key
, data
, NTDB_INSERT
) == 0);
54 ok1(ntdb_check(ntdb
, NULL
, NULL
) == 0);
56 /* Delete should succeed. */
57 ok1(ntdb_delete(ntdb
, key
) == 0);
58 ok1(ntdb_check(ntdb
, NULL
, NULL
) == 0);
60 /* Re-add it, then add collision. */
61 ok1(ntdb_store(ntdb
, key
, data
, NTDB_INSERT
) == 0);
63 ok1(ntdb_store(ntdb
, key
, data
, NTDB_INSERT
) == 0);
64 ok1(ntdb_check(ntdb
, NULL
, NULL
) == 0);
67 ok1(ntdb_fetch(ntdb
, key
, &d
) == NTDB_SUCCESS
);
68 ok1(d
.dsize
== data
.dsize
);
71 ok1(ntdb_fetch(ntdb
, key
, &d
) == NTDB_SUCCESS
);
72 ok1(d
.dsize
== data
.dsize
);
75 /* Delete second one. */
77 ok1(ntdb_delete(ntdb
, key
) == 0);
78 ok1(ntdb_check(ntdb
, NULL
, NULL
) == 0);
81 ok1(ntdb_store(ntdb
, key
, data
, NTDB_INSERT
) == 0);
82 ok1(ntdb_check(ntdb
, NULL
, NULL
) == 0);
84 /* Now, try deleting first one. */
86 ok1(ntdb_delete(ntdb
, key
) == 0);
87 ok1(ntdb_check(ntdb
, NULL
, NULL
) == 0);
89 /* Can still find second? */
91 ok1(ntdb_fetch(ntdb
, key
, &d
) == NTDB_SUCCESS
);
92 ok1(d
.dsize
== data
.dsize
);
95 /* Now, this will be ideally placed. */
97 ok1(ntdb_store(ntdb
, key
, data
, NTDB_INSERT
) == 0);
98 ok1(ntdb_check(ntdb
, NULL
, NULL
) == 0);
100 /* This will collide with both. */
102 ok1(ntdb_store(ntdb
, key
, data
, NTDB_INSERT
) == 0);
104 /* We can still find them all, right? */
105 ok1(ntdb_fetch(ntdb
, key
, &d
) == NTDB_SUCCESS
);
106 ok1(d
.dsize
== data
.dsize
);
109 ok1(ntdb_fetch(ntdb
, key
, &d
) == NTDB_SUCCESS
);
110 ok1(d
.dsize
== data
.dsize
);
113 ok1(ntdb_fetch(ntdb
, key
, &d
) == NTDB_SUCCESS
);
114 ok1(d
.dsize
== data
.dsize
);
117 /* And if we delete val + 1, that val + 2 should not move! */
119 ok1(ntdb_delete(ntdb
, key
) == 0);
120 ok1(ntdb_check(ntdb
, NULL
, NULL
) == 0);
123 ok1(ntdb_fetch(ntdb
, key
, &d
) == NTDB_SUCCESS
);
124 ok1(d
.dsize
== data
.dsize
);
127 ok1(ntdb_fetch(ntdb
, key
, &d
) == NTDB_SUCCESS
);
128 ok1(d
.dsize
== data
.dsize
);
131 /* Delete those two, so we are empty. */
132 ok1(ntdb_delete(ntdb
, key
) == 0);
134 ok1(ntdb_delete(ntdb
, key
) == 0);
136 ok1(ntdb_check(ntdb
, NULL
, NULL
) == 0);
139 int main(int argc
, char *argv
[])
142 struct ntdb_context
*ntdb
;
143 uint64_t seed
= 16014841315512641303ULL;
144 union ntdb_attribute clash_hattr
145 = { .hash
= { .base
= { NTDB_ATTRIBUTE_HASH
},
147 union ntdb_attribute fixed_hattr
148 = { .hash
= { .base
= { NTDB_ATTRIBUTE_HASH
},
151 int flags
[] = { NTDB_INTERNAL
, NTDB_DEFAULT
, NTDB_NOMMAP
,
152 NTDB_INTERNAL
|NTDB_CONVERT
, NTDB_CONVERT
,
153 NTDB_NOMMAP
|NTDB_CONVERT
};
154 /* These two values gave trouble before. */
155 int vals
[] = { 755, 837 };
157 clash_hattr
.base
.next
= &tap_log_attr
;
158 fixed_hattr
.base
.next
= &tap_log_attr
;
160 plan_tests(sizeof(flags
) / sizeof(flags
[0])
161 * (39 * 3 + 5 + sizeof(vals
)/sizeof(vals
[0])*2) + 1);
162 for (i
= 0; i
< sizeof(flags
) / sizeof(flags
[0]); i
++) {
163 ntdb
= ntdb_open("run-13-delete.ntdb", flags
[i
]|MAYBE_NOSYNC
,
164 O_RDWR
|O_CREAT
|O_TRUNC
, 0600, &clash_hattr
);
169 /* Check start of hash table. */
172 /* Check end of hash table. */
173 test_val(ntdb
, -1ULL);
175 /* Check mixed bitpattern. */
176 test_val(ntdb
, 0x123456789ABCDEF0ULL
);
178 ok1(!ntdb
->file
|| (ntdb
->file
->allrecord_lock
.count
== 0
179 && ntdb
->file
->num_lockrecs
== 0));
182 /* Deleting these entries in the db gave problems. */
183 ntdb
= ntdb_open("run-13-delete.ntdb", flags
[i
]|MAYBE_NOSYNC
,
184 O_RDWR
|O_CREAT
|O_TRUNC
, 0600, &fixed_hattr
);
189 ok1(store_records(ntdb
));
190 ok1(ntdb_check(ntdb
, NULL
, NULL
) == 0);
191 for (j
= 0; j
< sizeof(vals
)/sizeof(vals
[0]); j
++) {
194 key
.dptr
= (unsigned char *)&vals
[j
];
195 key
.dsize
= sizeof(vals
[j
]);
196 ok1(ntdb_delete(ntdb
, key
) == 0);
197 ok1(ntdb_check(ntdb
, NULL
, NULL
) == 0);
202 ok1(tap_log_messages
== 0);
203 return exit_status();