SearchJob.h 2.4 KB
Newer Older
W
wxyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.
17
#pragma once
W
wxyu 已提交
18

S
starlord 已提交
19 20
#include <condition_variable>
#include <deque>
W
wxyu 已提交
21
#include <list>
S
starlord 已提交
22 23
#include <memory>
#include <mutex>
W
wxyu 已提交
24
#include <queue>
S
starlord 已提交
25
#include <string>
W
wxyu 已提交
26
#include <thread>
S
starlord 已提交
27
#include <unordered_map>
S
starlord 已提交
28
#include <utility>
S
starlord 已提交
29
#include <vector>
W
wxyu 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43

#include "Job.h"
#include "db/meta/MetaTypes.h"

namespace milvus {
namespace scheduler {

using engine::meta::TableFileSchemaPtr;

using Id2IndexMap = std::unordered_map<size_t, TableFileSchemaPtr>;
using Id2DistanceMap = std::vector<std::pair<int64_t, double>>;
using ResultSet = std::vector<Id2DistanceMap>;

class SearchJob : public Job {
S
starlord 已提交
44
 public:
S
starlord 已提交
45
    SearchJob(JobId id, uint64_t topk, uint64_t nq, uint64_t nprobe, const float* vectors);
W
wxyu 已提交
46

S
starlord 已提交
47
 public:
W
wxyu 已提交
48
    bool
S
starlord 已提交
49
    AddIndexFile(const TableFileSchemaPtr& index_file);
W
wxyu 已提交
50 51 52 53 54 55 56

    void
    WaitResult();

    void
    SearchDone(size_t index_id);

S
starlord 已提交
57
    ResultSet&
W
wxyu 已提交
58 59
    GetResult();

S
starlord 已提交
60
    Status&
W
wxyu 已提交
61 62
    GetStatus();

S
starlord 已提交
63
 public:
W
wxyu 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77
    uint64_t
    topk() const {
        return topk_;
    }

    uint64_t
    nq() const {
        return nq_;
    }

    uint64_t
    nprobe() const {
        return nprobe_;
    }
S
starlord 已提交
78

S
starlord 已提交
79
    const float*
W
wxyu 已提交
80 81 82 83
    vectors() const {
        return vectors_;
    }

S
starlord 已提交
84
    Id2IndexMap&
85 86 87 88
    index_files() {
        return index_files_;
    }

S
starlord 已提交
89
 private:
W
wxyu 已提交
90 91 92 93
    uint64_t topk_ = 0;
    uint64_t nq_ = 0;
    uint64_t nprobe_ = 0;
    // TODO: smart pointer
S
starlord 已提交
94
    const float* vectors_ = nullptr;
W
wxyu 已提交
95 96 97 98

    Id2IndexMap index_files_;
    // TODO: column-base better ?
    ResultSet result_;
W
wxyu 已提交
99
    Status status_;
W
wxyu 已提交
100 101 102 103 104 105 106

    std::mutex mutex_;
    std::condition_variable cv_;
};

using SearchJobPtr = std::shared_ptr<SearchJob>;

S
starlord 已提交
107 108
}  // namespace scheduler
}  // namespace milvus