Skip to content

Commit ba9a12e

Browse files
committed
Mapper: Add byte type (8bit signed), closes elastic#620.
1 parent 38ea07c commit ba9a12e

File tree

18 files changed

+1446
-4
lines changed

18 files changed

+1446
-4
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/common/Numbers.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private Numbers() {
3737
* @return The int converted
3838
*/
3939
public static short bytesToShort(byte[] arr) {
40-
return (short) (((arr[2] & 0xff) << 8) | (arr[3] & 0xff));
40+
return (short) (((arr[0] & 0xff) << 8) | (arr[1] & 0xff));
4141
}
4242

4343
/**
@@ -105,8 +105,8 @@ public static byte[] intToBytes(int val) {
105105
*/
106106
public static byte[] shortToBytes(int val) {
107107
byte[] arr = new byte[2];
108-
arr[2] = (byte) (val >>> 8);
109-
arr[3] = (byte) (val);
108+
arr[0] = (byte) (val >>> 8);
109+
arr[1] = (byte) (val);
110110
return arr;
111111
}
112112

modules/elasticsearch/src/main/java/org/elasticsearch/common/xcontent/support/XContentMapValues.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ public static short nodeShortValue(Object node) {
8080
return Short.parseShort(node.toString());
8181
}
8282

83+
public static byte nodeByteValue(Object node) {
84+
if (node instanceof Number) {
85+
return ((Number) node).byteValue();
86+
}
87+
return Byte.parseByte(node.toString());
88+
}
89+
8390
public static long nodeLongValue(Object node) {
8491
if (node instanceof Number) {
8592
return ((Number) node).longValue();

modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/FieldDataType.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.lucene.index.IndexReader;
2323
import org.apache.lucene.search.FieldComparatorSource;
2424
import org.elasticsearch.index.cache.field.data.FieldDataCache;
25+
import org.elasticsearch.index.field.data.bytes.ByteFieldDataType;
2526
import org.elasticsearch.index.field.data.doubles.DoubleFieldDataType;
2627
import org.elasticsearch.index.field.data.floats.FloatFieldDataType;
2728
import org.elasticsearch.index.field.data.ints.IntFieldDataType;
@@ -38,6 +39,7 @@ public interface FieldDataType<T extends FieldData> {
3839

3940
public static final class DefaultTypes {
4041
public static final StringFieldDataType STRING = new StringFieldDataType();
42+
public static final ByteFieldDataType BYTE = new ByteFieldDataType();
4143
public static final ShortFieldDataType SHORT = new ShortFieldDataType();
4244
public static final IntFieldDataType INT = new IntFieldDataType();
4345
public static final LongFieldDataType LONG = new LongFieldDataType();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.field.data.bytes;
21+
22+
import org.elasticsearch.index.field.data.NumericDocFieldData;
23+
24+
/**
25+
* @author kimchy (shay.banon)
26+
*/
27+
public class ByteDocFieldData extends NumericDocFieldData<ByteFieldData> {
28+
29+
public ByteDocFieldData(ByteFieldData fieldData) {
30+
super(fieldData);
31+
}
32+
33+
public byte getValue() {
34+
return fieldData.value(docId);
35+
}
36+
37+
public byte[] getValues() {
38+
return fieldData.values(docId);
39+
}
40+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.field.data.bytes;
21+
22+
import org.apache.lucene.index.IndexReader;
23+
import org.apache.lucene.search.FieldCache;
24+
import org.elasticsearch.common.RamUsage;
25+
import org.elasticsearch.common.trove.TByteArrayList;
26+
import org.elasticsearch.index.field.data.FieldDataType;
27+
import org.elasticsearch.index.field.data.NumericFieldData;
28+
import org.elasticsearch.index.field.data.support.FieldDataLoader;
29+
30+
import java.io.IOException;
31+
32+
/**
33+
* @author kimchy (shay.banon)
34+
*/
35+
public abstract class ByteFieldData extends NumericFieldData<ByteDocFieldData> {
36+
37+
static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
38+
39+
protected final byte[] values;
40+
41+
protected ByteFieldData(String fieldName, byte[] values) {
42+
super(fieldName);
43+
this.values = values;
44+
}
45+
46+
@Override protected long computeSizeInBytes() {
47+
return 1 * values.length + RamUsage.NUM_BYTES_ARRAY_HEADER;
48+
}
49+
50+
abstract public byte value(int docId);
51+
52+
abstract public byte[] values(int docId);
53+
54+
@Override public ByteDocFieldData docFieldData(int docId) {
55+
return super.docFieldData(docId);
56+
}
57+
58+
@Override protected ByteDocFieldData createFieldData() {
59+
return new ByteDocFieldData(this);
60+
}
61+
62+
@Override public void forEachValue(StringValueProc proc) {
63+
for (int i = 1; i < values.length; i++) {
64+
proc.onValue(Byte.toString(values[i]));
65+
}
66+
}
67+
68+
@Override public String stringValue(int docId) {
69+
return Byte.toString(value(docId));
70+
}
71+
72+
@Override public byte byteValue(int docId) {
73+
return value(docId);
74+
}
75+
76+
@Override public short shortValue(int docId) {
77+
return value(docId);
78+
}
79+
80+
@Override public int intValue(int docId) {
81+
return (int) value(docId);
82+
}
83+
84+
@Override public long longValue(int docId) {
85+
return (long) value(docId);
86+
}
87+
88+
@Override public float floatValue(int docId) {
89+
return (float) value(docId);
90+
}
91+
92+
@Override public double doubleValue(int docId) {
93+
return (double) value(docId);
94+
}
95+
96+
@Override public FieldDataType type() {
97+
return FieldDataType.DefaultTypes.BYTE;
98+
}
99+
100+
public void forEachValue(ValueProc proc) {
101+
for (int i = 1; i < values.length; i++) {
102+
proc.onValue(values[i]);
103+
}
104+
}
105+
106+
public static interface ValueProc {
107+
void onValue(byte value);
108+
}
109+
110+
public abstract void forEachValueInDoc(int docId, ValueInDocProc proc);
111+
112+
public static interface ValueInDocProc {
113+
void onValue(int docId, byte value);
114+
}
115+
116+
public static ByteFieldData load(IndexReader reader, String field) throws IOException {
117+
return FieldDataLoader.load(reader, field, new ByteTypeLoader());
118+
}
119+
120+
static class ByteTypeLoader extends FieldDataLoader.FreqsTypeLoader<ByteFieldData> {
121+
122+
private final TByteArrayList terms = new TByteArrayList();
123+
124+
ByteTypeLoader() {
125+
super();
126+
// the first one indicates null value
127+
terms.add((byte) 0);
128+
}
129+
130+
@Override public void collectTerm(String term) {
131+
terms.add((byte) FieldCache.NUMERIC_UTILS_INT_PARSER.parseInt(term));
132+
}
133+
134+
@Override public ByteFieldData buildSingleValue(String field, int[] ordinals) {
135+
return new SingleValueByteFieldData(field, ordinals, terms.toNativeArray());
136+
}
137+
138+
@Override public ByteFieldData buildMultiValue(String field, int[][] ordinals) {
139+
return new MultiValueByteFieldData(field, ordinals, terms.toNativeArray());
140+
}
141+
}
142+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.field.data.bytes;
21+
22+
import org.elasticsearch.index.cache.field.data.FieldDataCache;
23+
import org.elasticsearch.index.field.data.FieldDataType;
24+
import org.elasticsearch.index.field.data.support.NumericFieldDataComparator;
25+
26+
/**
27+
* @author kimchy (shay.banon)
28+
*/
29+
// LUCENE MONITOR: Monitor against FieldComparator.Short
30+
public class ByteFieldDataComparator extends NumericFieldDataComparator {
31+
32+
private final byte[] values;
33+
private short bottom;
34+
35+
public ByteFieldDataComparator(int numHits, String fieldName, FieldDataCache fieldDataCache) {
36+
super(fieldName, fieldDataCache);
37+
values = new byte[numHits];
38+
}
39+
40+
@Override public FieldDataType fieldDataType() {
41+
return FieldDataType.DefaultTypes.BYTE;
42+
}
43+
44+
@Override public int compare(int slot1, int slot2) {
45+
return values[slot1] - values[slot2];
46+
}
47+
48+
@Override public int compareBottom(int doc) {
49+
return bottom - currentFieldData.shortValue(doc);
50+
}
51+
52+
@Override public void copy(int slot, int doc) {
53+
values[slot] = currentFieldData.byteValue(doc);
54+
}
55+
56+
@Override public void setBottom(final int bottom) {
57+
this.bottom = values[bottom];
58+
}
59+
60+
@Override public Comparable value(int slot) {
61+
return Byte.valueOf(values[slot]);
62+
}
63+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.field.data.bytes;
21+
22+
import org.apache.lucene.index.IndexReader;
23+
import org.apache.lucene.search.FieldComparator;
24+
import org.apache.lucene.search.FieldComparatorSource;
25+
import org.elasticsearch.index.cache.field.data.FieldDataCache;
26+
import org.elasticsearch.index.field.data.FieldDataType;
27+
import org.elasticsearch.index.field.data.shorts.ShortFieldDataComparator;
28+
29+
import java.io.IOException;
30+
31+
/**
32+
* @author kimchy (shay.banon)
33+
*/
34+
public class ByteFieldDataType implements FieldDataType<ByteFieldData> {
35+
36+
@Override public FieldComparatorSource newFieldComparatorSource(final FieldDataCache cache) {
37+
return new FieldComparatorSource() {
38+
@Override public FieldComparator newComparator(String fieldname, int numHits, int sortPos, boolean reversed) throws IOException {
39+
return new ShortFieldDataComparator(numHits, fieldname, cache);
40+
}
41+
};
42+
}
43+
44+
@Override public ByteFieldData load(IndexReader reader, String fieldName) throws IOException {
45+
return ByteFieldData.load(reader, fieldName);
46+
}
47+
}

0 commit comments

Comments
 (0)