BaseRequest.h 5.1 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
G
groot 已提交
2
//
3 4
// Licensed 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
G
groot 已提交
5
//
6 7 8 9 10
// 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.
G
groot 已提交
11 12 13

#pragma once

14
#include "db/Types.h"
G
groot 已提交
15 16 17 18
#include "db/meta/MetaTypes.h"
#include "grpc/gen-milvus/milvus.grpc.pb.h"
#include "grpc/gen-status/status.grpc.pb.h"
#include "grpc/gen-status/status.pb.h"
19
#include "query/GeneralQuery.h"
20
#include "utils/Json.h"
G
groot 已提交
21
#include "utils/Status.h"
22 23

#include <condition_variable>
G
groot 已提交
24 25 26 27
//#include <gperftools/profiler.h>
#include <memory>
#include <string>
#include <thread>
28
#include <utility>
G
groot 已提交
29 30 31 32 33
#include <vector>

namespace milvus {
namespace server {

J
Jin Hai 已提交
34 35
struct CollectionSchema {
    std::string collection_name_;
36 37 38 39
    int64_t dimension_;
    int64_t index_file_size_;
    int64_t metric_type_;

J
Jin Hai 已提交
40
    CollectionSchema() {
41 42 43 44 45
        dimension_ = 0;
        index_file_size_ = 0;
        metric_type_ = 0;
    }

J
Jin Hai 已提交
46 47 48
    CollectionSchema(const std::string& collection_name, int64_t dimension, int64_t index_file_size,
                     int64_t metric_type) {
        collection_name_ = collection_name;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        dimension_ = dimension;
        index_file_size_ = index_file_size;
        metric_type_ = metric_type;
    }
};

struct TopKQueryResult {
    int64_t row_num_;
    engine::ResultIds id_list_;
    engine::ResultDistances distance_list_;

    TopKQueryResult() {
        row_num_ = 0;
    }

    TopKQueryResult(int64_t row_num, const engine::ResultIds& id_list, const engine::ResultDistances& distance_list) {
        row_num_ = row_num;
        id_list_ = id_list;
        distance_list_ = distance_list;
    }
};

71 72 73 74
struct HybridQueryResult {
    int64_t row_num_;
    engine::ResultIds id_list_;
    engine::ResultDistances distance_list_;
Y
yukun 已提交
75 76 77

    std::vector<engine::VectorsData> vectors_;
    std::vector<engine::AttrsData> attrs_;
78 79
};

80
struct IndexParam {
J
Jin Hai 已提交
81
    std::string collection_name_;
82
    int64_t index_type_;
83
    std::string extra_params_;
84 85 86 87 88

    IndexParam() {
        index_type_ = 0;
    }

J
Jin Hai 已提交
89 90
    IndexParam(const std::string& collection_name, int64_t index_type) {
        collection_name_ = collection_name;
91 92 93 94 95
        index_type_ = index_type;
    }
};

struct PartitionParam {
J
Jin Hai 已提交
96
    std::string collection_name_;
97 98 99 100
    std::string tag_;

    PartitionParam() = default;

J
Jin Hai 已提交
101 102
    PartitionParam(const std::string& collection_name, const std::string& tag) {
        collection_name_ = collection_name;
103 104 105
        tag_ = tag;
    }
};
G
groot 已提交
106

107 108
class Context;

109
class BaseRequest {
110 111 112 113 114 115 116 117 118 119 120 121
 public:
    enum RequestType {
        // general operations
        kCmd = 100,

        // data operations
        kInsert = 200,
        kCompact,
        kFlush,
        kDeleteByID,
        kGetVectorByID,
        kGetVectorIDs,
122
        kInsertEntity,
Y
yukun 已提交
123
        kGetEntityByID,
124

J
Jin Hai 已提交
125
        // collection operations
126 127 128 129 130 131 132 133
        kShowCollections = 300,
        kCreateCollection,
        kHasCollection,
        kDescribeCollection,
        kCountCollection,
        kShowCollectionInfo,
        kDropCollection,
        kPreloadCollection,
134 135 136
        kCreateHybridCollection,
        kHasHybridCollection,
        kDescribeHybridCollection,
137
        kReloadSegments,
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

        // partition operations
        kCreatePartition = 400,
        kShowPartitions,
        kDropPartition,

        // index operations
        kCreateIndex = 500,
        kDescribeIndex,
        kDropIndex,

        // search operations
        kSearchByID = 600,
        kSearch,
        kSearchCombine,
153
        kHybridSearch,
154 155
    };

G
groot 已提交
156
 protected:
157 158
    BaseRequest(const std::shared_ptr<milvus::server::Context>& context, BaseRequest::RequestType type,
                bool async = false);
G
groot 已提交
159

160
    virtual ~BaseRequest();
G
groot 已提交
161 162

 public:
163 164 165
    Status
    PreExecute();

G
groot 已提交
166 167 168
    Status
    Execute();

169 170 171
    Status
    PostExecute();

G
groot 已提交
172 173 174 175 176 177
    void
    Done();

    Status
    WaitToFinish();

178 179 180 181 182
    RequestType
    GetRequestType() const {
        return type_;
    }

G
groot 已提交
183 184 185 186 187 188 189 190 191 192
    std::string
    RequestGroup() const {
        return request_group_;
    }

    const Status&
    status() const {
        return status_;
    }

193 194 195
    void
    set_status(const Status& status);

G
groot 已提交
196 197 198 199 200 201
    bool
    IsAsync() const {
        return async_;
    }

 protected:
202 203 204
    virtual Status
    OnPreExecute();

G
groot 已提交
205 206 207
    virtual Status
    OnExecute() = 0;

208 209 210
    virtual Status
    OnPostExecute();

G
groot 已提交
211
    std::string
G
groot 已提交
212
    CollectionNotExistMsg(const std::string& collection_name);
G
groot 已提交
213 214

 protected:
T
Tinkerrr 已提交
215
    const std::shared_ptr<milvus::server::Context> context_;
Z
Zhiru Zhu 已提交
216

G
groot 已提交
217 218 219
    mutable std::mutex finish_mtx_;
    std::condition_variable finish_cond_;

220
    RequestType type_;
G
groot 已提交
221 222 223 224
    std::string request_group_;
    bool async_;
    bool done_;
    Status status_;
225 226 227 228 229 230

 public:
    const std::shared_ptr<milvus::server::Context>&
    Context() const {
        return context_;
    }
G
groot 已提交
231 232
};

233
using BaseRequestPtr = std::shared_ptr<BaseRequest>;
G
groot 已提交
234 235 236

}  // namespace server
}  // namespace milvus