作者: 韩晨旭 10225101440 李畅 10225102463
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

380 wiersze
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. 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 unsigned char 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. unsigned char FilterKeyMatch(
  123. void* arg,
  124. const char* key, size_t length,
  125. const char* filter, size_t filter_length) {
  126. CheckCondition(filter_length == 4);
  127. CheckCondition(memcmp(filter, "fake", 4) == 0);
  128. return fake_filter_result;
  129. }
  130. int main(int argc, char** argv) {
  131. leveldb_t* db;
  132. leveldb_comparator_t* cmp;
  133. leveldb_cache_t* cache;
  134. leveldb_env_t* env;
  135. leveldb_options_t* options;
  136. leveldb_readoptions_t* roptions;
  137. leveldb_writeoptions_t* woptions;
  138. char* dbname;
  139. char* err = NULL;
  140. int run = -1;
  141. CheckCondition(leveldb_major_version() >= 1);
  142. CheckCondition(leveldb_minor_version() >= 1);
  143. StartPhase("create_objects");
  144. cmp = leveldb_comparator_create(NULL, CmpDestroy, CmpCompare, CmpName);
  145. env = leveldb_create_default_env();
  146. cache = leveldb_cache_create_lru(100000);
  147. dbname = leveldb_env_get_test_directory(env);
  148. CheckCondition(dbname != NULL);
  149. options = leveldb_options_create();
  150. leveldb_options_set_comparator(options, cmp);
  151. leveldb_options_set_error_if_exists(options, 1);
  152. leveldb_options_set_cache(options, cache);
  153. leveldb_options_set_env(options, env);
  154. leveldb_options_set_info_log(options, NULL);
  155. leveldb_options_set_write_buffer_size(options, 100000);
  156. leveldb_options_set_paranoid_checks(options, 1);
  157. leveldb_options_set_max_open_files(options, 10);
  158. leveldb_options_set_block_size(options, 1024);
  159. leveldb_options_set_block_restart_interval(options, 8);
  160. leveldb_options_set_max_file_size(options, 3 << 20);
  161. leveldb_options_set_compression(options, leveldb_no_compression);
  162. roptions = leveldb_readoptions_create();
  163. leveldb_readoptions_set_verify_checksums(roptions, 1);
  164. leveldb_readoptions_set_fill_cache(roptions, 0);
  165. woptions = leveldb_writeoptions_create();
  166. leveldb_writeoptions_set_sync(woptions, 1);
  167. StartPhase("destroy");
  168. leveldb_destroy_db(options, dbname, &err);
  169. Free(&err);
  170. StartPhase("open_error");
  171. db = leveldb_open(options, dbname, &err);
  172. CheckCondition(err != NULL);
  173. Free(&err);
  174. StartPhase("leveldb_free");
  175. db = leveldb_open(options, dbname, &err);
  176. CheckCondition(err != NULL);
  177. leveldb_free(err);
  178. err = NULL;
  179. StartPhase("open");
  180. leveldb_options_set_create_if_missing(options, 1);
  181. db = leveldb_open(options, dbname, &err);
  182. CheckNoError(err);
  183. CheckGet(db, roptions, "foo", NULL);
  184. StartPhase("put");
  185. leveldb_put(db, woptions, "foo", 3, "hello", 5, &err);
  186. CheckNoError(err);
  187. CheckGet(db, roptions, "foo", "hello");
  188. StartPhase("compactall");
  189. leveldb_compact_range(db, NULL, 0, NULL, 0);
  190. CheckGet(db, roptions, "foo", "hello");
  191. StartPhase("compactrange");
  192. leveldb_compact_range(db, "a", 1, "z", 1);
  193. CheckGet(db, roptions, "foo", "hello");
  194. StartPhase("writebatch");
  195. {
  196. leveldb_writebatch_t* wb = leveldb_writebatch_create();
  197. leveldb_writebatch_put(wb, "foo", 3, "a", 1);
  198. leveldb_writebatch_clear(wb);
  199. leveldb_writebatch_put(wb, "bar", 3, "b", 1);
  200. leveldb_writebatch_put(wb, "box", 3, "c", 1);
  201. leveldb_writebatch_delete(wb, "bar", 3);
  202. leveldb_write(db, woptions, wb, &err);
  203. CheckNoError(err);
  204. CheckGet(db, roptions, "foo", "hello");
  205. CheckGet(db, roptions, "bar", NULL);
  206. CheckGet(db, roptions, "box", "c");
  207. int pos = 0;
  208. leveldb_writebatch_iterate(wb, &pos, CheckPut, CheckDel);
  209. CheckCondition(pos == 3);
  210. leveldb_writebatch_destroy(wb);
  211. }
  212. StartPhase("iter");
  213. {
  214. leveldb_iterator_t* iter = leveldb_create_iterator(db, roptions);
  215. CheckCondition(!leveldb_iter_valid(iter));
  216. leveldb_iter_seek_to_first(iter);
  217. CheckCondition(leveldb_iter_valid(iter));
  218. CheckIter(iter, "box", "c");
  219. leveldb_iter_next(iter);
  220. CheckIter(iter, "foo", "hello");
  221. leveldb_iter_prev(iter);
  222. CheckIter(iter, "box", "c");
  223. leveldb_iter_prev(iter);
  224. CheckCondition(!leveldb_iter_valid(iter));
  225. leveldb_iter_seek_to_last(iter);
  226. CheckIter(iter, "foo", "hello");
  227. leveldb_iter_seek(iter, "b", 1);
  228. CheckIter(iter, "box", "c");
  229. leveldb_iter_get_error(iter, &err);
  230. CheckNoError(err);
  231. leveldb_iter_destroy(iter);
  232. }
  233. StartPhase("approximate_sizes");
  234. {
  235. int i;
  236. int n = 20000;
  237. char keybuf[100];
  238. char valbuf[100];
  239. uint64_t sizes[2];
  240. const char* start[2] = { "a", "k00000000000000010000" };
  241. size_t start_len[2] = { 1, 21 };
  242. const char* limit[2] = { "k00000000000000010000", "z" };
  243. size_t limit_len[2] = { 21, 1 };
  244. leveldb_writeoptions_set_sync(woptions, 0);
  245. for (i = 0; i < n; i++) {
  246. snprintf(keybuf, sizeof(keybuf), "k%020d", i);
  247. snprintf(valbuf, sizeof(valbuf), "v%020d", i);
  248. leveldb_put(db, woptions, keybuf, strlen(keybuf), valbuf, strlen(valbuf),
  249. &err);
  250. CheckNoError(err);
  251. }
  252. leveldb_approximate_sizes(db, 2, start, start_len, limit, limit_len, sizes);
  253. CheckCondition(sizes[0] > 0);
  254. CheckCondition(sizes[1] > 0);
  255. }
  256. StartPhase("property");
  257. {
  258. char* prop = leveldb_property_value(db, "nosuchprop");
  259. CheckCondition(prop == NULL);
  260. prop = leveldb_property_value(db, "leveldb.stats");
  261. CheckCondition(prop != NULL);
  262. Free(&prop);
  263. }
  264. StartPhase("snapshot");
  265. {
  266. const leveldb_snapshot_t* snap;
  267. snap = leveldb_create_snapshot(db);
  268. leveldb_delete(db, woptions, "foo", 3, &err);
  269. CheckNoError(err);
  270. leveldb_readoptions_set_snapshot(roptions, snap);
  271. CheckGet(db, roptions, "foo", "hello");
  272. leveldb_readoptions_set_snapshot(roptions, NULL);
  273. CheckGet(db, roptions, "foo", NULL);
  274. leveldb_release_snapshot(db, snap);
  275. }
  276. StartPhase("repair");
  277. {
  278. leveldb_close(db);
  279. leveldb_options_set_create_if_missing(options, 0);
  280. leveldb_options_set_error_if_exists(options, 0);
  281. leveldb_repair_db(options, dbname, &err);
  282. CheckNoError(err);
  283. db = leveldb_open(options, dbname, &err);
  284. CheckNoError(err);
  285. CheckGet(db, roptions, "foo", NULL);
  286. CheckGet(db, roptions, "bar", NULL);
  287. CheckGet(db, roptions, "box", "c");
  288. leveldb_options_set_create_if_missing(options, 1);
  289. leveldb_options_set_error_if_exists(options, 1);
  290. }
  291. StartPhase("filter");
  292. for (run = 0; run < 2; run++) {
  293. // First run uses custom filter, second run uses bloom filter
  294. CheckNoError(err);
  295. leveldb_filterpolicy_t* policy;
  296. if (run == 0) {
  297. policy = leveldb_filterpolicy_create(
  298. NULL, FilterDestroy, FilterCreate, FilterKeyMatch, FilterName);
  299. } else {
  300. policy = leveldb_filterpolicy_create_bloom(10);
  301. }
  302. // Create new database
  303. leveldb_close(db);
  304. leveldb_destroy_db(options, dbname, &err);
  305. leveldb_options_set_filter_policy(options, policy);
  306. db = leveldb_open(options, dbname, &err);
  307. CheckNoError(err);
  308. leveldb_put(db, woptions, "foo", 3, "foovalue", 8, &err);
  309. CheckNoError(err);
  310. leveldb_put(db, woptions, "bar", 3, "barvalue", 8, &err);
  311. CheckNoError(err);
  312. leveldb_compact_range(db, NULL, 0, NULL, 0);
  313. fake_filter_result = 1;
  314. CheckGet(db, roptions, "foo", "foovalue");
  315. CheckGet(db, roptions, "bar", "barvalue");
  316. if (phase == 0) {
  317. // Must not find value when custom filter returns false
  318. fake_filter_result = 0;
  319. CheckGet(db, roptions, "foo", NULL);
  320. CheckGet(db, roptions, "bar", NULL);
  321. fake_filter_result = 1;
  322. CheckGet(db, roptions, "foo", "foovalue");
  323. CheckGet(db, roptions, "bar", "barvalue");
  324. }
  325. leveldb_options_set_filter_policy(options, NULL);
  326. leveldb_filterpolicy_destroy(policy);
  327. }
  328. StartPhase("cleanup");
  329. leveldb_close(db);
  330. leveldb_options_destroy(options);
  331. leveldb_readoptions_destroy(roptions);
  332. leveldb_writeoptions_destroy(woptions);
  333. leveldb_free(dbname);
  334. leveldb_cache_destroy(cache);
  335. leveldb_comparator_destroy(cmp);
  336. leveldb_env_destroy(env);
  337. fprintf(stderr, "PASS\n");
  338. return 0;
  339. }