Operand.cpp 2.6 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 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
using std::string;

enum IndexType {
    Invalid_Option = 0,
    IVF = 1,
    IDMAP = 2
};

IndexType resolveIndexType(const string &index_type) {
    if (index_type == "IVF") { return IndexType::IVF; }
    if (index_type == "IDMap") { return IndexType::IDMAP; }
    return IndexType::Invalid_Option;
}

// nb at least 100
string Operand::get_index_type(const int &nb) {
    if (!index_str.empty()) { return index_str; }

    // TODO: support OPQ or ...
    if (!preproc.empty()) { index_str += (preproc + ","); }

    switch (resolveIndexType(index_type)) {
        case Invalid_Option: {
            // TODO: add exception
            break;
        }
        case IVF: {
Y
yu yunfeng 已提交
42 43 44 45 46 47

            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 已提交
48
            index_str += (ncent != 0 ? index_type + std::to_string(ncent) :
Y
yu yunfeng 已提交
49 50
                          index_type + std::to_string(int(nb / 1000000.0 * nlist)));
//            std::cout<<"nlist = "<<nlist<<std::endl;
X
xj.lin 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63
            break;
        }
        case IDMAP: {
            index_str += index_type;
            break;
        }
    }

    // TODO: support PQ or ...
    if (!postproc.empty()) { index_str += ("," + postproc); }
    return index_str;
}

X
xj.lin 已提交
64 65 66
std::ostream &operator<<(std::ostream &os, const Operand &obj) {
    os << obj.d << " "
       << obj.index_type << " "
X
xj.lin 已提交
67
       << obj.metric_type << " "
X
xj.lin 已提交
68 69 70 71 72 73 74 75 76
       << 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 已提交
77
       >> obj.metric_type
X
xj.lin 已提交
78 79 80 81 82 83 84 85
       >> obj.preproc
       >> obj.postproc
       >> obj.ncent;
    return is;
}

std::string operand_to_str(const Operand_ptr &opd) {
    std::ostringstream ss;
X
xj.lin 已提交
86
    ss << *opd;
X
xj.lin 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100
    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;
}

}
}
}