SearchContext.h 2.6 KB
Newer Older
G
groot 已提交
1 2 3 4 5 6 7
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/
#pragma once

G
groot 已提交
8
#include "IScheduleContext.h"
S
starlord 已提交
9
#include "db/meta/MetaTypes.h"
G
groot 已提交
10 11 12 13 14 15 16

#include <unordered_map>
#include <vector>
#include <memory>
#include <condition_variable>

namespace zilliz {
J
jinhai 已提交
17
namespace milvus {
G
groot 已提交
18 19 20 21
namespace engine {

using TableFileSchemaPtr = std::shared_ptr<meta::TableFileSchema>;

G
groot 已提交
22
class SearchContext : public IScheduleContext {
G
groot 已提交
23
public:
Y
Yu Kun 已提交
24
    SearchContext(uint64_t topk, uint64_t nq, uint64_t nprobe, const float* vectors);
G
groot 已提交
25 26 27

    bool AddIndexFile(TableFileSchemaPtr& index_file);

G
groot 已提交
28 29
    uint64_t topk() const { return topk_; }
    uint64_t nq() const  { return nq_; }
Y
Yu Kun 已提交
30
    uint64_t nprobe() const { return nprobe_; }
G
groot 已提交
31
    const float* vectors() const { return vectors_; }
G
groot 已提交
32 33 34 35

    using Id2IndexMap = std::unordered_map<size_t, TableFileSchemaPtr>;
    const Id2IndexMap& GetIndexMap() const { return map_index_files_; }

36 37
    using Id2DistanceMap = std::vector<std::pair<int64_t, double>>;
    using ResultSet = std::vector<Id2DistanceMap>;
G
groot 已提交
38 39 40
    const ResultSet& GetResult() const { return result_; }
    ResultSet& GetResult() { return result_; }

41 42 43
    const std::string& Identity() const { return identity_; }
    const Status& GetStatus() const { return status_; }
    Status& GetStatus() { return status_; }
S
starlord 已提交
44

G
groot 已提交
45 46 47
    void IndexSearchDone(size_t index_id);
    void WaitResult();

S
starlord 已提交
48 49 50 51 52 53 54 55
    void AccumLoadCost(double span) { time_cost_load_ += span; }
    void AccumSearchCost(double span) { time_cost_search_ += span; }
    void AccumReduceCost(double span) { time_cost_reduce_ += span; }

    double LoadCost() const { return time_cost_load_; }
    double SearchCost() const { return time_cost_search_; }
    double ReduceCost() const { return time_cost_reduce_; }

G
groot 已提交
56 57 58
private:
    uint64_t topk_ = 0;
    uint64_t nq_ = 0;
Y
Yu Kun 已提交
59
    uint64_t nprobe_ = 10;
G
groot 已提交
60 61 62 63 64 65 66 67 68
    const float* vectors_ = nullptr;

    Id2IndexMap map_index_files_;
    ResultSet result_;

    std::mutex mtx_;
    std::condition_variable done_cond_;

    std::string identity_; //for debug
69
    Status status_;
S
starlord 已提交
70 71 72 73

    double time_cost_load_ = 0.0; //time cost for load all index files, unit: us
    double time_cost_search_ = 0.0; //time cost for entire search, unit: us
    double time_cost_reduce_ = 0.0; //time cost for entire reduce, unit: us
G
groot 已提交
74 75 76 77 78 79 80 81 82
};

using SearchContextPtr = std::shared_ptr<SearchContext>;



}
}
}