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.

259 lines
8.2 KiB

пре 3 недеља
  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 "helpers/memenv/memenv.h"
  5. #include <string>
  6. #include <vector>
  7. #include "gtest/gtest.h"
  8. #include "db/db_impl.h"
  9. #include "leveldb/db.h"
  10. #include "leveldb/env.h"
  11. #include "util/testutil.h"
  12. namespace leveldb {
  13. class MemEnvTest : public testing::Test {
  14. public:
  15. MemEnvTest() : env_(NewMemEnv(Env::Default())) {}
  16. ~MemEnvTest() { delete env_; }
  17. Env* env_;
  18. };
  19. TEST_F(MemEnvTest, Basics) {
  20. uint64_t file_size;
  21. WritableFile* writable_file;
  22. std::vector<std::string> children;
  23. ASSERT_LEVELDB_OK(env_->CreateDir("/dir"));
  24. // Check that the directory is empty.
  25. ASSERT_TRUE(!env_->FileExists("/dir/non_existent"));
  26. ASSERT_TRUE(!env_->GetFileSize("/dir/non_existent", &file_size).ok());
  27. ASSERT_LEVELDB_OK(env_->GetChildren("/dir", &children));
  28. ASSERT_EQ(0, children.size());
  29. // Create a file.
  30. ASSERT_LEVELDB_OK(env_->NewWritableFile("/dir/f", &writable_file));
  31. ASSERT_LEVELDB_OK(env_->GetFileSize("/dir/f", &file_size));
  32. ASSERT_EQ(0, file_size);
  33. delete writable_file;
  34. // Check that the file exists.
  35. ASSERT_TRUE(env_->FileExists("/dir/f"));
  36. ASSERT_LEVELDB_OK(env_->GetFileSize("/dir/f", &file_size));
  37. ASSERT_EQ(0, file_size);
  38. ASSERT_LEVELDB_OK(env_->GetChildren("/dir", &children));
  39. ASSERT_EQ(1, children.size());
  40. ASSERT_EQ("f", children[0]);
  41. // Write to the file.
  42. ASSERT_LEVELDB_OK(env_->NewWritableFile("/dir/f", &writable_file));
  43. ASSERT_LEVELDB_OK(writable_file->Append("abc"));
  44. delete writable_file;
  45. // Check that append works.
  46. ASSERT_LEVELDB_OK(env_->NewAppendableFile("/dir/f", &writable_file));
  47. ASSERT_LEVELDB_OK(env_->GetFileSize("/dir/f", &file_size));
  48. ASSERT_EQ(3, file_size);
  49. ASSERT_LEVELDB_OK(writable_file->Append("hello"));
  50. delete writable_file;
  51. // Check for expected size.
  52. ASSERT_LEVELDB_OK(env_->GetFileSize("/dir/f", &file_size));
  53. ASSERT_EQ(8, file_size);
  54. // Check that renaming works.
  55. ASSERT_TRUE(!env_->RenameFile("/dir/non_existent", "/dir/g").ok());
  56. ASSERT_LEVELDB_OK(env_->RenameFile("/dir/f", "/dir/g"));
  57. ASSERT_TRUE(!env_->FileExists("/dir/f"));
  58. ASSERT_TRUE(env_->FileExists("/dir/g"));
  59. ASSERT_LEVELDB_OK(env_->GetFileSize("/dir/g", &file_size));
  60. ASSERT_EQ(8, file_size);
  61. // Check that opening non-existent file fails.
  62. SequentialFile* seq_file;
  63. RandomAccessFile* rand_file;
  64. ASSERT_TRUE(!env_->NewSequentialFile("/dir/non_existent", &seq_file).ok());
  65. ASSERT_TRUE(!seq_file);
  66. ASSERT_TRUE(!env_->NewRandomAccessFile("/dir/non_existent", &rand_file).ok());
  67. ASSERT_TRUE(!rand_file);
  68. // Check that deleting works.
  69. ASSERT_TRUE(!env_->RemoveFile("/dir/non_existent").ok());
  70. ASSERT_LEVELDB_OK(env_->RemoveFile("/dir/g"));
  71. ASSERT_TRUE(!env_->FileExists("/dir/g"));
  72. ASSERT_LEVELDB_OK(env_->GetChildren("/dir", &children));
  73. ASSERT_EQ(0, children.size());
  74. ASSERT_LEVELDB_OK(env_->RemoveDir("/dir"));
  75. }
  76. TEST_F(MemEnvTest, ReadWrite) {
  77. WritableFile* writable_file;
  78. SequentialFile* seq_file;
  79. RandomAccessFile* rand_file;
  80. Slice result;
  81. char scratch[100];
  82. ASSERT_LEVELDB_OK(env_->CreateDir("/dir"));
  83. ASSERT_LEVELDB_OK(env_->NewWritableFile("/dir/f", &writable_file));
  84. ASSERT_LEVELDB_OK(writable_file->Append("hello "));
  85. ASSERT_LEVELDB_OK(writable_file->Append("world"));
  86. delete writable_file;
  87. // Read sequentially.
  88. ASSERT_LEVELDB_OK(env_->NewSequentialFile("/dir/f", &seq_file));
  89. ASSERT_LEVELDB_OK(seq_file->Read(5, &result, scratch)); // Read "hello".
  90. ASSERT_EQ(0, result.compare("hello"));
  91. ASSERT_LEVELDB_OK(seq_file->Skip(1));
  92. ASSERT_LEVELDB_OK(seq_file->Read(1000, &result, scratch)); // Read "world".
  93. ASSERT_EQ(0, result.compare("world"));
  94. ASSERT_LEVELDB_OK(
  95. seq_file->Read(1000, &result, scratch)); // Try reading past EOF.
  96. ASSERT_EQ(0, result.size());
  97. ASSERT_LEVELDB_OK(seq_file->Skip(100)); // Try to skip past end of file.
  98. ASSERT_LEVELDB_OK(seq_file->Read(1000, &result, scratch));
  99. ASSERT_EQ(0, result.size());
  100. delete seq_file;
  101. // Random reads.
  102. ASSERT_LEVELDB_OK(env_->NewRandomAccessFile("/dir/f", &rand_file));
  103. ASSERT_LEVELDB_OK(rand_file->Read(6, 5, &result, scratch)); // Read "world".
  104. ASSERT_EQ(0, result.compare("world"));
  105. ASSERT_LEVELDB_OK(rand_file->Read(0, 5, &result, scratch)); // Read "hello".
  106. ASSERT_EQ(0, result.compare("hello"));
  107. ASSERT_LEVELDB_OK(rand_file->Read(10, 100, &result, scratch)); // Read "d".
  108. ASSERT_EQ(0, result.compare("d"));
  109. // Too high offset.
  110. ASSERT_TRUE(!rand_file->Read(1000, 5, &result, scratch).ok());
  111. delete rand_file;
  112. }
  113. TEST_F(MemEnvTest, Locks) {
  114. FileLock* lock;
  115. // These are no-ops, but we test they return success.
  116. ASSERT_LEVELDB_OK(env_->LockFile("some file", &lock));
  117. ASSERT_LEVELDB_OK(env_->UnlockFile(lock));
  118. }
  119. TEST_F(MemEnvTest, Misc) {
  120. std::string test_dir;
  121. ASSERT_LEVELDB_OK(env_->GetTestDirectory(&test_dir));
  122. ASSERT_TRUE(!test_dir.empty());
  123. WritableFile* writable_file;
  124. ASSERT_LEVELDB_OK(env_->NewWritableFile("/a/b", &writable_file));
  125. // These are no-ops, but we test they return success.
  126. ASSERT_LEVELDB_OK(writable_file->Sync());
  127. ASSERT_LEVELDB_OK(writable_file->Flush());
  128. ASSERT_LEVELDB_OK(writable_file->Close());
  129. delete writable_file;
  130. }
  131. TEST_F(MemEnvTest, LargeWrite) {
  132. const size_t kWriteSize = 300 * 1024;
  133. char* scratch = new char[kWriteSize * 2];
  134. std::string write_data;
  135. for (size_t i = 0; i < kWriteSize; ++i) {
  136. write_data.append(1, static_cast<char>(i));
  137. }
  138. WritableFile* writable_file;
  139. ASSERT_LEVELDB_OK(env_->NewWritableFile("/dir/f", &writable_file));
  140. ASSERT_LEVELDB_OK(writable_file->Append("foo"));
  141. ASSERT_LEVELDB_OK(writable_file->Append(write_data));
  142. delete writable_file;
  143. SequentialFile* seq_file;
  144. Slice result;
  145. ASSERT_LEVELDB_OK(env_->NewSequentialFile("/dir/f", &seq_file));
  146. ASSERT_LEVELDB_OK(seq_file->Read(3, &result, scratch)); // Read "foo".
  147. ASSERT_EQ(0, result.compare("foo"));
  148. size_t read = 0;
  149. std::string read_data;
  150. while (read < kWriteSize) {
  151. ASSERT_LEVELDB_OK(seq_file->Read(kWriteSize - read, &result, scratch));
  152. read_data.append(result.data(), result.size());
  153. read += result.size();
  154. }
  155. ASSERT_TRUE(write_data == read_data);
  156. delete seq_file;
  157. delete[] scratch;
  158. }
  159. TEST_F(MemEnvTest, OverwriteOpenFile) {
  160. const char kWrite1Data[] = "Write #1 data";
  161. const size_t kFileDataLen = sizeof(kWrite1Data) - 1;
  162. const std::string kTestFileName = testing::TempDir() + "leveldb-TestFile.dat";
  163. ASSERT_LEVELDB_OK(WriteStringToFile(env_, kWrite1Data, kTestFileName));
  164. RandomAccessFile* rand_file;
  165. ASSERT_LEVELDB_OK(env_->NewRandomAccessFile(kTestFileName, &rand_file));
  166. const char kWrite2Data[] = "Write #2 data";
  167. ASSERT_LEVELDB_OK(WriteStringToFile(env_, kWrite2Data, kTestFileName));
  168. // Verify that overwriting an open file will result in the new file data
  169. // being read from files opened before the write.
  170. Slice result;
  171. char scratch[kFileDataLen];
  172. ASSERT_LEVELDB_OK(rand_file->Read(0, kFileDataLen, &result, scratch));
  173. ASSERT_EQ(0, result.compare(kWrite2Data));
  174. delete rand_file;
  175. }
  176. TEST_F(MemEnvTest, DBTest) {
  177. Options options;
  178. options.create_if_missing = true;
  179. options.env = env_;
  180. DB* db;
  181. const Slice keys[] = {Slice("aaa"), Slice("bbb"), Slice("ccc")};
  182. const Slice vals[] = {Slice("foo"), Slice("bar"), Slice("baz")};
  183. ASSERT_LEVELDB_OK(DB::Open(options, "/dir/db", &db));
  184. for (size_t i = 0; i < 3; ++i) {
  185. ASSERT_LEVELDB_OK(db->Put(WriteOptions(), keys[i], vals[i]));
  186. }
  187. for (size_t i = 0; i < 3; ++i) {
  188. std::string res;
  189. ASSERT_LEVELDB_OK(db->Get(ReadOptions(), keys[i], &res));
  190. ASSERT_TRUE(res == vals[i]);
  191. }
  192. Iterator* iterator = db->NewIterator(ReadOptions());
  193. iterator->SeekToFirst();
  194. for (size_t i = 0; i < 3; ++i) {
  195. ASSERT_TRUE(iterator->Valid());
  196. ASSERT_TRUE(keys[i] == iterator->key());
  197. ASSERT_TRUE(vals[i] == iterator->value());
  198. iterator->Next();
  199. }
  200. ASSERT_TRUE(!iterator->Valid());
  201. delete iterator;
  202. DBImpl* dbi = reinterpret_cast<DBImpl*>(db);
  203. ASSERT_LEVELDB_OK(dbi->TEST_CompactMemTable());
  204. for (size_t i = 0; i < 3; ++i) {
  205. std::string res;
  206. ASSERT_LEVELDB_OK(db->Get(ReadOptions(), keys[i], &res));
  207. ASSERT_TRUE(res == vals[i]);
  208. }
  209. delete db;
  210. }
  211. } // namespace leveldb