-
Notifications
You must be signed in to change notification settings - Fork 12
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
separate-projections-poc: MigrationProjectionCache #76
Open
lavrukov
wants to merge
1
commit into
main
Choose a base branch
from
separate-projections-poc
base: main
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.
Open
Changes from all commits
Commits
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 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 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 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 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 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 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 |
---|---|---|
|
@@ -3,46 +3,48 @@ | |
import lombok.NonNull; | ||
import tech.ydb.yoj.repository.BaseDb; | ||
import tech.ydb.yoj.repository.db.TxOptions; | ||
import tech.ydb.yoj.repository.db.projection.MigrationProjectionCache; | ||
import tech.ydb.yoj.repository.db.projection.ProjectionCache; | ||
import tech.ydb.yoj.repository.db.projection.RoProjectionCache; | ||
import tech.ydb.yoj.repository.db.projection.RwProjectionCache; | ||
|
||
import java.util.IdentityHashMap; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
public class TransactionLocal { | ||
private final Map<Supplier<?>, Object> singletons = new IdentityHashMap<>(); | ||
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. I think that this singletons is made for case when in tx wasn't any query. But this objects is lightwave, maybe we can do it without singletons? Else I have to do some painful refactoring for this feature ;( |
||
|
||
private final Supplier<FirstLevelCache> firstLevelCacheSupplier; | ||
private final Supplier<ProjectionCache> projectionCacheSupplier; | ||
private final Supplier<TransactionLog> logSupplier; | ||
private final FirstLevelCache firstLevelCache; | ||
private final ProjectionCache projectionCache; | ||
private final TransactionLog log; | ||
|
||
public TransactionLocal(@NonNull TxOptions options) { | ||
this.firstLevelCacheSupplier = options.isFirstLevelCache() ? FirstLevelCache::create : FirstLevelCache::empty; | ||
this.projectionCacheSupplier = options.isMutable() ? RwProjectionCache::new : RoProjectionCache::new; | ||
this.logSupplier = () -> new TransactionLog(options.getLogLevel()); | ||
this.firstLevelCache = options.isFirstLevelCache() ? FirstLevelCache.create() : FirstLevelCache.empty(); | ||
this.projectionCache = createProjectionCache(firstLevelCache, options); | ||
this.log = new TransactionLog(options.getLogLevel()); | ||
} | ||
|
||
public static TransactionLocal get() { | ||
return BaseDb.current(Holder.class).getTransactionLocal(); | ||
private static ProjectionCache createProjectionCache(FirstLevelCache firstLevelCache, TxOptions options) { | ||
if (options.isMutable()) { | ||
if (options.isSeparateProjections()) { | ||
return new MigrationProjectionCache(firstLevelCache); | ||
} | ||
|
||
return new RwProjectionCache(); | ||
} | ||
|
||
return new RoProjectionCache(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <X> X instance(@NonNull Supplier<X> supplier) { | ||
return (X) singletons.computeIfAbsent(supplier, Supplier::get); | ||
public static TransactionLocal get() { | ||
return BaseDb.current(Holder.class).getTransactionLocal(); | ||
} | ||
|
||
public ProjectionCache projectionCache() { | ||
return instance(projectionCacheSupplier); | ||
return projectionCache; | ||
} | ||
|
||
public FirstLevelCache firstLevelCache() { | ||
return instance(firstLevelCacheSupplier); | ||
return firstLevelCache; | ||
} | ||
|
||
public TransactionLog log() { | ||
return instance(logSupplier); | ||
return log; | ||
} | ||
|
||
public interface Holder { | ||
|
61 changes: 61 additions & 0 deletions
61
repository/src/main/java/tech/ydb/yoj/repository/db/projection/MigrationProjectionCache.java
This file contains 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,61 @@ | ||
package tech.ydb.yoj.repository.db.projection; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import tech.ydb.yoj.repository.db.Entity; | ||
import tech.ydb.yoj.repository.db.RepositoryTransaction; | ||
import tech.ydb.yoj.repository.db.cache.FirstLevelCache; | ||
|
||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.Optional; | ||
|
||
@RequiredArgsConstructor | ||
public class MigrationProjectionCache implements ProjectionCache { | ||
private final FirstLevelCache cache; | ||
|
||
@Override | ||
public void load(Entity<?> entity) { | ||
} | ||
|
||
@Override | ||
public void save(RepositoryTransaction transaction, Entity<?> entity) { | ||
delete(transaction, entity.getId()); | ||
|
||
List<Entity<?>> newProjections = entity.createProjections(); | ||
for (Entity<?> projection : newProjections) { | ||
saveEntity(transaction, projection); | ||
} | ||
} | ||
|
||
@Override | ||
public void delete(RepositoryTransaction transaction, Entity.Id<?> id) { | ||
Optional<? extends Entity<?>> oldEntity; | ||
try { | ||
oldEntity = cache.peek(id); | ||
} catch (NoSuchElementException e) { | ||
return; | ||
} | ||
|
||
if (oldEntity.isPresent()) { | ||
List<Entity<?>> oldProjections = oldEntity.get().createProjections(); | ||
for (Entity<?> projection : oldProjections) { | ||
deleteEntity(transaction, projection.getId()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void applyProjectionChanges(RepositoryTransaction transaction) { | ||
} | ||
|
||
private <T extends Entity<T>> void deleteEntity(RepositoryTransaction transaction, Entity.Id<T> entityId) { | ||
transaction.table(entityId.getType()).delete(entityId); | ||
} | ||
|
||
private <T extends Entity<T>> void saveEntity(RepositoryTransaction transaction, Entity<T> entity) { | ||
@SuppressWarnings("unchecked") | ||
T castedEntity = (T) entity; | ||
|
||
transaction.table(entity.getId().getType()).save(castedEntity); | ||
} | ||
} |
This file contains 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 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 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
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.
It doesn't work. I gave birth to this, I will kill it