From a6369c7a4ebdc1580a8f68f5353d34f44d94aec2 Mon Sep 17 00:00:00 2001 From: Xu Peng Date: Wed, 24 Apr 2019 16:20:36 +0800 Subject: [PATCH] feat(db): cache for search Former-commit-id: 8e13efdaa593b4ebb83b06d33d5127180d756597 --- cpp/src/db/DBImpl.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/cpp/src/db/DBImpl.cpp b/cpp/src/db/DBImpl.cpp index 81a0bb1c89..a05d6001f2 100644 --- a/cpp/src/db/DBImpl.cpp +++ b/cpp/src/db/DBImpl.cpp @@ -86,7 +86,13 @@ Status DBImpl::search(const std::string &group_id, size_t k, size_t nq, // merge raw files and build flat index. faiss::Index *index(faiss::index_factory(dim, "IDMap,Flat")); for (auto &file : raw_files) { - auto file_index = dynamic_cast(faiss::read_index(file.location.c_str())); + auto to_merge = zilliz::vecwise::cache::CpuCacheMgr::GetInstance()->GetIndex(file.location); + if (!to_merge) { + LOG(DEBUG) << "Disk io from: " << file.location; + to_merge = read_index(file.location.c_str()); + zilliz::vecwise::cache::CpuCacheMgr::GetInstance()->InsertItem(file.location, to_merge); + } + auto file_index = dynamic_cast(to_merge->data().get()); index->add_with_ids(file_index->ntotal, dynamic_cast(file_index->index)->xb.data(), file_index->id_map.data()); @@ -123,7 +129,12 @@ Status DBImpl::search(const std::string &group_id, size_t k, size_t nq, // Search in index file for (auto &file : index_files) { - auto index = read_index(file.location.c_str()); + auto index = zilliz::vecwise::cache::CpuCacheMgr::GetInstance()->GetIndex(file.location); + if (!index) { + LOG(DEBUG) << "Disk io from: " << file.location; + index = read_index(file.location.c_str()); + zilliz::vecwise::cache::CpuCacheMgr::GetInstance()->InsertItem(file.location, index); + } index->search(nq, vectors, k, output_distence, output_ids); cluster(output_ids, output_distence); // cluster to each query memset(output_distence, 0, k * nq * sizeof(float));