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

import java.nio.ByteBuffer;

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

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

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

    @Override
    public Object decode(ByteBuffer b) {
        long dataLen = 0;
        long len = ((long) b.getInt()) & 0xffffffffL;
        boolean compressed;
        String field;

        // if MSB is set this is a compressed field.  set the compressed
        // flag accordingly and decompress the data
        compressed = ((len & 0x80000000) != 0);
        if (compressed) {
            len &= 0x7fffffff;
            dataLen = b.getInt();
            len -= 4;
        }

        byte[] tmp = new byte[(int) len];

        b.get(tmp);

        if (compressed) {
            SlowInflate inf = new SlowInflate();

            tmp = inf.unpack(tmp, (int) dataLen);
        }
        field = Utf8.toString(tmp);
        return field;
    }

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

    @Override
    public int getLength(ByteBuffer b) {
        int offset = b.position();
        // MSB = compression flag, re decode
        int len = b.getInt() & 0x7fffffff;
        b.position(offset + len + (Integer.SIZE >> 3));
        return len + (Integer.SIZE >> 3);
    }

    @Override
    public boolean isCompressed(ByteBuffer b) {
        int offset = b.position();
        // MSB = compression flag, re decode
        int compressed = b.getInt() & 0x80000000;
        b.position(offset);
        return compressed != 0;
    }

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

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