-
Notifications
You must be signed in to change notification settings - Fork 30
Add Prefixed database Access [ECR-4159]: #1382
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
Closed
dmitry-timofeev
wants to merge
4
commits into
numeric-index-ids-for-cache
from
add-prefixed-access-ECR-4159
Closed
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
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
106 changes: 106 additions & 0 deletions
106
...um-java-binding/core/src/main/java/com/exonum/binding/core/storage/database/Prefixed.java
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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright 2020 The Exonum Team | ||
* | ||
* Licensed 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 com.exonum.binding.core.storage.database; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.exonum.binding.core.proxy.Cleaner; | ||
import com.exonum.binding.core.proxy.NativeHandle; | ||
import com.exonum.binding.core.proxy.ProxyDestructor; | ||
import com.exonum.binding.core.storage.indices.IndexAddress; | ||
import com.exonum.binding.core.util.LibraryLoader; | ||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
/** | ||
* A prefixed database access. It uses a base Access, and adds an address resolution. | ||
* | ||
* <p>The Prefixed Access resolves the index addresses by prepending a namespace, followed by | ||
* a dot ('.'), to the {@linkplain IndexAddress#getName() name part} of the address. | ||
* | ||
* <p>This class is a native proxy of the {@code Prefixed} Rust Access. | ||
*/ | ||
public final class Prefixed extends AbstractAccess { | ||
|
||
static { | ||
LibraryLoader.load(); | ||
} | ||
|
||
private final Cleaner cleaner; | ||
|
||
Prefixed(NativeHandle prefixedNativeHandle, boolean canModify, Cleaner cleaner, | ||
OpenIndexRegistry registry) { | ||
super(prefixedNativeHandle, canModify, registry); | ||
this.cleaner = cleaner; | ||
} | ||
|
||
/** | ||
* Creates a new Prefixed access given the base database access and the namespace. | ||
* | ||
* <p>The new Prefixed access will inherit from the base access its "canModify" property; | ||
* and use its cleaner and registry of open indexes. When that cleaner gets closed, | ||
* this access will also be closed. | ||
* | ||
* @param namespace the namespace to use | ||
* @param baseAccess the base database access | ||
*/ | ||
@VisibleForTesting static Prefixed fromAccess(String namespace, AbstractAccess baseAccess) { | ||
// todo: If it is not used beyond tests, consider removing the _registry sharing_, | ||
// since that's the only method that uses (and needs) shared index registry. | ||
Cleaner cleaner = baseAccess.getCleaner(); | ||
OpenIndexRegistry registry = baseAccess.getOpenIndexes(); | ||
long handle = nativeCreate(namespace, baseAccess.getAccessNativeHandle()); | ||
return fromHandleInternal(handle, baseAccess.canModify(), cleaner, registry); | ||
} | ||
|
||
/** | ||
* Creates a Prefixed native peer given the base access native handle. | ||
* @throws RuntimeException if the base access is unsupported; or if the namespace is not valid | ||
*/ | ||
private static native long nativeCreate(String namespace, long accessNativeHandle); | ||
|
||
/** | ||
* Creates a new Prefixed access from the native handle. The destructor will be registered | ||
* in the given cleaner. | ||
* | ||
* @param prefixedNativeHandle a handle to the native Prefixed Access | ||
* @param canModify whether the base access allows modifications | ||
* @param cleaner a cleaner to destroy the native peer and any dependent objects | ||
*/ | ||
public static Prefixed fromHandle(long prefixedNativeHandle, boolean canModify, Cleaner cleaner) { | ||
checkNotNull(cleaner); | ||
// When the base Access is unknown (hidden in native) — use a separate pool of open indexes | ||
OpenIndexRegistry registry = new OpenIndexRegistry(); | ||
return fromHandleInternal(prefixedNativeHandle, canModify, cleaner, registry); | ||
} | ||
|
||
/** | ||
* Expects validated parameters so that it does not throw and registers the destructor | ||
* properly, which is *required* to prevent leaks. | ||
*/ | ||
private static Prefixed fromHandleInternal(long prefixedNativeHandle, boolean canModify, | ||
Cleaner cleaner, OpenIndexRegistry registry) { | ||
NativeHandle handle = new NativeHandle(prefixedNativeHandle); | ||
ProxyDestructor.newRegistered(cleaner, handle, Prefixed.class, | ||
/* todo [ECR-4161]: Verify after native implementation! */ Accesses::nativeFree); | ||
return new Prefixed(handle, canModify, cleaner, registry); | ||
} | ||
|
||
@Override | ||
public Cleaner getCleaner() { | ||
return cleaner; | ||
} | ||
} |
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.
nit: we usually use (handle, arguments....) order in native methods
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.
In
create*
a handle is just an argument, it is not a handle to "this".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.
Okay, I guess it's the matter of taste