c_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. Use of this source code is governed by a BSD-style license that can be
  3. found in the LICENSE file. See the AUTHORS file for names of contributors. */
  4. #include "leveldb/c.h"
  5. #include <stddef.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. const char* phase = "";
  10. static void StartPhase(const char* name) {
  11. fprintf(stderr, "=== Test %s\n", name);
  12. phase = name;
  13. }
  14. #define CheckNoError(err) \
  15. if ((err) != NULL) { \
  16. fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__, phase, (err)); \
  17. abort(); \
  18. }
  19. #define CheckCondition(cond) \
  20. if (!(cond)) { \
  21. fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__, phase, #cond); \
  22. abort(); \
  23. }
  24. static void CheckEqual(const char* expected, const char* v, size_t n) {
  25. if (expected == NULL && v == NULL) {
  26. // ok
  27. } else if (expected != NULL && v != NULL && n == strlen(expected) &&
  28. memcmp(expected, v, n) == 0) {
  29. // ok
  30. return;
  31. } else {
  32. fprintf(stderr, "%s: expected '%s', got '%s'\n",
  33. phase,
  34. (expected ? expected : "(null)"),
  35. (v ? v : "(null"));
  36. abort();
  37. }
  38. }
  39. static void Free(char** ptr) {
  40. if (*ptr) {
  41. free(*ptr);
  42. *ptr = NULL;
  43. }
  44. }
  45. static void CheckGet(
  46. leveldb_t* db,
  47. const leveldb_readoptions_t* options,
  48. const char* key,
  49. const char* expected) {
  50. char* err = NULL;
  51. size_t val_len;
  52. char* val;
  53. val = leveldb_get(db, options, key, strlen(key), &val_len, &err);
  54. CheckNoError(err);
  55. CheckEqual(expected, val, val_len);
  56. Free(&val);
  57. }
  58. static void CheckIter(leveldb_iterator_t* iter,
  59. const char* key, const char* val) {
  60. size_t len;
  61. const char* str;
  62. str = leveldb_iter_key(iter, &len);
  63. CheckEqual(key, str, len);
  64. str = leveldb_iter_value(iter, &len);
  65. CheckEqual(val, str, len);
  66. }
  67. // Callback from leveldb_writebatch_iterate()
  68. static void CheckPut(void* ptr,
  69. const char* k, size_t klen,
  70. const char* v, size_t vlen) {
  71. int* state = (int*) ptr;
  72. CheckCondition(*state < 2);
  73. switch (*state) {
  74. case 0:
  75. CheckEqual("bar", k, klen);
  76. CheckEqual("b", v, vlen);
  77. break;
  78. case 1:
  79. CheckEqual("box", k, klen);
  80. CheckEqual("c", v, vlen);
  81. break;
  82. }
  83. (*state)++;
  84. }
  85. // Callback from leveldb_writebatch_iterate()
  86. static void CheckDel(void* ptr, const char* k, size_t klen) {
  87. int* state = (int*) ptr;
  88. CheckCondition(*state == 2);
  89. CheckEqual("bar", k, klen);
  90. (*state)++;
  91. }
  92. static void CmpDestroy(void* arg) { }
  93. static int CmpCompare(void* arg, const char* a, size_t alen,
  94. const char* b, size_t blen) {
  95. int n = (alen < blen) ? alen : blen;
  96. int r = memcmp(a, b, n);
  97. if (r == 0) {
  98. if (alen < blen) r = -1;
  99. else if (alen > blen) r = +1;
  100. }
  101. return r;
  102. }
  103. static const char* CmpName(void* arg) {
  104. return "foo";
  105. }
  106. // Custom filter policy
  107. static uint8_t fake_filter_result = 1;
  108. static void FilterDestroy(void* arg) { }
  109. static const char* FilterName(void* arg) {
  110. return "TestFilter";
  111. }
  112. static char* FilterCreate(
  113. void* arg,
  114. const char* const* key_array, const size_t* key_length_array,
  115. int num_keys,
  116. size_t* filter_length) {
  117. *filter_length = 4;
  118. char* result = malloc(4);
  119. memcpy(result, "fake", 4);
  120. return result;
  121. }
  122. uint8_t FilterKeyMatch(void* arg, const char* key, size_t length,
  123. const char* filter, size_t filter_length) {
  124. CheckCondition(filter_length == 4);
  125. CheckCondition(memcmp(filter, "fake", 4) == 0);
  126. return fake_filter_result;
  127. }
  128. int main(int argc, char** argv) {
  129. leveldb_t* db;
  130. leveldb_comparator_t* cmp;
  131. leveldb_cache_t* cache;
  132. leveldb_env_t* env;
  133. leveldb_options_t* options;
  134. leveldb_readoptions_t* roptions;
  135. leveldb_writeoptions_t* woptions;
  136. char* dbname;
  137. char* err = NULL;
  138. int run = -1;
  139. CheckCondition(leveldb_major_version() >= 1);
  140. CheckCondition(leveldb_minor_version() >= 1);
  141. StartPhase("create_objects");
  142. cmp = leveldb_comparator_create(NULL, CmpDestroy, CmpCompare, CmpName);
  143. env = leveldb_create_default_env();
  144. cache = leveldb_cache_create_lru(100000);
  145. dbname = leveldb_env_get_test_directory(env);
  146. CheckCondition(dbname != NULL);
  147. options = leveldb_options_create();
  148. leveldb_options_set_comparator(options, cmp);
  149. leveldb_options_set_error_if_exists(options, 1);
  150. leveldb_options_set_cache(options, cache);
  151. leveldb_options_set_env(options, env);
  152. leveldb_options_set_info_log(options, NULL);
  153. leveldb_options_set_write_buffer_size(options, 100000);
  154. leveldb_options_set_paranoid_checks(options, 1);
  155. leveldb_options_set_max_open_files(options, 10);
  156. leveldb_options_set_block_size(options, 1024);
  157. leveldb_options_set_block_restart_interval(options, 8);
  158. leveldb_options_set_max_file_size(options, 3 << 20);
  159. leveldb_options_set_compression(options, leveldb_no_compression);
  160. roptions = leveldb_readoptions_create();
  161. leveldb_readoptions_set_verify_checksums(roptions, 1);
  162. leveldb_readoptions_set_fill_cache(roptions, 0);
  163. woptions = leveldb_writeoptions_create();
  164. leveldb_writeoptions_set_sync(woptions, 1);
  165. StartPhase("destroy");
  166. leveldb_destroy_db(options, dbname, &err);
  167. Free(&err);
  168. StartPhase("open_error");
  169. db = leveldb_open(options, dbname, &err);
  170. CheckCondition(err != NULL);
  171. Free(&err);
  172. StartPhase("leveldb_free");
  173. db = leveldb_open(options, dbname, &err);
  174. CheckCondition(err != NULL);
  175. leveldb_free(err);
  176. err = NULL;
  177. StartPhase("open");
  178. leveldb_options_set_create_if_missing(options, 1);
  179. db = leveldb_open(options, dbname, &err);
  180. CheckNoError(err);
  181. CheckGet(db, roptions, "foo", NULL);
  182. StartPhase("put");
  183. leveldb_put(db, woptions, "foo", 3, "hello", 5, &err);
  184. CheckNoError(err);
  185. CheckGet(db, roptions, "foo", "hello");
  186. StartPhase("compactall");
  187. leveldb_compact_range(db, NULL, 0, NULL, 0);
  188. CheckGet(db, roptions, "foo", "hello");
  189. StartPhase("compactrange");
  190. leveldb_compact_range(db, "a", 1, "z", 1);
  191. CheckGet(db, roptions, "foo", "hello");
  192. StartPhase("writebatch");
  193. {
  194. leveldb_writebatch_t* wb = leveldb_writebatch_create();
  195. leveldb_writebatch_put(wb, "foo", 3, "a", 1);
  196. leveldb_writebatch_clear(wb);
  197. leveldb_writebatch_put(wb, "bar", 3, "b", 1);
  198. leveldb_writebatch_put(wb, "box", 3, "c", 1);
  199. leveldb_writebatch_t* wb2 = leveldb_writebatch_create();
  200. leveldb_writebatch_delete(wb2, "bar", 3);
  201. leveldb_writebatch_append(wb, wb2);
  202. leveldb_writebatch_destroy(wb2);
  203. leveldb_write(db, woptions, wb, &err);
  204. CheckNoError(err);
  205. CheckGet(db, roptions, "foo", "hello");
  206. CheckGet(db, roptions, "bar", NULL);
  207. CheckGet(db, roptions, "box", "c");
  208. int pos = 0;
  209. leveldb_writebatch_iterate(wb, &pos, CheckPut, CheckDel);
  210. CheckCondition(pos == 3);
  211. leveldb_writebatch_destroy(wb);
  212. }
  213. StartPhase("iter");
  214. {
  215. leveldb_iterator_t* iter = leveldb_create_iterator(db, roptions);
  216. CheckCondition(!leveldb_iter_valid(iter));
  217. leveldb_iter_seek_to_first(iter);
  218. CheckCondition(leveldb_iter_valid(iter));
  219. CheckIter(iter, "box", "c");
  220. leveldb_iter_next(iter);
  221. CheckIter(iter, "foo", "hello");
  222. leveldb_iter_prev(iter);
  223. CheckIter(iter, "box", "c");
  224. leveldb_iter_prev(iter);
  225. CheckCondition(!leveldb_iter_valid(iter));
  226. leveldb_iter_seek_to_last(iter);
  227. CheckIter(iter, "foo", "hello");
  228. leveldb_iter_seek(iter, "b", 1);
  229. CheckIter(iter, "box", "c");
  230. leveldb_iter_get_error(iter, &err);
  231. CheckNoError(err);
  232. leveldb_iter_destroy(iter);
  233. }
  234. StartPhase("approximate_sizes");
  235. {
  236. int i;
  237. int n = 20000;
  238. char keybuf[100];
  239. char valbuf[100];
  240. uint64_t sizes[2];
  241. const char* start[2] = { "a", "k00000000000000010000" };
  242. size_t start_len[2] = { 1, 21 };
  243. const char* limit[2] = { "k00000000000000010000", "z" };
  244. size_t limit_len[2] = { 21, 1 };
  245. leveldb_writeoptions_set_sync(woptions, 0);
  246. for (i = 0; i < n; i++) {
  247. snprintf(keybuf, sizeof(keybuf), "k%020d", i);
  248. snprintf(valbuf, sizeof(valbuf), "v%020d", i);
  249. leveldb_put(db, woptions, keybuf, strlen(keybuf), valbuf, strlen(valbuf),
  250. &err);
  251. CheckNoError(err);
  252. }
  253. leveldb_approximate_sizes(db, 2, start, start_len, limit, limit_len, sizes);
  254. CheckCondition(sizes[0] > 0);
  255. CheckCondition(sizes[1] > 0);
  256. }
  257. StartPhase("property");
  258. {
  259. char* prop = leveldb_property_value(db, "nosuchprop");
  260. CheckCondition(prop == NULL);
  261. prop = leveldb_property_value(db, "leveldb.stats");
  262. CheckCondition(prop != NULL);
  263. Free(&prop);
  264. }
  265. StartPhase("snapshot");
  266. {
  267. const leveldb_snapshot_t* snap;
  268. snap = leveldb_create_snapshot(db);
  269. leveldb_delete(db, woptions, "foo", 3, &err);
  270. CheckNoError(err);
  271. leveldb_readoptions_set_snapshot(roptions, snap);
  272. CheckGet(db, roptions, "foo", "hello");
  273. leveldb_readoptions_set_snapshot(roptions, NULL);
  274. CheckGet(db, roptions, "foo", NULL);
  275. leveldb_release_snapshot(db, snap);
  276. }
  277. StartPhase("repair");
  278. {
  279. leveldb_close(db);
  280. leveldb_options_set_create_if_missing(options, 0);
  281. leveldb_options_set_error_if_exists(options, 0);
  282. leveldb_repair_db(options, dbname, &err);
  283. CheckNoError(err);
  284. db = leveldb_open(options, dbname, &err);
  285. CheckNoError(err);
  286. CheckGet(db, roptions, "foo", NULL);
  287. CheckGet(db, roptions, "bar", NULL);
  288. CheckGet(db, roptions, "box", "c");
  289. leveldb_options_set_create_if_missing(options, 1);
  290. leveldb_options_set_error_if_exists(options, 1);
  291. }
  292. StartPhase("filter");
  293. for (run = 0; run < 2; run++) {
  294. // First run uses custom filter, second run uses bloom filter
  295. CheckNoError(err);
  296. leveldb_filterpolicy_t* policy;
  297. if (run == 0) {
  298. policy = leveldb_filterpolicy_create(
  299. NULL, FilterDestroy, FilterCreate, FilterKeyMatch, FilterName);
  300. } else {
  301. policy = leveldb_filterpolicy_create_bloom(10);
  302. }
  303. // Create new database
  304. leveldb_close(db);
  305. leveldb_destroy_db(options, dbname, &err);
  306. leveldb_options_set_filter_policy(options, policy);
  307. db = leveldb_open(options, dbname, &err);
  308. CheckNoError(err);
  309. leveldb_put(db, woptions, "foo", 3, "foovalue", 8, &err);
  310. CheckNoError(err);
  311. leveldb_put(db, woptions, "bar", 3, "barvalue", 8, &err);
  312. CheckNoError(err);
  313. leveldb_compact_range(db, NULL, 0, NULL, 0);
  314. fake_filter_result = 1;
  315. CheckGet(db, roptions, "foo", "foovalue");
  316. CheckGet(db, roptions, "bar", "barvalue");
  317. if (phase == 0) {
  318. // Must not find value when custom filter returns false
  319. fake_filter_result = 0;
  320. CheckGet(db, roptions, "foo", NULL);
  321. CheckGet(db, roptions, "bar", NULL);
  322. fake_filter_result = 1;
  323. CheckGet(db, roptions, "foo", "foovalue");
  324. CheckGet(db, roptions, "bar", "barvalue");
  325. }
  326. leveldb_options_set_filter_policy(options, NULL);
  327. leveldb_filterpolicy_destroy(policy);
  328. }
  329. StartPhase("cleanup");
  330. leveldb_close(db);
  331. leveldb_options_destroy(options);
  332. leveldb_readoptions_destroy(roptions);
  333. leveldb_writeoptions_destroy(woptions);
  334. leveldb_free(dbname);
  335. leveldb_cache_destroy(cache);
  336. leveldb_comparator_destroy(cmp);
  337. leveldb_env_destroy(env);
  338. fprintf(stderr, "PASS\n");
  339. return 0;
  340. }