jpountz commented on code in PR #13948: URL: https://github.com/apache/lucene/pull/13948#discussion_r1888624444
########## lucene/core/src/test/org/apache/lucene/util/TestBytesRefArray.java: ########## @@ -43,8 +44,17 @@ public void testAppend() throws IOException { for (int i = 0; i < entries; i++) { String randomRealisticUnicodeString = TestUtil.randomRealisticUnicodeString(random); spare.copyChars(randomRealisticUnicodeString); - assertEquals(i + initSize, list.append(spare.get())); + if (random().nextBoolean()) { + BytesRef bytesRef = spare.get(); + RandomAccessInputRef inputRef = + new RandomAccessInputRef( + new ByteArrayRandomAccessInput(bytesRef.bytes), bytesRef.offset, bytesRef.length); + assertEquals(i + initSize, list.append(inputRef)); + } else { + assertEquals(i + initSize, list.append(spare.get())); + } stringList.add(randomRealisticUnicodeString); + ; Review Comment: remove? ########## lucene/monitor/src/java/org/apache/lucene/monitor/MonitorQuerySerializer.java: ########## @@ -48,38 +47,31 @@ public interface MonitorQuerySerializer { */ static MonitorQuerySerializer fromParser(Function<String, Query> parser) { return new MonitorQuerySerializer() { + @Override - public MonitorQuery deserialize(BytesRef binaryValue) { - ByteArrayInputStream is = - new ByteArrayInputStream(binaryValue.bytes, binaryValue.offset, binaryValue.length); - try (InputStreamDataInput data = new InputStreamDataInput(is)) { - String id = data.readString(); - String query = data.readString(); - Map<String, String> metadata = new HashMap<>(); - for (int i = data.readInt(); i > 0; i--) { - metadata.put(data.readString(), data.readString()); - } - return new MonitorQuery(id, parser.apply(query), query, metadata); - } catch (IOException e) { - throw new RuntimeException(e); // shouldn't happen, we're reading from a bytearray! + public MonitorQuery deserialize(RandomAccessInputRef input) throws IOException { + RandomAccessInputDataInput data = new RandomAccessInputDataInput(); + data.reset(input); + String id = data.readString(); + String query = data.readString(); + Map<String, String> metadata = new HashMap<>(); + for (int i = data.readInt(); i > 0; i--) { + metadata.put(data.readString(), data.readString()); } + return new MonitorQuery(id, parser.apply(query), query, metadata); Review Comment: Nice that it doesn't load everything it a giant byte[] anymore! ########## lucene/core/src/java/org/apache/lucene/util/RandomAccessInputRef.java: ########## @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.apache.lucene.util; + +import java.io.IOException; +import org.apache.lucene.store.RandomAccessInput; + +/** + * Represents a RandomAccessInput, as a slice (offset + length) into an existing RandomAccessInput. + * The {@link #bytes} member should never be null. In many ways, it is the off-heap equivalent of a + * {@link BytesRef}. + * + * @see BytesRef + */ +public final class RandomAccessInputRef { + + /** The contents of the RandomAccessInput. */ + public RandomAccessInput bytes; + + /** Offset of first valid byte. */ + public long offset; + + /** Length of used bytes. */ + public int length; + + public RandomAccessInputRef(RandomAccessInput bytes) { + this(bytes, 0, 0); + } + + public RandomAccessInputRef(RandomAccessInput bytes, long offset, int length) { + this.bytes = bytes; + this.offset = offset; + this.length = length; + } + + /** + * Interprets stored bytes as UTF-8 bytes, returning the resulting string. May throw an {@link + * AssertionError} or a {@link RuntimeException} if the data is not well-formed UTF-8. + */ + public String utf8ToString() throws IOException { + final char[] ref = new char[length]; + final int len = UnicodeUtil.UTF8toUTF16(toBytesRef(this), ref); + return new String(ref, 0, len); + } + + /** + * Creates a new BytesRef that points to a copy of the bytes from <code>input</code> starting at + * offset for length. + * + * <p>The returned BytesRef will have a offset of zero. + */ + public static BytesRef toBytesRef(RandomAccessInputRef input) throws IOException { Review Comment: I'd be keen to make it a method rather than a static function? ########## lucene/core/src/test/org/apache/lucene/index/Test2BBinaryDocValues.java: ########## @@ -88,12 +89,13 @@ public void testFixedBinary() throws Exception { LeafReader reader = context.reader(); BinaryDocValues dv = reader.getBinaryDocValues("dv"); for (int i = 0; i < reader.maxDoc(); i++) { + ; Review Comment: remove? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org