Skip to content

Apache Lucene open-source search software

add ram bytes

Robert Muir964960eccf31
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene41/Lucene41SimpleDocValuesConsumer.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene41/Lucene41SimpleDocValuesConsumer.java
index 42314100ca6..a802db5ce73 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene41/Lucene41SimpleDocValuesConsumer.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene41/Lucene41SimpleDocValuesConsumer.java
@@ -45,6 +45,10 @@ class Lucene41SimpleDocValuesConsumer extends SimpleDVConsumer {
static final int VERSION_START = 0;
static final int VERSION_CURRENT = VERSION_START;
+ static final byte NUMBER = 0;
+ static final byte BYTES = 1;
+ static final byte FST = 2;
+
final IndexOutput data, meta;
final int maxDoc;
@@ -69,6 +73,7 @@ class Lucene41SimpleDocValuesConsumer extends SimpleDVConsumer {
@Override
public void addNumericField(FieldInfo field, Iterable<Number> values) throws IOException {
meta.writeVInt(field.number);
+ meta.writeByte(NUMBER);
meta.writeLong(data.getFilePointer());
long minValue = Long.MAX_VALUE;
long maxValue = Long.MIN_VALUE;
@@ -161,10 +166,53 @@ class Lucene41SimpleDocValuesConsumer extends SimpleDVConsumer {
}
}
- // nocommit: have SimpleDVConsumer extend SimpleNormsConsumer?
@Override
- public void addBinaryField(FieldInfo field, Iterable<BytesRef> values) throws IOException {
- throw new AssertionError();
+ public void addBinaryField(FieldInfo field, final Iterable<BytesRef> values) throws IOException {
+ // write the byte[] data
+ meta.writeVInt(field.number);
+ meta.writeByte(BYTES);
+ int minLength = Integer.MAX_VALUE;
+ int maxLength = Integer.MIN_VALUE;
+ final long startFP = data.getFilePointer();
+ for(BytesRef v : values) {
+ minLength = Math.min(minLength, v.length);
+ maxLength = Math.max(maxLength, v.length);
+ data.writeBytes(v.bytes, v.offset, v.length);
+ }
+ meta.writeLong(startFP);
+ meta.writeLong(data.getFilePointer() - startFP);
+ meta.writeVInt(minLength);
+ meta.writeVInt(maxLength);
+
+ // if minLength == maxLength, its a fixed-length byte[], we are done (the addresses are implicit)
+ // otherwise, we need to record the length fields...
+ // TODO: make this more efficient. this is just as inefficient as 4.0 codec.... we can do much better.
+ if (minLength != maxLength) {
+ addNumericField(field, new Iterable<Number>() {
+ @Override
+ public Iterator<Number> iterator() {
+ final Iterator<BytesRef> inner = values.iterator();
+ return new Iterator<Number>() {
+ long addr = 0;
+
+ @Override
+ public boolean hasNext() {
+ return inner.hasNext();
+ }
+
+ @Override
+ public Number next() {
+ BytesRef b = inner.next();
+ addr += b.length;
+ return Long.valueOf(addr);
+ }
+
+ @Override
+ public void remove() { throw new UnsupportedOperationException(); }
+ };
+ }
+ });
+ }
}
@Override
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene41/Lucene41SimpleDocValuesProducer.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene41/Lucene41SimpleDocValuesProducer.java
index f55f88b3fa8..c31c2b8facd 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene41/Lucene41SimpleDocValuesProducer.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene41/Lucene41SimpleDocValuesProducer.java
@@ -31,17 +31,26 @@ import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.SortedDocValues;
import org.apache.lucene.store.IndexInput;
+import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.packed.PackedInts;
class Lucene41SimpleDocValuesProducer extends SimpleDVProducer {
+ // metadata maps (just file pointers and minimal stuff)
private final Map<Integer,NumericEntry> numerics;
+ private final Map<Integer,BinaryEntry> binaries;
+ private final Map<Integer,FSTEntry> fsts;
private final IndexInput data;
// ram instances we have already loaded
- private final Map<Integer,NumericDocValues> ramInstances =
+ private final Map<Integer,NumericDocValues> numericInstances =
new HashMap<Integer,NumericDocValues>();
+ // if this thing needs some TL state then we might put something
+ // else in this map.
+ private final Map<Integer,BinaryDocValues> binaryInstances =
+ new HashMap<Integer,BinaryDocValues>();
+
Lucene41SimpleDocValuesProducer(SegmentReadState state, String dataCodec, String dataExtension, String metaCodec, String metaExtension) throws IOException {
String metaName = IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, metaExtension);
// read in the entries from the metadata file.
@@ -52,6 +61,8 @@ class Lucene41SimpleDocValuesProducer extends SimpleDVProducer {
Lucene41SimpleDocValuesConsumer.VERSION_START,
Lucene41SimpleDocValuesConsumer.VERSION_START);
numerics = new HashMap<Integer,NumericEntry>();
+ binaries = new HashMap<Integer,BinaryEntry>();
+ fsts = new HashMap<Integer,FSTEntry>();
readFields(in, state.fieldInfos);
success = true;
} finally {
@@ -72,20 +83,30 @@ class Lucene41SimpleDocValuesProducer extends SimpleDVProducer {
private void readFields(IndexInput meta, FieldInfos infos) throws IOException {
int fieldNumber = meta.readVInt();
while (fieldNumber != -1) {
- NumericEntry entry = new NumericEntry();
- entry.offset = meta.readLong();
- entry.tableized = meta.readByte() != 0;
- numerics.put(fieldNumber, entry);
+ int fieldType = meta.readByte();
+ if (fieldType == Lucene41SimpleDocValuesConsumer.NUMBER) {
+ NumericEntry entry = new NumericEntry();
+ entry.offset = meta.readLong();
+ entry.tableized = meta.readByte() != 0;
+ numerics.put(fieldNumber, entry);
+ } else if (fieldType == Lucene41SimpleDocValuesConsumer.BYTES) {
+ BinaryEntry entry = new BinaryEntry();
+ entry.offset = meta.readLong();
+ entry.numBytes = meta.readLong();
+ entry.minLength = meta.readVInt();
+ entry.maxLength = meta.readVInt();
+ binaries.put(fieldNumber, entry);
+ }
fieldNumber = meta.readVInt();
}
}
@Override
public synchronized NumericDocValues getNumeric(FieldInfo field) throws IOException {
- NumericDocValues instance = ramInstances.get(field.number);
+ NumericDocValues instance = numericInstances.get(field.number);
if (instance == null) {
instance = loadNumeric(field);
- ramInstances.put(field.number, instance);
+ numericInstances.put(field.number, instance);
}
return instance;
}
@@ -122,9 +143,48 @@ class Lucene41SimpleDocValuesProducer extends SimpleDVProducer {
}
@Override
- public BinaryDocValues getBinary(FieldInfo field) throws IOException {
- throw new AssertionError();
+ public synchronized BinaryDocValues getBinary(FieldInfo field) throws IOException {
+ BinaryDocValues instance = binaryInstances.get(field.number);
+ if (instance == null) {
+ instance = loadBinary(field);
+ binaryInstances.put(field.number, instance);
+ }
+ return instance;
+ }
+
+ private BinaryDocValues loadBinary(FieldInfo field) throws IOException {
+ BinaryEntry entry = binaries.get(field.number);
+ final IndexInput data = this.data.clone();
+ data.seek(entry.offset);
+ assert entry.numBytes < Integer.MAX_VALUE; // nocommit
+ final byte[] bytes = new byte[(int)entry.numBytes];
+ data.readBytes(bytes, 0, bytes.length);
+ if (entry.minLength == entry.maxLength) {
+ final int fixedLength = entry.minLength;
+ return new BinaryDocValues() {
+ @Override
+ public void get(int docID, BytesRef result) {
+ result.bytes = bytes;
+ result.offset = docID * fixedLength;
+ result.length = fixedLength;
+ }
+ };
+ } else {
+ final NumericDocValues addresses = getNumeric(field);
+ return new BinaryDocValues() {
+ @Override
+ public void get(int docID, BytesRef result) {
+ int startAddress = docID == 0 ? 0 : (int) addresses.get(docID-1);
+ int endAddress = (int)addresses.get(docID);
+ result.bytes = bytes;
+ result.offset = startAddress;
+ result.length = endAddress - startAddress;
+ }
+ };
+ }
}
+
+
@Override
public SortedDocValues getSorted(FieldInfo field) throws IOException {
@@ -140,5 +200,17 @@ class Lucene41SimpleDocValuesProducer extends SimpleDVProducer {
long offset;
boolean tableized;
}
+
+ static class BinaryEntry {
+ long offset;
+ long numBytes;
+ int minLength;
+ int maxLength;
+ }
+
+ static class FSTEntry {
+ long offset;
+ int numOrds;
+ }
}