2011-09-12 17:21:10 +08:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#include "helpers/memenv/memenv.h"
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-11-26 01:29:06 +08:00
|
|
|
#include "gtest/gtest.h"
|
2011-09-12 17:21:10 +08:00
|
|
|
#include "db/db_impl.h"
|
|
|
|
#include "leveldb/db.h"
|
|
|
|
#include "leveldb/env.h"
|
2019-11-22 05:09:53 +08:00
|
|
|
#include "util/testutil.h"
|
2011-09-12 17:21:10 +08:00
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
class MemEnvTest : public testing::Test {
|
2011-09-12 17:21:10 +08:00
|
|
|
public:
|
2019-05-03 02:01:00 +08:00
|
|
|
MemEnvTest() : env_(NewMemEnv(Env::Default())) {}
|
|
|
|
~MemEnvTest() { delete env_; }
|
2019-05-04 00:31:18 +08:00
|
|
|
|
|
|
|
Env* env_;
|
2011-09-12 17:21:10 +08:00
|
|
|
};
|
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
TEST_F(MemEnvTest, Basics) {
|
2011-09-27 00:37:09 +08:00
|
|
|
uint64_t file_size;
|
2011-09-12 17:21:10 +08:00
|
|
|
WritableFile* writable_file;
|
|
|
|
std::vector<std::string> children;
|
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->CreateDir("/dir"));
|
2011-09-12 17:21:10 +08:00
|
|
|
|
|
|
|
// Check that the directory is empty.
|
|
|
|
ASSERT_TRUE(!env_->FileExists("/dir/non_existent"));
|
|
|
|
ASSERT_TRUE(!env_->GetFileSize("/dir/non_existent", &file_size).ok());
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->GetChildren("/dir", &children));
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_EQ(0, children.size());
|
|
|
|
|
|
|
|
// Create a file.
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->NewWritableFile("/dir/f", &writable_file));
|
|
|
|
ASSERT_LEVELDB_OK(env_->GetFileSize("/dir/f", &file_size));
|
2014-12-12 00:13:18 +08:00
|
|
|
ASSERT_EQ(0, file_size);
|
2011-09-12 17:21:10 +08:00
|
|
|
delete writable_file;
|
|
|
|
|
|
|
|
// Check that the file exists.
|
|
|
|
ASSERT_TRUE(env_->FileExists("/dir/f"));
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->GetFileSize("/dir/f", &file_size));
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_EQ(0, file_size);
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->GetChildren("/dir", &children));
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_EQ(1, children.size());
|
|
|
|
ASSERT_EQ("f", children[0]);
|
|
|
|
|
|
|
|
// Write to the file.
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->NewWritableFile("/dir/f", &writable_file));
|
|
|
|
ASSERT_LEVELDB_OK(writable_file->Append("abc"));
|
2011-09-12 17:21:10 +08:00
|
|
|
delete writable_file;
|
|
|
|
|
2014-12-12 00:13:18 +08:00
|
|
|
// Check that append works.
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->NewAppendableFile("/dir/f", &writable_file));
|
|
|
|
ASSERT_LEVELDB_OK(env_->GetFileSize("/dir/f", &file_size));
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_EQ(3, file_size);
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(writable_file->Append("hello"));
|
2014-12-12 00:13:18 +08:00
|
|
|
delete writable_file;
|
|
|
|
|
|
|
|
// Check for expected size.
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->GetFileSize("/dir/f", &file_size));
|
2014-12-12 00:13:18 +08:00
|
|
|
ASSERT_EQ(8, file_size);
|
2011-09-12 17:21:10 +08:00
|
|
|
|
|
|
|
// Check that renaming works.
|
|
|
|
ASSERT_TRUE(!env_->RenameFile("/dir/non_existent", "/dir/g").ok());
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->RenameFile("/dir/f", "/dir/g"));
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_TRUE(!env_->FileExists("/dir/f"));
|
|
|
|
ASSERT_TRUE(env_->FileExists("/dir/g"));
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->GetFileSize("/dir/g", &file_size));
|
2014-12-12 00:13:18 +08:00
|
|
|
ASSERT_EQ(8, file_size);
|
2011-09-12 17:21:10 +08:00
|
|
|
|
|
|
|
// Check that opening non-existent file fails.
|
|
|
|
SequentialFile* seq_file;
|
|
|
|
RandomAccessFile* rand_file;
|
|
|
|
ASSERT_TRUE(!env_->NewSequentialFile("/dir/non_existent", &seq_file).ok());
|
|
|
|
ASSERT_TRUE(!seq_file);
|
|
|
|
ASSERT_TRUE(!env_->NewRandomAccessFile("/dir/non_existent", &rand_file).ok());
|
|
|
|
ASSERT_TRUE(!rand_file);
|
|
|
|
|
|
|
|
// Check that deleting works.
|
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}.
The "DeleteFile" method name causes pain for Windows developers, because
<windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA.
Current code uses workarounds, like #undefining DeleteFile everywhere an
Env is declared, implemented, or used.
This CL removes the need for workarounds by renaming Env::DeleteFile to
Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to
Env::RemoveDir. A few internal methods are also renamed for consistency.
Software that supports Windows is expected to migrate any Env
implementations and usage to Remove{File,Dir}, and never use the name
Env::Delete{File,Dir} in its code.
The renaming is done in a backwards-compatible way, at the risk of
making it slightly more difficult to build a new correct Env
implementation. The backwards compatibility is achieved using the
following hacks:
1) Env::Remove{File,Dir} methods are added, with a default
implementation that calls into Env::Delete{File,Dir}. This makes old
Env implementations compatible with code that calls into the updated
API.
2) The Env::Delete{File,Dir} methods are no longer pure virtuals.
Instead, they gain a default implementation that calls into
Env::Remove{File,Dir}. This makes updated Env implementations
compatible with code that calls into the old API.
The cost of this approach is that it's possible to write an Env without
overriding either Rename{File,Dir} or Delete{File,Dir}, without getting
a compiler warning. However, attempting to run the test suite will
immediately fail with an infinite call stack ending in
{Remove,Delete}{File,Dir}, making developers aware of the problem.
PiperOrigin-RevId: 288710907
2020-01-09 01:14:53 +08:00
|
|
|
ASSERT_TRUE(!env_->RemoveFile("/dir/non_existent").ok());
|
|
|
|
ASSERT_LEVELDB_OK(env_->RemoveFile("/dir/g"));
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_TRUE(!env_->FileExists("/dir/g"));
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->GetChildren("/dir", &children));
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_EQ(0, children.size());
|
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}.
The "DeleteFile" method name causes pain for Windows developers, because
<windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA.
Current code uses workarounds, like #undefining DeleteFile everywhere an
Env is declared, implemented, or used.
This CL removes the need for workarounds by renaming Env::DeleteFile to
Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to
Env::RemoveDir. A few internal methods are also renamed for consistency.
Software that supports Windows is expected to migrate any Env
implementations and usage to Remove{File,Dir}, and never use the name
Env::Delete{File,Dir} in its code.
The renaming is done in a backwards-compatible way, at the risk of
making it slightly more difficult to build a new correct Env
implementation. The backwards compatibility is achieved using the
following hacks:
1) Env::Remove{File,Dir} methods are added, with a default
implementation that calls into Env::Delete{File,Dir}. This makes old
Env implementations compatible with code that calls into the updated
API.
2) The Env::Delete{File,Dir} methods are no longer pure virtuals.
Instead, they gain a default implementation that calls into
Env::Remove{File,Dir}. This makes updated Env implementations
compatible with code that calls into the old API.
The cost of this approach is that it's possible to write an Env without
overriding either Rename{File,Dir} or Delete{File,Dir}, without getting
a compiler warning. However, attempting to run the test suite will
immediately fail with an infinite call stack ending in
{Remove,Delete}{File,Dir}, making developers aware of the problem.
PiperOrigin-RevId: 288710907
2020-01-09 01:14:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->RemoveDir("/dir"));
|
2011-09-12 17:21:10 +08:00
|
|
|
}
|
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
TEST_F(MemEnvTest, ReadWrite) {
|
2011-09-12 17:21:10 +08:00
|
|
|
WritableFile* writable_file;
|
|
|
|
SequentialFile* seq_file;
|
|
|
|
RandomAccessFile* rand_file;
|
|
|
|
Slice result;
|
|
|
|
char scratch[100];
|
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->CreateDir("/dir"));
|
2011-09-12 17:21:10 +08:00
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->NewWritableFile("/dir/f", &writable_file));
|
|
|
|
ASSERT_LEVELDB_OK(writable_file->Append("hello "));
|
|
|
|
ASSERT_LEVELDB_OK(writable_file->Append("world"));
|
2011-09-12 17:21:10 +08:00
|
|
|
delete writable_file;
|
|
|
|
|
|
|
|
// Read sequentially.
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->NewSequentialFile("/dir/f", &seq_file));
|
|
|
|
ASSERT_LEVELDB_OK(seq_file->Read(5, &result, scratch)); // Read "hello".
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_EQ(0, result.compare("hello"));
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(seq_file->Skip(1));
|
|
|
|
ASSERT_LEVELDB_OK(seq_file->Read(1000, &result, scratch)); // Read "world".
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_EQ(0, result.compare("world"));
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(
|
|
|
|
seq_file->Read(1000, &result, scratch)); // Try reading past EOF.
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_EQ(0, result.size());
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(seq_file->Skip(100)); // Try to skip past end of file.
|
|
|
|
ASSERT_LEVELDB_OK(seq_file->Read(1000, &result, scratch));
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_EQ(0, result.size());
|
|
|
|
delete seq_file;
|
|
|
|
|
|
|
|
// Random reads.
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->NewRandomAccessFile("/dir/f", &rand_file));
|
|
|
|
ASSERT_LEVELDB_OK(rand_file->Read(6, 5, &result, scratch)); // Read "world".
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_EQ(0, result.compare("world"));
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(rand_file->Read(0, 5, &result, scratch)); // Read "hello".
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_EQ(0, result.compare("hello"));
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(rand_file->Read(10, 100, &result, scratch)); // Read "d".
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_EQ(0, result.compare("d"));
|
|
|
|
|
|
|
|
// Too high offset.
|
|
|
|
ASSERT_TRUE(!rand_file->Read(1000, 5, &result, scratch).ok());
|
|
|
|
delete rand_file;
|
|
|
|
}
|
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
TEST_F(MemEnvTest, Locks) {
|
2011-09-12 17:21:10 +08:00
|
|
|
FileLock* lock;
|
|
|
|
|
|
|
|
// These are no-ops, but we test they return success.
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->LockFile("some file", &lock));
|
|
|
|
ASSERT_LEVELDB_OK(env_->UnlockFile(lock));
|
2011-09-12 17:21:10 +08:00
|
|
|
}
|
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
TEST_F(MemEnvTest, Misc) {
|
2011-09-12 17:21:10 +08:00
|
|
|
std::string test_dir;
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->GetTestDirectory(&test_dir));
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_TRUE(!test_dir.empty());
|
|
|
|
|
|
|
|
WritableFile* writable_file;
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->NewWritableFile("/a/b", &writable_file));
|
2011-09-12 17:21:10 +08:00
|
|
|
|
|
|
|
// These are no-ops, but we test they return success.
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(writable_file->Sync());
|
|
|
|
ASSERT_LEVELDB_OK(writable_file->Flush());
|
|
|
|
ASSERT_LEVELDB_OK(writable_file->Close());
|
2011-09-12 17:21:10 +08:00
|
|
|
delete writable_file;
|
|
|
|
}
|
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
TEST_F(MemEnvTest, LargeWrite) {
|
2011-09-12 17:21:10 +08:00
|
|
|
const size_t kWriteSize = 300 * 1024;
|
|
|
|
char* scratch = new char[kWriteSize * 2];
|
|
|
|
|
|
|
|
std::string write_data;
|
|
|
|
for (size_t i = 0; i < kWriteSize; ++i) {
|
|
|
|
write_data.append(1, static_cast<char>(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
WritableFile* writable_file;
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->NewWritableFile("/dir/f", &writable_file));
|
|
|
|
ASSERT_LEVELDB_OK(writable_file->Append("foo"));
|
|
|
|
ASSERT_LEVELDB_OK(writable_file->Append(write_data));
|
2011-09-12 17:21:10 +08:00
|
|
|
delete writable_file;
|
|
|
|
|
|
|
|
SequentialFile* seq_file;
|
|
|
|
Slice result;
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->NewSequentialFile("/dir/f", &seq_file));
|
|
|
|
ASSERT_LEVELDB_OK(seq_file->Read(3, &result, scratch)); // Read "foo".
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_EQ(0, result.compare("foo"));
|
|
|
|
|
|
|
|
size_t read = 0;
|
|
|
|
std::string read_data;
|
|
|
|
while (read < kWriteSize) {
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(seq_file->Read(kWriteSize - read, &result, scratch));
|
2011-09-12 17:21:10 +08:00
|
|
|
read_data.append(result.data(), result.size());
|
|
|
|
read += result.size();
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(write_data == read_data);
|
|
|
|
delete seq_file;
|
2019-05-03 02:01:00 +08:00
|
|
|
delete[] scratch;
|
2011-09-12 17:21:10 +08:00
|
|
|
}
|
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
TEST_F(MemEnvTest, OverwriteOpenFile) {
|
2019-03-12 03:32:50 +08:00
|
|
|
const char kWrite1Data[] = "Write #1 data";
|
|
|
|
const size_t kFileDataLen = sizeof(kWrite1Data) - 1;
|
2019-11-22 05:09:53 +08:00
|
|
|
const std::string kTestFileName = testing::TempDir() + "leveldb-TestFile.dat";
|
2019-03-12 03:32:50 +08:00
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(WriteStringToFile(env_, kWrite1Data, kTestFileName));
|
2019-03-12 03:32:50 +08:00
|
|
|
|
|
|
|
RandomAccessFile* rand_file;
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->NewRandomAccessFile(kTestFileName, &rand_file));
|
2019-03-12 03:32:50 +08:00
|
|
|
|
|
|
|
const char kWrite2Data[] = "Write #2 data";
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(WriteStringToFile(env_, kWrite2Data, kTestFileName));
|
2019-03-12 03:32:50 +08:00
|
|
|
|
|
|
|
// Verify that overwriting an open file will result in the new file data
|
|
|
|
// being read from files opened before the write.
|
|
|
|
Slice result;
|
|
|
|
char scratch[kFileDataLen];
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(rand_file->Read(0, kFileDataLen, &result, scratch));
|
2019-03-12 03:32:50 +08:00
|
|
|
ASSERT_EQ(0, result.compare(kWrite2Data));
|
|
|
|
|
|
|
|
delete rand_file;
|
|
|
|
}
|
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
TEST_F(MemEnvTest, DBTest) {
|
2011-09-12 17:21:10 +08:00
|
|
|
Options options;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
options.env = env_;
|
|
|
|
DB* db;
|
|
|
|
|
|
|
|
const Slice keys[] = {Slice("aaa"), Slice("bbb"), Slice("ccc")};
|
|
|
|
const Slice vals[] = {Slice("foo"), Slice("bar"), Slice("baz")};
|
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(DB::Open(options, "/dir/db", &db));
|
2011-09-12 17:21:10 +08:00
|
|
|
for (size_t i = 0; i < 3; ++i) {
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(db->Put(WriteOptions(), keys[i], vals[i]));
|
2011-09-12 17:21:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 3; ++i) {
|
|
|
|
std::string res;
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(db->Get(ReadOptions(), keys[i], &res));
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_TRUE(res == vals[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator* iterator = db->NewIterator(ReadOptions());
|
|
|
|
iterator->SeekToFirst();
|
|
|
|
for (size_t i = 0; i < 3; ++i) {
|
|
|
|
ASSERT_TRUE(iterator->Valid());
|
|
|
|
ASSERT_TRUE(keys[i] == iterator->key());
|
|
|
|
ASSERT_TRUE(vals[i] == iterator->value());
|
|
|
|
iterator->Next();
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(!iterator->Valid());
|
|
|
|
delete iterator;
|
|
|
|
|
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db);
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(dbi->TEST_CompactMemTable());
|
2011-09-12 17:21:10 +08:00
|
|
|
|
|
|
|
for (size_t i = 0; i < 3; ++i) {
|
|
|
|
std::string res;
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(db->Get(ReadOptions(), keys[i], &res));
|
2011-09-12 17:21:10 +08:00
|
|
|
ASSERT_TRUE(res == vals[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete db;
|
|
|
|
}
|
|
|
|
|
2011-11-01 01:22:06 +08:00
|
|
|
} // namespace leveldb
|
2011-09-12 17:21:10 +08:00
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|