memenv_test.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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, OverwriteOpenFile) {
  162. const char kWrite1Data[] = "Write #1 data";
  163. const size_t kFileDataLen = sizeof(kWrite1Data) - 1;
  164. const std::string kTestFileName = test::TmpDir() + "/leveldb-TestFile.dat";
  165. ASSERT_OK(WriteStringToFile(env_, kWrite1Data, kTestFileName));
  166. RandomAccessFile* rand_file;
  167. ASSERT_OK(env_->NewRandomAccessFile(kTestFileName, &rand_file));
  168. const char kWrite2Data[] = "Write #2 data";
  169. ASSERT_OK(WriteStringToFile(env_, kWrite2Data, kTestFileName));
  170. // Verify that overwriting an open file will result in the new file data
  171. // being read from files opened before the write.
  172. Slice result;
  173. char scratch[kFileDataLen];
  174. ASSERT_OK(rand_file->Read(0, kFileDataLen, &result, scratch));
  175. ASSERT_EQ(0, result.compare(kWrite2Data));
  176. delete rand_file;
  177. }
  178. TEST(MemEnvTest, DBTest) {
  179. Options options;
  180. options.create_if_missing = true;
  181. options.env = env_;
  182. DB* db;
  183. const Slice keys[] = {Slice("aaa"), Slice("bbb"), Slice("ccc")};
  184. const Slice vals[] = {Slice("foo"), Slice("bar"), Slice("baz")};
  185. ASSERT_OK(DB::Open(options, "/dir/db", &db));
  186. for (size_t i = 0; i < 3; ++i) {
  187. ASSERT_OK(db->Put(WriteOptions(), keys[i], vals[i]));
  188. }
  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. Iterator* iterator = db->NewIterator(ReadOptions());
  195. iterator->SeekToFirst();
  196. for (size_t i = 0; i < 3; ++i) {
  197. ASSERT_TRUE(iterator->Valid());
  198. ASSERT_TRUE(keys[i] == iterator->key());
  199. ASSERT_TRUE(vals[i] == iterator->value());
  200. iterator->Next();
  201. }
  202. ASSERT_TRUE(!iterator->Valid());
  203. delete iterator;
  204. DBImpl* dbi = reinterpret_cast<DBImpl*>(db);
  205. ASSERT_OK(dbi->TEST_CompactMemTable());
  206. for (size_t i = 0; i < 3; ++i) {
  207. std::string res;
  208. ASSERT_OK(db->Get(ReadOptions(), keys[i], &res));
  209. ASSERT_TRUE(res == vals[i]);
  210. }
  211. delete db;
  212. }
  213. } // namespace leveldb
  214. int main(int argc, char** argv) {
  215. return leveldb::test::RunAllTests();
  216. }