-
Notifications
You must be signed in to change notification settings - Fork 13
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
443869c
commit 07f41b1
Showing
3 changed files
with
83 additions
and
24 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
66 changes: 66 additions & 0 deletions
66
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,66 @@ | ||
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.Tx; | ||
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(Entity<?> entity) { | ||
delete(entity.getId()); | ||
|
||
List<Entity<?>> newProjections = entity.createProjections(); | ||
for (Entity<?> projection : newProjections) { | ||
saveEntity(projection); | ||
} | ||
} | ||
|
||
@Override | ||
public void delete(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(projection.getId()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void applyProjectionChanges(RepositoryTransaction transaction) { | ||
} | ||
|
||
private <T extends Entity<T>> void deleteEntity(Entity.Id<T> entityId) { | ||
getTransaction().table(entityId.getType()).delete(entityId); | ||
} | ||
|
||
private <T extends Entity<T>> void saveEntity(Entity<T> entity) { | ||
@SuppressWarnings("unchecked") | ||
T castedEntity = (T) entity; | ||
|
||
getTransaction().table(entity.getId().getType()).save(castedEntity); | ||
} | ||
|
||
private static RepositoryTransaction getTransaction() { | ||
return Tx.Current.get().getRepositoryTransaction(); | ||
} | ||
} |