Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds immutable implementation of byte sequence #4748

Open
wants to merge 12 commits into
base: 3.1
Choose a base branch
from

Conversation

keith-turner
Copy link
Contributor

@keith-turner keith-turner commented Jul 22, 2024

This is a WIP to add an immutable implementation of ByteSequence. With this new class there will two implementations of ByteSequence in the Accumulo API, one being mutable and other immutable. The ByteSequence abstract class easily supports an immutable implementation with its current public methods and requires no changes to its public methods.

This commit is a WIP because it lacks unit test, posting the concept for review before implementing unit test. Would need to implement the unit test before taking out of draft. There are TODOs about optimizing also, those could be done in a follow on PR if this is committed.

There is a single example of where this new class is used in VisibilityFilter where an iterator caches some data that it does not expect to change for the lifetime of the class. Using the new immutable byte sequence this expectation can be enforced. In a follow on PR could analyze the codebase for other places where this new class could be used.

This PR was created based on discussions on #4735

With this new class there will two implementations of ByteSequence
in the Accumulo API, one being mutable and other immutable. The
ByteSequence abstract class easily supports an immutable implementation
with its current public methods and requires no changes to its public
methods.

There is a single example of where this new class is used in VisibilityFilter
where an iterator caches some data that it does not expect to change for the
lifetime of the class. Using the new immutable byte sequence this expectation
can be enforced.  In a follow on PR could analyze the codebase for other
places where this new class could be used.

New test were written that achieve 100% coverage of both ByteSequence
implementations. Tests were written for some existing code that this
PR did not change.
@keith-turner keith-turner marked this pull request as ready for review July 30, 2024 19:10
Comment on lines 100 to 103
int len1 = bs1.length();
int len2 = bs2.length();

int minLen = Math.min(len1, len2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably make these final.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in a7bf73f


@Override
public byte byteAt(int i) {
throw new IndexOutOfBoundsException();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if this should be UnsupportedOperationException instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was trying to make all of the different impls of this method throw the same exception. Made some improvements related to the exception in a7bf73f

* @since 3.1.0
*/
public static ByteSequence of() {
return EmptyByteSequence.INSTANCE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently ImmutableByteSequence is package private. Was loosely following the pattern of the java of methods on collections where the concrete type they might return is not public. Do you think ImmutableByteSequence should be made public?

* @param data byte data
*/
ImmutableByteSequence(byte[] data) {
this.data = Arrays.copyOf(data, data.length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in 1320c9b

Comment on lines 50 to 55
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else of this method and the other method have a difference in that one cares if the byte array ref escapes and the other does not. Made some changes in b52c685 related to this to avoid two allocations.

As for adding a copy method to public API there are already two way in public API to copy. For immutable copy the new of method. For mutable copy the existing ArrayByteSequence constructor.

@dlmarion dlmarion changed the base branch from main to 3.1 August 26, 2024 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants