Operand.cpp 3.3 KB
Newer Older
J
jinhai 已提交
1

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

X
xj.lin 已提交
8
#if 0
X
xj.lin 已提交
9 10
#include "Operand.h"

X
xj.lin 已提交
11

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

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

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

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

S
starlord 已提交
32 33 34 35 36 37 38 39 40
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 已提交
41 42 43 44 45 46 47 48 49 50
// 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 已提交
51 52 53 54 55 56

            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 已提交
57
            index_str += (ncent != 0 ? index_type + std::to_string(ncent) :
S
starlord 已提交
58
                          index_type + std::to_string(CalcBacketCount(nb, nlist)));
S
starlord 已提交
59 60 61 62 63 64 65 66 67 68 69 70
//            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 已提交
71
                          "IVF" + std::to_string(CalcBacketCount(nb, nlist)));
S
starlord 已提交
72
            index_str += ",SQ8";
Y
yu yunfeng 已提交
73
//            std::cout<<"nlist = "<<nlist<<std::endl;
X
xj.lin 已提交
74 75 76 77
            break;
        }
        case IDMAP: {
            index_str += index_type;
S
starlord 已提交
78
            if (!postproc.empty()) { index_str += ("," + postproc); }
X
xj.lin 已提交
79 80 81 82 83 84 85
            break;
        }
    }

    return index_str;
}

X
xj.lin 已提交
86 87 88
std::ostream &operator<<(std::ostream &os, const Operand &obj) {
    os << obj.d << " "
       << obj.index_type << " "
X
xj.lin 已提交
89
       << obj.metric_type << " "
X
xj.lin 已提交
90 91 92 93 94 95 96 97 98
       << 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 已提交
99
       >> obj.metric_type
X
xj.lin 已提交
100 101 102 103 104 105 106 107
       >> obj.preproc
       >> obj.postproc
       >> obj.ncent;
    return is;
}

std::string operand_to_str(const Operand_ptr &opd) {
    std::ostringstream ss;
X
xj.lin 已提交
108
    ss << *opd;
X
xj.lin 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122
    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;
}

}
}
}
X
xj.lin 已提交
123
#endif