-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate-projections-poc: MigrationProjectionCache
- Loading branch information
Alexander Lavrukov
committed
Jun 2, 2024
1 parent
9ef88c6
commit c9b0228
Showing
10 changed files
with
110 additions
and
87 deletions.
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
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