Browse Source

add a unique test

master
alexfisher 8 months ago
parent
commit
aa42bfeda8
4 changed files with 46 additions and 6 deletions
  1. +3
    -2
      db/db_impl.cc
  2. +2
    -0
      include/leveldb/options.h
  3. +38
    -1
      test/test.cpp
  4. +3
    -3
      设计文档.md

+ 3
- 2
db/db_impl.cc View File

@ -1388,8 +1388,9 @@ Status DBImpl::Write(const WriteOptions& options, WriteBatch* updates) {
// into mem_. // into mem_.
{ {
mutex_.Unlock(); mutex_.Unlock();
status = log_->AddRecord(WriteBatchInternal::Contents(write_batch));
if(!options.crash_test)
status = log_->AddRecord(WriteBatchInternal::Contents(write_batch));
else status=Status::Corruption("test");
bool sync_error = false; bool sync_error = false;
if (status.ok() && options.sync) { if (status.ok() && options.sync) {
status = logfile_->Sync(); status = logfile_->Sync();

+ 2
- 0
include/leveldb/options.h View File

@ -208,6 +208,8 @@ struct LEVELDB_EXPORT WriteOptions {
// system call followed by "fsync()". // system call followed by "fsync()".
bool sync = false; bool sync = false;
bool valuelog_write=false; bool valuelog_write=false;
bool crash_test=false;
}; };
} // namespace leveldb } // namespace leveldb

+ 38
- 1
test/test.cpp View File

@ -73,7 +73,7 @@ std::string GenKeyByNum(int num,int len){
std::string GenValueByNum(int num,int len){ std::string GenValueByNum(int num,int len){
std::string value; std::string value;
for(int i=0;i<len;i++){ for(int i=0;i<len;i++){
value+=std::to_string(i);
value+=std::to_string(num);
} }
return value; return value;
} }
@ -98,6 +98,43 @@ bool CompareKey(const std::vector a, std::vector b) {
return true; return true;
} }
TEST(Test, valuelog_corrupt_in_write_test){
DB *db;
WriteOptions writeOptions;
ReadOptions readOptions;
Options dboptions;
std::vector<std::string> values;
dboptions.use_valuelog_length=100;
writeOptions.sync=true;
if(OpenDB(&db,dboptions).ok() == false) {
std::cerr << "open db failed" << std::endl;
abort();
}
for(int i=0;i<5;i++){
std::string key=GenKeyByNum(i,5);
std::string value=GenValueByNum(i,5000);
values.push_back(value);
if(i==4)writeOptions.crash_test=true;
else writeOptions.crash_test=false;
Status s=db->Put(writeOptions,key,value);
if(i<4)assert(s.ok());
else assert(!s.ok());
}
delete db;
if(OpenDB(&db,dboptions,false).ok() == false) {
std::cerr << "open db failed" << std::endl;
abort();
}
for(int i=0;i<5;i++){
std::string key=GenKeyByNum(i,5);
std::string value;
Status s=db->Get(readOptions,key,&value);
if(i<4)assert(s.ok());
else assert(s.IsNotFound());
}
delete db;
}
TEST(Test, valuelog_iterator_test) { TEST(Test, valuelog_iterator_test) {
DB *db; DB *db;
WriteOptions writeOptions; WriteOptions writeOptions;

+ 3
- 3
设计文档.md View File

@ -210,7 +210,7 @@ Value设计为:1字节标志位+Varint64文件ID+Varint64偏移量+Varint64长
**Using count在Value Log添加键值对时进行+1**。 **Using count在Value Log添加键值对时进行+1**。
**Using count**在**其中任意键值对被合并**,**并且 该键值对由于合并时被更加新的键值对覆盖 或者 该键值对的True using Sign=False**时,进行**-1**。
**Using count**在**其中任意键值对被合并**,**并且 该键值对由于合并时被更加新的键值对覆盖 或者 该键值对的True using Sign=False**时,进行 **-1**
在一个Value通过SSTable索引到Value Log后,其索引到的开头是一个**Value True Using Sign**。该标志位同样是一字节,标志了当前该Value是否是真正的Value。 在一个Value通过SSTable索引到Value Log后,其索引到的开头是一个**Value True Using Sign**。该标志位同样是一字节,标志了当前该Value是否是真正的Value。
@ -270,7 +270,7 @@ Value设计为:1字节标志位+Varint64文件ID+Varint64偏移量+Varint64长
在sstable中key对应的value位置存储了对应valuelog文件的id和在文件中的offset。 在sstable中key对应的value位置存储了对应valuelog文件的id和在文件中的offset。
**trick1:在valueLog中重复存key会导致写方法,但是是有必要的,详见GC过程**
**trick1:在valueLog中重复存key会导致写放大,但是是有必要的,详见GC过程**
**trick2:将Key放在Value后,使得不启用CRC校验码时可以无需读取key_len和key,加速read** **trick2:将Key放在Value后,使得不启用CRC校验码时可以无需读取key_len和key,加速read**
@ -787,7 +787,7 @@ WriteOptions新增:
我们写了共九个大测试。 我们写了共九个大测试。
分别为: 分别为:
- 写ValueLog完成后未进入WAL日志的崩溃恢复测试(通过查看ValueLog内部可以看到恢复后未完成的数据从ValueLog中删除)
- 数据全部处于valueLog 的iterator测试 - 数据全部处于valueLog 的iterator测试
- 数据部分处于valueLog 的iterator测试 - 数据部分处于valueLog 的iterator测试
- Unordered iterator测试 - Unordered iterator测试

Loading…
Cancel
Save