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.

1222 lines
36 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 "include/db.h"
  5. #include "db/db_impl.h"
  6. #include "db/filename.h"
  7. #include "db/version_set.h"
  8. #include "db/write_batch_internal.h"
  9. #include "include/env.h"
  10. #include "include/table.h"
  11. #include "util/logging.h"
  12. #include "util/testharness.h"
  13. #include "util/testutil.h"
  14. namespace leveldb {
  15. static std::string RandomString(Random* rnd, int len) {
  16. std::string r;
  17. test::RandomString(rnd, len, &r);
  18. return r;
  19. }
  20. class DBTest {
  21. public:
  22. std::string dbname_;
  23. Env* env_;
  24. DB* db_;
  25. Options last_options_;
  26. DBTest() : env_(Env::Default()) {
  27. dbname_ = test::TmpDir() + "/db_test";
  28. DestroyDB(dbname_, Options());
  29. db_ = NULL;
  30. Reopen();
  31. }
  32. ~DBTest() {
  33. delete db_;
  34. DestroyDB(dbname_, Options());
  35. }
  36. DBImpl* dbfull() {
  37. return reinterpret_cast<DBImpl*>(db_);
  38. }
  39. void Reopen(Options* options = NULL) {
  40. ASSERT_OK(TryReopen(options));
  41. }
  42. void DestroyAndReopen(Options* options = NULL) {
  43. delete db_;
  44. db_ = NULL;
  45. DestroyDB(dbname_, Options());
  46. ASSERT_OK(TryReopen(options));
  47. }
  48. Status TryReopen(Options* options) {
  49. delete db_;
  50. db_ = NULL;
  51. Options opts;
  52. if (options != NULL) {
  53. opts = *options;
  54. } else {
  55. opts.create_if_missing = true;
  56. }
  57. last_options_ = opts;
  58. return DB::Open(opts, dbname_, &db_);
  59. }
  60. Status Put(const std::string& k, const std::string& v) {
  61. WriteOptions options;
  62. options.sync = false;
  63. WriteBatch batch;
  64. batch.Put(k, v);
  65. return db_->Write(options, &batch);
  66. }
  67. Status Delete(const std::string& k) {
  68. WriteOptions options;
  69. options.sync = false;
  70. WriteBatch batch;
  71. batch.Delete(k);
  72. return db_->Write(options, &batch);
  73. }
  74. std::string Get(const std::string& k, const Snapshot* snapshot = NULL) {
  75. ReadOptions options;
  76. options.snapshot = snapshot;
  77. std::string result;
  78. Status s = db_->Get(options, k, &result);
  79. if (s.IsNotFound()) {
  80. result = "NOT_FOUND";
  81. } else if (!s.ok()) {
  82. result = s.ToString();
  83. }
  84. return result;
  85. }
  86. std::string AllEntriesFor(const Slice& user_key) {
  87. Iterator* iter = dbfull()->TEST_NewInternalIterator();
  88. InternalKey target(user_key, kMaxSequenceNumber, kTypeValue);
  89. iter->Seek(target.Encode());
  90. std::string result;
  91. if (!iter->status().ok()) {
  92. result = iter->status().ToString();
  93. } else {
  94. result = "[ ";
  95. bool first = true;
  96. while (iter->Valid()) {
  97. ParsedInternalKey ikey;
  98. if (!ParseInternalKey(iter->key(), &ikey)) {
  99. result += "CORRUPTED";
  100. } else {
  101. if (last_options_.comparator->Compare(
  102. ikey.user_key, user_key) != 0) {
  103. break;
  104. }
  105. if (!first) {
  106. result += ", ";
  107. }
  108. first = false;
  109. switch (ikey.type) {
  110. case kTypeValue:
  111. result += iter->value().ToString();
  112. break;
  113. case kTypeLargeValueRef:
  114. result += "LARGEVALUE(" + EscapeString(iter->value()) + ")";
  115. break;
  116. case kTypeDeletion:
  117. result += "DEL";
  118. break;
  119. }
  120. }
  121. iter->Next();
  122. }
  123. if (!first) {
  124. result += " ";
  125. }
  126. result += "]";
  127. }
  128. delete iter;
  129. return result;
  130. }
  131. int NumTableFilesAtLevel(int level) {
  132. uint64_t val;
  133. ASSERT_TRUE(
  134. db_->GetProperty("leveldb.num-files-at-level" + NumberToString(level),
  135. &val));
  136. return val;
  137. }
  138. uint64_t Size(const Slice& start, const Slice& limit) {
  139. Range r(start, limit);
  140. uint64_t size;
  141. db_->GetApproximateSizes(&r, 1, &size);
  142. return size;
  143. }
  144. std::set<LargeValueRef> LargeValueFiles() const {
  145. // Return the set of large value files that exist in the database
  146. std::vector<std::string> filenames;
  147. env_->GetChildren(dbname_, &filenames); // Ignoring errors on purpose
  148. uint64_t number;
  149. LargeValueRef large_ref;
  150. FileType type;
  151. std::set<LargeValueRef> live;
  152. for (int i = 0; i < filenames.size(); i++) {
  153. if (ParseFileName(filenames[i], &number, &large_ref, &type) &&
  154. type == kLargeValueFile) {
  155. fprintf(stderr, " live: %s\n",
  156. LargeValueRefToFilenameString(large_ref).c_str());
  157. live.insert(large_ref);
  158. }
  159. }
  160. fprintf(stderr, "Found %d live large value files\n", (int)live.size());
  161. return live;
  162. }
  163. void Compact(const Slice& start, const Slice& limit) {
  164. dbfull()->TEST_CompactMemTable();
  165. int max_level_with_files = 1;
  166. for (int level = 1; level < config::kNumLevels; level++) {
  167. uint64_t v;
  168. char name[100];
  169. snprintf(name, sizeof(name), "leveldb.num-files-at-level%d", level);
  170. if (dbfull()->GetProperty(name, &v) && v > 0) {
  171. max_level_with_files = level;
  172. }
  173. }
  174. for (int level = 0; level < max_level_with_files; level++) {
  175. dbfull()->TEST_CompactRange(level, "", "~");
  176. }
  177. }
  178. void DumpFileCounts(const char* label) {
  179. fprintf(stderr, "---\n%s:\n", label);
  180. fprintf(stderr, "maxoverlap: %lld\n",
  181. static_cast<long long>(
  182. dbfull()->TEST_MaxNextLevelOverlappingBytes()));
  183. for (int level = 0; level < config::kNumLevels; level++) {
  184. int num = NumTableFilesAtLevel(level);
  185. if (num > 0) {
  186. fprintf(stderr, " level %3d : %d files\n", level, num);
  187. }
  188. }
  189. }
  190. std::string IterStatus(Iterator* iter) {
  191. std::string result;
  192. if (iter->Valid()) {
  193. result = iter->key().ToString() + "->" + iter->value().ToString();
  194. } else {
  195. result = "(invalid)";
  196. }
  197. return result;
  198. }
  199. };
  200. TEST(DBTest, Empty) {
  201. ASSERT_TRUE(db_ != NULL);
  202. ASSERT_EQ("NOT_FOUND", Get("foo"));
  203. }
  204. TEST(DBTest, ReadWrite) {
  205. ASSERT_OK(Put("foo", "v1"));
  206. ASSERT_EQ("v1", Get("foo"));
  207. ASSERT_OK(Put("bar", "v2"));
  208. ASSERT_OK(Put("foo", "v3"));
  209. ASSERT_EQ("v3", Get("foo"));
  210. ASSERT_EQ("v2", Get("bar"));
  211. }
  212. TEST(DBTest, PutDeleteGet) {
  213. ASSERT_OK(db_->Put(WriteOptions(), "foo", "v1"));
  214. ASSERT_EQ("v1", Get("foo"));
  215. ASSERT_OK(db_->Put(WriteOptions(), "foo", "v2"));
  216. ASSERT_EQ("v2", Get("foo"));
  217. ASSERT_OK(db_->Delete(WriteOptions(), "foo"));
  218. ASSERT_EQ("NOT_FOUND", Get("foo"));
  219. }
  220. TEST(DBTest, IterEmpty) {
  221. Iterator* iter = db_->NewIterator(ReadOptions());
  222. iter->SeekToFirst();
  223. ASSERT_EQ(IterStatus(iter), "(invalid)");
  224. iter->SeekToLast();
  225. ASSERT_EQ(IterStatus(iter), "(invalid)");
  226. iter->Seek("foo");
  227. ASSERT_EQ(IterStatus(iter), "(invalid)");
  228. delete iter;
  229. }
  230. TEST(DBTest, IterSingle) {
  231. ASSERT_OK(Put("a", "va"));
  232. Iterator* iter = db_->NewIterator(ReadOptions());
  233. iter->SeekToFirst();
  234. ASSERT_EQ(IterStatus(iter), "a->va");
  235. iter->Next();
  236. ASSERT_EQ(IterStatus(iter), "(invalid)");
  237. iter->SeekToFirst();
  238. ASSERT_EQ(IterStatus(iter), "a->va");
  239. iter->Prev();
  240. ASSERT_EQ(IterStatus(iter), "(invalid)");
  241. iter->SeekToLast();
  242. ASSERT_EQ(IterStatus(iter), "a->va");
  243. iter->Next();
  244. ASSERT_EQ(IterStatus(iter), "(invalid)");
  245. iter->SeekToLast();
  246. ASSERT_EQ(IterStatus(iter), "a->va");
  247. iter->Prev();
  248. ASSERT_EQ(IterStatus(iter), "(invalid)");
  249. iter->Seek("");
  250. ASSERT_EQ(IterStatus(iter), "a->va");
  251. iter->Next();
  252. ASSERT_EQ(IterStatus(iter), "(invalid)");
  253. iter->Seek("a");
  254. ASSERT_EQ(IterStatus(iter), "a->va");
  255. iter->Next();
  256. ASSERT_EQ(IterStatus(iter), "(invalid)");
  257. iter->Seek("b");
  258. ASSERT_EQ(IterStatus(iter), "(invalid)");
  259. delete iter;
  260. }
  261. TEST(DBTest, IterMulti) {
  262. ASSERT_OK(Put("a", "va"));
  263. ASSERT_OK(Put("b", "vb"));
  264. ASSERT_OK(Put("c", "vc"));
  265. Iterator* iter = db_->NewIterator(ReadOptions());
  266. iter->SeekToFirst();
  267. ASSERT_EQ(IterStatus(iter), "a->va");
  268. iter->Next();
  269. ASSERT_EQ(IterStatus(iter), "b->vb");
  270. iter->Next();
  271. ASSERT_EQ(IterStatus(iter), "c->vc");
  272. iter->Next();
  273. ASSERT_EQ(IterStatus(iter), "(invalid)");
  274. iter->SeekToFirst();
  275. ASSERT_EQ(IterStatus(iter), "a->va");
  276. iter->Prev();
  277. ASSERT_EQ(IterStatus(iter), "(invalid)");
  278. iter->SeekToLast();
  279. ASSERT_EQ(IterStatus(iter), "c->vc");
  280. iter->Prev();
  281. ASSERT_EQ(IterStatus(iter), "b->vb");
  282. iter->Prev();
  283. ASSERT_EQ(IterStatus(iter), "a->va");
  284. iter->Prev();
  285. ASSERT_EQ(IterStatus(iter), "(invalid)");
  286. iter->SeekToLast();
  287. ASSERT_EQ(IterStatus(iter), "c->vc");
  288. iter->Next();
  289. ASSERT_EQ(IterStatus(iter), "(invalid)");
  290. iter->Seek("");
  291. ASSERT_EQ(IterStatus(iter), "a->va");
  292. iter->Seek("a");
  293. ASSERT_EQ(IterStatus(iter), "a->va");
  294. iter->Seek("ax");
  295. ASSERT_EQ(IterStatus(iter), "b->vb");
  296. iter->Seek("b");
  297. ASSERT_EQ(IterStatus(iter), "b->vb");
  298. iter->Seek("z");
  299. ASSERT_EQ(IterStatus(iter), "(invalid)");
  300. // Switch from reverse to forward
  301. iter->SeekToLast();
  302. iter->Prev();
  303. iter->Prev();
  304. iter->Next();
  305. ASSERT_EQ(IterStatus(iter), "b->vb");
  306. // Switch from forward to reverse
  307. iter->SeekToFirst();
  308. iter->Next();
  309. iter->Next();
  310. iter->Prev();
  311. ASSERT_EQ(IterStatus(iter), "b->vb");
  312. // Make sure iter stays at snapshot
  313. ASSERT_OK(Put("a", "va2"));
  314. ASSERT_OK(Put("a2", "va3"));
  315. ASSERT_OK(Put("b", "vb2"));
  316. ASSERT_OK(Put("c", "vc2"));
  317. ASSERT_OK(Delete("b"));
  318. iter->SeekToFirst();
  319. ASSERT_EQ(IterStatus(iter), "a->va");
  320. iter->Next();
  321. ASSERT_EQ(IterStatus(iter), "b->vb");
  322. iter->Next();
  323. ASSERT_EQ(IterStatus(iter), "c->vc");
  324. iter->Next();
  325. ASSERT_EQ(IterStatus(iter), "(invalid)");
  326. iter->SeekToLast();
  327. ASSERT_EQ(IterStatus(iter), "c->vc");
  328. iter->Prev();
  329. ASSERT_EQ(IterStatus(iter), "b->vb");
  330. iter->Prev();
  331. ASSERT_EQ(IterStatus(iter), "a->va");
  332. iter->Prev();
  333. ASSERT_EQ(IterStatus(iter), "(invalid)");
  334. delete iter;
  335. }
  336. TEST(DBTest, IterSmallAndLargeMix) {
  337. ASSERT_OK(Put("a", "va"));
  338. ASSERT_OK(Put("b", std::string(100000, 'b')));
  339. ASSERT_OK(Put("c", "vc"));
  340. ASSERT_OK(Put("d", std::string(100000, 'd')));
  341. ASSERT_OK(Put("e", std::string(100000, 'e')));
  342. Iterator* iter = db_->NewIterator(ReadOptions());
  343. iter->SeekToFirst();
  344. ASSERT_EQ(IterStatus(iter), "a->va");
  345. iter->Next();
  346. ASSERT_EQ(IterStatus(iter), "b->" + std::string(100000, 'b'));
  347. iter->Next();
  348. ASSERT_EQ(IterStatus(iter), "c->vc");
  349. iter->Next();
  350. ASSERT_EQ(IterStatus(iter), "d->" + std::string(100000, 'd'));
  351. iter->Next();
  352. ASSERT_EQ(IterStatus(iter), "e->" + std::string(100000, 'e'));
  353. iter->Next();
  354. ASSERT_EQ(IterStatus(iter), "(invalid)");
  355. iter->SeekToLast();
  356. ASSERT_EQ(IterStatus(iter), "e->" + std::string(100000, 'e'));
  357. iter->Prev();
  358. ASSERT_EQ(IterStatus(iter), "d->" + std::string(100000, 'd'));
  359. iter->Prev();
  360. ASSERT_EQ(IterStatus(iter), "c->vc");
  361. iter->Prev();
  362. ASSERT_EQ(IterStatus(iter), "b->" + std::string(100000, 'b'));
  363. iter->Prev();
  364. ASSERT_EQ(IterStatus(iter), "a->va");
  365. iter->Prev();
  366. ASSERT_EQ(IterStatus(iter), "(invalid)");
  367. delete iter;
  368. }
  369. TEST(DBTest, Recover) {
  370. ASSERT_OK(Put("foo", "v1"));
  371. ASSERT_OK(Put("baz", "v5"));
  372. Reopen();
  373. ASSERT_EQ("v1", Get("foo"));
  374. ASSERT_EQ("v1", Get("foo"));
  375. ASSERT_EQ("v5", Get("baz"));
  376. ASSERT_OK(Put("bar", "v2"));
  377. ASSERT_OK(Put("foo", "v3"));
  378. Reopen();
  379. ASSERT_EQ("v3", Get("foo"));
  380. ASSERT_OK(Put("foo", "v4"));
  381. ASSERT_EQ("v4", Get("foo"));
  382. ASSERT_EQ("v2", Get("bar"));
  383. ASSERT_EQ("v5", Get("baz"));
  384. }
  385. TEST(DBTest, RecoveryWithEmptyLog) {
  386. ASSERT_OK(Put("foo", "v1"));
  387. ASSERT_OK(Put("foo", "v2"));
  388. Reopen();
  389. Reopen();
  390. ASSERT_OK(Put("foo", "v3"));
  391. Reopen();
  392. ASSERT_EQ("v3", Get("foo"));
  393. }
  394. static std::string Key(int i) {
  395. char buf[100];
  396. snprintf(buf, sizeof(buf), "key%06d", i);
  397. return std::string(buf);
  398. }
  399. TEST(DBTest, MinorCompactionsHappen) {
  400. Options options;
  401. options.write_buffer_size = 10000;
  402. Reopen(&options);
  403. const int N = 100;
  404. int starting_num_tables = NumTableFilesAtLevel(0);
  405. for (int i = 0; i < N; i++) {
  406. ASSERT_OK(Put(Key(i), Key(i) + std::string(1000, 'v')));
  407. }
  408. int ending_num_tables = NumTableFilesAtLevel(0);
  409. ASSERT_GT(ending_num_tables, starting_num_tables);
  410. for (int i = 0; i < N; i++) {
  411. ASSERT_EQ(Key(i) + std::string(1000, 'v'), Get(Key(i)));
  412. }
  413. Reopen();
  414. for (int i = 0; i < N; i++) {
  415. ASSERT_EQ(Key(i) + std::string(1000, 'v'), Get(Key(i)));
  416. }
  417. }
  418. TEST(DBTest, RecoverWithLargeLog) {
  419. {
  420. Options options;
  421. options.large_value_threshold = 1048576;
  422. Reopen(&options);
  423. ASSERT_OK(Put("big1", std::string(200000, '1')));
  424. ASSERT_OK(Put("big2", std::string(200000, '2')));
  425. ASSERT_OK(Put("small3", std::string(10, '3')));
  426. ASSERT_OK(Put("small4", std::string(10, '4')));
  427. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  428. }
  429. // Make sure that if we re-open with a small write buffer size that
  430. // we flush table files in the middle of a large log file.
  431. Options options;
  432. options.write_buffer_size = 100000;
  433. options.large_value_threshold = 1048576;
  434. Reopen(&options);
  435. ASSERT_EQ(NumTableFilesAtLevel(0), 3);
  436. ASSERT_EQ(std::string(200000, '1'), Get("big1"));
  437. ASSERT_EQ(std::string(200000, '2'), Get("big2"));
  438. ASSERT_EQ(std::string(10, '3'), Get("small3"));
  439. ASSERT_EQ(std::string(10, '4'), Get("small4"));
  440. ASSERT_GT(NumTableFilesAtLevel(0), 1);
  441. }
  442. TEST(DBTest, CompactionsGenerateMultipleFiles) {
  443. Options options;
  444. options.write_buffer_size = 100000000; // Large write buffer
  445. options.large_value_threshold = 1048576;
  446. Reopen(&options);
  447. Random rnd(301);
  448. // Write 8MB (80 values, each 100K)
  449. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  450. std::vector<std::string> values;
  451. for (int i = 0; i < 80; i++) {
  452. values.push_back(RandomString(&rnd, 100000));
  453. ASSERT_OK(Put(Key(i), values[i]));
  454. }
  455. // Reopening moves updates to level-0
  456. Reopen(&options);
  457. dbfull()->TEST_CompactRange(0, "", Key(100000));
  458. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  459. ASSERT_GT(NumTableFilesAtLevel(1), 1);
  460. for (int i = 0; i < 80; i++) {
  461. ASSERT_EQ(Get(Key(i)), values[i]);
  462. }
  463. }
  464. TEST(DBTest, SparseMerge) {
  465. Options options;
  466. options.compression = kNoCompression;
  467. Reopen(&options);
  468. // Suppose there is:
  469. // small amount of data with prefix A
  470. // large amount of data with prefix B
  471. // small amount of data with prefix C
  472. // and that recent updates have made small changes to all three prefixes.
  473. // Check that we do not do a compaction that merges all of B in one shot.
  474. const std::string value(1000, 'x');
  475. Put("A", "va");
  476. // Write approximately 100MB of "B" values
  477. for (int i = 0; i < 100000; i++) {
  478. char key[100];
  479. snprintf(key, sizeof(key), "B%010d", i);
  480. Put(key, value);
  481. }
  482. Put("C", "vc");
  483. Compact("", "z");
  484. // Make sparse update
  485. Put("A", "va2");
  486. Put("B100", "bvalue2");
  487. Put("C", "vc2");
  488. dbfull()->TEST_CompactMemTable();
  489. // Compactions should not cause us to create a situation where
  490. // a file overlaps too much data at the next level.
  491. ASSERT_LE(dbfull()->TEST_MaxNextLevelOverlappingBytes(), 20*1048576);
  492. dbfull()->TEST_CompactRange(0, "", "z");
  493. ASSERT_LE(dbfull()->TEST_MaxNextLevelOverlappingBytes(), 20*1048576);
  494. dbfull()->TEST_CompactRange(1, "", "z");
  495. ASSERT_LE(dbfull()->TEST_MaxNextLevelOverlappingBytes(), 20*1048576);
  496. }
  497. static bool Between(uint64_t val, uint64_t low, uint64_t high) {
  498. bool result = (val >= low) && (val <= high);
  499. if (!result) {
  500. fprintf(stderr, "Value %llu is not in range [%llu, %llu]\n",
  501. (unsigned long long)(val),
  502. (unsigned long long)(low),
  503. (unsigned long long)(high));
  504. }
  505. return result;
  506. }
  507. TEST(DBTest, ApproximateSizes) {
  508. for (int test = 0; test < 2; test++) {
  509. // test==0: default large_value_threshold
  510. // test==1: 1 MB large_value_threshold
  511. Options options;
  512. options.large_value_threshold = (test == 0) ? 65536 : 1048576;
  513. options.write_buffer_size = 100000000; // Large write buffer
  514. options.compression = kNoCompression;
  515. DestroyAndReopen();
  516. ASSERT_TRUE(Between(Size("", "xyz"), 0, 0));
  517. Reopen(&options);
  518. ASSERT_TRUE(Between(Size("", "xyz"), 0, 0));
  519. // Write 8MB (80 values, each 100K)
  520. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  521. const int N = 80;
  522. Random rnd(301);
  523. for (int i = 0; i < N; i++) {
  524. ASSERT_OK(Put(Key(i), RandomString(&rnd, 100000)));
  525. }
  526. if (test == 1) {
  527. // 0 because GetApproximateSizes() does not account for memtable space for
  528. // non-large values
  529. ASSERT_TRUE(Between(Size("", Key(50)), 0, 0));
  530. } else {
  531. ASSERT_TRUE(Between(Size("", Key(50)), 100000*50, 100000*50 + 10000));
  532. ASSERT_TRUE(Between(Size(Key(20), Key(30)),
  533. 100000*10, 100000*10 + 10000));
  534. }
  535. // Check sizes across recovery by reopening a few times
  536. for (int run = 0; run < 3; run++) {
  537. Reopen(&options);
  538. for (int compact_start = 0; compact_start < N; compact_start += 10) {
  539. for (int i = 0; i < N; i += 10) {
  540. ASSERT_TRUE(Between(Size("", Key(i)), 100000*i, 100000*i + 10000));
  541. ASSERT_TRUE(Between(Size("", Key(i)+".suffix"),
  542. 100000 * (i+1), 100000 * (i+1) + 10000));
  543. ASSERT_TRUE(Between(Size(Key(i), Key(i+10)),
  544. 100000 * 10, 100000 * 10 + 10000));
  545. }
  546. ASSERT_TRUE(Between(Size("", Key(50)), 5000000, 5010000));
  547. ASSERT_TRUE(Between(Size("", Key(50)+".suffix"), 5100000, 5110000));
  548. dbfull()->TEST_CompactRange(0,
  549. Key(compact_start),
  550. Key(compact_start + 9));
  551. }
  552. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  553. ASSERT_GT(NumTableFilesAtLevel(1), 0);
  554. }
  555. }
  556. }
  557. TEST(DBTest, ApproximateSizes_MixOfSmallAndLarge) {
  558. Options options;
  559. options.large_value_threshold = 65536;
  560. options.compression = kNoCompression;
  561. Reopen();
  562. Random rnd(301);
  563. std::string big1 = RandomString(&rnd, 100000);
  564. ASSERT_OK(Put(Key(0), RandomString(&rnd, 10000)));
  565. ASSERT_OK(Put(Key(1), RandomString(&rnd, 10000)));
  566. ASSERT_OK(Put(Key(2), big1));
  567. ASSERT_OK(Put(Key(3), RandomString(&rnd, 10000)));
  568. ASSERT_OK(Put(Key(4), big1));
  569. ASSERT_OK(Put(Key(5), RandomString(&rnd, 10000)));
  570. ASSERT_OK(Put(Key(6), RandomString(&rnd, 300000)));
  571. ASSERT_OK(Put(Key(7), RandomString(&rnd, 10000)));
  572. // Check sizes across recovery by reopening a few times
  573. for (int run = 0; run < 3; run++) {
  574. Reopen(&options);
  575. ASSERT_TRUE(Between(Size("", Key(0)), 0, 0));
  576. ASSERT_TRUE(Between(Size("", Key(1)), 10000, 11000));
  577. ASSERT_TRUE(Between(Size("", Key(2)), 20000, 21000));
  578. ASSERT_TRUE(Between(Size("", Key(3)), 120000, 121000));
  579. ASSERT_TRUE(Between(Size("", Key(4)), 130000, 131000));
  580. ASSERT_TRUE(Between(Size("", Key(5)), 230000, 231000));
  581. ASSERT_TRUE(Between(Size("", Key(6)), 240000, 241000));
  582. ASSERT_TRUE(Between(Size("", Key(7)), 540000, 541000));
  583. ASSERT_TRUE(Between(Size("", Key(8)), 550000, 551000));
  584. ASSERT_TRUE(Between(Size(Key(3), Key(5)), 110000, 111000));
  585. dbfull()->TEST_CompactRange(0, Key(0), Key(100));
  586. }
  587. }
  588. TEST(DBTest, IteratorPinsRef) {
  589. Put("foo", "hello");
  590. // Get iterator that will yield the current contents of the DB.
  591. Iterator* iter = db_->NewIterator(ReadOptions());
  592. // Write to force compactions
  593. Put("foo", "newvalue1");
  594. for (int i = 0; i < 100; i++) {
  595. ASSERT_OK(Put(Key(i), Key(i) + std::string(100000, 'v'))); // 100K values
  596. }
  597. Put("foo", "newvalue2");
  598. iter->SeekToFirst();
  599. ASSERT_TRUE(iter->Valid());
  600. ASSERT_EQ("foo", iter->key().ToString());
  601. ASSERT_EQ("hello", iter->value().ToString());
  602. iter->Next();
  603. ASSERT_TRUE(!iter->Valid());
  604. delete iter;
  605. }
  606. TEST(DBTest, Snapshot) {
  607. Put("foo", "v1");
  608. const Snapshot* s1 = db_->GetSnapshot();
  609. Put("foo", "v2");
  610. const Snapshot* s2 = db_->GetSnapshot();
  611. Put("foo", "v3");
  612. const Snapshot* s3 = db_->GetSnapshot();
  613. Put("foo", "v4");
  614. ASSERT_EQ("v1", Get("foo", s1));
  615. ASSERT_EQ("v2", Get("foo", s2));
  616. ASSERT_EQ("v3", Get("foo", s3));
  617. ASSERT_EQ("v4", Get("foo"));
  618. db_->ReleaseSnapshot(s3);
  619. ASSERT_EQ("v1", Get("foo", s1));
  620. ASSERT_EQ("v2", Get("foo", s2));
  621. ASSERT_EQ("v4", Get("foo"));
  622. db_->ReleaseSnapshot(s1);
  623. ASSERT_EQ("v2", Get("foo", s2));
  624. ASSERT_EQ("v4", Get("foo"));
  625. db_->ReleaseSnapshot(s2);
  626. ASSERT_EQ("v4", Get("foo"));
  627. }
  628. TEST(DBTest, HiddenValuesAreRemoved) {
  629. Random rnd(301);
  630. std::string big = RandomString(&rnd, 50000);
  631. Put("foo", big);
  632. Put("pastfoo", "v");
  633. const Snapshot* snapshot = db_->GetSnapshot();
  634. Put("foo", "tiny");
  635. Put("pastfoo2", "v2"); // Advance sequence number one more
  636. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  637. ASSERT_GT(NumTableFilesAtLevel(0), 0);
  638. ASSERT_EQ(big, Get("foo", snapshot));
  639. ASSERT_TRUE(Between(Size("", "pastfoo"), 50000, 60000));
  640. db_->ReleaseSnapshot(snapshot);
  641. ASSERT_EQ(AllEntriesFor("foo"), "[ tiny, " + big + " ]");
  642. dbfull()->TEST_CompactRange(0, "", "x");
  643. ASSERT_EQ(AllEntriesFor("foo"), "[ tiny ]");
  644. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  645. ASSERT_GE(NumTableFilesAtLevel(1), 1);
  646. dbfull()->TEST_CompactRange(1, "", "x");
  647. ASSERT_EQ(AllEntriesFor("foo"), "[ tiny ]");
  648. ASSERT_TRUE(Between(Size("", "pastfoo"), 0, 1000));
  649. }
  650. TEST(DBTest, DeletionMarkers1) {
  651. Put("foo", "v1");
  652. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  653. dbfull()->TEST_CompactRange(0, "", "z");
  654. dbfull()->TEST_CompactRange(1, "", "z");
  655. ASSERT_EQ(NumTableFilesAtLevel(2), 1); // foo => v1 is now in level 2 file
  656. Delete("foo");
  657. Put("foo", "v2");
  658. ASSERT_EQ(AllEntriesFor("foo"), "[ v2, DEL, v1 ]");
  659. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  660. ASSERT_EQ(AllEntriesFor("foo"), "[ v2, DEL, v1 ]");
  661. dbfull()->TEST_CompactRange(0, "", "z");
  662. // DEL eliminated, but v1 remains because we aren't compacting that level
  663. // (DEL can be eliminated because v2 hides v1).
  664. ASSERT_EQ(AllEntriesFor("foo"), "[ v2, v1 ]");
  665. dbfull()->TEST_CompactRange(1, "", "z");
  666. // Merging L1 w/ L2, so we are the base level for "foo", so DEL is removed.
  667. // (as is v1).
  668. ASSERT_EQ(AllEntriesFor("foo"), "[ v2 ]");
  669. }
  670. TEST(DBTest, DeletionMarkers2) {
  671. Put("foo", "v1");
  672. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  673. dbfull()->TEST_CompactRange(0, "", "z");
  674. dbfull()->TEST_CompactRange(1, "", "z");
  675. ASSERT_EQ(NumTableFilesAtLevel(2), 1); // foo => v1 is now in level 2 file
  676. Delete("foo");
  677. ASSERT_EQ(AllEntriesFor("foo"), "[ DEL, v1 ]");
  678. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  679. ASSERT_EQ(AllEntriesFor("foo"), "[ DEL, v1 ]");
  680. dbfull()->TEST_CompactRange(0, "", "z");
  681. // DEL kept: L2 file overlaps
  682. ASSERT_EQ(AllEntriesFor("foo"), "[ DEL, v1 ]");
  683. dbfull()->TEST_CompactRange(1, "", "z");
  684. // Merging L1 w/ L2, so we are the base level for "foo", so DEL is removed.
  685. // (as is v1).
  686. ASSERT_EQ(AllEntriesFor("foo"), "[ ]");
  687. }
  688. TEST(DBTest, ComparatorCheck) {
  689. class NewComparator : public Comparator {
  690. public:
  691. virtual const char* Name() const { return "leveldb.NewComparator"; }
  692. virtual int Compare(const Slice& a, const Slice& b) const {
  693. return BytewiseComparator()->Compare(a, b);
  694. }
  695. virtual void FindShortestSeparator(std::string* s, const Slice& l) const {
  696. BytewiseComparator()->FindShortestSeparator(s, l);
  697. }
  698. virtual void FindShortSuccessor(std::string* key) const {
  699. BytewiseComparator()->FindShortSuccessor(key);
  700. }
  701. };
  702. NewComparator cmp;
  703. Options new_options;
  704. new_options.comparator = &cmp;
  705. Status s = TryReopen(&new_options);
  706. ASSERT_TRUE(!s.ok());
  707. ASSERT_TRUE(s.ToString().find("comparator") != std::string::npos)
  708. << s.ToString();
  709. }
  710. static bool LargeValuesOK(DBTest* db,
  711. const std::set<LargeValueRef>& expected) {
  712. std::set<LargeValueRef> actual = db->LargeValueFiles();
  713. if (actual.size() != expected.size()) {
  714. fprintf(stderr, "Sets differ in size: %d vs %d\n",
  715. (int)actual.size(), (int)expected.size());
  716. return false;
  717. }
  718. for (std::set<LargeValueRef>::const_iterator it = expected.begin();
  719. it != expected.end();
  720. ++it) {
  721. if (actual.count(*it) != 1) {
  722. fprintf(stderr, " key '%s' not found in actual set\n",
  723. LargeValueRefToFilenameString(*it).c_str());
  724. return false;
  725. }
  726. }
  727. return true;
  728. }
  729. TEST(DBTest, LargeValues1) {
  730. Options options;
  731. options.large_value_threshold = 10000;
  732. Reopen(&options);
  733. Random rnd(301);
  734. std::string big1;
  735. test::CompressibleString(&rnd, 1.0, 100000, &big1); // Not compressible
  736. std::set<LargeValueRef> expected;
  737. ASSERT_OK(Put("big1", big1));
  738. expected.insert(LargeValueRef::Make(big1, kNoCompression));
  739. ASSERT_TRUE(LargeValuesOK(this, expected));
  740. ASSERT_OK(Delete("big1"));
  741. ASSERT_TRUE(LargeValuesOK(this, expected));
  742. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  743. // No handling of deletion markers on memtable compactions, so big1 remains
  744. ASSERT_TRUE(LargeValuesOK(this, expected));
  745. dbfull()->TEST_CompactRange(0, "", "z");
  746. expected.erase(LargeValueRef::Make(big1, kNoCompression));
  747. ASSERT_TRUE(LargeValuesOK(this, expected));
  748. }
  749. static bool SnappyCompressionSupported() {
  750. std::string out;
  751. Slice in = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  752. return port::Snappy_Compress(in.data(), in.size(), &out);
  753. }
  754. TEST(DBTest, LargeValues2) {
  755. Options options;
  756. options.large_value_threshold = 10000;
  757. Reopen(&options);
  758. Random rnd(301);
  759. std::string big1, big2;
  760. test::CompressibleString(&rnd, 1.0, 20000, &big1); // Not compressible
  761. test::CompressibleString(&rnd, 0.6, 40000, &big2); // Compressible
  762. std::set<LargeValueRef> expected;
  763. ASSERT_TRUE(LargeValuesOK(this, expected));
  764. ASSERT_OK(Put("big1", big1));
  765. expected.insert(LargeValueRef::Make(big1, kNoCompression));
  766. ASSERT_EQ(big1, Get("big1"));
  767. ASSERT_TRUE(LargeValuesOK(this, expected));
  768. ASSERT_OK(Put("big2", big2));
  769. ASSERT_EQ(big2, Get("big2"));
  770. if (SnappyCompressionSupported()) {
  771. expected.insert(LargeValueRef::Make(big2, kSnappyCompression));
  772. } else {
  773. expected.insert(LargeValueRef::Make(big2, kNoCompression));
  774. }
  775. ASSERT_TRUE(LargeValuesOK(this, expected));
  776. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  777. ASSERT_TRUE(LargeValuesOK(this, expected));
  778. dbfull()->TEST_CompactRange(0, "", "z");
  779. ASSERT_TRUE(LargeValuesOK(this, expected));
  780. ASSERT_OK(Put("big2", big2));
  781. ASSERT_OK(Put("big2_b", big2));
  782. ASSERT_EQ(big1, Get("big1"));
  783. ASSERT_EQ(big2, Get("big2"));
  784. ASSERT_EQ(big2, Get("big2_b"));
  785. ASSERT_TRUE(LargeValuesOK(this, expected));
  786. ASSERT_OK(Delete("big1"));
  787. ASSERT_EQ("NOT_FOUND", Get("big1"));
  788. ASSERT_TRUE(LargeValuesOK(this, expected));
  789. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  790. ASSERT_TRUE(LargeValuesOK(this, expected));
  791. dbfull()->TEST_CompactRange(0, "", "z");
  792. expected.erase(LargeValueRef::Make(big1, kNoCompression));
  793. ASSERT_TRUE(LargeValuesOK(this, expected));
  794. dbfull()->TEST_CompactRange(1, "", "z");
  795. ASSERT_OK(Delete("big2"));
  796. ASSERT_EQ("NOT_FOUND", Get("big2"));
  797. ASSERT_EQ(big2, Get("big2_b"));
  798. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  799. ASSERT_TRUE(LargeValuesOK(this, expected));
  800. dbfull()->TEST_CompactRange(0, "", "z");
  801. ASSERT_TRUE(LargeValuesOK(this, expected));
  802. // Make sure the large value refs survive a reload and compactions after
  803. // the reload.
  804. Reopen();
  805. ASSERT_TRUE(LargeValuesOK(this, expected));
  806. ASSERT_OK(Put("foo", "bar"));
  807. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  808. dbfull()->TEST_CompactRange(0, "", "z");
  809. ASSERT_TRUE(LargeValuesOK(this, expected));
  810. }
  811. TEST(DBTest, LargeValues3) {
  812. // Make sure we don't compress values if
  813. Options options;
  814. options.large_value_threshold = 10000;
  815. options.compression = kNoCompression;
  816. Reopen(&options);
  817. Random rnd(301);
  818. std::string big1 = std::string(100000, 'x'); // Very compressible
  819. std::set<LargeValueRef> expected;
  820. ASSERT_OK(Put("big1", big1));
  821. ASSERT_EQ(big1, Get("big1"));
  822. expected.insert(LargeValueRef::Make(big1, kNoCompression));
  823. ASSERT_TRUE(LargeValuesOK(this, expected));
  824. }
  825. TEST(DBTest, DBOpen_Options) {
  826. std::string dbname = test::TmpDir() + "/db_options_test";
  827. DestroyDB(dbname, Options());
  828. // Does not exist, and create_if_missing == false: error
  829. DB* db = NULL;
  830. Options opts;
  831. opts.create_if_missing = false;
  832. Status s = DB::Open(opts, dbname, &db);
  833. ASSERT_TRUE(strstr(s.ToString().c_str(), "does not exist") != NULL);
  834. ASSERT_TRUE(db == NULL);
  835. // Does not exist, and create_if_missing == true: OK
  836. opts.create_if_missing = true;
  837. s = DB::Open(opts, dbname, &db);
  838. ASSERT_OK(s);
  839. ASSERT_TRUE(db != NULL);
  840. delete db;
  841. db = NULL;
  842. // Does exist, and error_if_exists == true: error
  843. opts.create_if_missing = false;
  844. opts.error_if_exists = true;
  845. s = DB::Open(opts, dbname, &db);
  846. ASSERT_TRUE(strstr(s.ToString().c_str(), "exists") != NULL);
  847. ASSERT_TRUE(db == NULL);
  848. // Does exist, and error_if_exists == false: OK
  849. opts.create_if_missing = true;
  850. opts.error_if_exists = false;
  851. s = DB::Open(opts, dbname, &db);
  852. ASSERT_OK(s);
  853. ASSERT_TRUE(db != NULL);
  854. delete db;
  855. db = NULL;
  856. }
  857. class ModelDB: public DB {
  858. public:
  859. explicit ModelDB(const Options& options): options_(options) { }
  860. ~ModelDB() { }
  861. virtual Status Put(const WriteOptions& o, const Slice& k, const Slice& v) {
  862. return DB::Put(o, k, v);
  863. }
  864. virtual Status Delete(const WriteOptions& o, const Slice& key) {
  865. return DB::Delete(o, key);
  866. }
  867. virtual Status Get(const ReadOptions& options,
  868. const Slice& key, std::string* value) {
  869. assert(false); // Not implemented
  870. return Status::NotFound(key);
  871. }
  872. virtual Iterator* NewIterator(const ReadOptions& options) {
  873. if (options.snapshot == NULL) {
  874. KVMap* saved = new KVMap;
  875. *saved = map_;
  876. return new ModelIter(saved, true);
  877. } else {
  878. const KVMap* snapshot_state =
  879. reinterpret_cast<const KVMap*>(options.snapshot->number_);
  880. return new ModelIter(snapshot_state, false);
  881. }
  882. }
  883. virtual const Snapshot* GetSnapshot() {
  884. KVMap* saved = new KVMap;
  885. *saved = map_;
  886. return snapshots_.New(
  887. reinterpret_cast<SequenceNumber>(saved));
  888. }
  889. virtual void ReleaseSnapshot(const Snapshot* snapshot) {
  890. const KVMap* saved = reinterpret_cast<const KVMap*>(snapshot->number_);
  891. delete saved;
  892. snapshots_.Delete(snapshot);
  893. }
  894. virtual Status Write(const WriteOptions& options, WriteBatch* batch) {
  895. assert(options.post_write_snapshot == NULL); // Not supported
  896. for (WriteBatchInternal::Iterator it(*batch); !it.Done(); it.Next()) {
  897. switch (it.op()) {
  898. case kTypeValue:
  899. map_[it.key().ToString()] = it.value().ToString();
  900. break;
  901. case kTypeLargeValueRef:
  902. assert(false); // Should not occur
  903. break;
  904. case kTypeDeletion:
  905. map_.erase(it.key().ToString());
  906. break;
  907. }
  908. }
  909. return Status::OK();
  910. }
  911. virtual bool GetProperty(const Slice& property, uint64_t* value) {
  912. return false;
  913. }
  914. virtual void GetApproximateSizes(const Range* r, int n, uint64_t* sizes) {
  915. for (int i = 0; i < n; i++) {
  916. sizes[i] = 0;
  917. }
  918. }
  919. private:
  920. typedef std::map<std::string, std::string> KVMap;
  921. class ModelIter: public Iterator {
  922. public:
  923. ModelIter(const KVMap* map, bool owned)
  924. : map_(map), owned_(owned), iter_(map_->end()) {
  925. }
  926. ~ModelIter() {
  927. if (owned_) delete map_;
  928. }
  929. virtual bool Valid() const { return iter_ != map_->end(); }
  930. virtual void SeekToFirst() { iter_ = map_->begin(); }
  931. virtual void SeekToLast() {
  932. if (map_->empty()) {
  933. iter_ = map_->end();
  934. } else {
  935. iter_ = map_->find(map_->rbegin()->first);
  936. }
  937. }
  938. virtual void Seek(const Slice& k) {
  939. iter_ = map_->lower_bound(k.ToString());
  940. }
  941. virtual void Next() { ++iter_; }
  942. virtual void Prev() { --iter_; }
  943. virtual Slice key() const { return iter_->first; }
  944. virtual Slice value() const { return iter_->second; }
  945. virtual Status status() const { return Status::OK(); }
  946. private:
  947. const KVMap* const map_;
  948. const bool owned_; // Do we own map_
  949. KVMap::const_iterator iter_;
  950. };
  951. const Options options_;
  952. KVMap map_;
  953. SnapshotList snapshots_;
  954. };
  955. static std::string RandomKey(Random* rnd) {
  956. int len = (rnd->OneIn(3)
  957. ? 1 // Short sometimes to encourage collisions
  958. : (rnd->OneIn(100) ? rnd->Skewed(10) : rnd->Uniform(10)));
  959. return test::RandomKey(rnd, len);
  960. }
  961. static bool CompareIterators(int step,
  962. DB* model,
  963. DB* db,
  964. const Snapshot* model_snap,
  965. const Snapshot* db_snap) {
  966. ReadOptions options;
  967. options.snapshot = model_snap;
  968. Iterator* miter = model->NewIterator(options);
  969. options.snapshot = db_snap;
  970. Iterator* dbiter = db->NewIterator(options);
  971. bool ok = true;
  972. int count = 0;
  973. for (miter->SeekToFirst(), dbiter->SeekToFirst();
  974. ok && miter->Valid() && dbiter->Valid();
  975. miter->Next(), dbiter->Next()) {
  976. count++;
  977. if (miter->key().compare(dbiter->key()) != 0) {
  978. fprintf(stderr, "step %d: Key mismatch: '%s' vs. '%s'\n",
  979. step,
  980. EscapeString(miter->key()).c_str(),
  981. EscapeString(dbiter->key()).c_str());
  982. ok = false;
  983. break;
  984. }
  985. if (miter->value().compare(dbiter->value()) != 0) {
  986. fprintf(stderr, "step %d: Value mismatch for key '%s': '%s' vs. '%s'\n",
  987. step,
  988. EscapeString(miter->key()).c_str(),
  989. EscapeString(miter->value()).c_str(),
  990. EscapeString(miter->value()).c_str());
  991. ok = false;
  992. }
  993. }
  994. if (ok) {
  995. if (miter->Valid() != dbiter->Valid()) {
  996. fprintf(stderr, "step %d: Mismatch at end of iterators: %d vs. %d\n",
  997. step, miter->Valid(), dbiter->Valid());
  998. ok = false;
  999. }
  1000. }
  1001. fprintf(stderr, "%d entries compared: ok=%d\n", count, ok);
  1002. delete miter;
  1003. delete dbiter;
  1004. return ok;
  1005. }
  1006. TEST(DBTest, Randomized) {
  1007. Random rnd(test::RandomSeed());
  1008. ModelDB model(last_options_);
  1009. const int N = 10000;
  1010. const Snapshot* model_snap = NULL;
  1011. const Snapshot* db_snap = NULL;
  1012. std::string k, v;
  1013. for (int step = 0; step < N; step++) {
  1014. if (step % 100 == 0) {
  1015. fprintf(stderr, "Step %d of %d\n", step, N);
  1016. }
  1017. int p = rnd.Uniform(100);
  1018. if (p < 45) { // Put
  1019. k = RandomKey(&rnd);
  1020. v = RandomString(&rnd,
  1021. rnd.OneIn(20)
  1022. ? 100 + rnd.Uniform(100)
  1023. : rnd.Uniform(8));
  1024. ASSERT_OK(model.Put(WriteOptions(), k, v));
  1025. ASSERT_OK(db_->Put(WriteOptions(), k, v));
  1026. } else if (p < 90) { // Delete
  1027. k = RandomKey(&rnd);
  1028. ASSERT_OK(model.Delete(WriteOptions(), k));
  1029. ASSERT_OK(db_->Delete(WriteOptions(), k));
  1030. } else { // Multi-element batch
  1031. WriteBatch b;
  1032. const int num = rnd.Uniform(8);
  1033. for (int i = 0; i < num; i++) {
  1034. if (i == 0 || !rnd.OneIn(10)) {
  1035. k = RandomKey(&rnd);
  1036. } else {
  1037. // Periodically re-use the same key from the previous iter, so
  1038. // we have multiple entries in the write batch for the same key
  1039. }
  1040. if (rnd.OneIn(2)) {
  1041. v = RandomString(&rnd, rnd.Uniform(10));
  1042. b.Put(k, v);
  1043. } else {
  1044. b.Delete(k);
  1045. }
  1046. }
  1047. ASSERT_OK(model.Write(WriteOptions(), &b));
  1048. ASSERT_OK(db_->Write(WriteOptions(), &b));
  1049. }
  1050. if ((step % 100) == 0) {
  1051. ASSERT_TRUE(CompareIterators(step, &model, db_, NULL, NULL));
  1052. ASSERT_TRUE(CompareIterators(step, &model, db_, model_snap, db_snap));
  1053. // Save a snapshot from each DB this time that we'll use next
  1054. // time we compare things, to make sure the current state is
  1055. // preserved with the snapshot
  1056. if (model_snap != NULL) model.ReleaseSnapshot(model_snap);
  1057. if (db_snap != NULL) db_->ReleaseSnapshot(db_snap);
  1058. Reopen();
  1059. ASSERT_TRUE(CompareIterators(step, &model, db_, NULL, NULL));
  1060. model_snap = model.GetSnapshot();
  1061. db_snap = db_->GetSnapshot();
  1062. }
  1063. }
  1064. if (model_snap != NULL) model.ReleaseSnapshot(model_snap);
  1065. if (db_snap != NULL) db_->ReleaseSnapshot(db_snap);
  1066. }
  1067. }
  1068. int main(int argc, char** argv) {
  1069. return leveldb::test::RunAllTests();
  1070. }