作者: 韩晨旭 10225101440 李畅 10225102463
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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