Collection.cpp 5.1 KB
Newer Older
1
#include "Collection.h"
2 3 4
#include "pb/common.pb.h"
#include "pb/schema.pb.h"
#include "pb/etcd_meta.pb.h"
5
#include <google/protobuf/text_format.h>
B
bigsheeper 已提交
6
#include <knowhere/index/vector_index/adapter/VectorAdapter.h>
F
FluorineDog 已提交
7
#include <cstring>
B
bigsheeper 已提交
8

G
GuoRentong 已提交
9
namespace milvus::segcore {
B
bigsheeper 已提交
10

11 12
Collection::Collection(std::string& collection_name, std::string& schema)
    : collection_name_(collection_name), schema_json_(schema) {
Z
zhenshan.cao 已提交
13
    parse();
B
bigsheeper 已提交
14
    index_ = nullptr;
Z
zhenshan.cao 已提交
15
}
F
FluorineDog 已提交
16
#if 0
B
bigsheeper 已提交
17 18 19 20 21
void
Collection::AddIndex(const grpc::IndexParam& index_param) {
    auto& index_name = index_param.index_name();
    auto& field_name = index_param.field_name();

B
bigsheeper 已提交
22 23
    Assert(!index_name.empty());
    Assert(!field_name.empty());
B
bigsheeper 已提交
24 25 26 27 28 29 30 31 32 33

    auto index_type = knowhere::IndexEnum::INDEX_FAISS_IVFPQ;
    auto index_mode = knowhere::IndexMode::MODE_CPU;
    knowhere::Config index_conf;

    bool found_index_type = false;
    bool found_index_mode = false;
    bool found_index_conf = false;

    auto extra_params = index_param.extra_params();
34
    for (auto& extra_param : extra_params) {
B
bigsheeper 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        if (extra_param.key() == "index_type") {
            index_type = extra_param.value().data();
            found_index_type = true;
            continue;
        }
        if (extra_param.key() == "index_mode") {
            auto index_mode_int = stoi(extra_param.value());
            if (index_mode_int == 0) {
                found_index_mode = true;
                continue;
            } else if (index_mode_int == 1) {
                index_mode = knowhere::IndexMode::MODE_GPU;
                found_index_mode = true;
                continue;
            } else {
                throw std::runtime_error("Illegal index mode, only 0 or 1 is supported.");
            }
        }
        if (extra_param.key() == "params") {
            index_conf = nlohmann::json::parse(extra_param.value());
            found_index_conf = true;
            continue;
        }
    }

    if (!found_index_type) {
        std::cout << "WARN: Not specify index type, use default index type: INDEX_FAISS_IVFPQ" << std::endl;
    }
    if (!found_index_mode) {
        std::cout << "WARN: Not specify index mode, use default index mode: MODE_CPU" << std::endl;
    }
    if (!found_index_conf) {
        int dim = 0;

69
        for (auto& field : schema_->get_fields()) {
B
bigsheeper 已提交
70
            if (field.get_data_type() == DataType::VECTOR_FLOAT) {
71
                dim = field.get_dim();
B
bigsheeper 已提交
72 73
            }
        }
B
bigsheeper 已提交
74
        Assert(dim != 0);
B
bigsheeper 已提交
75 76

        index_conf = milvus::knowhere::Config{
77 78 79 80
            {knowhere::meta::DIM, dim},         {knowhere::IndexParams::nlist, 100},
            {knowhere::IndexParams::nprobe, 4}, {knowhere::IndexParams::m, 4},
            {knowhere::IndexParams::nbits, 8},  {knowhere::Metric::TYPE, milvus::knowhere::Metric::L2},
            {knowhere::meta::DEVICEID, 0},
B
bigsheeper 已提交
81 82 83 84 85 86 87
        };
        std::cout << "WARN: Not specify index config, use default index config" << std::endl;
    }

    index_->AddEntry(index_name, field_name, index_type, index_mode, index_conf);
}

B
bigsheeper 已提交
88
void
89 90
Collection::CreateIndex(std::string& index_config) {
    if (index_config.empty()) {
B
bigsheeper 已提交
91 92 93 94 95
        index_ = nullptr;
        std::cout << "null index config when create index" << std::endl;
        return;
    }

96 97
    milvus::proto::etcd::CollectionMeta collection_meta;
    auto suc = google::protobuf::TextFormat::ParseFromString(index_config, &collection_meta);
B
bigsheeper 已提交
98 99 100 101 102 103 104

    if (!suc) {
        std::cerr << "unmarshal index string failed" << std::endl;
    }

    index_ = std::make_shared<IndexMeta>(schema_);

105 106 107 108 109
    // for (const auto& index : collection_meta.indexes()) {
    //     std::cout << "add index, index name =" << index.index_name() << ", field_name = " << index.field_name()
    //               << std::endl;
    //     AddIndex(index);
    // }
B
bigsheeper 已提交
110
}
F
FluorineDog 已提交
111
#endif
B
bigsheeper 已提交
112

B
bigsheeper 已提交
113
void
Z
zhenshan.cao 已提交
114
Collection::parse() {
115
    if (schema_json_.empty()) {
B
bigsheeper 已提交
116
        std::cout << "WARN: Use default schema" << std::endl;
117 118 119 120 121 122 123
        auto schema = std::make_shared<Schema>();
        schema->AddField("fakevec", DataType::VECTOR_FLOAT, 16);
        schema->AddField("age", DataType::INT32);
        schema_ = schema;
        return;
    }

124 125
    milvus::proto::etcd::CollectionMeta collection_meta;
    auto suc = google::protobuf::TextFormat::ParseFromString(schema_json_, &collection_meta);
126 127

    if (!suc) {
B
bigsheeper 已提交
128
        std::cerr << "unmarshal schema string failed" << std::endl;
129
    }
Z
zhenshan.cao 已提交
130
    auto schema = std::make_shared<Schema>();
131
    for (const milvus::proto::schema::FieldSchema& child : collection_meta.schema().fields()) {
G
GuoRentong 已提交
132
        const auto& type_params = child.type_params();
133
        int dim = 16;
G
GuoRentong 已提交
134 135
        for (const auto& type_param : type_params) {
            if (type_param.key() == "dim") {
F
FluorineDog 已提交
136
                dim = strtoll(type_param.value().c_str(), nullptr, 10);
137 138
            }
        }
G
GuoRentong 已提交
139 140
        std::cout << "add Field, name :" << child.name() << ", datatype :" << child.data_type() << ", dim :" << dim
                  << std::endl;
141
        schema->AddField(std::string_view(child.name()), DataType(child.data_type()), dim);
142 143
    }
    /*
Z
zhenshan.cao 已提交
144 145
    schema->AddField("fakevec", DataType::VECTOR_FLOAT, 16);
    schema->AddField("age", DataType::INT32);
146
    */
Z
zhenshan.cao 已提交
147
    schema_ = schema;
B
bigsheeper 已提交
148
}
Z
zhenshan.cao 已提交
149

G
GuoRentong 已提交
150
}  // namespace milvus::segcore