MilvusApi.h 9.5 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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.

J
jinhai 已提交
18 19 20 21
#pragma once

#include "Status.h"

S
starlord 已提交
22
#include <memory>
J
jinhai 已提交
23 24 25
#include <string>
#include <vector>

G
groot 已提交
26
/** \brief Milvus SDK namespace
J
jinhai 已提交
27
 */
G
groot 已提交
28
namespace milvus {
J
jinhai 已提交
29 30 31 32 33

/**
 * @brief Index Type
 */
enum class IndexType {
G
groot 已提交
34 35 36
    invalid = 0,
    cpu_idmap,
    gpu_ivfflat,
S
starlord 已提交
37
    gpu_ivfsq8,
X
xj.lin 已提交
38
    mix_nsg,
W
wxyu 已提交
39
    ivfsq8h,
J
jinhai 已提交
40 41
};

S
starlord 已提交
42 43 44 45 46
enum class MetricType {
    L2 = 1,
    IP = 2,
};

J
jinhai 已提交
47 48 49 50
/**
 * @brief Connect API parameter
 */
struct ConnectParam {
S
starlord 已提交
51 52
    std::string ip_address;  ///< Server IP address
    std::string port;        ///< Server PORT
J
jinhai 已提交
53 54 55 56 57 58
};

/**
 * @brief Table Schema
 */
struct TableSchema {
S
starlord 已提交
59 60 61 62
    std::string table_name;                   ///< Table name
    int64_t dimension = 0;                    ///< Vector dimension, must be a positive value
    int64_t index_file_size = 0;              ///< Index file size, must be a positive value
    MetricType metric_type = MetricType::L2;  ///< Index metric type
J
jinhai 已提交
63 64 65 66
};

/**
 * @brief Range information
G
groot 已提交
67
 * for DATE partition, the format is like: 'year-month-day'
J
jinhai 已提交
68 69
 */
struct Range {
S
starlord 已提交
70 71
    std::string start_value;  ///< Range start
    std::string end_value;    ///< Range stop
J
jinhai 已提交
72 73 74 75 76 77
};

/**
 * @brief Record inserted
 */
struct RowRecord {
S
starlord 已提交
78
    std::vector<float> data;  ///< Vector raw data
J
jinhai 已提交
79 80 81 82 83 84
};

/**
 * @brief Query result
 */
struct QueryResult {
S
starlord 已提交
85 86
    int64_t id;       ///< Output result
    double distance;  ///< Vector similarity distance
J
jinhai 已提交
87 88 89 90 91 92
};

/**
 * @brief TopK query result
 */
struct TopKQueryResult {
S
starlord 已提交
93
    std::vector<QueryResult> query_result_arrays;  ///< TopK query result
J
jinhai 已提交
94 95
};

Y
Yu Kun 已提交
96 97 98 99 100
/**
 * @brief index parameters
 */
struct IndexParam {
    std::string table_name;
101 102
    IndexType index_type;
    int32_t nlist;
Y
Yu Kun 已提交
103
};
G
groot 已提交
104

J
jinhai 已提交
105 106 107 108
/**
 * @brief SDK main class
 */
class Connection {
K
kun yu 已提交
109
 public:
J
jinhai 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    /**
     * @brief CreateConnection
     *
     * Create a connection instance and return it's shared pointer
     *
     * @return Connection instance pointer
     */

    static std::shared_ptr<Connection>
    Create();

    /**
     * @brief DestroyConnection
     *
     * Destroy the connection instance
     *
     * @param connection, the shared pointer to the instance to be destroyed
     *
     * @return if destroy is successful
     */

    static Status
S
starlord 已提交
132
    Destroy(std::shared_ptr<Connection>& connection_ptr);
J
jinhai 已提交
133 134 135 136 137 138 139 140 141 142 143 144

    /**
     * @brief Connect
     *
     * Connect function should be called before any operations
     * Server will be connected after Connect return OK
     *
     * @param param, use to provide server information
     *
     * @return Indicate if connect is successful
     */

Y
Yu Kun 已提交
145
    virtual Status
S
starlord 已提交
146
    Connect(const ConnectParam& param) = 0;
J
jinhai 已提交
147 148 149 150 151 152 153

    /**
     * @brief Connect
     *
     * Connect function should be called before any operations
     * Server will be connected after Connect return OK
     *
G
groot 已提交
154
     * @param uri, use to provide server information, example: milvus://ipaddress:port
J
jinhai 已提交
155 156 157
     *
     * @return Indicate if connect is successful
     */
Y
Yu Kun 已提交
158
    virtual Status
S
starlord 已提交
159
    Connect(const std::string& uri) = 0;
J
jinhai 已提交
160 161 162 163 164 165 166 167

    /**
     * @brief connected
     *
     * Connection status.
     *
     * @return Indicate if connection status
     */
Y
Yu Kun 已提交
168 169
    virtual Status
    Connected() const = 0;
J
jinhai 已提交
170 171 172 173 174 175 176 177

    /**
     * @brief Disconnect
     *
     * Server will be disconnected after Disconnect return OK
     *
     * @return Indicate if disconnect is successful
     */
Y
Yu Kun 已提交
178 179
    virtual Status
    Disconnect() = 0;
J
jinhai 已提交
180 181 182 183 184 185 186 187 188 189

    /**
     * @brief Create table method
     *
     * This method is used to create table
     *
     * @param param, use to provide table information to be created.
     *
     * @return Indicate if table is created successfully
     */
Y
Yu Kun 已提交
190
    virtual Status
S
starlord 已提交
191
    CreateTable(const TableSchema& param) = 0;
J
jinhai 已提交
192

G
groot 已提交
193 194 195 196 197 198 199 200 201
    /**
     * @brief Test table existence method
     *
     * This method is used to create table
     *
     * @param table_name, table name is going to be tested.
     *
     * @return Indicate if table is cexist
     */
Y
Yu Kun 已提交
202
    virtual bool
S
starlord 已提交
203
    HasTable(const std::string& table_name) = 0;
G
groot 已提交
204

J
jinhai 已提交
205 206 207 208 209 210 211 212 213
    /**
     * @brief Delete table method
     *
     * This method is used to delete table.
     *
     * @param table_name, table name is going to be deleted.
     *
     * @return Indicate if table is delete successfully.
     */
Y
Yu Kun 已提交
214
    virtual Status
S
starlord 已提交
215
    DropTable(const std::string& table_name) = 0;
K
kun yu 已提交
216

217
    /**
Y
Yu Kun 已提交
218
     * @brief Create index method
219
     *
Y
Yu Kun 已提交
220
     * This method is used to create index for whole table
221
     *
Y
Yu Kun 已提交
222 223 224 225 226
     * @param IndexParam
     *  table_name, table name is going to be create index.
     *  index type,
     *  nlist,
     *  index file size
227 228 229
     *
     * @return Indicate if build index successfully.
     */
Y
Yu Kun 已提交
230
    virtual Status
S
starlord 已提交
231
    CreateIndex(const IndexParam& index_param) = 0;
232

J
jinhai 已提交
233 234 235 236 237 238 239 240 241 242 243
    /**
     * @brief Add vector to table
     *
     * This method is used to add vector array to table.
     *
     * @param table_name, table_name is inserted.
     * @param record_array, vector array is inserted.
     * @param id_array, after inserted every vector is given a id.
     *
     * @return Indicate if vector array are inserted successfully
     */
Y
Yu Kun 已提交
244
    virtual Status
S
starlord 已提交
245 246
    Insert(const std::string& table_name, const std::vector<RowRecord>& record_array,
           std::vector<int64_t>& id_array) = 0;
J
jinhai 已提交
247 248 249 250 251 252 253 254

    /**
     * @brief Search vector
     *
     * This method is used to query vector in table.
     *
     * @param table_name, table_name is queried.
     * @param query_record_array, all vector are going to be queried.
G
groot 已提交
255
     * @param query_range_array, time ranges, if not specified, will search in whole table
J
jinhai 已提交
256
     * @param topk, how many similarity vectors will be searched.
G
groot 已提交
257
     * @param topk_query_result_array, result array.
J
jinhai 已提交
258 259 260
     *
     * @return Indicate if query is successful.
     */
Y
Yu Kun 已提交
261
    virtual Status
S
starlord 已提交
262 263 264
    Search(const std::string& table_name, const std::vector<RowRecord>& query_record_array,
           const std::vector<Range>& query_range_array, int64_t topk, int64_t nprobe,
           std::vector<TopKQueryResult>& topk_query_result_array) = 0;
J
jinhai 已提交
265 266 267 268 269 270 271 272 273 274 275

    /**
     * @brief Show table description
     *
     * This method is used to show table information.
     *
     * @param table_name, which table is show.
     * @param table_schema, table_schema is given when operation is successful.
     *
     * @return Indicate if this operation is successful.
     */
Y
Yu Kun 已提交
276
    virtual Status
S
starlord 已提交
277
    DescribeTable(const std::string& table_name, TableSchema& table_schema) = 0;
J
jinhai 已提交
278

G
groot 已提交
279 280 281 282 283 284 285 286 287 288
    /**
     * @brief Get table row count
     *
     * This method is used to get table row count.
     *
     * @param table_name, table's name.
     * @param row_count, table total row count.
     *
     * @return Indicate if this operation is successful.
     */
Y
Yu Kun 已提交
289
    virtual Status
S
starlord 已提交
290
    CountTable(const std::string& table_name, int64_t& row_count) = 0;
G
groot 已提交
291

J
jinhai 已提交
292 293 294 295 296 297 298 299 300
    /**
     * @brief Show all tables in database
     *
     * This method is used to list all tables.
     *
     * @param table_array, all tables are push into the array.
     *
     * @return Indicate if this operation is successful.
     */
Y
Yu Kun 已提交
301
    virtual Status
S
starlord 已提交
302
    ShowTables(std::vector<std::string>& table_array) = 0;
J
jinhai 已提交
303 304 305 306 307 308 309 310

    /**
     * @brief Give the client version
     *
     * This method is used to give the client version.
     *
     * @return Client version.
     */
Y
Yu Kun 已提交
311 312
    virtual std::string
    ClientVersion() const = 0;
J
jinhai 已提交
313 314 315 316 317 318 319 320

    /**
     * @brief Give the server version
     *
     * This method is used to give the server version.
     *
     * @return Server version.
     */
Y
Yu Kun 已提交
321 322
    virtual std::string
    ServerVersion() const = 0;
J
jinhai 已提交
323 324 325 326 327 328 329 330

    /**
     * @brief Give the server status
     *
     * This method is used to give the server status.
     *
     * @return Server status.
     */
Y
Yu Kun 已提交
331 332
    virtual std::string
    ServerStatus() const = 0;
Y
Yu Kun 已提交
333

Y
Yu Kun 已提交
334 335 336
    virtual std::string
    DumpTaskTables() const = 0;

Y
Yu Kun 已提交
337 338 339 340 341 342 343 344 345 346 347
    /**
     * @brief delete tables by range
     *
     * This method is used to delete tables by range.
     *
     * @param Range, table range to delete.
     * @param table_name
     *
     * @return Indicate if this operation is successful.
     */
    virtual Status
S
starlord 已提交
348
    DeleteByRange(Range& range, const std::string& table_name) = 0;
Y
Yu Kun 已提交
349 350 351 352 353 354 355 356 357 358 359

    /**
     * @brief preload table
     *
     * This method is used to preload table
     *
     * @param table_name
     *
     * @return Indicate if this operation is successful.
     */
    virtual Status
S
starlord 已提交
360
    PreloadTable(const std::string& table_name) const = 0;
Y
Yu Kun 已提交
361 362 363 364 365 366 367 368 369 370

    /**
     * @brief describe index
     *
     * This method is used to describe index
     *
     * @param table_name
     *
     * @return index informations and indicate if this operation is successful.
     */
371
    virtual Status
S
starlord 已提交
372
    DescribeIndex(const std::string& table_name, IndexParam& index_param) const = 0;
Y
Yu Kun 已提交
373 374 375 376 377 378 379 380 381 382 383

    /**
     * @brief drop index
     *
     * This method is used to drop index
     *
     * @param table_name
     *
     * @return Indicate if this operation is successful.
     */
    virtual Status
S
starlord 已提交
384
    DropIndex(const std::string& table_name) const = 0;
J
jinhai 已提交
385 386
};

S
starlord 已提交
387
}  // namespace milvus