Skip to content

Commit 112a6d7

Browse files
committed
noticket: Use EntitySchema to centrally retrieve ID and view schemas related to the entity
...instead of directly calling `EntityIdSchema` and `ViewSchema` static factories
1 parent 21dff7c commit 112a6d7

File tree

15 files changed

+170
-93
lines changed

15 files changed

+170
-93
lines changed

repository-inmemory/src/main/java/tech/ydb/yoj/repository/test/inmemory/InMemoryDataShard.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import tech.ydb.yoj.repository.db.Range;
88
import tech.ydb.yoj.repository.db.Table;
99
import tech.ydb.yoj.repository.db.TableDescriptor;
10-
import tech.ydb.yoj.repository.db.ViewSchema;
1110
import tech.ydb.yoj.repository.db.exception.EntityAlreadyExistsException;
1211
import tech.ydb.yoj.repository.db.exception.OptimisticLockException;
1312

@@ -39,25 +38,26 @@ private InMemoryDataShard(
3938
this.entityLines = entityLines;
4039
}
4140

42-
public InMemoryDataShard(TableDescriptor<T> tableDescriptor) {
41+
public InMemoryDataShard(TableDescriptor<T> tableDescriptor, EntitySchema<T> schema) {
4342
this(
4443
tableDescriptor,
45-
EntitySchema.of(tableDescriptor.entityType()),
46-
createEmptyLines(tableDescriptor.entityType())
44+
schema,
45+
createEmptyLines(schema)
4746
);
4847
}
4948

50-
private static <T extends Entity<T>> Map<Entity.Id<T>, InMemoryEntityLine> createEmptyLines(Class<T> type) {
49+
private static <T extends Entity<T>> Map<Entity.Id<T>, InMemoryEntityLine> createEmptyLines(EntitySchema<T> schema) {
50+
EntityIdSchema<Entity.Id<T>> idSchema = schema.getIdSchema();
5151
if ("oninsert".equals(MAP_IMPLEMENTATION)) {
52-
return new EntityIdMap<>(EntityIdSchema.getIdComparator(type));
52+
return new EntityIdMap<>(idSchema);
5353
} else if ("onget".equals(MAP_IMPLEMENTATION)) {
54-
return new EntityIdMapOnGet<>(EntityIdSchema.getIdComparator(type));
54+
return new EntityIdMapOnGet<>(idSchema);
5555
}
56-
return new TreeMap<>(EntityIdSchema.getIdComparator(type));
56+
return new TreeMap<>(idSchema);
5757
}
5858

5959
public synchronized InMemoryDataShard<T> createSnapshot() {
60-
Map<Entity.Id<T>, InMemoryEntityLine> snapshotLines = createEmptyLines(tableDescriptor.entityType());
60+
Map<Entity.Id<T>, InMemoryEntityLine> snapshotLines = createEmptyLines(schema);
6161
for (Map.Entry<Entity.Id<T>, InMemoryEntityLine> entry : entityLines.entrySet()) {
6262
snapshotLines.put(entry.getKey(), entry.getValue().createSnapshot());
6363
}
@@ -133,7 +133,7 @@ public synchronized <V extends Table.View> V find(
133133
return null;
134134
}
135135
Columns columns = entityLine.get(txId, version);
136-
return columns != null ? columns.toSchema(ViewSchema.of(viewType)) : null;
136+
return columns != null ? columns.toSchema(schema.getViewSchema(viewType)) : null;
137137
}
138138

139139
public synchronized List<T> findAll(long txId, long version, InMemoryTxLockWatcher watcher) {

repository-inmemory/src/main/java/tech/ydb/yoj/repository/test/inmemory/InMemoryStorage.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tech.ydb.yoj.repository.test.inmemory;
22

33
import tech.ydb.yoj.repository.db.Entity;
4+
import tech.ydb.yoj.repository.db.EntitySchema;
45
import tech.ydb.yoj.repository.db.TableDescriptor;
56

67
import java.util.HashMap;
@@ -108,7 +109,10 @@ public synchronized <T extends Entity<T>> void createTable(TableDescriptor<T> ta
108109
if (containsTable(tableDescriptor)) {
109110
return;
110111
}
111-
shards.put(tableDescriptor, new InMemoryDataShard<>(tableDescriptor));
112+
113+
// TODO(nvamelichev): In the future, InMemoryStorage/SchemaOperations should take SchemaRegistry instead of assuming the default one...
114+
EntitySchema<T> schema = EntitySchema.of(tableDescriptor.entityType());
115+
shards.put(tableDescriptor, new InMemoryDataShard<>(tableDescriptor, schema));
112116
}
113117

114118
public synchronized boolean dropTable(TableDescriptor<?> tableDescriptor) {

repository-inmemory/src/main/java/tech/ydb/yoj/repository/test/inmemory/InMemoryTable.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ public InMemoryTable(DbMemory<T> memory) {
5151
}
5252

5353
public InMemoryTable(InMemoryRepositoryTransaction transaction, Class<T> type) {
54-
this(transaction, TableDescriptor.from(EntitySchema.of(type)));
54+
this.schema = EntitySchema.of(type);
55+
this.tableDescriptor = TableDescriptor.from(schema);
56+
this.transaction = transaction;
5557
}
5658

5759
public InMemoryTable(InMemoryRepositoryTransaction transaction, TableDescriptor<T> tableDescriptor) {
@@ -62,7 +64,7 @@ public InMemoryTable(InMemoryRepositoryTransaction transaction, TableDescriptor<
6264

6365
@Override
6466
public List<T> findAll() {
65-
transaction.getWatcher().markTableRead(tableDescriptor);
67+
transaction.getWatcher().markTableRead(tableDescriptor, schema);
6668
return findAll0();
6769
}
6870

@@ -92,9 +94,7 @@ public void update(Entity.Id<T> id, Changeset changeset) {
9294
}
9395
Map<String, Object> cells = new HashMap<>(schema.flatten(found));
9496

95-
changeset.toMap().forEach((k, v) -> {
96-
cells.putAll(schema.flattenOneField(k, v));
97-
});
97+
changeset.toMap().forEach((k, v) -> cells.putAll(schema.flattenOneField(k, v)));
9898

9999
T newInstance = schema.newInstance(cells);
100100

@@ -285,7 +285,7 @@ public <ID extends Entity.Id<T>> List<T> findUncached(
285285
Set<Map<String, Object>> idsSet = ids.stream().map(idSchema::flatten).collect(toUnmodifiableSet());
286286
Set<Set<String>> idFieldsSet = idsSet.stream().map(Map::keySet).collect(toUnmodifiableSet());
287287

288-
Preconditions.checkArgument(idFieldsSet.size() > 0, "ids must have at least one non-null field");
288+
Preconditions.checkArgument(!idFieldsSet.isEmpty(), "ids must have at least one non-null field");
289289
Preconditions.checkArgument(idFieldsSet.size() == 1, "ids must have nulls in the same fields");
290290

291291
Set<String> idFields = Iterables.getOnlyElement(idFieldsSet);
@@ -349,7 +349,7 @@ public <KEY> List<T> find(
349349
Set<Map<String, Object>> keysSet = keys.stream().map(keySchema::flatten).collect(toUnmodifiableSet());
350350
Set<Set<String>> keyFieldsSet = keysSet.stream().map(Map::keySet).collect(toUnmodifiableSet());
351351

352-
Preconditions.checkArgument(keyFieldsSet.size() != 0, "keys should have at least one non-null field");
352+
Preconditions.checkArgument(!keyFieldsSet.isEmpty(), "keys should have at least one non-null field");
353353
Preconditions.checkArgument(keyFieldsSet.size() == 1, "keys should have nulls in the same fields");
354354

355355
Set<String> keyFields = Iterables.getOnlyElement(keyFieldsSet);
@@ -377,7 +377,7 @@ public <KEY> List<T> find(
377377
);
378378

379379
for (Map<String, Object> id : keysSet) {
380-
transaction.getWatcher().markRangeRead(tableDescriptor, id);
380+
transaction.getWatcher().markRangeRead(tableDescriptor, schema, id);
381381
}
382382

383383
Stream<T> result = getAllEntries().stream()
@@ -413,7 +413,7 @@ private <ID extends Entity.Id<T>> void markKeyRead(ID id) {
413413
EntityIdSchema<Entity.Id<T>> idSchema = schema.getIdSchema();
414414
if (idSchema.flattenFieldNames().size() != idSchema.flatten(id).size()) {
415415
// Partial key, will throw error when not searching by PK prefix
416-
transaction.getWatcher().markRangeRead(tableDescriptor, Range.create(id, id));
416+
transaction.getWatcher().markRangeRead(tableDescriptor, Range.create(idSchema, id));
417417
} else {
418418
transaction.getWatcher().markRowRead(tableDescriptor, id);
419419
}
@@ -482,7 +482,8 @@ public <ID extends Entity.Id<T>> Stream<T> streamPartial(ID partial, int batchSi
482482
Preconditions.checkArgument(1 <= batchSize && batchSize <= 5000,
483483
"batchSize must be in range [1, 5000], got %s", batchSize);
484484

485-
Range<ID> range = partial == null ? null : Range.create(partial);
485+
EntityIdSchema<ID> idSchema = schema.getIdSchema();
486+
Range<ID> range = partial == null ? null : Range.create(idSchema, partial);
486487
markRangeRead(range);
487488

488489
return streamPartial0(range);
@@ -515,15 +516,16 @@ public <ID extends Entity.Id<T>> Stream<ID> streamPartialIds(ID partial, int bat
515516
Preconditions.checkArgument(1 <= batchSize && batchSize <= 10000,
516517
"batchSize must be in range [1, 10000], got %s", batchSize);
517518

518-
Range<ID> range = partial == null ? null : Range.create(partial);
519+
EntityIdSchema<ID> idSchema = schema.getIdSchema();
520+
Range<ID> range = partial == null ? null : Range.create(idSchema, partial);
519521
markRangeRead(range);
520522

521523
return streamPartial0(range).map(e -> (ID) e.getId());
522524
}
523525

524526
private <ID extends Entity.Id<T>> void markRangeRead(Range<ID> range) {
525527
if (range == null) {
526-
transaction.getWatcher().markTableRead(tableDescriptor);
528+
transaction.getWatcher().markTableRead(tableDescriptor, schema);
527529
} else {
528530
transaction.getWatcher().markRangeRead(tableDescriptor, range);
529531
}
@@ -552,15 +554,16 @@ private <ID extends Entity.Id<T>> boolean readTableFilter(T e, ReadTableParams<I
552554
@SuppressWarnings("unchecked")
553555
ID id = (ID) e.getId();
554556
ID from = params.getFromKey();
557+
EntityIdSchema<ID> idSchema = schema.getIdSchema();
555558
if (from != null) {
556-
int compare = EntityIdSchema.ofEntity(id.getType()).compare(id, from);
559+
int compare = idSchema.compare(id, from);
557560
if (params.isFromInclusive() ? compare < 0 : compare <= 0) {
558561
return false;
559562
}
560563
}
561564
ID to = params.getToKey();
562565
if (to != null) {
563-
int compare = EntityIdSchema.ofEntity(id.getType()).compare(id, to);
566+
int compare = idSchema.compare(id, to);
564567
return params.isToInclusive() ? compare <= 0 : compare < 0;
565568
}
566569
return true;
@@ -589,7 +592,7 @@ private static <V extends Table.View, T extends Entity<T>> V toView(
589592
return null;
590593
}
591594

592-
ViewSchema<V> viewSchema = ViewSchema.of(viewType);
595+
ViewSchema<V> viewSchema = schema.getViewSchema(viewType);
593596
return Columns.fromEntity(schema, entity).toSchema(viewSchema);
594597
}
595598

repository-inmemory/src/main/java/tech/ydb/yoj/repository/test/inmemory/InMemoryTxLockWatcher.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ public <T extends Entity<T>, ID extends Entity.Id<T>> void markRangeRead(TableDe
3333
readRanges.computeIfAbsent(tableDescriptor, __ -> new ArrayList<>()).add(range);
3434
}
3535

36-
public <T extends Entity<T>, ID extends Entity.Id<T>> void markRangeRead(TableDescriptor<T> tableDescriptor, Map<String, Object> map) {
37-
Range<ID> range = Range.create(EntitySchema.of(tableDescriptor.entityType()).getIdSchema(), map);
36+
public <T extends Entity<T>, ID extends Entity.Id<T>> void markRangeRead(TableDescriptor<T> tableDescriptor, EntitySchema<T> schema, Map<String, Object> map) {
37+
Range<ID> range = Range.create(schema.getIdSchema(), map);
3838
markRangeRead(tableDescriptor, range);
3939
}
4040

41-
public <T extends Entity<T>, ID extends Entity.Id<T>> void markTableRead(TableDescriptor<T> tableDescriptor) {
42-
Range<ID> range = Range.create(EntitySchema.of(tableDescriptor.entityType()).getIdSchema(), Map.of());
41+
public <T extends Entity<T>, ID extends Entity.Id<T>> void markTableRead(TableDescriptor<T> tableDescriptor, EntitySchema<T> schema) {
42+
Range<ID> range = Range.create(schema.getIdSchema(), Map.of());
4343
markRangeRead(tableDescriptor, range);
4444
}
4545

0 commit comments

Comments
 (0)