Skip to content

Commit

Permalink
doc: update README
Browse files Browse the repository at this point in the history
  • Loading branch information
MoveCloudROY committed Apr 11, 2024
1 parent 3493601 commit 27ffdfd
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 17 deletions.
32 changes: 27 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
## 参考资料
1. [Lucene原理](https://www.infoq.cn/article/ejeg02vroegvalw4j_ll)
<div align="center">
<h1>OurSearchEngine</code></h1>

<p>
<strong>A simple search engine written in <code>Cpp</code></strong>
</p>
</div>

## Requires
## Features
-


## Architecture
![Architecture](doc/pic/Arch.png)


## How to use
### Build
```bash
yay -S jsoncpp-cmake
cd OurSearchEngine
git submodule update --init --recursive
```
cmake -B build
cmake --build build
```

### Use



## Reference
1. [Lucene](https://www.infoq.cn/article/ejeg02vroegvalw4j_ll)
4 changes: 2 additions & 2 deletions src/backends/src/ServerCtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ void ServerCtl::search(const drogon::HttpRequestPtr &req, ResponseCallback &&cal

Json::StreamWriterBuilder builder;
builder.settings_["emitUTF8"] = true;
spdlog::info("\n{}", Json::writeString(builder, ret));
auto resp = drogon::HttpResponse::newHttpJsonResponse(ret);
// spdlog::info("\n{}", Json::writeString(builder, ret));
auto resp = drogon::HttpResponse::newHttpJsonResponse(ret);


// std::ifstream inputFile("../assets/test.json");
Expand Down
1 change: 1 addition & 0 deletions src/query/include/SearchResultBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class SearchResultBuilder {
void addItem(SearchResultItem &&item);
void addItems(std::vector<std::unique_ptr<SG::SearchResultItem>> &&items);
void addPartsInfo(const std::map<std::string, uint64_t> &parts);
void addPartsInfo(std::map<std::string, uint64_t> &&parts);
void addItemTotalNumber(uint64_t tot);
Json::Value build();

Expand Down
16 changes: 8 additions & 8 deletions src/query/src/Queryer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ using SG::Core::SkipList;
Json::Value Queryer::get(const std::string &content, uint64_t rkBegin, uint64_t rkEnd) {
SG::SearchResultBuilder ret;

SG::PartsInfo partsInfo = createPartsInfo(content);
ret.addPartsInfo(partsInfo);
SG::PartsInfo partsInfo = createPartsInfo(content);
auto [resultList, totalCnt] = createResultList(partsInfo, rkBegin, rkEnd);
ret.addPartsInfo(std::move(partsInfo));
ret.addItems(std::move(resultList));
ret.addItemTotalNumber(totalCnt);
return ret.build();
Expand Down Expand Up @@ -58,7 +58,7 @@ std::pair<std::vector<std::unique_ptr<SearchResultItem>>, uint64_t> Queryer::cre
uint64_t outputID = output / 400;
std::ifstream inputFile("../assets/library/skl/" + std::to_string(outputID) + ".lib");
if (!inputFile.is_open()) {
spdlog::error("[Queryer::creatResultList] Failed to open json file");
spdlog::error("[Queryer::creatResultList] Failed to open skl library file {}", std::to_string(outputID) + ".lib");
inputFile.close();
break;
}
Expand All @@ -78,11 +78,11 @@ std::pair<std::vector<std::unique_ptr<SearchResultItem>>, uint64_t> Queryer::cre

std::vector<std::vector<Doc>> combineResult = SkipList<Doc>::combine(sls);

std::cout << combineResult.size() << std::endl;
for (auto &i : combineResult) {
std::cout << i.size() << std::endl;
}
std::cout << idfs.size() << std::endl;
// std::cout << combineResult.size() << std::endl;
// for (auto &i : combineResult) {
// std::cout << i.size() << std::endl;
// }
// std::cout << idfs.size() << std::endl;

for (int i = 0; i < combineResult.size(); ++i) {
double score = 0;
Expand Down
4 changes: 4 additions & 0 deletions src/query/src/SearchResultBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ void SearchResultBuilder::addPartsInfo(const std::map<std::string, uint64_t> &pa
partsInfo = parts;
}

void SearchResultBuilder::addPartsInfo(std::map<std::string, uint64_t> &&parts) {
partsInfo = std::move(parts);
}

void SearchResultBuilder::addItem(SearchResultItem &&item) {
results.push_back(std::make_unique<SearchResultItem>(item));
}
Expand Down
11 changes: 11 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ target_link_libraries(test_queryer PUBLIC
query
)

find_package(benchmark REQUIRED)

add_executable(test_benchmark test_benchmark.cpp)
target_link_libraries(test_benchmark PUBLIC
query
)
target_link_libraries(test_benchmark PUBLIC
benchmark::benchmark
)


if(UNIX AND CMAKE_BUILD_TYPE MATCHES "Debug")
target_compile_options(test_skiplist PRIVATE -fno-omit-frame-pointer)
target_compile_options(test_skiplist PRIVATE -fsanitize=address)
Expand Down
16 changes: 16 additions & 0 deletions test/test_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "Queryer.h"
#include <spdlog/spdlog.h>
#include <benchmark/benchmark.h>
using namespace SG;


static void BM_Query(benchmark::State &state) {
Queryer &queryer = Queryer::getInstance();
for (auto _ : state) {
auto ans = queryer.get("寿命", 0, 10);
benchmark::DoNotOptimize(ans);
}
}
BENCHMARK(BM_Query);

BENCHMARK_MAIN();
7 changes: 5 additions & 2 deletions test/test_queryer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
#include <spdlog/spdlog.h>
using namespace SG;
int main() {
Queryer &queryer = Queryer::getInstance();
auto ans = queryer.get("使用寿命", 0, 10);
Queryer &queryer = Queryer::getInstance();
auto ans = queryer.get("使用寿命", 0, 10);
auto ans2 = queryer.get("使用寿命", 0, 10);

Json::StreamWriterBuilder builder;
builder.settings_["emitUTF8"] = true;
spdlog::info("\n{}", Json::writeString(builder, ans));
spdlog::info("\n{}", Json::writeString(builder, ans2));
}
109 changes: 109 additions & 0 deletions test/test_skp_eff.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "SkipList.h"
#include <chrono>
#include <fstream>
#include <iostream>
#include <vector>
#include <json/json.h>

struct Item {
int id;
Item() = default;
Item(int id)
: id{id} {}
bool operator==(const Item &t) const {
return id == t.id;
}
bool operator<(const Item &t) const & {
return id < t.id;
}
bool operator<(const Item &t) const && {
return id < t.id;
}

friend std::ostream &operator<<(std::ostream &os, const Item &t) {
os << '(' << t.id << ')';
return os;
}

static Json::Value toJson(const Item &t) {
Json::Value ret;
ret["id"] = t.id;
return ret;
}
static Item fromJson(const Json::Value &src) {
return {src["id"].asInt()};
}
};
using SG::Core::SkipList;

void test(const int &size, std::ofstream &outputFile) {
srand(time(nullptr));

SkipList<Item> skipList;

const int largeSize = size;

// test insertion
std::vector<int> insertData;
srand(time(nullptr));
for (int i = 0; i < largeSize; i++) {
insertData.push_back(rand() % largeSize);
}

auto start = std::chrono::steady_clock::now();
for (int i = 0; i < insertData.size(); i++) {
skipList.insert(Item(insertData[i]));
}
auto end = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
outputFile << size << "," << duration.count();

// outputFile << "\n========\n"
// << skipList.dump().toStyledString() << "\n========\n";
// std::cout << "=====\n";
// test deletion
// std::vector<int> removeData;
// srand(time(nullptr));
// for (int i = 0; i < largeSize; i++) {
// removeData.push_back(rand() % largeSize);
// }

// start = std::chrono::steady_clock::now();
// for (int i = 0; i < removeData.size(); i++) {
// skipList.remove(removeData[i]);
// }
// end = std::chrono::steady_clock::now();
// duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
// outputFile << "," << duration.count();

// test search
std::vector<int> searchData;
for (int i = 0; i < largeSize; i++) {
searchData.push_back(rand() % largeSize);
}

start = std::chrono::steady_clock::now();
for (int i = 0; i < searchData.size(); i++) {
auto l = skipList.search(Item(searchData[i]));
}
end = std::chrono::steady_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
outputFile << "," << duration.count() << std::endl;

// std::cout << "=====\n";

// skipList.print();
}

int main() {
std::ofstream outputFile("result.csv");
outputFile << "DataSize,InsertionTime,SearchTime" << std::endl;

for (int i = 100; i <= 10000000; i *= 10) {
test(i, outputFile);
}

outputFile.close();

return 0;
}

0 comments on commit 27ffdfd

Please sign in to comment.