MilvusApi.h 8.9 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8
#pragma once

#include "Status.h"

#include <string>
#include <vector>
#include <memory>

G
groot 已提交
9
/** \brief Milvus SDK namespace
J
jinhai 已提交
10
 */
G
groot 已提交
11
namespace milvus {
J
jinhai 已提交
12 13 14 15 16 17


/**
 * @brief Index Type
 */
enum class IndexType {
G
groot 已提交
18 19 20
    invalid = 0,
    cpu_idmap,
    gpu_ivfflat,
S
starlord 已提交
21
    gpu_ivfsq8,
X
xj.lin 已提交
22
    mix_nsg,
J
jinhai 已提交
23 24 25 26 27 28
};

/**
 * @brief Connect API parameter
 */
struct ConnectParam {
G
groot 已提交
29 30
    std::string ip_address;                                ///< Server IP address
    std::string port;                                      ///< Server PORT
J
jinhai 已提交
31 32 33 34 35 36
};

/**
 * @brief Table Schema
 */
struct TableSchema {
G
groot 已提交
37 38 39 40
    std::string table_name;                                ///< Table name
    IndexType index_type = IndexType::invalid;             ///< Index type
    int64_t dimension = 0;                                 ///< Vector dimension, must be a positive value
    bool store_raw_vector = false;                          ///< Is vector raw data stored in the table
J
jinhai 已提交
41 42 43 44
};

/**
 * @brief Range information
G
groot 已提交
45
 * for DATE partition, the format is like: 'year-month-day'
J
jinhai 已提交
46 47
 */
struct Range {
G
groot 已提交
48 49
    std::string start_value;                                ///< Range start
    std::string end_value;                                  ///< Range stop
J
jinhai 已提交
50 51 52 53 54 55
};

/**
 * @brief Record inserted
 */
struct RowRecord {
G
groot 已提交
56
    std::vector<float> data;                               ///< Vector raw data
J
jinhai 已提交
57 58 59 60 61 62
};

/**
 * @brief Query result
 */
struct QueryResult {
G
groot 已提交
63
    int64_t id;                                             ///< Output result
J
jinhai 已提交
64
    double distance;                                        ///< Vector similarity distance
J
jinhai 已提交
65 66 67 68 69 70
};

/**
 * @brief TopK query result
 */
struct TopKQueryResult {
G
groot 已提交
71
    std::vector<QueryResult> query_result_arrays;           ///< TopK query result
J
jinhai 已提交
72 73
};

Y
Yu Kun 已提交
74 75 76 77 78 79 80 81 82
/**
 * @brief index parameters
 */
struct IndexParam {
    std::string table_name;
    int32_t index_type;
    int64_t nlist;
    int32_t index_file_size;
};
G
groot 已提交
83

J
jinhai 已提交
84 85 86 87
/**
 * @brief SDK main class
 */
class Connection {
K
kun yu 已提交
88
 public:
J
jinhai 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

    /**
     * @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
K
kun yu 已提交
112
    Destroy(std::shared_ptr<Connection>& connection_ptr);
J
jinhai 已提交
113 114 115 116 117 118 119 120 121 122 123 124

    /**
     * @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 已提交
125 126
    virtual Status
    Connect(const ConnectParam &param) = 0;
J
jinhai 已提交
127 128 129 130 131 132 133

    /**
     * @brief Connect
     *
     * Connect function should be called before any operations
     * Server will be connected after Connect return OK
     *
G
groot 已提交
134
     * @param uri, use to provide server information, example: milvus://ipaddress:port
J
jinhai 已提交
135 136 137
     *
     * @return Indicate if connect is successful
     */
Y
Yu Kun 已提交
138 139
    virtual Status
    Connect(const std::string &uri) = 0;
J
jinhai 已提交
140 141 142 143 144 145 146 147

    /**
     * @brief connected
     *
     * Connection status.
     *
     * @return Indicate if connection status
     */
Y
Yu Kun 已提交
148 149
    virtual Status
    Connected() const = 0;
J
jinhai 已提交
150 151 152 153 154 155 156 157

    /**
     * @brief Disconnect
     *
     * Server will be disconnected after Disconnect return OK
     *
     * @return Indicate if disconnect is successful
     */
Y
Yu Kun 已提交
158 159
    virtual Status
    Disconnect() = 0;
J
jinhai 已提交
160 161 162 163 164 165 166 167 168 169 170


    /**
     * @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 已提交
171 172
    virtual Status
    CreateTable(const TableSchema &param) = 0;
J
jinhai 已提交
173 174


G
groot 已提交
175 176 177 178 179 180 181 182 183
    /**
     * @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 已提交
184 185
    virtual bool
    HasTable(const std::string &table_name) = 0;
G
groot 已提交
186 187


J
jinhai 已提交
188 189 190 191 192 193 194 195 196
    /**
     * @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 已提交
197 198
    virtual Status
    DropTable(const std::string &table_name) = 0;
K
kun yu 已提交
199

200
    /**
Y
Yu Kun 已提交
201
     * @brief Create index method
202
     *
Y
Yu Kun 已提交
203
     * This method is used to create index for whole table
204
     *
Y
Yu Kun 已提交
205 206 207 208 209
     * @param IndexParam
     *  table_name, table name is going to be create index.
     *  index type,
     *  nlist,
     *  index file size
210 211 212
     *
     * @return Indicate if build index successfully.
     */
Y
Yu Kun 已提交
213
    virtual Status
Y
Yu Kun 已提交
214
    CreateIndex(const IndexParam &index_param) = 0;
215

J
jinhai 已提交
216 217 218 219 220 221 222 223 224 225 226
    /**
     * @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 已提交
227
    virtual Status
Y
Yu Kun 已提交
228 229 230
    Insert(const std::string &table_name,
                 const std::vector<RowRecord> &record_array,
                 std::vector<int64_t> &id_array) = 0;
J
jinhai 已提交
231 232 233 234 235 236 237 238

    /**
     * @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 已提交
239
     * @param query_range_array, time ranges, if not specified, will search in whole table
J
jinhai 已提交
240
     * @param topk, how many similarity vectors will be searched.
G
groot 已提交
241
     * @param topk_query_result_array, result array.
J
jinhai 已提交
242 243 244
     *
     * @return Indicate if query is successful.
     */
Y
Yu Kun 已提交
245
    virtual Status
Y
Yu Kun 已提交
246 247 248 249 250
    Search(const std::string &table_name,
                 const std::vector<RowRecord> &query_record_array,
                 const std::vector<Range> &query_range_array,
                 int64_t topk,
                 std::vector<TopKQueryResult> &topk_query_result_array) = 0;
J
jinhai 已提交
251 252 253 254 255 256 257 258 259 260 261

    /**
     * @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 已提交
262 263
    virtual Status
    DescribeTable(const std::string &table_name, TableSchema &table_schema) = 0;
J
jinhai 已提交
264

G
groot 已提交
265 266 267 268 269 270 271 272 273 274
    /**
     * @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 已提交
275
    virtual Status
Y
Yu Kun 已提交
276 277
    CountTable(const std::string &table_name,
            int64_t &row_count) = 0;
G
groot 已提交
278

J
jinhai 已提交
279 280 281 282 283 284 285 286 287
    /**
     * @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 已提交
288 289
    virtual Status
    ShowTables(std::vector<std::string> &table_array) = 0;
J
jinhai 已提交
290 291 292 293 294 295 296 297

    /**
     * @brief Give the client version
     *
     * This method is used to give the client version.
     *
     * @return Client version.
     */
Y
Yu Kun 已提交
298 299
    virtual std::string
    ClientVersion() const = 0;
J
jinhai 已提交
300 301 302 303 304 305 306 307

    /**
     * @brief Give the server version
     *
     * This method is used to give the server version.
     *
     * @return Server version.
     */
Y
Yu Kun 已提交
308 309
    virtual std::string
    ServerVersion() const = 0;
J
jinhai 已提交
310 311 312 313 314 315 316 317

    /**
     * @brief Give the server status
     *
     * This method is used to give the server status.
     *
     * @return Server status.
     */
Y
Yu Kun 已提交
318 319
    virtual std::string
    ServerStatus() const = 0;
Y
Yu Kun 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

    /**
     * @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
    DeleteByRange(Range &range,
                  const std::string &table_name) = 0;

    /**
     * @brief preload table
     *
     * This method is used to preload table
     *
     * @param table_name
     *
     * @return Indicate if this operation is successful.
     */
    virtual Status
    PreloadTable(const std::string &table_name) const = 0;

    /**
     * @brief describe index
     *
     * This method is used to describe index
     *
     * @param table_name
     *
     * @return index informations and indicate if this operation is successful.
     */
    virtual IndexParam
    DescribeIndex(const std::string &table_name) const = 0;

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

}