Bläddra i källkod

正常退出的恢复测试

pull/2/head
augurier 8 månader sedan
förälder
incheckning
6c0b64cfa3
4 ändrade filer med 101 tillägg och 4 borttagningar
  1. +5
    -0
      CMakeLists.txt
  2. +9
    -4
      fielddb/field_db.cpp
  3. +1
    -0
      fielddb/field_db.h
  4. +86
    -0
      test/recover_test.cc

+ 5
- 0
CMakeLists.txt Visa fil

@ -535,3 +535,8 @@ add_executable(parallel_test
"${PROJECT_SOURCE_DIR}/test/parallel_test.cc"
)
target_link_libraries(parallel_test PRIVATE leveldb gtest)
add_executable(recover_test
"${PROJECT_SOURCE_DIR}/test/recover_test.cc"
)
target_link_libraries(recover_test PRIVATE leveldb gtest)

+ 9
- 4
fielddb/field_db.cpp Visa fil

@ -60,11 +60,11 @@ Status FieldDB::Recover() {
std::string IndexKey;
Iter->SeekToFirst();
while(Iter->Valid()) {
IndexKey = Iter->value().ToString();
IndexKey = Iter->key().ToString();
ParsedInternalIndexKey ParsedIndex;
ParseInternalIndexKey(Slice(IndexKey),&ParsedIndex);
index_[ParsedIndex.name_.ToString()] = {Exist,nullptr};
std::cout << "Existed Index : " << ParsedIndex.name_.ToString() << std::endl;
//std::cout << "Existed Index : " << ParsedIndex.name_.ToString() << std::endl;
//构建下一个搜索的对象,在原来的fieldname的基础上加一个最大的ascii字符(不可见字符)
//TODO:不知道这个做法有没有道理
@ -183,8 +183,7 @@ Again:
// return status;
}
//这里把一个空串作为常规put的name
// 这里把一个空串作为常规put的name
Status FieldDB::Put(const WriteOptions &options, const Slice &key, const Slice &value) {
FieldArray FA = {{"",value.ToString()}};
return PutFields(options, key, FA);
@ -364,4 +363,10 @@ Status DestroyDB(const std::string& name, const Options& options) {
return s;
}
FieldDB::~FieldDB() {
delete indexDB_;
delete kvDB_;
delete metaDB_;
}
} // namespace fielddb

+ 1
- 0
fielddb/field_db.h Visa fil

@ -33,6 +33,7 @@ public:
//FieldDB *db = new FieldDB()openDB *db
FieldDB() : indexDB_(nullptr), kvDB_(nullptr), metaDB_(nullptr) {};
~FieldDB();
/*lab1的要求,作为db派生类要实现的虚函数*/
Status Put(const WriteOptions &options, const Slice &key, const Slice &value) override;
Status PutFields(const WriteOptions &, const Slice &key, const FieldArray &fields) override;

+ 86
- 0
test/recover_test.cc Visa fil

@ -0,0 +1,86 @@
#include "gtest/gtest.h"
// #include "leveldb/env.h"
// #include "leveldb/db.h"
#include "fielddb/field_db.h"
#include "test/helper.cc"
#include <thread>
#include <csignal>
#include <exception>
using namespace fielddb;
// std::atomic<bool> thread_has_error(false);
// void signalHandler(int signum) {
// // 捕捉段错误
// }
TEST(TestNormalRecover, Recover) {
fielddb::DestroyDB("testdb3.1",Options());
FieldDB *db = new FieldDB();
if(OpenDB("testdb3.1", &db).ok() == false) {
std::cerr << "open db failed" << std::endl;
abort();
}
db->CreateIndexOnField("address");
db->CreateIndexOnField("age");
InsertFieldData(db);
bool allowNotFound = false;
GetFieldData(db, allowNotFound);
findKeysByCityIndex(db, true);
findKeysByAgeIndex(db, true);
delete db;
db = new FieldDB();
if(OpenDB("testdb3.1", &db).ok() == false) {
std::cerr << "open db failed" << std::endl;
abort();
}
//仍然能读到之前写入的数据和索引
GetFieldData(db, allowNotFound);
findKeysByCityIndex(db, true);
findKeysByAgeIndex(db, true);
}
// TEST(TestParalPutRecover, Recover) {
// signal(SIGSEGV, signalHandler);
// fielddb::DestroyDB("testdb3.2",Options());
// FieldDB *db = new FieldDB();
// if(OpenDB("testdb3.2", &db).ok() == false) {
// std::cerr << "open db failed" << std::endl;
// abort();
// }
// db->CreateIndexOnField("address");
// db->CreateIndexOnField("age");
// shanghaiKeys.clear();
// age20Keys.clear();
// int thread_num_ = 2;
// std::vector<std::thread> threads(thread_num_);
// threads[0] = std::thread([db](){
// InsertFieldData(db);
// });
// threads[1] = std::thread([db](){
// InsertOneField(db);
// delete db;
// });
// if (threads[1].joinable()) {
// threads[1].join();
// }
// db = new FieldDB();
// if(OpenDB("testdb3.2", &db).ok() == false) {
// std::cerr << "open db failed" << std::endl;
// abort();
// }
// GetOneField(db);
// checkDataInKVAndIndex(db);
// }
int main(int argc, char** argv) {
// All tests currently run with the same read-only file limits.
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

Laddar…
Avbryt
Spara