小组成员:10215300402-朱维清 & 10222140408 谷杰
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.

381 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("open");
  182. leveldb_options_set_create_if_missing(options, 1);
  183. db = leveldb_open(options, dbname, &err);
  184. CheckNoError(err);
  185. CheckGet(db, roptions, "foo", NULL);
  186. StartPhase("put");
  187. leveldb_put(db, woptions, "foo", 3, "hello", 5, &err);
  188. CheckNoError(err);
  189. CheckGet(db, roptions, "foo", "hello");
  190. StartPhase("compactall");
  191. leveldb_compact_range(db, NULL, 0, NULL, 0);
  192. CheckGet(db, roptions, "foo", "hello");
  193. StartPhase("compactrange");
  194. leveldb_compact_range(db, "a", 1, "z", 1);
  195. CheckGet(db, roptions, "foo", "hello");
  196. StartPhase("writebatch");
  197. {
  198. leveldb_writebatch_t* wb = leveldb_writebatch_create();
  199. leveldb_writebatch_put(wb, "foo", 3, "a", 1);
  200. leveldb_writebatch_clear(wb);
  201. leveldb_writebatch_put(wb, "bar", 3, "b", 1);
  202. leveldb_writebatch_put(wb, "box", 3, "c", 1);
  203. leveldb_writebatch_delete(wb, "bar", 3);
  204. leveldb_write(db, woptions, wb, &err);
  205. CheckNoError(err);
  206. CheckGet(db, roptions, "foo", "hello");
  207. CheckGet(db, roptions, "bar", NULL);
  208. CheckGet(db, roptions, "box", "c");
  209. int pos = 0;
  210. leveldb_writebatch_iterate(wb, &pos, CheckPut, CheckDel);
  211. CheckCondition(pos == 3);
  212. leveldb_writebatch_destroy(wb);
  213. }
  214. StartPhase("iter");
  215. {
  216. leveldb_iterator_t* iter = leveldb_create_iterator(db, roptions);
  217. CheckCondition(!leveldb_iter_valid(iter));
  218. leveldb_iter_seek_to_first(iter);
  219. CheckCondition(leveldb_iter_valid(iter));
  220. CheckIter(iter, "box", "c");
  221. leveldb_iter_next(iter);
  222. CheckIter(iter, "foo", "hello");
  223. leveldb_iter_prev(iter);
  224. CheckIter(iter, "box", "c");
  225. leveldb_iter_prev(iter);
  226. CheckCondition(!leveldb_iter_valid(iter));
  227. leveldb_iter_seek_to_last(iter);
  228. CheckIter(iter, "foo", "hello");
  229. leveldb_iter_seek(iter, "b", 1);
  230. CheckIter(iter, "box", "c");
  231. leveldb_iter_get_error(iter, &err);
  232. CheckNoError(err);
  233. leveldb_iter_destroy(iter);
  234. }
  235. StartPhase("approximate_sizes");
  236. {
  237. int i;
  238. int n = 20000;
  239. char keybuf[100];
  240. char valbuf[100];
  241. uint64_t sizes[2];
  242. const char* start[2] = { "a", "k00000000000000010000" };
  243. size_t start_len[2] = { 1, 21 };
  244. const char* limit[2] = { "k00000000000000010000", "z" };
  245. size_t limit_len[2] = { 21, 1 };
  246. leveldb_writeoptions_set_sync(woptions, 0);
  247. for (i = 0; i < n; i++) {
  248. snprintf(keybuf, sizeof(keybuf), "k%020d", i);
  249. snprintf(valbuf, sizeof(valbuf), "v%020d", i);
  250. leveldb_put(db, woptions, keybuf, strlen(keybuf), valbuf, strlen(valbuf),
  251. &err);
  252. CheckNoError(err);
  253. }
  254. leveldb_approximate_sizes(db, 2, start, start_len, limit, limit_len, sizes);
  255. CheckCondition(sizes[0] > 0);
  256. CheckCondition(sizes[1] > 0);
  257. }
  258. StartPhase("property");
  259. {
  260. char* prop = leveldb_property_value(db, "nosuchprop");
  261. CheckCondition(prop == NULL);
  262. prop = leveldb_property_value(db, "leveldb.stats");
  263. CheckCondition(prop != NULL);
  264. Free(&prop);
  265. }
  266. StartPhase("snapshot");
  267. {
  268. const leveldb_snapshot_t* snap;
  269. snap = leveldb_create_snapshot(db);
  270. leveldb_delete(db, woptions, "foo", 3, &err);
  271. CheckNoError(err);
  272. leveldb_readoptions_set_snapshot(roptions, snap);
  273. CheckGet(db, roptions, "foo", "hello");
  274. leveldb_readoptions_set_snapshot(roptions, NULL);
  275. CheckGet(db, roptions, "foo", NULL);
  276. leveldb_release_snapshot(db, snap);
  277. }
  278. StartPhase("repair");
  279. {
  280. leveldb_close(db);
  281. leveldb_options_set_create_if_missing(options, 0);
  282. leveldb_options_set_error_if_exists(options, 0);
  283. leveldb_repair_db(options, dbname, &err);
  284. CheckNoError(err);
  285. db = leveldb_open(options, dbname, &err);
  286. CheckNoError(err);
  287. CheckGet(db, roptions, "foo", NULL);
  288. CheckGet(db, roptions, "bar", NULL);
  289. CheckGet(db, roptions, "box", "c");
  290. leveldb_options_set_create_if_missing(options, 1);
  291. leveldb_options_set_error_if_exists(options, 1);
  292. }
  293. StartPhase("filter");
  294. for (run = 0; run < 2; run++) {
  295. // First run uses custom filter, second run uses bloom filter
  296. CheckNoError(err);
  297. leveldb_filterpolicy_t* policy;
  298. if (run == 0) {
  299. policy = leveldb_filterpolicy_create(
  300. NULL, FilterDestroy, FilterCreate, FilterKeyMatch, FilterName);
  301. } else {
  302. policy = leveldb_filterpolicy_create_bloom(10);
  303. }
  304. // Create new database
  305. leveldb_close(db);
  306. leveldb_destroy_db(options, dbname, &err);
  307. leveldb_options_set_filter_policy(options, policy);
  308. db = leveldb_open(options, dbname, &err);
  309. CheckNoError(err);
  310. leveldb_put(db, woptions, "foo", 3, "foovalue", 8, &err);
  311. CheckNoError(err);
  312. leveldb_put(db, woptions, "bar", 3, "barvalue", 8, &err);
  313. CheckNoError(err);
  314. leveldb_compact_range(db, NULL, 0, NULL, 0);
  315. fake_filter_result = 1;
  316. CheckGet(db, roptions, "foo", "foovalue");
  317. CheckGet(db, roptions, "bar", "barvalue");
  318. if (phase == 0) {
  319. // Must not find value when custom filter returns false
  320. fake_filter_result = 0;
  321. CheckGet(db, roptions, "foo", NULL);
  322. CheckGet(db, roptions, "bar", NULL);
  323. fake_filter_result = 1;
  324. CheckGet(db, roptions, "foo", "foovalue");
  325. CheckGet(db, roptions, "bar", "barvalue");
  326. }
  327. leveldb_options_set_filter_policy(options, NULL);
  328. leveldb_filterpolicy_destroy(policy);
  329. }
  330. StartPhase("cleanup");
  331. leveldb_close(db);
  332. leveldb_options_destroy(options);
  333. leveldb_readoptions_destroy(roptions);
  334. leveldb_writeoptions_destroy(woptions);
  335. leveldb_cache_destroy(cache);
  336. leveldb_comparator_destroy(cmp);
  337. leveldb_env_destroy(env);
  338. fprintf(stderr, "PASS\n");
  339. return 0;
  340. }