aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchcommon/attribute/collectiontype.h
blob: 35cb7612ed0c2240470a589df4f897ef9a05d5f9 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/vespalib/stllike/string.h>

namespace search::attribute {

class CollectionType
{
 public:
    enum Type {
        /**
         * Single value type with one value stored for each document.
         **/
        SINGLE = 0,
        /**
         * Array type with zero to n values stored for each document.
         **/
        ARRAY = 1,
        /**
         * Weighted set type with zero to n unique values stored for each document.
         * In addition each unique value is accociated with a weight.
         **/
        WSET = 2,
        MAX_TYPE
    };

    CollectionType(Type t = SINGLE, bool remove = false, bool create = false) :
        _type(t),
        _removeIfZero(remove),
        _createIfNonExistant(create)
    {
    }

    explicit
    CollectionType(const vespalib::string & t, bool remove = false, bool create = false) :
        _type(asType(t)),
        _removeIfZero(remove),
        _createIfNonExistant(create)
    {
    }

    Type type()                const { return _type; }
    bool isMultiValue()        const { return _type != SINGLE; }
    bool isWeightedSet()       const { return _type == WSET; }
    bool isArray()             const { return _type == ARRAY; }
    bool removeIfZero()        const { return _removeIfZero; }
    bool createIfNonExistant() const { return _createIfNonExistant; }
    const char * asString()    const { return asString(_type); }
    void removeIfZero(bool newValue) { _removeIfZero = newValue; }
    void createIfNonExistant(bool newValue) { _createIfNonExistant = newValue; }
    bool operator!=(const CollectionType &b) const { return !(operator==(b)); }
    bool operator==(const CollectionType &b) const {
        return _type == b._type &&
               _removeIfZero == b._removeIfZero &&
               _createIfNonExistant == b._createIfNonExistant;
    }

  private:
    struct TypeInfo {
        Type _type;
        const char * _name;
    };

    static const char * asString(Type t) { return _typeTable[t]._name; }
    static Type asType(const vespalib::string &t);

    Type _type;
    bool _removeIfZero;
    bool _createIfNonExistant;
    static const TypeInfo _typeTable[MAX_TYPE];
};

}