Index: java/org/apache/lucene/document/AbstractField.java
===================================================================
--- java/org/apache/lucene/document/AbstractField.java (revision 635923)
+++ java/org/apache/lucene/document/AbstractField.java (working copy)
@@ -36,6 +36,9 @@
protected float boost = 1.0f;
// the one and only data object for all different kind of field values
protected Object fieldsData = null;
+ //length/offset for all primitive types
+ protected int dataLength;
+ protected int dataOffset;
protected AbstractField()
{
@@ -199,7 +202,30 @@
/** True iff the value of the filed is stored as binary */
public final boolean isBinary() { return isBinary; }
+
+ /**
+ * @returns reference to the Field value as byte[]
+ */
+ public byte[] getBinaryValue() { return fieldsData instanceof byte[] ? (byte[])fieldsData : null; }
+ /**
+ * Returns length of byte[] segment that is used as value, if Field is not binary
+ * returned value is undefined
+ * @returns length of byte[] segment that represents this Field value
+ */
+ public int getLength() {
+ return dataLength;
+ }
+
+ /**
+ * Returns offset into byte[] segment that is used as value, if Field is not binary
+ * returned value is undefined
+ * @returns index of the first character in byte[] segment that represents this Field value
+ */
+ public int getOffset() {
+ return dataOffset;
+ }
+
/** True if norms are omitted for this indexed field */
public boolean getOmitNorms() { return omitNorms; }
Index: java/org/apache/lucene/document/Field.java
===================================================================
--- java/org/apache/lucene/document/Field.java (revision 635923)
+++ java/org/apache/lucene/document/Field.java (working copy)
@@ -147,8 +147,19 @@
/** The value of the field in Binary, or null. If null, the Reader value,
* String value, or TokenStream value is used. Exactly one of stringValue(),
- * readerValue(), binaryValue(), and tokenStreamValue() must be set. */
- public byte[] binaryValue() { return fieldsData instanceof byte[] ? (byte[])fieldsData : null; }
+ * readerValue(), binaryValue(), and tokenStreamValue() must be set.
+ * @deprecated use {@link AbstractField#getBinaryValue()}
+ */
+ public byte[] binaryValue() {
+ if(!(fieldsData instanceof byte[])) return null;
+ final byte[] data = (byte[])fieldsData;
+ if(dataOffset==0 && data.length==dataLength) return data; //Optimization
+
+ final byte[] ret = new byte[dataLength];
+ System.arraycopy(data, dataOffset, ret, 0, dataLength);
+ return ret;
+ }
+
/** The value of the field as a TokesStream, or null. If null, the Reader value,
* String value, or binary value is used. Exactly one of stringValue(),
@@ -182,9 +193,19 @@
/** Expert: change the value of this field. See setValue(String). */
public void setValue(byte[] value) {
fieldsData = value;
+ dataLength = value.length;
+ dataOffset = 0;
}
/** Expert: change the value of this field. See setValue(String). */
+ public void setValue(byte[] value, int offset, int length) {
+ fieldsData = value;
+ dataLength = length;
+ dataOffset = offset;
+ }
+
+
+ /** Expert: change the value of this field. See setValue(String). */
public void setValue(TokenStream value) {
fieldsData = value;
}
@@ -403,6 +424,8 @@
this.isTokenized = false;
this.isBinary = true;
+ this.dataLength = value.length;
+ this.dataOffset = 0;
setStoreTermVector(TermVector.NO);
}
Index: java/org/apache/lucene/document/Fieldable.java
===================================================================
--- java/org/apache/lucene/document/Fieldable.java (revision 635923)
+++ java/org/apache/lucene/document/Fieldable.java (working copy)
@@ -77,6 +77,10 @@
* readerValue(), binaryValue(), and tokenStreamValue() must be set. */
public byte[] binaryValue();
+ public byte[] getBinaryValue();
+ public int getLength();
+ public int getOffset();
+
/** The value of the field as a TokenStream, or null. If null, the Reader value,
* String value, or binary value is used. Exactly one of stringValue(),
* readerValue(), binaryValue(), and tokenStreamValue() must be set. */
Index: java/org/apache/lucene/index/FieldsReader.java
===================================================================
--- java/org/apache/lucene/index/FieldsReader.java (revision 635923)
+++ java/org/apache/lucene/index/FieldsReader.java (working copy)
@@ -430,7 +430,14 @@
throw new FieldReaderException(e);
}
}
- return fieldsData instanceof byte[] ? (byte[]) fieldsData : null;
+ dataOffset = 0;
+ if(fieldsData instanceof byte[]){
+ byte[] x = (byte[]) fieldsData;
+ dataLength = x.length;
+ return x;
+ }
+ return null;
+
}
/** The value of the field as a Reader, or null. If null, the String value,
Index: java/org/apache/lucene/index/FieldsWriter.java
===================================================================
--- java/org/apache/lucene/index/FieldsWriter.java (revision 635923)
+++ java/org/apache/lucene/index/FieldsWriter.java (working copy)
@@ -97,31 +97,38 @@
if (field.isCompressed()) {
// compression is enabled for the current field
byte[] data = null;
-
+ final int len;
+ final int offset;
if (disableCompression) {
// optimized case for merging, the data
// is already compressed
- data = field.binaryValue();
+ data = field.getBinaryValue();
+ len = field.getLength();
+ offset = field.getOffset();
} else {
// check if it is a binary field
if (field.isBinary()) {
- data = compress(field.binaryValue());
+ data = compress(field.getBinaryValue(), field.getOffset(), field.getLength());
}
else {
- data = compress(field.stringValue().getBytes("UTF-8"));
+ byte x[] = field.stringValue().getBytes("UTF-8");
+ data = compress(x, 0,x.length);
}
+ len = data.length;
+ offset = 0;
}
- final int len = data.length;
+
fieldsStream.writeVInt(len);
- fieldsStream.writeBytes(data, len);
+ fieldsStream.writeBytes(data, offset, len);
}
else {
// compression is disabled for the current field
if (field.isBinary()) {
- byte[] data = field.binaryValue();
- final int len = data.length;
+ final byte[] data = field.getBinaryValue();
+ final int len = field.getLength();
+ final int offset = field.getOffset();
fieldsStream.writeVInt(len);
- fieldsStream.writeBytes(data, len);
+ fieldsStream.writeBytes(data, offset, len);
}
else {
fieldsStream.writeString(field.stringValue());
@@ -165,14 +172,14 @@
}
}
- private final byte[] compress (byte[] input) {
+ private final byte[] compress (byte[] input, int offset, int length) {
// Create the compressor with highest level of compression
Deflater compressor = new Deflater();
compressor.setLevel(Deflater.BEST_COMPRESSION);
// Give the compressor the data to compress
- compressor.setInput(input);
+ compressor.setInput(input,offset, length);
compressor.finish();
/*
@@ -181,7 +188,7 @@
* there is no guarantee that the compressed data will be smaller than
* the uncompressed data.
*/
- ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
+ ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
// Compress the data
byte[] buf = new byte[1024];