作者: 谢瑞阳 10225101483 徐翔宇 10225101535
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.

963 regels
29 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. WriteBatch batch;
  62. batch.Put(k, v);
  63. return db_->Write(WriteOptions(), &batch);
  64. }
  65. Status Delete(const std::string& k) {
  66. WriteBatch batch;
  67. batch.Delete(k);
  68. return db_->Write(WriteOptions(), &batch);
  69. }
  70. std::string Get(const std::string& k, const Snapshot* snapshot = NULL) {
  71. ReadOptions options;
  72. options.snapshot = snapshot;
  73. std::string result;
  74. Status s = db_->Get(options, k, &result);
  75. if (s.IsNotFound()) {
  76. result = "NOT_FOUND";
  77. } else if (!s.ok()) {
  78. result = s.ToString();
  79. }
  80. return result;
  81. }
  82. std::string AllEntriesFor(const Slice& user_key) {
  83. Iterator* iter = dbfull()->TEST_NewInternalIterator();
  84. InternalKey target(user_key, kMaxSequenceNumber, kTypeValue);
  85. iter->Seek(target.Encode());
  86. std::string result;
  87. if (!iter->status().ok()) {
  88. result = iter->status().ToString();
  89. } else {
  90. result = "[ ";
  91. bool first = true;
  92. while (iter->Valid()) {
  93. ParsedInternalKey ikey;
  94. if (!ParseInternalKey(iter->key(), &ikey)) {
  95. result += "CORRUPTED";
  96. } else {
  97. if (last_options_.comparator->Compare(
  98. ikey.user_key, user_key) != 0) {
  99. break;
  100. }
  101. if (!first) {
  102. result += ", ";
  103. }
  104. first = false;
  105. switch (ikey.type) {
  106. case kTypeValue:
  107. result += iter->value().ToString();
  108. break;
  109. case kTypeLargeValueRef:
  110. result += "LARGEVALUE(" + EscapeString(iter->value()) + ")";
  111. break;
  112. case kTypeDeletion:
  113. result += "DEL";
  114. break;
  115. }
  116. }
  117. iter->Next();
  118. }
  119. if (!first) {
  120. result += " ";
  121. }
  122. result += "]";
  123. }
  124. delete iter;
  125. return result;
  126. }
  127. int NumTableFilesAtLevel(int level) {
  128. uint64_t val;
  129. ASSERT_TRUE(
  130. db_->GetProperty("leveldb.num-files-at-level" + NumberToString(level),
  131. &val));
  132. return val;
  133. }
  134. uint64_t Size(const Slice& start, const Slice& limit) {
  135. Range r(start, limit);
  136. uint64_t size;
  137. db_->GetApproximateSizes(&r, 1, &size);
  138. return size;
  139. }
  140. std::set<LargeValueRef> LargeValueFiles() const {
  141. // Return the set of large value files that exist in the database
  142. std::vector<std::string> filenames;
  143. env_->GetChildren(dbname_, &filenames); // Ignoring errors on purpose
  144. uint64_t number;
  145. LargeValueRef large_ref;
  146. FileType type;
  147. std::set<LargeValueRef> live;
  148. for (int i = 0; i < filenames.size(); i++) {
  149. if (ParseFileName(filenames[i], &number, &large_ref, &type) &&
  150. type == kLargeValueFile) {
  151. fprintf(stderr, " live: %s\n",
  152. LargeValueRefToFilenameString(large_ref).c_str());
  153. live.insert(large_ref);
  154. }
  155. }
  156. fprintf(stderr, "Found %d live large value files\n", (int)live.size());
  157. return live;
  158. }
  159. };
  160. TEST(DBTest, Empty) {
  161. ASSERT_TRUE(db_ != NULL);
  162. ASSERT_EQ("NOT_FOUND", Get("foo"));
  163. }
  164. TEST(DBTest, ReadWrite) {
  165. ASSERT_OK(Put("foo", "v1"));
  166. ASSERT_EQ("v1", Get("foo"));
  167. ASSERT_OK(Put("bar", "v2"));
  168. ASSERT_OK(Put("foo", "v3"));
  169. ASSERT_EQ("v3", Get("foo"));
  170. ASSERT_EQ("v2", Get("bar"));
  171. }
  172. TEST(DBTest, PutDeleteGet) {
  173. ASSERT_OK(db_->Put(WriteOptions(), "foo", "v1"));
  174. ASSERT_EQ("v1", Get("foo"));
  175. ASSERT_OK(db_->Put(WriteOptions(), "foo", "v2"));
  176. ASSERT_EQ("v2", Get("foo"));
  177. ASSERT_OK(db_->Delete(WriteOptions(), "foo"));
  178. ASSERT_EQ("NOT_FOUND", Get("foo"));
  179. }
  180. TEST(DBTest, Recover) {
  181. ASSERT_OK(Put("foo", "v1"));
  182. ASSERT_OK(Put("baz", "v5"));
  183. Reopen();
  184. ASSERT_EQ("v1", Get("foo"));
  185. ASSERT_EQ("v1", Get("foo"));
  186. ASSERT_EQ("v5", Get("baz"));
  187. ASSERT_OK(Put("bar", "v2"));
  188. ASSERT_OK(Put("foo", "v3"));
  189. Reopen();
  190. ASSERT_EQ("v3", Get("foo"));
  191. ASSERT_OK(Put("foo", "v4"));
  192. ASSERT_EQ("v4", Get("foo"));
  193. ASSERT_EQ("v2", Get("bar"));
  194. ASSERT_EQ("v5", Get("baz"));
  195. }
  196. TEST(DBTest, RecoveryWithEmptyLog) {
  197. ASSERT_OK(Put("foo", "v1"));
  198. ASSERT_OK(Put("foo", "v2"));
  199. Reopen();
  200. Reopen();
  201. ASSERT_OK(Put("foo", "v3"));
  202. Reopen();
  203. ASSERT_EQ("v3", Get("foo"));
  204. }
  205. static std::string Key(int i) {
  206. char buf[100];
  207. snprintf(buf, sizeof(buf), "key%06d", i);
  208. return std::string(buf);
  209. }
  210. TEST(DBTest, MinorCompactionsHappen) {
  211. Options options;
  212. options.write_buffer_size = 10000;
  213. Reopen(&options);
  214. const int N = 100;
  215. int starting_num_tables = NumTableFilesAtLevel(0);
  216. for (int i = 0; i < N; i++) {
  217. ASSERT_OK(Put(Key(i), Key(i) + std::string(1000, 'v')));
  218. }
  219. int ending_num_tables = NumTableFilesAtLevel(0);
  220. ASSERT_GT(ending_num_tables, starting_num_tables);
  221. for (int i = 0; i < N; i++) {
  222. ASSERT_EQ(Key(i) + std::string(1000, 'v'), Get(Key(i)));
  223. }
  224. Reopen();
  225. for (int i = 0; i < N; i++) {
  226. ASSERT_EQ(Key(i) + std::string(1000, 'v'), Get(Key(i)));
  227. }
  228. }
  229. TEST(DBTest, RecoverWithLargeLog) {
  230. {
  231. Options options;
  232. options.large_value_threshold = 1048576;
  233. Reopen(&options);
  234. ASSERT_OK(Put("big1", std::string(200000, '1')));
  235. ASSERT_OK(Put("big2", std::string(200000, '2')));
  236. ASSERT_OK(Put("small3", std::string(10, '3')));
  237. ASSERT_OK(Put("small4", std::string(10, '4')));
  238. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  239. }
  240. // Make sure that if we re-open with a small write buffer size that
  241. // we flush table files in the middle of a large log file.
  242. Options options;
  243. options.write_buffer_size = 100000;
  244. options.large_value_threshold = 1048576;
  245. Reopen(&options);
  246. ASSERT_EQ(NumTableFilesAtLevel(0), 3);
  247. ASSERT_EQ(std::string(200000, '1'), Get("big1"));
  248. ASSERT_EQ(std::string(200000, '2'), Get("big2"));
  249. ASSERT_EQ(std::string(10, '3'), Get("small3"));
  250. ASSERT_EQ(std::string(10, '4'), Get("small4"));
  251. ASSERT_GT(NumTableFilesAtLevel(0), 1);
  252. }
  253. TEST(DBTest, CompactionsGenerateMultipleFiles) {
  254. Options options;
  255. options.write_buffer_size = 100000000; // Large write buffer
  256. options.large_value_threshold = 1048576;
  257. Reopen(&options);
  258. Random rnd(301);
  259. // Write 8MB (80 values, each 100K)
  260. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  261. std::vector<std::string> values;
  262. for (int i = 0; i < 80; i++) {
  263. values.push_back(RandomString(&rnd, 100000));
  264. ASSERT_OK(Put(Key(i), values[i]));
  265. }
  266. // Reopening moves updates to level-0
  267. Reopen(&options);
  268. dbfull()->TEST_CompactRange(0, "", Key(100000));
  269. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  270. ASSERT_GT(NumTableFilesAtLevel(1), 1);
  271. for (int i = 0; i < 80; i++) {
  272. ASSERT_EQ(Get(Key(i)), values[i]);
  273. }
  274. }
  275. static bool Between(uint64_t val, uint64_t low, uint64_t high) {
  276. bool result = (val >= low) && (val <= high);
  277. if (!result) {
  278. fprintf(stderr, "Value %llu is not in range [%llu, %llu]\n",
  279. (unsigned long long)(val),
  280. (unsigned long long)(low),
  281. (unsigned long long)(high));
  282. }
  283. return result;
  284. }
  285. TEST(DBTest, ApproximateSizes) {
  286. for (int test = 0; test < 2; test++) {
  287. // test==0: default large_value_threshold
  288. // test==1: 1 MB large_value_threshold
  289. Options options;
  290. options.large_value_threshold = (test == 0) ? 65536 : 1048576;
  291. options.write_buffer_size = 100000000; // Large write buffer
  292. options.compression = kNoCompression;
  293. DestroyAndReopen();
  294. ASSERT_TRUE(Between(Size("", "xyz"), 0, 0));
  295. Reopen(&options);
  296. ASSERT_TRUE(Between(Size("", "xyz"), 0, 0));
  297. // Write 8MB (80 values, each 100K)
  298. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  299. const int N = 80;
  300. Random rnd(301);
  301. for (int i = 0; i < N; i++) {
  302. ASSERT_OK(Put(Key(i), RandomString(&rnd, 100000)));
  303. }
  304. if (test == 1) {
  305. // 0 because GetApproximateSizes() does not account for memtable space for
  306. // non-large values
  307. ASSERT_TRUE(Between(Size("", Key(50)), 0, 0));
  308. } else {
  309. ASSERT_TRUE(Between(Size("", Key(50)), 100000*50, 100000*50 + 10000));
  310. ASSERT_TRUE(Between(Size(Key(20), Key(30)),
  311. 100000*10, 100000*10 + 10000));
  312. }
  313. // Check sizes across recovery by reopening a few times
  314. for (int run = 0; run < 3; run++) {
  315. Reopen(&options);
  316. for (int compact_start = 0; compact_start < N; compact_start += 10) {
  317. for (int i = 0; i < N; i += 10) {
  318. ASSERT_TRUE(Between(Size("", Key(i)), 100000*i, 100000*i + 10000));
  319. ASSERT_TRUE(Between(Size("", Key(i)+".suffix"),
  320. 100000 * (i+1), 100000 * (i+1) + 10000));
  321. ASSERT_TRUE(Between(Size(Key(i), Key(i+10)),
  322. 100000 * 10, 100000 * 10 + 10000));
  323. }
  324. ASSERT_TRUE(Between(Size("", Key(50)), 5000000, 5010000));
  325. ASSERT_TRUE(Between(Size("", Key(50)+".suffix"), 5100000, 5110000));
  326. dbfull()->TEST_CompactRange(0,
  327. Key(compact_start),
  328. Key(compact_start + 9));
  329. }
  330. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  331. ASSERT_GT(NumTableFilesAtLevel(1), 0);
  332. }
  333. }
  334. }
  335. TEST(DBTest, ApproximateSizes_MixOfSmallAndLarge) {
  336. Options options;
  337. options.large_value_threshold = 65536;
  338. options.compression = kNoCompression;
  339. Reopen();
  340. Random rnd(301);
  341. std::string big1 = RandomString(&rnd, 100000);
  342. ASSERT_OK(Put(Key(0), RandomString(&rnd, 10000)));
  343. ASSERT_OK(Put(Key(1), RandomString(&rnd, 10000)));
  344. ASSERT_OK(Put(Key(2), big1));
  345. ASSERT_OK(Put(Key(3), RandomString(&rnd, 10000)));
  346. ASSERT_OK(Put(Key(4), big1));
  347. ASSERT_OK(Put(Key(5), RandomString(&rnd, 10000)));
  348. ASSERT_OK(Put(Key(6), RandomString(&rnd, 300000)));
  349. ASSERT_OK(Put(Key(7), RandomString(&rnd, 10000)));
  350. // Check sizes across recovery by reopening a few times
  351. for (int run = 0; run < 3; run++) {
  352. Reopen(&options);
  353. ASSERT_TRUE(Between(Size("", Key(0)), 0, 0));
  354. ASSERT_TRUE(Between(Size("", Key(1)), 10000, 11000));
  355. ASSERT_TRUE(Between(Size("", Key(2)), 20000, 21000));
  356. ASSERT_TRUE(Between(Size("", Key(3)), 120000, 121000));
  357. ASSERT_TRUE(Between(Size("", Key(4)), 130000, 131000));
  358. ASSERT_TRUE(Between(Size("", Key(5)), 230000, 231000));
  359. ASSERT_TRUE(Between(Size("", Key(6)), 240000, 241000));
  360. ASSERT_TRUE(Between(Size("", Key(7)), 540000, 541000));
  361. ASSERT_TRUE(Between(Size("", Key(8)), 550000, 551000));
  362. ASSERT_TRUE(Between(Size(Key(3), Key(5)), 110000, 111000));
  363. dbfull()->TEST_CompactRange(0, Key(0), Key(100));
  364. }
  365. }
  366. TEST(DBTest, IteratorPinsRef) {
  367. Put("foo", "hello");
  368. // Get iterator that will yield the current contents of the DB.
  369. Iterator* iter = db_->NewIterator(ReadOptions());
  370. // Write to force compactions
  371. Put("foo", "newvalue1");
  372. for (int i = 0; i < 100; i++) {
  373. ASSERT_OK(Put(Key(i), Key(i) + std::string(100000, 'v'))); // 100K values
  374. }
  375. Put("foo", "newvalue2");
  376. iter->SeekToFirst();
  377. ASSERT_TRUE(iter->Valid());
  378. ASSERT_EQ("foo", iter->key().ToString());
  379. ASSERT_EQ("hello", iter->value().ToString());
  380. iter->Next();
  381. ASSERT_TRUE(!iter->Valid());
  382. delete iter;
  383. }
  384. TEST(DBTest, Snapshot) {
  385. Put("foo", "v1");
  386. const Snapshot* s1 = db_->GetSnapshot();
  387. Put("foo", "v2");
  388. const Snapshot* s2 = db_->GetSnapshot();
  389. Put("foo", "v3");
  390. const Snapshot* s3 = db_->GetSnapshot();
  391. Put("foo", "v4");
  392. ASSERT_EQ("v1", Get("foo", s1));
  393. ASSERT_EQ("v2", Get("foo", s2));
  394. ASSERT_EQ("v3", Get("foo", s3));
  395. ASSERT_EQ("v4", Get("foo"));
  396. db_->ReleaseSnapshot(s3);
  397. ASSERT_EQ("v1", Get("foo", s1));
  398. ASSERT_EQ("v2", Get("foo", s2));
  399. ASSERT_EQ("v4", Get("foo"));
  400. db_->ReleaseSnapshot(s1);
  401. ASSERT_EQ("v2", Get("foo", s2));
  402. ASSERT_EQ("v4", Get("foo"));
  403. db_->ReleaseSnapshot(s2);
  404. ASSERT_EQ("v4", Get("foo"));
  405. }
  406. TEST(DBTest, HiddenValuesAreRemoved) {
  407. Random rnd(301);
  408. std::string big = RandomString(&rnd, 50000);
  409. Put("foo", big);
  410. Put("pastfoo", "v");
  411. const Snapshot* snapshot = db_->GetSnapshot();
  412. Put("foo", "tiny");
  413. Put("pastfoo2", "v2"); // Advance sequence number one more
  414. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  415. ASSERT_GT(NumTableFilesAtLevel(0), 0);
  416. ASSERT_EQ(big, Get("foo", snapshot));
  417. ASSERT_TRUE(Between(Size("", "pastfoo"), 50000, 60000));
  418. db_->ReleaseSnapshot(snapshot);
  419. ASSERT_EQ(AllEntriesFor("foo"), "[ tiny, " + big + " ]");
  420. dbfull()->TEST_CompactRange(0, "", "x");
  421. ASSERT_EQ(AllEntriesFor("foo"), "[ tiny ]");
  422. ASSERT_EQ(NumTableFilesAtLevel(0), 0);
  423. ASSERT_GE(NumTableFilesAtLevel(1), 1);
  424. dbfull()->TEST_CompactRange(1, "", "x");
  425. ASSERT_EQ(AllEntriesFor("foo"), "[ tiny ]");
  426. ASSERT_TRUE(Between(Size("", "pastfoo"), 0, 1000));
  427. }
  428. TEST(DBTest, DeletionMarkers1) {
  429. Put("foo", "v1");
  430. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  431. dbfull()->TEST_CompactRange(0, "", "z");
  432. dbfull()->TEST_CompactRange(1, "", "z");
  433. ASSERT_EQ(NumTableFilesAtLevel(2), 1); // foo => v1 is now in level 2 file
  434. Delete("foo");
  435. Put("foo", "v2");
  436. ASSERT_EQ(AllEntriesFor("foo"), "[ v2, DEL, v1 ]");
  437. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  438. ASSERT_EQ(AllEntriesFor("foo"), "[ v2, DEL, v1 ]");
  439. dbfull()->TEST_CompactRange(0, "", "z");
  440. // DEL eliminated, but v1 remains because we aren't compacting that level
  441. // (DEL can be eliminated because v2 hides v1).
  442. ASSERT_EQ(AllEntriesFor("foo"), "[ v2, v1 ]");
  443. dbfull()->TEST_CompactRange(1, "", "z");
  444. // Merging L1 w/ L2, so we are the base level for "foo", so DEL is removed.
  445. // (as is v1).
  446. ASSERT_EQ(AllEntriesFor("foo"), "[ v2 ]");
  447. }
  448. TEST(DBTest, DeletionMarkers2) {
  449. Put("foo", "v1");
  450. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  451. dbfull()->TEST_CompactRange(0, "", "z");
  452. dbfull()->TEST_CompactRange(1, "", "z");
  453. ASSERT_EQ(NumTableFilesAtLevel(2), 1); // foo => v1 is now in level 2 file
  454. Delete("foo");
  455. ASSERT_EQ(AllEntriesFor("foo"), "[ DEL, v1 ]");
  456. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  457. ASSERT_EQ(AllEntriesFor("foo"), "[ DEL, v1 ]");
  458. dbfull()->TEST_CompactRange(0, "", "z");
  459. // DEL kept: L2 file overlaps
  460. ASSERT_EQ(AllEntriesFor("foo"), "[ DEL, v1 ]");
  461. dbfull()->TEST_CompactRange(1, "", "z");
  462. // Merging L1 w/ L2, so we are the base level for "foo", so DEL is removed.
  463. // (as is v1).
  464. ASSERT_EQ(AllEntriesFor("foo"), "[ ]");
  465. }
  466. TEST(DBTest, ComparatorCheck) {
  467. class NewComparator : public Comparator {
  468. public:
  469. virtual const char* Name() const { return "leveldb.NewComparator"; }
  470. virtual int Compare(const Slice& a, const Slice& b) const {
  471. return BytewiseComparator()->Compare(a, b);
  472. }
  473. virtual void FindShortestSeparator(std::string* s, const Slice& l) const {
  474. BytewiseComparator()->FindShortestSeparator(s, l);
  475. }
  476. virtual void FindShortSuccessor(std::string* key) const {
  477. BytewiseComparator()->FindShortSuccessor(key);
  478. }
  479. };
  480. NewComparator cmp;
  481. Options new_options;
  482. new_options.comparator = &cmp;
  483. Status s = TryReopen(&new_options);
  484. ASSERT_TRUE(!s.ok());
  485. ASSERT_TRUE(s.ToString().find("comparator") != std::string::npos)
  486. << s.ToString();
  487. }
  488. static bool LargeValuesOK(DBTest* db,
  489. const std::set<LargeValueRef>& expected) {
  490. std::set<LargeValueRef> actual = db->LargeValueFiles();
  491. if (actual.size() != expected.size()) {
  492. fprintf(stderr, "Sets differ in size: %d vs %d\n",
  493. (int)actual.size(), (int)expected.size());
  494. return false;
  495. }
  496. for (std::set<LargeValueRef>::const_iterator it = expected.begin();
  497. it != expected.end();
  498. ++it) {
  499. if (actual.count(*it) != 1) {
  500. fprintf(stderr, " key '%s' not found in actual set\n",
  501. LargeValueRefToFilenameString(*it).c_str());
  502. return false;
  503. }
  504. }
  505. return true;
  506. }
  507. TEST(DBTest, LargeValues1) {
  508. Options options;
  509. options.large_value_threshold = 10000;
  510. Reopen(&options);
  511. Random rnd(301);
  512. std::string big1;
  513. test::CompressibleString(&rnd, 1.0, 100000, &big1); // Not compressible
  514. std::set<LargeValueRef> expected;
  515. ASSERT_OK(Put("big1", big1));
  516. expected.insert(LargeValueRef::Make(big1, kNoCompression));
  517. ASSERT_TRUE(LargeValuesOK(this, expected));
  518. ASSERT_OK(Delete("big1"));
  519. ASSERT_TRUE(LargeValuesOK(this, expected));
  520. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  521. // No handling of deletion markers on memtable compactions, so big1 remains
  522. ASSERT_TRUE(LargeValuesOK(this, expected));
  523. dbfull()->TEST_CompactRange(0, "", "z");
  524. expected.erase(LargeValueRef::Make(big1, kNoCompression));
  525. ASSERT_TRUE(LargeValuesOK(this, expected));
  526. }
  527. TEST(DBTest, LargeValues2) {
  528. Options options;
  529. options.large_value_threshold = 10000;
  530. Reopen(&options);
  531. Random rnd(301);
  532. std::string big1, big2;
  533. test::CompressibleString(&rnd, 1.0, 20000, &big1); // Not compressible
  534. test::CompressibleString(&rnd, 0.6, 40000, &big2); // Compressible
  535. std::set<LargeValueRef> expected;
  536. ASSERT_TRUE(LargeValuesOK(this, expected));
  537. ASSERT_OK(Put("big1", big1));
  538. expected.insert(LargeValueRef::Make(big1, kNoCompression));
  539. ASSERT_EQ(big1, Get("big1"));
  540. ASSERT_TRUE(LargeValuesOK(this, expected));
  541. ASSERT_OK(Put("big2", big2));
  542. ASSERT_EQ(big2, Get("big2"));
  543. #if defined(LEVELDB_PLATFORM_POSIX) || defined(LEVELDB_PLATFORM_CHROMIUM)
  544. // TODO(sanjay) Reenable after compression support is added
  545. expected.insert(LargeValueRef::Make(big2, kNoCompression));
  546. #else
  547. expected.insert(LargeValueRef::Make(big2, kLightweightCompression));
  548. #endif
  549. ASSERT_TRUE(LargeValuesOK(this, expected));
  550. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  551. ASSERT_TRUE(LargeValuesOK(this, expected));
  552. dbfull()->TEST_CompactRange(0, "", "z");
  553. ASSERT_TRUE(LargeValuesOK(this, expected));
  554. ASSERT_OK(Put("big2", big2));
  555. ASSERT_OK(Put("big2_b", big2));
  556. ASSERT_EQ(big1, Get("big1"));
  557. ASSERT_EQ(big2, Get("big2"));
  558. ASSERT_EQ(big2, Get("big2_b"));
  559. ASSERT_TRUE(LargeValuesOK(this, expected));
  560. ASSERT_OK(Delete("big1"));
  561. ASSERT_EQ("NOT_FOUND", Get("big1"));
  562. ASSERT_TRUE(LargeValuesOK(this, expected));
  563. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  564. ASSERT_TRUE(LargeValuesOK(this, expected));
  565. dbfull()->TEST_CompactRange(0, "", "z");
  566. expected.erase(LargeValueRef::Make(big1, kNoCompression));
  567. ASSERT_TRUE(LargeValuesOK(this, expected));
  568. dbfull()->TEST_CompactRange(1, "", "z");
  569. ASSERT_OK(Delete("big2"));
  570. ASSERT_EQ("NOT_FOUND", Get("big2"));
  571. ASSERT_EQ(big2, Get("big2_b"));
  572. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  573. ASSERT_TRUE(LargeValuesOK(this, expected));
  574. dbfull()->TEST_CompactRange(0, "", "z");
  575. ASSERT_TRUE(LargeValuesOK(this, expected));
  576. // Make sure the large value refs survive a reload and compactions after
  577. // the reload.
  578. Reopen();
  579. ASSERT_TRUE(LargeValuesOK(this, expected));
  580. ASSERT_OK(Put("foo", "bar"));
  581. ASSERT_OK(dbfull()->TEST_CompactMemTable());
  582. dbfull()->TEST_CompactRange(0, "", "z");
  583. ASSERT_TRUE(LargeValuesOK(this, expected));
  584. }
  585. TEST(DBTest, LargeValues3) {
  586. // Make sure we don't compress values if
  587. Options options;
  588. options.large_value_threshold = 10000;
  589. options.compression = kNoCompression;
  590. Reopen(&options);
  591. Random rnd(301);
  592. std::string big1 = std::string(100000, 'x'); // Very compressible
  593. std::set<LargeValueRef> expected;
  594. ASSERT_OK(Put("big1", big1));
  595. ASSERT_EQ(big1, Get("big1"));
  596. expected.insert(LargeValueRef::Make(big1, kNoCompression));
  597. ASSERT_TRUE(LargeValuesOK(this, expected));
  598. }
  599. TEST(DBTest, DBOpen_Options) {
  600. std::string dbname = test::TmpDir() + "/db_options_test";
  601. DestroyDB(dbname, Options());
  602. // Does not exist, and create_if_missing == false: error
  603. DB* db = NULL;
  604. Options opts;
  605. opts.create_if_missing = false;
  606. Status s = DB::Open(opts, dbname, &db);
  607. ASSERT_TRUE(strstr(s.ToString().c_str(), "does not exist") != NULL);
  608. ASSERT_TRUE(db == NULL);
  609. // Does not exist, and create_if_missing == true: OK
  610. opts.create_if_missing = true;
  611. s = DB::Open(opts, dbname, &db);
  612. ASSERT_OK(s);
  613. ASSERT_TRUE(db != NULL);
  614. delete db;
  615. db = NULL;
  616. // Does exist, and error_if_exists == true: error
  617. opts.create_if_missing = false;
  618. opts.error_if_exists = true;
  619. s = DB::Open(opts, dbname, &db);
  620. ASSERT_TRUE(strstr(s.ToString().c_str(), "exists") != NULL);
  621. ASSERT_TRUE(db == NULL);
  622. // Does exist, and error_if_exists == false: OK
  623. opts.create_if_missing = true;
  624. opts.error_if_exists = false;
  625. s = DB::Open(opts, dbname, &db);
  626. ASSERT_OK(s);
  627. ASSERT_TRUE(db != NULL);
  628. delete db;
  629. db = NULL;
  630. }
  631. class ModelDB: public DB {
  632. public:
  633. explicit ModelDB(const Options& options): options_(options) { }
  634. ~ModelDB() { }
  635. virtual Status Put(const WriteOptions& o, const Slice& k, const Slice& v) {
  636. return DB::Put(o, k, v);
  637. }
  638. virtual Status Delete(const WriteOptions& o, const Slice& key) {
  639. return DB::Delete(o, key);
  640. }
  641. virtual Status Get(const ReadOptions& options,
  642. const Slice& key, std::string* value) {
  643. assert(false); // Not implemented
  644. return Status::NotFound(key);
  645. }
  646. virtual Iterator* NewIterator(const ReadOptions& options) {
  647. if (options.snapshot == NULL) {
  648. KVMap* saved = new KVMap;
  649. *saved = map_;
  650. return new ModelIter(saved, true);
  651. } else {
  652. const KVMap* snapshot_state =
  653. reinterpret_cast<const KVMap*>(options.snapshot->number_);
  654. return new ModelIter(snapshot_state, false);
  655. }
  656. }
  657. virtual const Snapshot* GetSnapshot() {
  658. KVMap* saved = new KVMap;
  659. *saved = map_;
  660. return snapshots_.New(
  661. reinterpret_cast<SequenceNumber>(saved));
  662. }
  663. virtual void ReleaseSnapshot(const Snapshot* snapshot) {
  664. const KVMap* saved = reinterpret_cast<const KVMap*>(snapshot->number_);
  665. delete saved;
  666. snapshots_.Delete(snapshot);
  667. }
  668. virtual Status Write(const WriteOptions& options, WriteBatch* batch) {
  669. assert(options.post_write_snapshot == NULL); // Not supported
  670. for (WriteBatchInternal::Iterator it(*batch); !it.Done(); it.Next()) {
  671. switch (it.op()) {
  672. case kTypeValue:
  673. map_[it.key().ToString()] = it.value().ToString();
  674. break;
  675. case kTypeLargeValueRef:
  676. assert(false); // Should not occur
  677. break;
  678. case kTypeDeletion:
  679. map_.erase(it.key().ToString());
  680. break;
  681. }
  682. }
  683. return Status::OK();
  684. }
  685. virtual bool GetProperty(const Slice& property, uint64_t* value) {
  686. return false;
  687. }
  688. virtual void GetApproximateSizes(const Range* r, int n, uint64_t* sizes) {
  689. for (int i = 0; i < n; i++) {
  690. sizes[i] = 0;
  691. }
  692. }
  693. private:
  694. typedef std::map<std::string, std::string> KVMap;
  695. class ModelIter: public Iterator {
  696. public:
  697. ModelIter(const KVMap* map, bool owned)
  698. : map_(map), owned_(owned), iter_(map_->end()) {
  699. }
  700. ~ModelIter() {
  701. if (owned_) delete map_;
  702. }
  703. virtual bool Valid() const { return iter_ != map_->end(); }
  704. virtual void SeekToFirst() { iter_ = map_->begin(); }
  705. virtual void SeekToLast() {
  706. if (map_->empty()) {
  707. iter_ = map_->end();
  708. } else {
  709. iter_ = map_->find(map_->rbegin()->first);
  710. }
  711. }
  712. virtual void Seek(const Slice& k) {
  713. iter_ = map_->lower_bound(k.ToString());
  714. }
  715. virtual void Next() { ++iter_; }
  716. virtual void Prev() { --iter_; }
  717. virtual Slice key() const { return iter_->first; }
  718. virtual Slice value() const { return iter_->second; }
  719. virtual Status status() const { return Status::OK(); }
  720. private:
  721. const KVMap* const map_;
  722. const bool owned_; // Do we own map_
  723. KVMap::const_iterator iter_;
  724. };
  725. const Options options_;
  726. KVMap map_;
  727. SnapshotList snapshots_;
  728. };
  729. static std::string RandomKey(Random* rnd) {
  730. int len = (rnd->OneIn(3)
  731. ? 1 // Short sometimes to encourage collisions
  732. : (rnd->OneIn(100) ? rnd->Skewed(10) : rnd->Uniform(10)));
  733. return test::RandomKey(rnd, len);
  734. }
  735. static bool CompareIterators(int step,
  736. DB* model,
  737. DB* db,
  738. const Snapshot* model_snap,
  739. const Snapshot* db_snap) {
  740. ReadOptions options;
  741. options.snapshot = model_snap;
  742. Iterator* miter = model->NewIterator(options);
  743. options.snapshot = db_snap;
  744. Iterator* dbiter = db->NewIterator(options);
  745. bool ok = true;
  746. int count = 0;
  747. for (miter->SeekToFirst(), dbiter->SeekToFirst();
  748. ok && miter->Valid() && dbiter->Valid();
  749. miter->Next(), dbiter->Next()) {
  750. count++;
  751. if (miter->key().compare(dbiter->key()) != 0) {
  752. fprintf(stderr, "step %d: Key mismatch: '%s' vs. '%s'\n",
  753. step,
  754. EscapeString(miter->key()).c_str(),
  755. EscapeString(dbiter->key()).c_str());
  756. ok = false;
  757. break;
  758. }
  759. if (miter->value().compare(dbiter->value()) != 0) {
  760. fprintf(stderr, "step %d: Value mismatch for key '%s': '%s' vs. '%s'\n",
  761. step,
  762. EscapeString(miter->key()).c_str(),
  763. EscapeString(miter->value()).c_str(),
  764. EscapeString(miter->value()).c_str());
  765. ok = false;
  766. }
  767. }
  768. if (ok) {
  769. if (miter->Valid() != dbiter->Valid()) {
  770. fprintf(stderr, "step %d: Mismatch at end of iterators: %d vs. %d\n",
  771. step, miter->Valid(), dbiter->Valid());
  772. ok = false;
  773. }
  774. }
  775. fprintf(stderr, "%d entries compared: ok=%d\n", count, ok);
  776. delete miter;
  777. delete dbiter;
  778. return ok;
  779. }
  780. TEST(DBTest, Randomized) {
  781. Random rnd(test::RandomSeed());
  782. ModelDB model(last_options_);
  783. const int N = 10000;
  784. const Snapshot* model_snap = NULL;
  785. const Snapshot* db_snap = NULL;
  786. std::string k, v;
  787. for (int step = 0; step < N; step++) {
  788. if (step % 100 == 0) {
  789. fprintf(stderr, "Step %d of %d\n", step, N);
  790. }
  791. int p = rnd.Uniform(100);
  792. if (p < 45) { // Put
  793. k = RandomKey(&rnd);
  794. v = RandomString(&rnd,
  795. rnd.OneIn(20)
  796. ? 100 + rnd.Uniform(100)
  797. : rnd.Uniform(8));
  798. ASSERT_OK(model.Put(WriteOptions(), k, v));
  799. ASSERT_OK(db_->Put(WriteOptions(), k, v));
  800. } else if (p < 90) { // Delete
  801. k = RandomKey(&rnd);
  802. ASSERT_OK(model.Delete(WriteOptions(), k));
  803. ASSERT_OK(db_->Delete(WriteOptions(), k));
  804. } else { // Multi-element batch
  805. WriteBatch b;
  806. const int num = rnd.Uniform(8);
  807. for (int i = 0; i < num; i++) {
  808. if (i == 0 || !rnd.OneIn(10)) {
  809. k = RandomKey(&rnd);
  810. } else {
  811. // Periodically re-use the same key from the previous iter, so
  812. // we have multiple entries in the write batch for the same key
  813. }
  814. if (rnd.OneIn(2)) {
  815. v = RandomString(&rnd, rnd.Uniform(10));
  816. b.Put(k, v);
  817. } else {
  818. b.Delete(k);
  819. }
  820. }
  821. ASSERT_OK(model.Write(WriteOptions(), &b));
  822. ASSERT_OK(db_->Write(WriteOptions(), &b));
  823. }
  824. if ((step % 100) == 0) {
  825. ASSERT_TRUE(CompareIterators(step, &model, db_, NULL, NULL));
  826. ASSERT_TRUE(CompareIterators(step, &model, db_, model_snap, db_snap));
  827. // Save a snapshot from each DB this time that we'll use next
  828. // time we compare things, to make sure the current state is
  829. // preserved with the snapshot
  830. if (model_snap != NULL) model.ReleaseSnapshot(model_snap);
  831. if (db_snap != NULL) db_->ReleaseSnapshot(db_snap);
  832. Reopen();
  833. ASSERT_TRUE(CompareIterators(step, &model, db_, NULL, NULL));
  834. model_snap = model.GetSnapshot();
  835. db_snap = db_->GetSnapshot();
  836. }
  837. }
  838. if (model_snap != NULL) model.ReleaseSnapshot(model_snap);
  839. if (db_snap != NULL) db_->ReleaseSnapshot(db_snap);
  840. }
  841. }
  842. int main(int argc, char** argv) {
  843. return leveldb::test::RunAllTests();
  844. }