Operand.cpp 3.3 KB
Newer Older
X
xj.lin 已提交
1 2 3 4 5 6
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////

Y
yu yunfeng 已提交
7
#include "src/server/ServerConfig.h"
X
xj.lin 已提交
8 9
#include "Operand.h"

X
xj.lin 已提交
10

X
xj.lin 已提交
11
namespace zilliz {
J
jinhai 已提交
12
namespace milvus {
X
xj.lin 已提交
13 14
namespace engine {

X
xj.lin 已提交
15 16 17 18 19
using std::string;

enum IndexType {
    Invalid_Option = 0,
    IVF = 1,
S
starlord 已提交
20 21
    IDMAP = 2,
    IVFSQ8 = 3,
X
xj.lin 已提交
22 23 24 25 26
};

IndexType resolveIndexType(const string &index_type) {
    if (index_type == "IVF") { return IndexType::IVF; }
    if (index_type == "IDMap") { return IndexType::IDMAP; }
S
starlord 已提交
27
    if (index_type == "IVFSQ8") { return IndexType::IVFSQ8; }
X
xj.lin 已提交
28 29 30
    return IndexType::Invalid_Option;
}

S
starlord 已提交
31 32 33 34 35 36 37 38 39
int CalcBacketCount(int nb, size_t nlist) {
    int backet_count = int(nb / 1000000.0 * nlist);
    if(backet_count == 0) {
        backet_count = 1; //avoid faiss rash
    }

    return backet_count;
}

X
xj.lin 已提交
40 41 42 43 44 45 46 47 48 49
// nb at least 100
string Operand::get_index_type(const int &nb) {
    if (!index_str.empty()) { return index_str; }

    switch (resolveIndexType(index_type)) {
        case Invalid_Option: {
            // TODO: add exception
            break;
        }
        case IVF: {
Y
yu yunfeng 已提交
50 51 52 53 54 55

            using namespace zilliz::milvus::server;
            ServerConfig &config = ServerConfig::GetInstance();
            ConfigNode engine_config = config.GetConfig(CONFIG_ENGINE);
            size_t nlist = engine_config.GetInt32Value(CONFIG_NLIST, 16384);

X
xj.lin 已提交
56
            index_str += (ncent != 0 ? index_type + std::to_string(ncent) :
S
starlord 已提交
57
                          index_type + std::to_string(CalcBacketCount(nb, nlist)));
S
starlord 已提交
58 59 60 61 62 63 64 65 66 67 68 69
//            std::cout<<"nlist = "<<nlist<<std::endl;
            if (!postproc.empty()) { index_str += ("," + postproc); }
            break;
        }
        case IVFSQ8: {

            using namespace zilliz::milvus::server;
            ServerConfig &config = ServerConfig::GetInstance();
            ConfigNode engine_config = config.GetConfig(CONFIG_ENGINE);
            size_t nlist = engine_config.GetInt32Value(CONFIG_NLIST, 16384);

            index_str += (ncent != 0 ? "IVF" + std::to_string(ncent) :
S
starlord 已提交
70
                          "IVF" + std::to_string(CalcBacketCount(nb, nlist)));
S
starlord 已提交
71
            index_str += ",SQ8";
Y
yu yunfeng 已提交
72
//            std::cout<<"nlist = "<<nlist<<std::endl;
X
xj.lin 已提交
73 74 75 76
            break;
        }
        case IDMAP: {
            index_str += index_type;
S
starlord 已提交
77
            if (!postproc.empty()) { index_str += ("," + postproc); }
X
xj.lin 已提交
78 79 80 81 82 83 84
            break;
        }
    }

    return index_str;
}

X
xj.lin 已提交
85 86 87
std::ostream &operator<<(std::ostream &os, const Operand &obj) {
    os << obj.d << " "
       << obj.index_type << " "
X
xj.lin 已提交
88
       << obj.metric_type << " "
X
xj.lin 已提交
89 90 91 92 93 94 95 96 97
       << obj.preproc << " "
       << obj.postproc << " "
       << obj.ncent;
    return os;
}

std::istream &operator>>(std::istream &is, Operand &obj) {
    is >> obj.d
       >> obj.index_type
X
xj.lin 已提交
98
       >> obj.metric_type
X
xj.lin 已提交
99 100 101 102 103 104 105 106
       >> obj.preproc
       >> obj.postproc
       >> obj.ncent;
    return is;
}

std::string operand_to_str(const Operand_ptr &opd) {
    std::ostringstream ss;
X
xj.lin 已提交
107
    ss << *opd;
X
xj.lin 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121
    return ss.str();
}

Operand_ptr str_to_operand(const std::string &input) {
    std::istringstream is(input);
    auto opd = std::make_shared<Operand>();
    is >> *(opd.get());

    return opd;
}

}
}
}