From 58d07b2ffc8ebbbe4f021a4c0ec472c065b3a9eb Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Tue, 18 Aug 2020 10:53:40 +0200 Subject: [PATCH] Remove Unused ByteBufferReference (#61116) (#61250) We only work with heap byte buffers at this point and those we can and do unwrap the `byte[]` ourselves and use `BytesArray` instead of a needless level of indirection via `ByteBuffer`. --- .../common/bytes/ByteBufferReference.java | 109 ------------------ .../common/bytes/BytesReference.java | 6 +- .../bytes/ByteBufferReferenceTests.java | 44 ------- 3 files changed, 2 insertions(+), 157 deletions(-) delete mode 100644 server/src/main/java/org/elasticsearch/common/bytes/ByteBufferReference.java delete mode 100644 server/src/test/java/org/elasticsearch/common/bytes/ByteBufferReferenceTests.java diff --git a/server/src/main/java/org/elasticsearch/common/bytes/ByteBufferReference.java b/server/src/main/java/org/elasticsearch/common/bytes/ByteBufferReference.java deleted file mode 100644 index ad381c28402..00000000000 --- a/server/src/main/java/org/elasticsearch/common/bytes/ByteBufferReference.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.bytes; - -import org.apache.lucene.util.BytesRef; -import org.apache.lucene.util.FutureObjects; - -import java.nio.ByteBuffer; - -/** - * This is a {@link BytesReference} backed by a {@link ByteBuffer}. The byte buffer can either be a heap or - * direct byte buffer. The reference is composed of the space between the {@link ByteBuffer#position()} and - * {@link ByteBuffer#limit()} at construction time. If the position or limit of the underlying byte buffer is - * changed, those changes will not be reflected in this reference. Any changes to the underlying data in the - * byte buffer will be reflected in this reference. - */ -public class ByteBufferReference extends AbstractBytesReference { - - private final ByteBuffer buffer; - private final int length; - - ByteBufferReference(ByteBuffer buffer) { - this.buffer = buffer.slice(); - this.length = buffer.remaining(); - } - - @Override - public byte get(int index) { - return buffer.get(index); - } - - @Override - public int getInt(int index) { - return buffer.getInt(index); - } - - @Override - public int indexOf(byte marker, int from) { - final int remainingBytes = Math.max(length - from, 0); - FutureObjects.checkFromIndexSize(from, remainingBytes, length); - if (buffer.hasArray()) { - int startIndex = from + buffer.arrayOffset(); - int endIndex = startIndex + remainingBytes; - final byte[] array = buffer.array(); - for (int i = startIndex; i < endIndex; i++) { - if (array[i] == marker) { - return (i - buffer.arrayOffset()); - } - } - return -1; - } else { - return super.indexOf(marker, from); - } - } - - @Override - public int length() { - return length; - } - - @Override - public BytesReference slice(int from, int length) { - FutureObjects.checkFromIndexSize(from, length, this.length); - buffer.position(from); - buffer.limit(from + length); - ByteBufferReference newByteBuffer = new ByteBufferReference(buffer); - buffer.position(0); - buffer.limit(this.length); - return newByteBuffer; - } - - /** - * This will return a bytes ref composed of the bytes. If this is a direct byte buffer, the bytes will - * have to be copied. - * - * @return the bytes ref - */ - @Override - public BytesRef toBytesRef() { - if (buffer.hasArray()) { - return new BytesRef(buffer.array(), buffer.arrayOffset(), length); - } - final byte[] copy = new byte[length]; - buffer.get(copy, 0, length); - return new BytesRef(copy); - } - - @Override - public long ramBytesUsed() { - return buffer.capacity(); - } -} diff --git a/server/src/main/java/org/elasticsearch/common/bytes/BytesReference.java b/server/src/main/java/org/elasticsearch/common/bytes/BytesReference.java index f830ebf9b8e..d66b4691cfb 100644 --- a/server/src/main/java/org/elasticsearch/common/bytes/BytesReference.java +++ b/server/src/main/java/org/elasticsearch/common/bytes/BytesReference.java @@ -107,10 +107,8 @@ public interface BytesReference extends Comparable, ToXContentFr * Returns BytesReference composed of the provided ByteBuffer. */ static BytesReference fromByteBuffer(ByteBuffer buffer) { - if (buffer.hasArray()) { - return new BytesArray(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); - } - return new ByteBufferReference(buffer); + assert buffer.hasArray(); + return new BytesArray(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } /** diff --git a/server/src/test/java/org/elasticsearch/common/bytes/ByteBufferReferenceTests.java b/server/src/test/java/org/elasticsearch/common/bytes/ByteBufferReferenceTests.java deleted file mode 100644 index 9560fd40038..00000000000 --- a/server/src/test/java/org/elasticsearch/common/bytes/ByteBufferReferenceTests.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common.bytes; - -import java.io.IOException; -import java.nio.ByteBuffer; - -public class ByteBufferReferenceTests extends AbstractBytesReferenceTestCase { - - private void initializeBytes(byte[] bytes) { - for (int i = 0 ; i < bytes.length; ++i) { - bytes[i] = (byte) i; - } - } - - @Override - protected BytesReference newBytesReference(int length) throws IOException { - return newBytesReferenceWithOffsetOfZero(length); - } - - @Override - protected BytesReference newBytesReferenceWithOffsetOfZero(int length) throws IOException { - byte[] bytes = new byte[length]; - initializeBytes(bytes); - return new ByteBufferReference(ByteBuffer.wrap(bytes)); - } -}