-
Notifications
You must be signed in to change notification settings - Fork 4k
Sai prefixv2 #4909
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
Draft
pranavshenoy
wants to merge
4
commits into
apache:trunk
Choose a base branch
from
pranavshenoy:sai_prefixv2
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Sai prefixv2 #4909
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| import java.util.Iterator; | ||
| import java.util.NoSuchElementException; | ||
| import java.util.concurrent.atomic.AtomicReferenceArray; | ||
| import java.util.function.IntPredicate; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
|
|
||
|
|
@@ -809,6 +810,17 @@ public interface UpsertTransformer<T, U> | |
| * @return The combined value to use. Cannot be null. | ||
| */ | ||
| T apply(T existing, U update); | ||
|
|
||
| /** | ||
| * Called when intermediate-node content is applied during a recursive put driven by an insertion policy | ||
| * (see {@link #putSingleton(ByteComparable, Object, UpsertTransformer, boolean, IntPredicate)}). | ||
| * The default implementation delegates to {@link #apply(Object, Object)}; transformers that need to | ||
| * distinguish intermediate (prefix) applications from terminal (exact) ones should override it. | ||
| */ | ||
| default T applyIntermediate(T existing, U update) | ||
| { | ||
| return apply(existing, update); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -899,6 +911,69 @@ public <R> void putRecursive(ByteComparable key, R value, final UpsertTransforme | |
| root = newRoot; | ||
| } | ||
|
|
||
| /** | ||
| * A version of {@link #putSingleton(ByteComparable, Object, UpsertTransformer, boolean)} which, in addition to | ||
| * applying the value at the terminal node, also applies it as intermediate content at every depth selected by | ||
| * {@code accumulateIntermediateAtDepth}. Intermediate applications go through | ||
| * {@link UpsertTransformer#applyIntermediate}, while the terminal application uses {@link UpsertTransformer#apply}. | ||
| * <p> | ||
| * Depth is 1-based on the key bytes (the first byte is depth 1); the empty prefix (depth 0, the root) is never | ||
| * accumulated. A null policy is equivalent to {@link #putSingleton(ByteComparable, Object, UpsertTransformer, boolean)}. | ||
| */ | ||
| public <R> void putSingleton(ByteComparable key, | ||
| R value, | ||
| UpsertTransformer<T, ? super R> transformer, | ||
| boolean useRecursive, | ||
| IntPredicate accumulateIntermediateAtDepth) throws SpaceExhaustedException | ||
| { | ||
| if (accumulateIntermediateAtDepth == null) | ||
| { | ||
| putSingleton(key, value, transformer, useRecursive); | ||
| return; | ||
| } | ||
| putRecursiveWithPolicy(key, value, transformer, accumulateIntermediateAtDepth); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public <R> void putRecursiveWithPolicy(ByteComparable key, | ||
| R value, | ||
| UpsertTransformer<T, ? super R> transformer, | ||
| IntPredicate accumulateIntermediateAtDepth) throws SpaceExhaustedException | ||
| { | ||
| int newRoot = putRecursiveWithPolicy(root, key.asComparableBytes(BYTE_COMPARABLE_VERSION), value, | ||
| (UpsertTransformer<T, R>) transformer, accumulateIntermediateAtDepth, 0); | ||
| if (newRoot != root) | ||
| root = newRoot; | ||
| } | ||
|
|
||
| private <R> int putRecursiveWithPolicy(int node, ByteSource key, R value, final UpsertTransformer<T, R> transformer, | ||
| IntPredicate accumulateIntermediateAtDepth, int depth) throws SpaceExhaustedException | ||
| { | ||
| int transition = key.next(); | ||
| if (transition == ByteSource.END_OF_STREAM) | ||
| return applyContent(node, value, transformer, false); | ||
|
|
||
| boolean appliedHere = false; | ||
| if (depth >= 1 && accumulateIntermediateAtDepth.test(depth)) | ||
| { | ||
| node = applyContent(node, value, transformer, true); | ||
| appliedHere = true; | ||
| } | ||
|
|
||
| int child = getChild(node, transition); | ||
|
|
||
| int newChild = putRecursiveWithPolicy(child, key, value, transformer, accumulateIntermediateAtDepth, depth + 1); | ||
| if (newChild == child && !appliedHere) | ||
| return node; | ||
|
|
||
| int skippedContent = followContentTransition(node); | ||
| int attachedChild = !isNull(skippedContent) | ||
| ? attachChild(skippedContent, transition, newChild) // Single path, no copying required | ||
| : expandOrCreateChainNode(transition, newChild); | ||
|
|
||
| return preserveContent(node, skippedContent, attachedChild); | ||
| } | ||
|
|
||
| private <R> int putRecursive(int node, ByteSource key, R value, final UpsertTransformer<T, R> transformer) throws SpaceExhaustedException | ||
| { | ||
| int transition = key.next(); | ||
|
|
@@ -920,25 +995,35 @@ private <R> int putRecursive(int node, ByteSource key, R value, final UpsertTran | |
| } | ||
|
|
||
| private <R> int applyContent(int node, R value, UpsertTransformer<T, R> transformer) throws SpaceExhaustedException | ||
| { | ||
| return applyContent(node, value, transformer, false); | ||
| } | ||
|
|
||
| private <R> int applyContent(int node, R value, UpsertTransformer<T, R> transformer, boolean intermediate) throws SpaceExhaustedException | ||
| { | ||
| if (isNull(node)) | ||
| return ~addContent(transformer.apply(null, value)); | ||
| return ~addContent(combine(transformer, null, value, intermediate)); | ||
|
|
||
| if (isLeaf(node)) | ||
| { | ||
| int contentIndex = ~node; | ||
| setContent(contentIndex, transformer.apply(getContent(contentIndex), value)); | ||
| setContent(contentIndex, combine(transformer, getContent(contentIndex), value, intermediate)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we overload transformer.apply() with an extra arg instead of applyIntermeditate? |
||
| return node; | ||
| } | ||
|
|
||
| if (offset(node) == PREFIX_OFFSET) | ||
| { | ||
| int contentIndex = getInt(node + PREFIX_CONTENT_OFFSET); | ||
| setContent(contentIndex, transformer.apply(getContent(contentIndex), value)); | ||
| setContent(contentIndex, combine(transformer, getContent(contentIndex), value, intermediate)); | ||
| return node; | ||
| } | ||
| else | ||
| return createPrefixNode(addContent(transformer.apply(null, value)), node, false); | ||
| return createPrefixNode(addContent(combine(transformer, null, value, intermediate)), node, false); | ||
| } | ||
|
|
||
| private <R> T combine(UpsertTransformer<T, R> transformer, T existing, R value, boolean intermediate) | ||
| { | ||
| return intermediate ? transformer.applyIntermediate(existing, value) : transformer.apply(existing, value); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: avoid code duplication. probably we could pass a new arg to putSingleton() and/or putRecursive.
We might need to support both the flows as well.