summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/fastsearch/StringField.java
blob: 92ea7efee2528a9b93623a84f4c862b8144a592b (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 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * Class representing a string field in the result set
 *
 */
package com.yahoo.prelude.fastsearch;


import java.nio.ByteBuffer;

import com.yahoo.text.Utf8;
import com.yahoo.data.access.Inspector;


/**
 * @author Bjørn Borud
 */
public class StringField extends DocsumField implements VariableLengthField {

    public StringField(String name) {
        super(name);
    }

    @Override
    public Object decode(ByteBuffer b) {
        int length = ((int) b.getShort()) & 0xffff;
        Object field;

        field = Utf8.toString(b.array(), b.arrayOffset() + b.position(), length);
        b.position(b.position() + length);
        return field;
    }

    @Override
    public Object decode(ByteBuffer b, FastHit hit) {
        Object field = decode(b);
        hit.setField(name, field);
        return field;
    }

    @Override
    public String toString() {
        return "field " + getName() + " type string";
    }

    @Override
    public int getLength(ByteBuffer b) {
        int offset = b.position();
        int len = ((int) b.getShort()) & 0xffff;
        b.position(offset + len + (Short.SIZE >> 3));
        return len + (Short.SIZE >> 3);
    }

    @Override
    public int sizeOfLength() {
        return Short.SIZE >> 3;
    }

    @Override
    public Object convert(Inspector value) {
        return value.asString("");
    }

}