aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/configconverter.cpp
blob: ac0d94d80b57676443642081961277e5512cfb7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "configconverter.h"
#include <vespa/searchcommon/attribute/config.h>

using namespace vespa::config::search;

namespace search::attribute {

namespace {

using vespalib::eval::ValueType;
using vespalib::eval::CellType;

using DataTypeMap = std::map<AttributesConfig::Attribute::Datatype, BasicType::Type>;
using CollectionTypeMap = std::map<AttributesConfig::Attribute::Collectiontype, CollectionType::Type>;

DataTypeMap
getDataTypeMap()
{
    DataTypeMap map;
    map[AttributesConfig::Attribute::Datatype::STRING] = BasicType::STRING;
    map[AttributesConfig::Attribute::Datatype::BOOL] = BasicType::BOOL;
    map[AttributesConfig::Attribute::Datatype::UINT2] = BasicType::UINT2;
    map[AttributesConfig::Attribute::Datatype::UINT4] = BasicType::UINT4;
    map[AttributesConfig::Attribute::Datatype::INT8] = BasicType::INT8;
    map[AttributesConfig::Attribute::Datatype::INT16] = BasicType::INT16;
    map[AttributesConfig::Attribute::Datatype::INT32] = BasicType::INT32;
    map[AttributesConfig::Attribute::Datatype::INT64] = BasicType::INT64;
    map[AttributesConfig::Attribute::Datatype::FLOAT] = BasicType::FLOAT;
    map[AttributesConfig::Attribute::Datatype::DOUBLE] = BasicType::DOUBLE;
    map[AttributesConfig::Attribute::Datatype::PREDICATE] = BasicType::PREDICATE;
    map[AttributesConfig::Attribute::Datatype::TENSOR] = BasicType::TENSOR;
    map[AttributesConfig::Attribute::Datatype::REFERENCE] = BasicType::REFERENCE;
    map[AttributesConfig::Attribute::Datatype::RAW] = BasicType::RAW;
    map[AttributesConfig::Attribute::Datatype::NONE] = BasicType::NONE;
    return map;
}

CollectionTypeMap
getCollectionTypeMap()
{
    CollectionTypeMap map;
    map[AttributesConfig::Attribute::Collectiontype::SINGLE] = CollectionType::SINGLE;
    map[AttributesConfig::Attribute::Collectiontype::ARRAY] = CollectionType::ARRAY;
    map[AttributesConfig::Attribute::Collectiontype::WEIGHTEDSET] = CollectionType::WSET;
    return map;
}

DataTypeMap _dataTypeMap = getDataTypeMap();
CollectionTypeMap _collectionTypeMap = getCollectionTypeMap();

DictionaryConfig::Type
convert(AttributesConfig::Attribute::Dictionary::Type type_cfg) {
    switch (type_cfg) {
    case AttributesConfig::Attribute::Dictionary::Type::BTREE:
        return DictionaryConfig::Type::BTREE;
    case AttributesConfig::Attribute::Dictionary::Type::HASH:
        return DictionaryConfig::Type::HASH;
    case AttributesConfig::Attribute::Dictionary::Type::BTREE_AND_HASH:
        return DictionaryConfig::Type::BTREE_AND_HASH;
    }
    assert(false);
}

DictionaryConfig::Match
convert(AttributesConfig::Attribute::Dictionary::Match match_cfg) {
    switch (match_cfg) {
    case AttributesConfig::Attribute::Dictionary::Match::CASE_SENSITIVE:
    case AttributesConfig::Attribute::Dictionary::Match::CASED:
        return DictionaryConfig::Match::CASED;
    case AttributesConfig::Attribute::Dictionary::Match::CASE_INSENSITIVE:
    case AttributesConfig::Attribute::Dictionary::Match::UNCASED:
        return DictionaryConfig::Match::UNCASED;
    }
    assert(false);
}

DictionaryConfig
convert_dictionary(const AttributesConfig::Attribute::Dictionary & dictionary) {
    return {convert(dictionary.type), convert(dictionary.match)};
}

Config::Match
convertMatch(AttributesConfig::Attribute::Match match_cfg) {
    switch (match_cfg) {
        case AttributesConfig::Attribute::Match::CASED:
            return Config::Match::CASED;
        case AttributesConfig::Attribute::Match::UNCASED:
            return Config::Match::UNCASED;
    }
    assert(false);
}

}

Config
ConfigConverter::convert(const AttributesConfig::Attribute & cfg)
{
    BasicType bType(_dataTypeMap[cfg.datatype]);
    CollectionType cType(_collectionTypeMap[cfg.collectiontype]);
    cType.removeIfZero(cfg.removeifzero);
    cType.createIfNonExistant(cfg.createifnonexistent);
    Config retval(bType, cType);
    PredicateParams predicateParams;
    retval.setFastSearch(cfg.fastsearch);
    retval.setIsFilter(cfg.enableonlybitvector);
    retval.setFastAccess(cfg.fastaccess);
    retval.setMutable(cfg.ismutable);
    retval.setPaged(cfg.paged);
    retval.setMaxUnCommittedMemory(cfg.maxuncommittedmemory);
    predicateParams.setArity(cfg.arity);
    predicateParams.setBounds(cfg.lowerbound, cfg.upperbound);
    predicateParams.setDensePostingListThreshold(cfg.densepostinglistthreshold);
    retval.setPredicateParams(predicateParams);
    retval.set_dictionary_config(convert_dictionary(cfg.dictionary));
    retval.set_match(convertMatch(cfg.match));
    using CfgDm = AttributesConfig::Attribute::Distancemetric;
    DistanceMetric dm(DistanceMetric::Euclidean);
    switch (cfg.distancemetric) {
        case CfgDm::EUCLIDEAN:
            dm = DistanceMetric::Euclidean;
            break;
        case CfgDm::ANGULAR:
            dm = DistanceMetric::Angular;
            break;
        case CfgDm::GEODEGREES:
            dm = DistanceMetric::GeoDegrees;
            break;
        case CfgDm::INNERPRODUCT:
            dm = DistanceMetric::InnerProduct;
            break;
        case CfgDm::HAMMING:
            dm = DistanceMetric::Hamming;
            break;
        case CfgDm::PRENORMALIZED_ANGULAR:
            dm = DistanceMetric::PrenormalizedAngular;
            break;
        case CfgDm::DOTPRODUCT:
            dm = DistanceMetric::Dotproduct;
            break;
    }
    retval.set_distance_metric(dm);
    if (cfg.index.hnsw.enabled) {
        retval.set_hnsw_index_params(HnswIndexParams(cfg.index.hnsw.maxlinkspernode,
                                                     cfg.index.hnsw.neighborstoexploreatinsert,
                                                     dm, cfg.index.hnsw.multithreadedindexing));
    }
    if (retval.basicType().type() == BasicType::Type::TENSOR) {
        if (!cfg.tensortype.empty()) {
            retval.setTensorType(ValueType::from_spec(cfg.tensortype));
        } else {
            retval.setTensorType(ValueType::double_type());
        }
    }
    return retval;
}

}