lucene 4: replace UnicodeUtil.UTF8Result with BytesRef

This commit is contained in:
Igor Motov 2012-10-27 20:34:32 -04:00 committed by Shay Banon
parent f444ed4dff
commit a49078dfc1
8 changed files with 92 additions and 43 deletions

View File

@ -19,7 +19,7 @@
package org.elasticsearch.common.compress; package org.elasticsearch.common.compress;
import org.apache.lucene.util.UnicodeUtil; import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Unicode; import org.elasticsearch.common.Unicode;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
@ -79,8 +79,8 @@ public class CompressedString implements Streamable {
} }
public CompressedString(String str) throws IOException { public CompressedString(String str) throws IOException {
UnicodeUtil.UTF8Result result = Unicode.unsafeFromStringAsUtf8(str); BytesRef result = Unicode.unsafeFromStringAsUtf8(str);
this.bytes = CompressorFactory.defaultCompressor().compress(result.result, 0, result.length); this.bytes = CompressorFactory.defaultCompressor().compress(result.bytes, result.offset, result.length);
} }
public byte[] compressed() { public byte[] compressed() {

View File

@ -102,15 +102,15 @@ public class NettyHttpChannel implements HttpChannel {
releaseContentListener = new NettyTransport.CacheFutureListener((CachedStreamOutput.Entry) builder.payload()); releaseContentListener = new NettyTransport.CacheFutureListener((CachedStreamOutput.Entry) builder.payload());
buf = builder.bytes().toChannelBuffer(); buf = builder.bytes().toChannelBuffer();
} else if (response.contentThreadSafe()) { } else if (response.contentThreadSafe()) {
buf = ChannelBuffers.wrappedBuffer(response.content(), 0, response.contentLength()); buf = ChannelBuffers.wrappedBuffer(response.content(), response.contentOffset(), response.contentLength());
} else { } else {
buf = ChannelBuffers.copiedBuffer(response.content(), 0, response.contentLength()); buf = ChannelBuffers.copiedBuffer(response.content(), response.contentOffset(), response.contentLength());
} }
} else { } else {
if (response.contentThreadSafe()) { if (response.contentThreadSafe()) {
buf = ChannelBuffers.wrappedBuffer(response.content(), 0, response.contentLength()); buf = ChannelBuffers.wrappedBuffer(response.content(), response.contentOffset(), response.contentLength());
} else { } else {
buf = ChannelBuffers.copiedBuffer(response.content(), 0, response.contentLength()); buf = ChannelBuffers.copiedBuffer(response.content(), response.contentOffset(), response.contentLength());
} }
} }
} catch (IOException e) { } catch (IOException e) {
@ -119,11 +119,11 @@ public class NettyHttpChannel implements HttpChannel {
if (response.prefixContent() != null || response.suffixContent() != null) { if (response.prefixContent() != null || response.suffixContent() != null) {
ChannelBuffer prefixBuf = ChannelBuffers.EMPTY_BUFFER; ChannelBuffer prefixBuf = ChannelBuffers.EMPTY_BUFFER;
if (response.prefixContent() != null) { if (response.prefixContent() != null) {
prefixBuf = ChannelBuffers.copiedBuffer(response.prefixContent(), 0, response.prefixContentLength()); prefixBuf = ChannelBuffers.copiedBuffer(response.prefixContent(), response.prefixContentOffset(), response.prefixContentLength());
} }
ChannelBuffer suffixBuf = ChannelBuffers.EMPTY_BUFFER; ChannelBuffer suffixBuf = ChannelBuffers.EMPTY_BUFFER;
if (response.suffixContent() != null) { if (response.suffixContent() != null) {
suffixBuf = ChannelBuffers.copiedBuffer(response.suffixContent(), 0, response.suffixContentLength()); suffixBuf = ChannelBuffers.copiedBuffer(response.suffixContent(), response.suffixContentOffset(), response.suffixContentLength());
} }
buf = ChannelBuffers.wrappedBuffer(prefixBuf, buf, suffixBuf); buf = ChannelBuffers.wrappedBuffer(prefixBuf, buf, suffixBuf);
} }

View File

@ -34,6 +34,11 @@ public abstract class AbstractRestResponse implements RestResponse {
return -1; return -1;
} }
@Override
public int prefixContentOffset() {
return 0;
}
@Override @Override
public byte[] suffixContent() { public byte[] suffixContent() {
return null; return null;
@ -43,4 +48,9 @@ public abstract class AbstractRestResponse implements RestResponse {
public int suffixContentLength() { public int suffixContentLength() {
return -1; return -1;
} }
@Override
public int suffixContentOffset() {
return 0;
}
} }

View File

@ -52,6 +52,11 @@ public class BytesRestResponse extends AbstractRestResponse {
return bytes.length; return bytes.length;
} }
@Override
public int contentOffset() throws IOException {
return 0;
}
@Override @Override
public RestStatus status() { public RestStatus status() {
return RestStatus.OK; return RestStatus.OK;

View File

@ -44,13 +44,19 @@ public interface RestResponse {
*/ */
int contentLength() throws IOException; int contentLength() throws IOException;
int contentOffset() throws IOException;
byte[] prefixContent(); byte[] prefixContent();
int prefixContentLength(); int prefixContentLength();
int prefixContentOffset();
byte[] suffixContent(); byte[] suffixContent();
int suffixContentLength(); int suffixContentLength();
int suffixContentOffset();
RestStatus status(); RestStatus status();
} }

View File

@ -19,6 +19,7 @@
package org.elasticsearch.rest; package org.elasticsearch.rest;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.UnicodeUtil; import org.apache.lucene.util.UnicodeUtil;
import org.elasticsearch.common.util.concurrent.ThreadLocals; import org.elasticsearch.common.util.concurrent.ThreadLocals;
@ -27,10 +28,10 @@ import org.elasticsearch.common.util.concurrent.ThreadLocals;
*/ */
public class StringRestResponse extends Utf8RestResponse { public class StringRestResponse extends Utf8RestResponse {
private static ThreadLocal<ThreadLocals.CleanableValue<UnicodeUtil.UTF8Result>> cache = new ThreadLocal<ThreadLocals.CleanableValue<UnicodeUtil.UTF8Result>>() { private static ThreadLocal<ThreadLocals.CleanableValue<BytesRef>> cache = new ThreadLocal<ThreadLocals.CleanableValue<BytesRef>>() {
@Override @Override
protected ThreadLocals.CleanableValue<UnicodeUtil.UTF8Result> initialValue() { protected ThreadLocals.CleanableValue<BytesRef> initialValue() {
return new ThreadLocals.CleanableValue<UnicodeUtil.UTF8Result>(new UnicodeUtil.UTF8Result()); return new ThreadLocals.CleanableValue<BytesRef>(new BytesRef());
} }
}; };
@ -42,8 +43,8 @@ public class StringRestResponse extends Utf8RestResponse {
super(status, convert(content)); super(status, convert(content));
} }
private static UnicodeUtil.UTF8Result convert(String content) { private static BytesRef convert(String content) {
UnicodeUtil.UTF8Result result = cache.get().get(); BytesRef result = cache.get().get();
UnicodeUtil.UTF16toUTF8(content, 0, content.length(), result); UnicodeUtil.UTF16toUTF8(content, 0, content.length(), result);
return result; return result;
} }

View File

@ -19,10 +19,10 @@
package org.elasticsearch.rest; package org.elasticsearch.rest;
import org.apache.lucene.util.UnicodeUtil; import org.apache.lucene.util.BytesRef;
/** /**
* An http response that is built on top of {@link org.apache.lucene.util.UnicodeUtil.UTF8Result}. * An http response that is built on top of {@link org.apache.lucene.util.BytesRef}.
* <p/> * <p/>
* <p>Note, this class assumes that the utf8 result is not thread safe. * <p>Note, this class assumes that the utf8 result is not thread safe.
* *
@ -30,33 +30,26 @@ import org.apache.lucene.util.UnicodeUtil;
*/ */
public class Utf8RestResponse extends AbstractRestResponse implements RestResponse { public class Utf8RestResponse extends AbstractRestResponse implements RestResponse {
public static final UnicodeUtil.UTF8Result EMPTY; public static final BytesRef EMPTY = new BytesRef();
static {
UnicodeUtil.UTF8Result temp = new UnicodeUtil.UTF8Result();
temp.result = new byte[0];
temp.length = 0;
EMPTY = temp;
}
private final RestStatus status; private final RestStatus status;
private final UnicodeUtil.UTF8Result utf8Result; private final BytesRef utf8Result;
private final UnicodeUtil.UTF8Result prefixUtf8Result; private final BytesRef prefixUtf8Result;
private final UnicodeUtil.UTF8Result suffixUtf8Result; private final BytesRef suffixUtf8Result;
public Utf8RestResponse(RestStatus status) { public Utf8RestResponse(RestStatus status) {
this(status, EMPTY); this(status, EMPTY);
} }
public Utf8RestResponse(RestStatus status, UnicodeUtil.UTF8Result utf8Result) { public Utf8RestResponse(RestStatus status, BytesRef utf8Result) {
this(status, utf8Result, null, null); this(status, utf8Result, null, null);
} }
public Utf8RestResponse(RestStatus status, UnicodeUtil.UTF8Result utf8Result, public Utf8RestResponse(RestStatus status, BytesRef utf8Result,
UnicodeUtil.UTF8Result prefixUtf8Result, UnicodeUtil.UTF8Result suffixUtf8Result) { BytesRef prefixUtf8Result, BytesRef suffixUtf8Result) {
this.status = status; this.status = status;
this.utf8Result = utf8Result; this.utf8Result = utf8Result;
this.prefixUtf8Result = prefixUtf8Result; this.prefixUtf8Result = prefixUtf8Result;
@ -75,7 +68,7 @@ public class Utf8RestResponse extends AbstractRestResponse implements RestRespon
@Override @Override
public byte[] content() { public byte[] content() {
return utf8Result.result; return utf8Result.bytes;
} }
@Override @Override
@ -83,6 +76,11 @@ public class Utf8RestResponse extends AbstractRestResponse implements RestRespon
return utf8Result.length; return utf8Result.length;
} }
@Override
public int contentOffset() {
return utf8Result.offset;
}
@Override @Override
public RestStatus status() { public RestStatus status() {
return status; return status;
@ -90,7 +88,7 @@ public class Utf8RestResponse extends AbstractRestResponse implements RestRespon
@Override @Override
public byte[] prefixContent() { public byte[] prefixContent() {
return prefixUtf8Result != null ? prefixUtf8Result.result : null; return prefixUtf8Result != null ? prefixUtf8Result.bytes : null;
} }
@Override @Override
@ -98,13 +96,23 @@ public class Utf8RestResponse extends AbstractRestResponse implements RestRespon
return prefixUtf8Result != null ? prefixUtf8Result.length : 0; return prefixUtf8Result != null ? prefixUtf8Result.length : 0;
} }
@Override
public int prefixContentOffset() {
return prefixUtf8Result != null ? prefixUtf8Result.offset : 0;
}
@Override @Override
public byte[] suffixContent() { public byte[] suffixContent() {
return suffixUtf8Result != null ? suffixUtf8Result.result : null; return suffixUtf8Result != null ? suffixUtf8Result.bytes : null;
} }
@Override @Override
public int suffixContentLength() { public int suffixContentLength() {
return suffixUtf8Result != null ? suffixUtf8Result.length : 0; return suffixUtf8Result != null ? suffixUtf8Result.length : 0;
} }
@Override
public int suffixContentOffset() {
return suffixUtf8Result != null ? suffixUtf8Result.offset : 0;
}
} }

View File

@ -19,6 +19,7 @@
package org.elasticsearch.rest; package org.elasticsearch.rest;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.UnicodeUtil; import org.apache.lucene.util.UnicodeUtil;
import org.elasticsearch.common.util.concurrent.ThreadLocals; import org.elasticsearch.common.util.concurrent.ThreadLocals;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
@ -33,20 +34,20 @@ public class XContentRestResponse extends AbstractRestResponse {
private static final byte[] END_JSONP; private static final byte[] END_JSONP;
static { static {
UnicodeUtil.UTF8Result U_END_JSONP = new UnicodeUtil.UTF8Result(); BytesRef U_END_JSONP = new BytesRef();
UnicodeUtil.UTF16toUTF8(");", 0, ");".length(), U_END_JSONP); UnicodeUtil.UTF16toUTF8(");", 0, ");".length(), U_END_JSONP);
END_JSONP = new byte[U_END_JSONP.length]; END_JSONP = new byte[U_END_JSONP.length];
System.arraycopy(U_END_JSONP.result, 0, END_JSONP, 0, U_END_JSONP.length); System.arraycopy(U_END_JSONP.bytes, U_END_JSONP.offset, END_JSONP, 0, U_END_JSONP.length);
} }
private static ThreadLocal<ThreadLocals.CleanableValue<UnicodeUtil.UTF8Result>> prefixCache = new ThreadLocal<ThreadLocals.CleanableValue<UnicodeUtil.UTF8Result>>() { private static ThreadLocal<ThreadLocals.CleanableValue<BytesRef>> prefixCache = new ThreadLocal<ThreadLocals.CleanableValue<BytesRef>>() {
@Override @Override
protected ThreadLocals.CleanableValue<UnicodeUtil.UTF8Result> initialValue() { protected ThreadLocals.CleanableValue<BytesRef> initialValue() {
return new ThreadLocals.CleanableValue<UnicodeUtil.UTF8Result>(new UnicodeUtil.UTF8Result()); return new ThreadLocals.CleanableValue<BytesRef>(new BytesRef());
} }
}; };
private final UnicodeUtil.UTF8Result prefixUtf8Result; private final BytesRef prefixUtf8Result;
private final RestStatus status; private final RestStatus status;
@ -82,6 +83,11 @@ public class XContentRestResponse extends AbstractRestResponse {
return builder.bytes().length(); return builder.bytes().length();
} }
@Override
public int contentOffset() throws IOException {
return 0;
}
@Override @Override
public RestStatus status() { public RestStatus status() {
return this.status; return this.status;
@ -90,7 +96,7 @@ public class XContentRestResponse extends AbstractRestResponse {
@Override @Override
public byte[] prefixContent() { public byte[] prefixContent() {
if (prefixUtf8Result != null) { if (prefixUtf8Result != null) {
return prefixUtf8Result.result; return prefixUtf8Result.bytes;
} }
return null; return null;
} }
@ -103,6 +109,14 @@ public class XContentRestResponse extends AbstractRestResponse {
return 0; return 0;
} }
@Override
public int prefixContentOffset() {
if (prefixUtf8Result != null) {
return prefixUtf8Result.offset;
}
return 0;
}
@Override @Override
public byte[] suffixContent() { public byte[] suffixContent() {
if (prefixUtf8Result != null) { if (prefixUtf8Result != null) {
@ -119,14 +133,19 @@ public class XContentRestResponse extends AbstractRestResponse {
return 0; return 0;
} }
private static UnicodeUtil.UTF8Result startJsonp(RestRequest request) { @Override
public int suffixContentOffset() {
return 0;
}
private static BytesRef startJsonp(RestRequest request) {
String callback = request.param("callback"); String callback = request.param("callback");
if (callback == null) { if (callback == null) {
return null; return null;
} }
UnicodeUtil.UTF8Result result = prefixCache.get().get(); BytesRef result = prefixCache.get().get();
UnicodeUtil.UTF16toUTF8(callback, 0, callback.length(), result); UnicodeUtil.UTF16toUTF8(callback, 0, callback.length(), result);
result.result[result.length] = '('; result.bytes[result.length] = '(';
result.length++; result.length++;
return result; return result;
} }