blob: 0ccc8b03e3bb863201c40c8b476745979679ad34 (
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
|
// Copyright 2016 Yahoo Inc. 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.io.SlowInflate;
import com.yahoo.prelude.hitfield.XMLString;
import com.yahoo.text.Utf8;
import com.yahoo.data.access.Inspector;
/**
* @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
*/
public class XMLField extends DocsumField implements VariableLengthField {
public XMLField(String name) {
super(name);
}
private Object convert(String value) {
return new XMLString(value);
}
@Override
public Object decode(ByteBuffer b) {
long dataLen = 0;
long len = ((long) b.getInt()) & 0xffffffffL;
boolean compressed;
// 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);
}
return convert(Utf8.toString(tmp));
}
@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 XMLString";
}
@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;
}
public Object convert(Inspector value) {
return convert(value.asString(""));
}
}
|