10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

387 lines
11 KiB

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