-
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.
- Loading branch information
Alexander Lavrukov
committed
Apr 8, 2024
1 parent
6fbd377
commit b034465
Showing
5 changed files
with
88 additions
and
53 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
68 changes: 68 additions & 0 deletions
68
repository/src/main/java/tech/ydb/yoj/repository/db/CommonTable.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,68 @@ | ||
package tech.ydb.yoj.repository.db; | ||
|
||
import com.google.common.collect.Sets; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import tech.ydb.yoj.repository.db.cache.TransactionLocal; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.util.stream.Collectors.toSet; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public final class CommonTable { | ||
public static <E extends Entity<E>, ID extends Entity.Id<E>> List<E> find( | ||
TransactionLocal transactionLocal, Table<E> table, Set<ID> ids) { | ||
if (ids.isEmpty()) { | ||
return List.of(); | ||
} | ||
|
||
var orderBy = EntityExpressions.defaultOrder(table.getType()); | ||
var cache = Tx.Current.get().getRepositoryTransaction().getTransactionLocal().firstLevelCache(); | ||
var isPartialIdMode = ids.iterator().next().isPartial(); | ||
|
||
var foundInCache = ids.stream() | ||
.filter(cache::containsKey) | ||
.map(cache::peek) | ||
.flatMap(Optional::stream) | ||
.collect(Collectors.toMap(Entity::getId, Function.identity())); | ||
var remainingIds = Sets.difference(ids, foundInCache.keySet()); | ||
var foundInDb = table.findUncached(remainingIds, null, orderBy, null); | ||
|
||
var merged = new HashMap<Entity.Id<E>, E>(); | ||
|
||
// some entries found in db with partial id query may already be in cache (after update/delete), | ||
// so we must return actual entries from cache | ||
for (var entry : foundInDb) { | ||
var id = entry.getId(); | ||
if (cache.containsKey(id)) { | ||
var cached = cache.peek(id); | ||
cached.ifPresent(t -> merged.put(id, t)); | ||
// not present means marked as deleted in cache | ||
} else { | ||
merged.put(id, table.postLoad(entry)); | ||
} | ||
} | ||
|
||
// add entries found in cache and not fetched from db | ||
for (var pair : foundInCache.entrySet()) { | ||
var id = pair.getKey(); | ||
var entry = pair.getValue(); | ||
merged.put(id, entry); | ||
} | ||
|
||
if (!isPartialIdMode) { | ||
Set<Entity.Id<E>> foundInDbIds = foundInDb.stream().map(Entity::getId).collect(toSet()); | ||
Set<Entity.Id<E>> foundInCacheIds = new HashSet<>(foundInCache.keySet()); | ||
Sets.difference(Sets.difference(ids, foundInDbIds), foundInCacheIds).forEach(cache::putEmpty); | ||
} | ||
|
||
return merged.values().stream().sorted(EntityIdSchema.SORT_ENTITY_BY_ID).collect(Collectors.toList()); | ||
} | ||
} |
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