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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -19,10 +19,10 @@
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>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 static final UnicodeUtil.UTF8Result EMPTY;
static {
UnicodeUtil.UTF8Result temp = new UnicodeUtil.UTF8Result();
temp.result = new byte[0];
temp.length = 0;
EMPTY = temp;
}
public static final BytesRef EMPTY = new BytesRef();
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) {
this(status, EMPTY);
}
public Utf8RestResponse(RestStatus status, UnicodeUtil.UTF8Result utf8Result) {
public Utf8RestResponse(RestStatus status, BytesRef utf8Result) {
this(status, utf8Result, null, null);
}
public Utf8RestResponse(RestStatus status, UnicodeUtil.UTF8Result utf8Result,
UnicodeUtil.UTF8Result prefixUtf8Result, UnicodeUtil.UTF8Result suffixUtf8Result) {
public Utf8RestResponse(RestStatus status, BytesRef utf8Result,
BytesRef prefixUtf8Result, BytesRef suffixUtf8Result) {
this.status = status;
this.utf8Result = utf8Result;
this.prefixUtf8Result = prefixUtf8Result;
@ -75,7 +68,7 @@ public class Utf8RestResponse extends AbstractRestResponse implements RestRespon
@Override
public byte[] content() {
return utf8Result.result;
return utf8Result.bytes;
}
@Override
@ -83,6 +76,11 @@ public class Utf8RestResponse extends AbstractRestResponse implements RestRespon
return utf8Result.length;
}
@Override
public int contentOffset() {
return utf8Result.offset;
}
@Override
public RestStatus status() {
return status;
@ -90,7 +88,7 @@ public class Utf8RestResponse extends AbstractRestResponse implements RestRespon
@Override
public byte[] prefixContent() {
return prefixUtf8Result != null ? prefixUtf8Result.result : null;
return prefixUtf8Result != null ? prefixUtf8Result.bytes : null;
}
@Override
@ -98,13 +96,23 @@ public class Utf8RestResponse extends AbstractRestResponse implements RestRespon
return prefixUtf8Result != null ? prefixUtf8Result.length : 0;
}
@Override
public int prefixContentOffset() {
return prefixUtf8Result != null ? prefixUtf8Result.offset : 0;
}
@Override
public byte[] suffixContent() {
return suffixUtf8Result != null ? suffixUtf8Result.result : null;
return suffixUtf8Result != null ? suffixUtf8Result.bytes : null;
}
@Override
public int suffixContentLength() {
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;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.UnicodeUtil;
import org.elasticsearch.common.util.concurrent.ThreadLocals;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -33,20 +34,20 @@ public class XContentRestResponse extends AbstractRestResponse {
private static final byte[] END_JSONP;
static {
UnicodeUtil.UTF8Result U_END_JSONP = new UnicodeUtil.UTF8Result();
BytesRef U_END_JSONP = new BytesRef();
UnicodeUtil.UTF16toUTF8(");", 0, ");".length(), U_END_JSONP);
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
protected ThreadLocals.CleanableValue<UnicodeUtil.UTF8Result> initialValue() {
return new ThreadLocals.CleanableValue<UnicodeUtil.UTF8Result>(new UnicodeUtil.UTF8Result());
protected ThreadLocals.CleanableValue<BytesRef> initialValue() {
return new ThreadLocals.CleanableValue<BytesRef>(new BytesRef());
}
};
private final UnicodeUtil.UTF8Result prefixUtf8Result;
private final BytesRef prefixUtf8Result;
private final RestStatus status;
@ -82,6 +83,11 @@ public class XContentRestResponse extends AbstractRestResponse {
return builder.bytes().length();
}
@Override
public int contentOffset() throws IOException {
return 0;
}
@Override
public RestStatus status() {
return this.status;
@ -90,7 +96,7 @@ public class XContentRestResponse extends AbstractRestResponse {
@Override
public byte[] prefixContent() {
if (prefixUtf8Result != null) {
return prefixUtf8Result.result;
return prefixUtf8Result.bytes;
}
return null;
}
@ -103,6 +109,14 @@ public class XContentRestResponse extends AbstractRestResponse {
return 0;
}
@Override
public int prefixContentOffset() {
if (prefixUtf8Result != null) {
return prefixUtf8Result.offset;
}
return 0;
}
@Override
public byte[] suffixContent() {
if (prefixUtf8Result != null) {
@ -119,14 +133,19 @@ public class XContentRestResponse extends AbstractRestResponse {
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");
if (callback == null) {
return null;
}
UnicodeUtil.UTF8Result result = prefixCache.get().get();
BytesRef result = prefixCache.get().get();
UnicodeUtil.UTF16toUTF8(callback, 0, callback.length(), result);
result.result[result.length] = '(';
result.bytes[result.length] = '(';
result.length++;
return result;
}