VecImpl.h 3.3 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.

X
MS-154  
xj.lin 已提交
18 19 20

#pragma once

X
xiaojun.lin 已提交
21
#include "knowhere/index/vector_index/VectorIndex.h"
22
#include "VecIndex.h"
X
MS-154  
xj.lin 已提交
23

G
groot 已提交
24 25
#include <utility>
#include <memory>
X
MS-154  
xj.lin 已提交
26 27

namespace zilliz {
X
xj.lin 已提交
28
namespace milvus {
X
MS-154  
xj.lin 已提交
29 30 31 32
namespace engine {

class VecIndexImpl : public VecIndex {
 public:
X
xj.lin 已提交
33
    explicit VecIndexImpl(std::shared_ptr<zilliz::knowhere::VectorIndex> index, const IndexType &type)
G
groot 已提交
34 35
        : index_(std::move(index)), type(type) {
    }
36 37

    Status
G
groot 已提交
38
    BuildAll(const int64_t &nb,
39
             const float *xb,
G
groot 已提交
40
             const int64_t *ids,
41
             const Config &cfg,
G
groot 已提交
42
             const int64_t &nt,
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
             const float *xt) override;

    VecIndexPtr
    CopyToGpu(const int64_t &device_id, const Config &cfg) override;

    VecIndexPtr
    CopyToCpu(const Config &cfg) override;

    IndexType
    GetType() override;

    int64_t
    Dimension() override;

    int64_t
    Count() override;

    Status
G
groot 已提交
61
    Add(const int64_t &nb, const float *xb, const int64_t *ids, const Config &cfg) override;
62 63 64 65 66 67 68 69 70 71 72 73 74 75

    zilliz::knowhere::BinarySet
    Serialize() override;

    Status
    Load(const zilliz::knowhere::BinarySet &index_binary) override;

    VecIndexPtr
    Clone() override;

    int64_t
    GetDeviceId() override;

    Status
G
groot 已提交
76
    Search(const int64_t &nq, const float *xq, float *dist, int64_t *ids, const Config &cfg) override;
X
MS-154  
xj.lin 已提交
77

X
xj.lin 已提交
78
 protected:
X
xj.lin 已提交
79
    int64_t dim = 0;
80

X
xj.lin 已提交
81
    IndexType type = IndexType::INVALID;
82

X
MS-154  
xj.lin 已提交
83 84 85
    std::shared_ptr<zilliz::knowhere::VectorIndex> index_ = nullptr;
};

X
xj.lin 已提交
86 87
class IVFMixIndex : public VecIndexImpl {
 public:
X
xj.lin 已提交
88
    explicit IVFMixIndex(std::shared_ptr<zilliz::knowhere::VectorIndex> index, const IndexType &type)
G
groot 已提交
89 90
        : VecIndexImpl(std::move(index), type) {
    }
X
xj.lin 已提交
91

92
    Status
G
groot 已提交
93
    BuildAll(const int64_t &nb,
94
             const float *xb,
G
groot 已提交
95
             const int64_t *ids,
96
             const Config &cfg,
G
groot 已提交
97
             const int64_t &nt,
98 99 100 101
             const float *xt) override;

    Status
    Load(const zilliz::knowhere::BinarySet &index_binary) override;
X
xj.lin 已提交
102 103
};

X
xj.lin 已提交
104 105
class BFIndex : public VecIndexImpl {
 public:
X
xj.lin 已提交
106
    explicit BFIndex(std::shared_ptr<zilliz::knowhere::VectorIndex> index) : VecIndexImpl(std::move(index),
G
groot 已提交
107 108
                                                                                          IndexType::FAISS_IDMAP) {
    }
109 110 111 112 113 114 115 116

    ErrorCode
    Build(const Config &cfg);

    float *
    GetRawVectors();

    Status
G
groot 已提交
117
    BuildAll(const int64_t &nb,
118
             const float *xb,
G
groot 已提交
119
             const int64_t *ids,
120
             const Config &cfg,
G
groot 已提交
121
             const int64_t &nt,
122 123 124 125
             const float *xt) override;

    int64_t *
    GetRawIds();
X
xj.lin 已提交
126 127
};

G
groot 已提交
128 129 130
} // namespace engine
} // namespace milvus
} // namespace zilliz