SearchJob.cpp 2.2 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.
W
wxyu 已提交
17

G
groot 已提交
18
#include "scheduler/job/SearchJob.h"
W
wxyu 已提交
19 20 21 22 23
#include "utils/Log.h"

namespace milvus {
namespace scheduler {

W
wxyu 已提交
24 25
SearchJob::SearchJob(uint64_t topk, uint64_t nq, uint64_t nprobe, const float* vectors)
    : Job(JobType::SEARCH), topk_(topk), nq_(nq), nprobe_(nprobe), vectors_(vectors) {
G
groot 已提交
26
}
W
wxyu 已提交
27 28

bool
G
groot 已提交
29
SearchJob::AddIndexFile(const TableFileSchemaPtr& index_file) {
W
wxyu 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43
    std::unique_lock<std::mutex> lock(mutex_);
    if (index_file == nullptr || index_files_.find(index_file->id_) != index_files_.end()) {
        return false;
    }

    SERVER_LOG_DEBUG << "SearchJob " << id() << " add index file: " << index_file->id_;

    index_files_[index_file->id_] = index_file;
    return true;
}

void
SearchJob::WaitResult() {
    std::unique_lock<std::mutex> lock(mutex_);
G
groot 已提交
44
    cv_.wait(lock, [this] { return index_files_.empty(); });
W
wxyu 已提交
45 46 47 48 49 50 51 52 53 54 55
    SERVER_LOG_DEBUG << "SearchJob " << id() << " all done";
}

void
SearchJob::SearchDone(size_t index_id) {
    std::unique_lock<std::mutex> lock(mutex_);
    index_files_.erase(index_id);
    cv_.notify_all();
    SERVER_LOG_DEBUG << "SearchJob " << id() << " finish index file: " << index_id;
}

G
groot 已提交
56
ResultSet&
W
wxyu 已提交
57 58 59 60
SearchJob::GetResult() {
    return result_;
}

G
groot 已提交
61
Status&
W
wxyu 已提交
62 63 64 65
SearchJob::GetStatus() {
    return status_;
}

W
wxyu 已提交
66 67 68 69 70 71 72
json
SearchJob::Dump() const {
    json ret{
        {"topk", topk_},
        {"nq", nq_},
        {"nprobe", nprobe_},
    };
W
wxyu 已提交
73 74
    auto base = Job::Dump();
    ret.insert(base.begin(), base.end());
W
wxyu 已提交
75 76 77
    return ret;
}

G
groot 已提交
78 79
}  // namespace scheduler
}  // namespace milvus