aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/schema/derived/Index.java
blob: 2f5b674abee61949915a78855b96eed376f3273f (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.derived;

import com.yahoo.document.CollectionDataType;
import com.yahoo.document.DataType;
import com.yahoo.document.NumericDataType;
import com.yahoo.document.datatypes.*;

/**
 * A type of an index structure
 *
 * @author bratseth
 */
public class Index {

    /** The index type enumeration */
    public static class Type {

        public static final Type TEXT = new Type("text");
        public static final Type INT64 = new Type("long");
        public static final Type BOOLEANTREE = new Type("booleantree");

        private final String name;

        private Type(String name) {
            this.name=name;
        }

        public int hashCode() {
            return name.hashCode();
        }

        public String getName() { return name; }

        public boolean equals(Object other) {
            if ( ! (other instanceof Type)) return false;
            return this.name.equals(((Type)other).name);
        }

        public String toString() {
            return "type: " + name;
        }

    }

    /** Sets the right index type from a field type */
    public static Type convertType(DataType fieldType) {
        FieldValue fval = fieldType.createFieldValue();
        if (fieldType instanceof NumericDataType) {
            return Type.INT64;
        } else if (fval instanceof StringFieldValue) {
            return Type.TEXT;
        } else if (fval instanceof Raw) {
            return Type.BOOLEANTREE;
        } else if (fval instanceof PredicateFieldValue) {
            return Type.BOOLEANTREE;
        } else if (fieldType instanceof CollectionDataType) {
            return convertType(((CollectionDataType) fieldType).getNestedType());
        } else {
            throw new IllegalArgumentException("Don't know which index type to " +
                    "convert " + fieldType + " to");
        }
    }
}