dlmarion commented on code in PR #4748: URL: https://github.com/apache/accumulo/pull/4748#discussion_r1705423725
########## core/src/main/java/org/apache/accumulo/core/data/EmptyByteSequence.java: ########## @@ -0,0 +1,87 @@ +/* + * 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 + * + * https://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.accumulo.core.data; + +import java.util.Objects; + +/** + * An immutable implementation of {@link ByteSequence} specialized for the empty case. + * + * <p> + * This class is package private because direct construction is not expected, instead use of + * {@link ByteSequence#of()} is how instances of this should be created. + * </p> + */ +final class EmptyByteSequence extends ByteSequence { + + private static final long serialVersionUID = 1L; + + static final ByteSequence INSTANCE = new EmptyByteSequence(); + + private static final byte[] EMPTY = new byte[0]; + + // constructor is private because this class is a singleton + private EmptyByteSequence() {} + + @Override + public byte byteAt(int i) { + throw new IndexOutOfBoundsException(); Review Comment: Curious if this should be `UnsupportedOperationException` instead? ########## core/src/main/java/org/apache/accumulo/core/data/ByteSequence.java: ########## @@ -157,4 +185,85 @@ public int hashCode() { return hash; } + /** + * This method returns an empty immutable byte sequence. The same object is always returned. + * + * @since 3.1.0 + */ + public static ByteSequence of() { + return EmptyByteSequence.INSTANCE; Review Comment: I'm wondering if `EmptyByteSequence` should extend `ImmutableByteSequence`. This would allow you to change the return type of these `of` methods to `ImmutableByteSequence` for stronger typing. ########## core/src/main/java/org/apache/accumulo/core/data/ImmutableByteSequence.java: ########## @@ -0,0 +1,174 @@ +/* + * 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 + * + * https://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.accumulo.core.data; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Objects; + +import org.apache.accumulo.core.util.ByteBufferUtil; + +/** + * An immutable implementation of {@link ByteSequence} that copies data in its constructor and never + * lets its internal byte array escape. This class is final to prevent it from being made mutable in + * a subclass. + * + * <p> + * This class is package private because direct construction is not expected, instead use of methods + * like {@link ByteSequence#of(byte[])} is how instances of this should be created. + * </p> + */ +final class ImmutableByteSequence extends ByteSequence { + + private static final long serialVersionUID = 1L; + + // A reference to this must never escape this class. + private final byte[] data; + + private int hash = 1; + private boolean hashIsOne = false; + + ImmutableByteSequence(ByteSequence data) { + if (data.isBackedByArray()) { + this.data = + Arrays.copyOfRange(data.getBackingArray(), data.offset(), data.offset() + data.length()); + } else { + var dataArray = data.toArray(); + this.data = Arrays.copyOf(dataArray, dataArray.length); + } + } + + /** + * Creates a new sequence. The array is copied into an new internal array. + * + * @param data byte data + */ + ImmutableByteSequence(byte[] data) { + this.data = Arrays.copyOf(data, data.length); Review Comment: Are you calling `Arrays.copyOf` here instead of `System.arraycopy` for the possible intrinsic? It seems like you could just call `this(data, 0, data.length)`. ########## core/src/main/java/org/apache/accumulo/core/data/ByteSequence.java: ########## @@ -96,7 +97,10 @@ public abstract class ByteSequence implements Comparable<ByteSequence>, Serializ */ public static int compareBytes(ByteSequence bs1, ByteSequence bs2) { - int minLen = Math.min(bs1.length(), bs2.length()); + int len1 = bs1.length(); + int len2 = bs2.length(); + + int minLen = Math.min(len1, len2); Review Comment: Should probably make these `final`. ########## core/src/main/java/org/apache/accumulo/core/data/ImmutableByteSequence.java: ########## @@ -0,0 +1,174 @@ +/* + * 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 + * + * https://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.accumulo.core.data; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.Objects; + +import org.apache.accumulo.core.util.ByteBufferUtil; + +/** + * An immutable implementation of {@link ByteSequence} that copies data in its constructor and never + * lets its internal byte array escape. This class is final to prevent it from being made mutable in + * a subclass. + * + * <p> + * This class is package private because direct construction is not expected, instead use of methods + * like {@link ByteSequence#of(byte[])} is how instances of this should be created. + * </p> + */ +final class ImmutableByteSequence extends ByteSequence { + + private static final long serialVersionUID = 1L; + + // A reference to this must never escape this class. + private final byte[] data; + + private int hash = 1; + private boolean hashIsOne = false; + + ImmutableByteSequence(ByteSequence data) { + if (data.isBackedByArray()) { + this.data = + Arrays.copyOfRange(data.getBackingArray(), data.offset(), data.offset() + data.length()); + } else { + var dataArray = data.toArray(); + this.data = Arrays.copyOf(dataArray, dataArray.length); Review Comment: There is a private static method , `ArrayByteSequence.copy`, that has very simliar logic. I wonder if that method should be made public and moved into `ByteSequence`. Then this becomes: ``` this.data = ByteSequence.copy(data); ``` -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
