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