aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/fastsearch/DocsumField.java
blob: 777919286dd896801854f98934031d9986cb5da0 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.fastsearch;

import com.yahoo.container.search.LegacyEmulationConfig;
import com.yahoo.data.access.Inspector;
import com.yahoo.log.LogLevel;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @author Bjørn Borud
 * @author Steinar Knutsen
 */
public abstract class DocsumField {

    private static final Logger log = Logger.getLogger(DocsumField.class.getName());
    private static FieldFactory fieldFactory;

    private static class FieldFactory {

        Map<String, Constructor<? extends DocsumField>> constructors = new HashMap<>();

        void put(String typename, Class<? extends DocsumField> fieldClass)
                throws NoSuchMethodException, SecurityException {
            Constructor<? extends DocsumField> constructor = fieldClass.getConstructor(String.class);
            constructors.put(typename, constructor);
        }

        DocsumField create(String typename, String name, LegacyEmulationConfig emulConfig)
                throws InstantiationException, IllegalAccessException,
                       IllegalArgumentException, InvocationTargetException {
            DocsumField f = constructors.get(typename).newInstance(name);
            f.emulConfig = emulConfig;
            return f;
        }
    }

    private LegacyEmulationConfig emulConfig;

    final LegacyEmulationConfig getEmulConfig() { return emulConfig; }

    static {
        fieldFactory = new FieldFactory();

        try {
            fieldFactory.put("bool", BoolField.class);
            fieldFactory.put("byte", ByteField.class);
            fieldFactory.put("short", ShortField.class);
            fieldFactory.put("integer", IntegerField.class);
            fieldFactory.put("int64", Int64Field.class);
            fieldFactory.put("float16", Float16Field.class);
            fieldFactory.put("float", FloatField.class);
            fieldFactory.put("double", DoubleField.class);
            fieldFactory.put("string", StringField.class);
            fieldFactory.put("data", DataField.class);
            fieldFactory.put("longstring", LongstringField.class);
            fieldFactory.put("longdata", LongdataField.class);
            fieldFactory.put("jsonstring", StructDataField.class);
            fieldFactory.put("featuredata", FeatureDataField.class);
            fieldFactory.put("xmlstring", XMLField.class);
            fieldFactory.put("tensor", TensorField.class);
        } catch (Exception e) {
            log.log(LogLevel.ERROR, "Could not initialize docsum decoding properly.", e);
        }
    }

    protected String name;

    protected DocsumField(String name) {
        this.name = name;
    }

    /* For unit test only */
    public static DocsumField create(String name, String typename) {
        return create(name, typename, new LegacyEmulationConfig(new LegacyEmulationConfig.Builder()));
    }

    public static DocsumField create(String name, String typename, LegacyEmulationConfig emulConfig) {
        try {
            return fieldFactory.create(typename, name, emulConfig);
        } catch (Exception e) {
            throw new RuntimeException("Unknown field type '" + typename + "'", e);
        }
    }

    public String getName() {
        return name;
    }

    /**
     * Convert a generic value into an object of the appropriate type
     * for this field.
     */
    public abstract Object convert(Inspector value);

    /** Returns whether this is the string field type. */
    boolean isString() { return false; }

}