#include "gtest/gtest.h" #include // #include "leveldb/env.h" // #include "leveldb/db.h" #include "fielddb/field_db.h" #include "test/helper.cc" using namespace fielddb; // 测试中read/write都表示带索引的读写 //读写有索引数据的并发 TEST(TestReadWrite, Parallel) { fielddb::DestroyDB("testdb2.1",Options()); FieldDB *db = new FieldDB(); if(OpenDB("testdb2.1", &db).ok() == false) { std::cerr << "open db failed" << std::endl; abort(); } // ClearDB(db); int thread_num_ = 5; std::vector threads(thread_num_); //二写三读 for (size_t i = 0; i < thread_num_; i++) { if (i == 0) {//写随机序列0 threads[i] = std::thread(InsertFieldData, db, 0); } else if (i == 1) {//写随机序列1 threads[i] = std::thread(InsertFieldData, db, 1); } else {//读 bool allowNotFound = true; threads[i] = std::thread(GetFieldData, db, allowNotFound, 0); } } for (auto& t : threads) { if (t.joinable()) { t.join(); } } // 此时写已完成,一定能读到两次写 bool allowNotFound = false; GetFieldData(db, allowNotFound); GetFieldData(db, allowNotFound, 1); findKeysByCity(db); delete db; } //创建索引与写有该索引数据的并发 TEST(TestWriteCreatei, Parallel) { fielddb::DestroyDB("testdb2.2",Options()); FieldDB *db = new FieldDB(); if(OpenDB("testdb2.2", &db).ok() == false) { std::cerr << "open db failed" << std::endl; abort(); } // ClearDB(db); shanghaiKeys.clear(); InsertFieldData(db); int thread_num_ = 2; std::vector threads(thread_num_); for (size_t i = 0; i < thread_num_; i++) { if (i == 0) {//创建索引 threads[i] = std::thread([db](){ db->CreateIndexOnField("address"); std::cout << "finish create index\n"; }); } else {//写 threads[i] = std::thread([db](){ while (db->GetIndexStatus("address") == NotExist){ continue; //开始创建了再并发的写 } InsertOneField(db); //先插一条 }); } } for (auto& t : threads) { if (t.joinable()) { t.join(); } } //检查索引是否创建成功 bool haveIndex = true; findKeysByCityIndex(db, haveIndex); //检查写入是否成功 GetOneField(db); delete db; } //创建删除不同索引的并发 TEST(TestCreateiCreatei, Parallel) { fielddb::DestroyDB("testdb2.3",Options()); FieldDB *db = new FieldDB(); if(OpenDB("testdb2.3", &db).ok() == false) { std::cerr << "open db failed" << std::endl; abort(); } // ClearDB(db); shanghaiKeys.clear(); age20Keys.clear(); InsertFieldData(db); int thread_num_ = 3; std::vector threads(thread_num_); for (size_t i = 0; i < thread_num_; i++) { //3线程并发创建索引address threads[i] = std::thread([db](){ db->CreateIndexOnField("address"); std::cout << "finish create index address\n"; }); } for (auto& t : threads) { if (t.joinable()) { t.join(); } } //检查索引是否创建成功 bool haveIndex = true; findKeysByCityIndex(db, haveIndex); findKeysByAgeIndex(db, false); for (size_t i = 0; i < thread_num_; i++) { if (i == 0 || i == 1) {//2线程删除索引address threads[i] = std::thread([db](){ db->DeleteIndex("address"); std::cout << "finish delete index address\n"; }); } else {//1线程创建索引age threads[i] = std::thread([db](){ db->CreateIndexOnField("age"); std::cout << "finish create index age\n"; }); } } for (auto& t : threads) { if (t.joinable()) { t.join(); } } //检查 findKeysByCityIndex(db, false); findKeysByAgeIndex(db, true); delete 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(); }