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

Adjust BufferAggregator.get() impls to return copies #7464

Merged
merged 3 commits into from
Apr 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public Object get(final ByteBuffer buf, final int position)
final Lock lock = stripedLock.getAt(lockIndex(position)).readLock();
lock.lock();
try {
return sketchCache.get(buf).get(position);
return sketchCache.get(buf).get(position).copy();
}
finally {
lock.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ public Object get(ByteBuffer buf, int position)
// | k (byte) | numLongs (int) | bitset (long[numLongs]) |
int sizeBytes = 1 + Integer.BYTES + (buf.getInt(position + 1) * Long.BYTES);
mutationBuffer.limit(position + sizeBytes);
return mutationBuffer.slice();

ByteBuffer resultCopy = ByteBuffer.allocate(sizeBytes);
resultCopy.put(mutationBuffer.slice());
return resultCopy;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public interface BufferAggregator extends HotLoopCallee
*
* Converts the given byte buffer representation into an intermediate aggregate Object
*
* <b>Implementations must not change the position, limit or mark of the given buffer</b>
* <b>Implementations must not change the position, limit or mark of the given buffer.</b>
*
* <b>The object returned must not have any references to the given buffer (i.e., make a copy).</b>
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd include rationale for this: it's because the object returned by get may live beyond the lifetime of the underlying buffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the rationale here.

*
* @param buf byte buffer storing the byte array representation of the aggregate
* @param position offset within the byte buffer at which the aggregate value is stored
Expand Down