VecImpl.h 3.1 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
#pragma once

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

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

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

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

    Status
G
groot 已提交
37 38
    BuildAll(const int64_t& nb, const float* xb, const int64_t* ids, const Config& cfg, const int64_t& nt,
             const float* xt) override;
39 40

    VecIndexPtr
G
groot 已提交
41
    CopyToGpu(const int64_t& device_id, const Config& cfg) override;
42 43

    VecIndexPtr
G
groot 已提交
44
    CopyToCpu(const Config& cfg) override;
45 46 47 48 49 50 51 52 53 54 55

    IndexType
    GetType() override;

    int64_t
    Dimension() override;

    int64_t
    Count() override;

    Status
G
groot 已提交
56
    Add(const int64_t& nb, const float* xb, const int64_t* ids, const Config& cfg) override;
57 58 59 60 61

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

    Status
G
groot 已提交
62
    Load(const zilliz::knowhere::BinarySet& index_binary) override;
63 64 65 66 67 68 69 70

    VecIndexPtr
    Clone() override;

    int64_t
    GetDeviceId() override;

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

X
xj.lin 已提交
73
 protected:
X
xj.lin 已提交
74
    int64_t dim = 0;
75

X
xj.lin 已提交
76
    IndexType type = IndexType::INVALID;
77

X
MS-154  
xj.lin 已提交
78 79 80
    std::shared_ptr<zilliz::knowhere::VectorIndex> index_ = nullptr;
};

X
xj.lin 已提交
81 82
class IVFMixIndex : public VecIndexImpl {
 public:
G
groot 已提交
83
    explicit IVFMixIndex(std::shared_ptr<zilliz::knowhere::VectorIndex> index, const IndexType& type)
G
groot 已提交
84 85
        : VecIndexImpl(std::move(index), type) {
    }
X
xj.lin 已提交
86

87
    Status
G
groot 已提交
88 89
    BuildAll(const int64_t& nb, const float* xb, const int64_t* ids, const Config& cfg, const int64_t& nt,
             const float* xt) override;
90 91

    Status
G
groot 已提交
92
    Load(const zilliz::knowhere::BinarySet& index_binary) override;
X
xj.lin 已提交
93 94
};

X
xj.lin 已提交
95 96
class BFIndex : public VecIndexImpl {
 public:
G
groot 已提交
97 98
    explicit BFIndex(std::shared_ptr<zilliz::knowhere::VectorIndex> index)
        : VecIndexImpl(std::move(index), IndexType::FAISS_IDMAP) {
G
groot 已提交
99
    }
100 101

    ErrorCode
G
groot 已提交
102
    Build(const Config& cfg);
103

G
groot 已提交
104
    float*
105 106 107
    GetRawVectors();

    Status
G
groot 已提交
108 109 110 111
    BuildAll(const int64_t& nb, const float* xb, const int64_t* ids, const Config& cfg, const int64_t& nt,
             const float* xt) override;

    int64_t*
112
    GetRawIds();
X
xj.lin 已提交
113 114
};

G
groot 已提交
115 116 117
}  // namespace engine
}  // namespace milvus
}  // namespace zilliz