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

#include "Operand.h"

X
xj.lin 已提交
9

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

X
xj.lin 已提交
14 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: {
            index_str += (ncent != 0 ? index_type + std::to_string(ncent) :
Y
yu yunfeng 已提交
42
                          index_type + std::to_string(int(nb / 1000000.0 * 1638)));
X
xj.lin 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55
            break;
        }
        case IDMAP: {
            index_str += index_type;
            break;
        }
    }

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

X
xj.lin 已提交
56 57 58
std::ostream &operator<<(std::ostream &os, const Operand &obj) {
    os << obj.d << " "
       << obj.index_type << " "
X
xj.lin 已提交
59
       << obj.metric_type << " "
X
xj.lin 已提交
60 61 62 63 64 65 66 67 68
       << 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 已提交
69
       >> obj.metric_type
X
xj.lin 已提交
70 71 72 73 74 75 76 77
       >> obj.preproc
       >> obj.postproc
       >> obj.ncent;
    return is;
}

std::string operand_to_str(const Operand_ptr &opd) {
    std::ostringstream ss;
X
xj.lin 已提交
78
    ss << *opd;
X
xj.lin 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92
    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;
}

}
}
}