From e6fd1d3f66f0568144f9296ef43b3e9bac0c2245 Mon Sep 17 00:00:00 2001 From: Alexander Lavrukov Date: Wed, 4 Dec 2024 11:32:07 +0300 Subject: [PATCH] Remove static find() method from YqlStatement --- .../ydb/statement/FindStatement.java | 34 +++++--- .../ydb/statement/FindYqlStatement.java | 4 - .../ydb/statement/YqlStatement.java | 78 ------------------- .../yoj/repository/ydb/table/YdbTable.java | 23 ++++-- .../ydb/YdbRepositoryCacheTest.java | 6 +- .../ydb/YdbRepositoryIntegrationTest.java | 6 +- .../ydb/merge/QueriesMergerTest.java | 6 +- 7 files changed, 56 insertions(+), 101 deletions(-) diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindStatement.java index bf0e71bd..2608b807 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindStatement.java @@ -4,27 +4,43 @@ import tech.ydb.yoj.databind.schema.Schema; import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; -import tech.ydb.yoj.repository.ydb.yql.YqlPredicate; +import tech.ydb.yoj.repository.ydb.yql.YqlOrderBy; import tech.ydb.yoj.repository.ydb.yql.YqlStatementPart; +import java.util.ArrayList; import java.util.Collection; -import java.util.function.Function; +import java.util.List; import static java.util.Comparator.comparing; import static java.util.stream.Collectors.joining; public class FindStatement, RESULT> extends PredicateStatement>, ENTITY, RESULT> { private final boolean distinct; - private final Collection> parts; + private final List> parts; - public FindStatement( + public static , R> FindStatement from( + @NonNull EntitySchema schema, + @NonNull Schema outSchema, + @NonNull Collection> parts, + boolean distinct + ) { + ArrayList> partsList = new ArrayList<>(parts); + if (!distinct) { + if (parts.stream().noneMatch(s -> s.getType().equals(YqlOrderBy.TYPE))) { + partsList.add(ORDER_BY_ID_ASCENDING); + } + } + + return new FindStatement<>(schema, outSchema, partsList, distinct); + } + + private FindStatement( @NonNull EntitySchema schema, @NonNull Schema outSchema, - @NonNull Collection> parts, - @NonNull Function>, YqlPredicate> predicateFrom, - boolean distinct, - String tableName) { - super(schema, outSchema, parts, predicateFrom, tableName); + @NonNull List> parts, + boolean distinct + ) { + super(schema, outSchema, parts, YqlStatement::predicateFrom); this.distinct = distinct; this.parts = parts; } diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindYqlStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindYqlStatement.java index 6b93189b..09077543 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindYqlStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindYqlStatement.java @@ -17,10 +17,6 @@ public FindYqlStatement(@NonNull EntitySchema schema, @NonNull Schema schema, @NonNull Schema resultSchema, String tableName) { - super(schema, resultSchema, tableName); - } - @Override public List getParams() { return schema.flattenId().stream() diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/YqlStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/YqlStatement.java index d5b69840..51fa291f 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/YqlStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/YqlStatement.java @@ -20,7 +20,6 @@ import tech.ydb.yoj.repository.ydb.yql.YqlStatementPart; import tech.ydb.yoj.repository.ydb.yql.YqlType; -import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; @@ -77,26 +76,6 @@ public static > Statement return new UpsertYqlStatement<>(type); } - public static > Statement find( - Class type - ) { - EntitySchema schema = EntitySchema.of(type); - return find(schema, schema); - } - - public static , VIEW extends View> Statement find( - Class type, - Class viewType - ) { - return find(EntitySchema.of(type), ViewSchema.of(viewType)); - } - - private static , RESULT> Statement find( - EntitySchema schema, - Schema resultSchema) { - return new FindYqlStatement<>(schema, resultSchema); - } - public static , ID extends Entity.Id> Statement, ENTITY> findRange( Class type, Range range @@ -188,38 +167,6 @@ private static , RESULT> Statement(schema, outSchema); } - public static > Statement>, ENTITY> find( - Class type, - Collection> parts - ) { - EntitySchema schema = EntitySchema.of(type); - return find(schema, schema, false, parts); - } - - public static , VIEW extends View> Statement>, VIEW> find( - Class type, - Class viewType, - Collection> parts - ) { - return find(type, viewType, false, parts); - } - - public static , VIEW extends View> Statement>, VIEW> find( - Class type, - Class viewType, - boolean distinct, - Collection> parts - ) { - return find(EntitySchema.of(type), ViewSchema.of(viewType), distinct, parts); - } - - public static , ID extends Entity.Id> Statement>, ID> findIds( - Class type, - Collection> parts - ) { - return find(EntitySchema.of(type), EntityIdSchema.ofEntity(type), false, parts); - } - public static , ID extends Entity.Id> Statement, ID> findIds( Class type, Range range @@ -247,31 +194,6 @@ public String getDeclaration(String name, String type) { return String.format("DECLARE %s AS %s;\n", name, type); } - private static , RESULT> Statement>, RESULT> find( - EntitySchema schema, - Schema resultSchema, - boolean distinct, - Collection> parts - ) { - return find(schema, resultSchema, distinct, parts, schema.getName()); - } - - static , RESULT> Statement>, RESULT> find( - EntitySchema schema, - Schema resultSchema, - boolean distinct, - Collection> parts, - String tableName - ) { - List> partList = new ArrayList<>(parts); - if (!distinct) { - if (parts.stream().noneMatch(s -> s.getType().equals(YqlOrderBy.TYPE))) { - partList.add(ORDER_BY_ID_ASCENDING); - } - } - return new FindStatement<>(schema, resultSchema, parts, YqlStatement::predicateFrom, distinct, tableName); - } - public static > Statement>, Count> count( Class entityType, Collection> parts diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/table/YdbTable.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/table/YdbTable.java index bab85ec8..af6563cf 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/table/YdbTable.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/table/YdbTable.java @@ -24,6 +24,8 @@ import tech.ydb.yoj.repository.ydb.bulk.BulkMapperImpl; import tech.ydb.yoj.repository.ydb.readtable.EntityIdKeyMapper; import tech.ydb.yoj.repository.ydb.readtable.ReadTableMapper; +import tech.ydb.yoj.repository.ydb.statement.FindStatement; +import tech.ydb.yoj.repository.ydb.statement.FindYqlStatement; import tech.ydb.yoj.repository.ydb.statement.Statement; import tech.ydb.yoj.repository.ydb.statement.UpdateInStatement; import tech.ydb.yoj.repository.ydb.statement.UpdateModel; @@ -223,14 +225,17 @@ public T find(Entity.Id id) { throw new IllegalArgumentException("Cannot use partial id in find method"); } return executor.getTransactionLocal().firstLevelCache().get(id, __ -> { - List res = postLoad(executor.execute(YqlStatement.find(type), id)); + var statement = new FindYqlStatement<>(schema, schema); + List res = postLoad(executor.execute(statement, id)); return res.isEmpty() ? null : res.get(0); }); } @Override public V find(Class viewType, Entity.Id id) { - List res = executor.execute(YqlStatement.find(type, viewType), id); + ViewSchema viewSchema = ViewSchema.of(viewType); + var statement = new FindYqlStatement<>(schema, viewSchema); + List res = executor.execute(statement, id); return res.isEmpty() ? null : res.get(0); } @@ -254,7 +259,8 @@ public final List find(YqlStatementPart part, YqlStatementPart... other } public List find(Collection> parts) { - return postLoad(executor.execute(YqlStatement.find(type, parts), parts)); + var statement = FindStatement.from(schema, schema, parts, false); + return postLoad(executor.execute(statement, parts)); } @Override @@ -385,7 +391,9 @@ public List find(Class viewType, YqlStatementPart part } public List find(Class viewType, Collection> parts, boolean distinct) { - return executor.execute(YqlStatement.find(type, viewType, distinct, parts), parts); + ViewSchema viewSchema = ViewSchema.of(viewType); + var statement = FindStatement.from(schema, viewSchema, parts, distinct); + return executor.execute(statement, parts); } public > List findIds(YqlStatementPart part, YqlStatementPart... otherParts) { @@ -393,7 +401,9 @@ public > List findIds(YqlStatementPart part, YqlS } private > List findIds(Collection> parts) { - return executor.execute(YqlStatement.findIds(type, parts), parts); + EntityIdSchema idSchema = EntityIdSchema.ofEntity(type); + var statement = FindStatement.from(schema, idSchema, parts, false); + return executor.execute(statement, parts); } @Override @@ -450,7 +460,8 @@ public void delete(Entity.Id id) { * @param entity ID type */ public > void migrate(ID id) { - List foundRaw = executor.execute(YqlStatement.find(type), id); + var statement = new FindYqlStatement<>(schema, schema); + List foundRaw = executor.execute(statement, id); if (foundRaw.isEmpty()) { return; } diff --git a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/YdbRepositoryCacheTest.java b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/YdbRepositoryCacheTest.java index bee9ee3b..1b9474fc 100644 --- a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/YdbRepositoryCacheTest.java +++ b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/YdbRepositoryCacheTest.java @@ -14,6 +14,7 @@ import tech.ydb.table.query.DataQueryResult; import tech.ydb.table.query.Params; import tech.ydb.table.values.StructType; +import tech.ydb.yoj.repository.db.EntitySchema; import tech.ydb.yoj.repository.db.Range; import tech.ydb.yoj.repository.db.exception.EntityAlreadyExistsException; import tech.ydb.yoj.repository.test.sample.TestEntityOperations; @@ -21,6 +22,7 @@ import tech.ydb.yoj.repository.test.sample.model.Complex.Id; import tech.ydb.yoj.repository.ydb.client.SessionManager; import tech.ydb.yoj.repository.ydb.client.YdbConverter; +import tech.ydb.yoj.repository.ydb.statement.FindYqlStatement; import tech.ydb.yoj.repository.ydb.statement.MultipleVarsYqlStatement; import tech.ydb.yoj.repository.ydb.statement.YqlStatement; @@ -350,6 +352,8 @@ private CompletableFuture> convertEntity(List c } private Params convertId(Id id) { - return YdbConverter.convertToParams(YqlStatement.find(Complex.class).toQueryParameters(id)); + EntitySchema schema = EntitySchema.of(Complex.class); + var statement = new FindYqlStatement<>(schema, schema); + return YdbConverter.convertToParams(statement.toQueryParameters(id)); } } diff --git a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/YdbRepositoryIntegrationTest.java b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/YdbRepositoryIntegrationTest.java index 980c5d56..dd7fef28 100644 --- a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/YdbRepositoryIntegrationTest.java +++ b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/YdbRepositoryIntegrationTest.java @@ -72,6 +72,7 @@ import tech.ydb.yoj.repository.ydb.sample.model.HintInt64Range; import tech.ydb.yoj.repository.ydb.sample.model.HintTablePreset; import tech.ydb.yoj.repository.ydb.sample.model.HintUniform; +import tech.ydb.yoj.repository.ydb.statement.FindStatement; import tech.ydb.yoj.repository.ydb.statement.YqlStatement; import tech.ydb.yoj.repository.ydb.table.YdbTable; import tech.ydb.yoj.repository.ydb.yql.YqlPredicate; @@ -888,12 +889,13 @@ public void complexIdLtYsingYqlPredicate() { private void executeQuery(String expectSqlQuery, List expectRows, Collection> query) { - var statement = YqlStatement.find(IndexedEntity.class, query); + EntitySchema schema = EntitySchema.of(IndexedEntity.class); + var statement = FindStatement.from(schema, schema, new ArrayList<>(query), false); var sqlQuery = statement.getQuery("ts/"); assertEquals(expectSqlQuery, sqlQuery); // Check we use index and query was not failed - var actual = db.tx(() -> ((YdbTable) db.indexedTable()).find(query)); + var actual = db.tx(() -> ((YdbTable) db.indexedTable()).find(new ArrayList<>(query))); assertEquals(expectRows, actual); } diff --git a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/merge/QueriesMergerTest.java b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/merge/QueriesMergerTest.java index 62d32960..f6fa46e0 100644 --- a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/merge/QueriesMergerTest.java +++ b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/merge/QueriesMergerTest.java @@ -3,11 +3,13 @@ import org.assertj.core.api.Assertions; import org.junit.Test; import tech.ydb.yoj.repository.db.Entity; +import tech.ydb.yoj.repository.db.EntitySchema; import tech.ydb.yoj.repository.db.cache.RepositoryCache; import tech.ydb.yoj.repository.db.cache.RepositoryCacheImpl; import tech.ydb.yoj.repository.test.sample.model.Primitive; import tech.ydb.yoj.repository.test.sample.model.Project; import tech.ydb.yoj.repository.ydb.YdbRepository; +import tech.ydb.yoj.repository.ydb.statement.FindYqlStatement; import tech.ydb.yoj.repository.ydb.statement.Statement; import tech.ydb.yoj.repository.ydb.statement.YqlStatement; @@ -154,7 +156,9 @@ private > YdbRepository.Query insert(T p) { @SuppressWarnings("unchecked") private > YdbRepository.Query find(T p) { - return new YdbRepository.Query<>(YqlStatement.find((Class) p.getClass()), p.getId()); + EntitySchema schema = EntitySchema.of((Class) p.getClass()); + var statement = new FindYqlStatement<>(schema, schema); + return new YdbRepository.Query<>(statement, p.getId()); } @SuppressWarnings("unchecked")