From 05b48e8a73679220adc270f1b5dd1fee0115ba79 Mon Sep 17 00:00:00 2001 From: Alexander Lavrukov Date: Wed, 4 Dec 2024 13:31:33 +0300 Subject: [PATCH] Remove static count(), delete() and deleteAll() methods from YqlStatement --- .../ydb/merge/ByEntityYqlQueriesMerger.java | 4 ++- .../ydb/statement/CountAllStatement.java | 28 ++++------------- .../ydb/statement/DeleteAllStatement.java | 11 ++----- .../ydb/statement/DeleteByIdStatement.java | 9 ++---- .../ydb/statement/FindStatement.java | 3 +- .../statement/MultipleVarsYqlStatement.java | 4 +++ .../ydb/statement/YqlStatement.java | 31 ------------------- .../yoj/repository/ydb/table/YdbTable.java | 10 ++++-- .../yoj/repository/ydb/yql/YqlPredicate.java | 10 ++++++ .../ydb/merge/QueriesMergerTest.java | 6 ++-- .../statement/DeleteByIdStatementTest.java | 3 +- 11 files changed, 44 insertions(+), 75 deletions(-) diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/merge/ByEntityYqlQueriesMerger.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/merge/ByEntityYqlQueriesMerger.java index 438b2f01..b458d960 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/merge/ByEntityYqlQueriesMerger.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/merge/ByEntityYqlQueriesMerger.java @@ -6,10 +6,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.exception.EntityAlreadyExistsException; import tech.ydb.yoj.repository.ydb.YdbRepository; import tech.ydb.yoj.repository.ydb.exception.YdbRepositoryException; +import tech.ydb.yoj.repository.ydb.statement.DeleteByIdStatement; import tech.ydb.yoj.repository.ydb.statement.Statement; import tech.ydb.yoj.repository.ydb.statement.YqlStatement; @@ -163,7 +165,7 @@ private static YdbRepository.Query convertInsertToUpsert(YdbRepository.Query @SuppressWarnings("unchecked") private static YdbRepository.Query convertInsertToDelete(YdbRepository.Query query) { - return new YdbRepository.Query<>(YqlStatement.delete(getEntityClass(query)), getEntityId(query)); + return new YdbRepository.Query<>(new DeleteByIdStatement<>(EntitySchema.of(getEntityClass(query))), getEntityId(query)); } private static Entity.Id getEntityId(YdbRepository.Query query) { diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/CountAllStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/CountAllStatement.java index 1fdb8236..a054e0bb 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/CountAllStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/CountAllStatement.java @@ -1,38 +1,22 @@ package tech.ydb.yoj.repository.ydb.statement; -import tech.ydb.yoj.databind.schema.Schema; +import tech.ydb.yoj.databind.schema.ObjectSchema; 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.YqlStatementPart; 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; -class CountAllStatement> extends PredicateStatement>, ENTITY, Count> { - private final Collection> parts; +public class CountAllStatement> extends PredicateStatement>, ENTITY, Count> { + private final List> parts; - public CountAllStatement( - EntitySchema schema, - Schema resultSchema, - Collection> parts, - Function>, YqlPredicate> predicateFrom - ) { - super(schema, resultSchema, parts, predicateFrom); - this.parts = parts; - } - - public CountAllStatement( - EntitySchema schema, - Schema resultSchema, - Collection> parts, - Function>, YqlPredicate> predicateFrom, - String tableName - ) { - super(schema, resultSchema, parts, predicateFrom, tableName); + public CountAllStatement(EntitySchema schema, List> parts) { + super(schema, ObjectSchema.of(Count.class), parts, YqlPredicate::from); this.parts = parts; } diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteAllStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteAllStatement.java index 1a3f57ed..3552ccfc 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteAllStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteAllStatement.java @@ -1,16 +1,11 @@ package tech.ydb.yoj.repository.ydb.statement; -import lombok.NonNull; import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; -class DeleteAllStatement> extends YqlStatement { - public DeleteAllStatement(@NonNull Class type) { - super(EntitySchema.of(type), EntitySchema.of(type)); - } - - public DeleteAllStatement(@NonNull EntitySchema schema, String tableName) { - super(schema, schema, tableName); +public class DeleteAllStatement> extends YqlStatement { + public DeleteAllStatement(EntitySchema schema) { + super(schema, schema); } @Override diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteByIdStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteByIdStatement.java index 9e5b6772..bbec2595 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteByIdStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteByIdStatement.java @@ -1,17 +1,14 @@ package tech.ydb.yoj.repository.ydb.statement; import tech.ydb.yoj.repository.db.Entity; +import tech.ydb.yoj.repository.db.EntitySchema; import java.util.Map; import java.util.function.Function; public class DeleteByIdStatement> extends MultipleVarsYqlStatement.Simple { - DeleteByIdStatement(Class type) { - super(type); - } - - DeleteByIdStatement(Class type, String tableName) { - super(type, tableName); + public DeleteByIdStatement(EntitySchema schema) { + super(schema); } @Override 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 2608b807..2453621a 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 @@ -5,6 +5,7 @@ import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; import tech.ydb.yoj.repository.ydb.yql.YqlOrderBy; +import tech.ydb.yoj.repository.ydb.yql.YqlPredicate; import tech.ydb.yoj.repository.ydb.yql.YqlStatementPart; import java.util.ArrayList; @@ -40,7 +41,7 @@ private FindStatement( @NonNull List> parts, boolean distinct ) { - super(schema, outSchema, parts, YqlStatement::predicateFrom); + super(schema, outSchema, parts, YqlPredicate::from); this.distinct = distinct; this.parts = parts; } diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/MultipleVarsYqlStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/MultipleVarsYqlStatement.java index 2c3e076c..3c8ce045 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/MultipleVarsYqlStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/MultipleVarsYqlStatement.java @@ -104,5 +104,9 @@ public Simple(@NonNull Class type) { public Simple(@NonNull Class type, String tableName) { super(EntitySchema.of(type), EntitySchema.of(type), tableName); } + + public Simple(EntitySchema schema) { + super(schema, schema); + } } } 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 db8e2f3f..8845f521 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 @@ -4,7 +4,6 @@ import com.google.protobuf.NullValue; import lombok.NonNull; import tech.ydb.proto.ValueProtos; -import tech.ydb.yoj.databind.schema.ObjectSchema; import tech.ydb.yoj.databind.schema.Schema; import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntityIdSchema; @@ -14,7 +13,6 @@ import tech.ydb.yoj.repository.db.ViewSchema; import tech.ydb.yoj.repository.db.cache.RepositoryCache; import tech.ydb.yoj.repository.ydb.yql.YqlOrderBy; -import tech.ydb.yoj.repository.ydb.yql.YqlPredicate; import tech.ydb.yoj.repository.ydb.yql.YqlStatementPart; import tech.ydb.yoj.repository.ydb.yql.YqlType; @@ -138,27 +136,6 @@ public String getDeclaration(String name, String type) { return String.format("DECLARE %s AS %s;\n", name, type); } - public static > Statement>, Count> count( - Class entityType, - Collection> parts - ) { - return count(EntitySchema.of(entityType), parts); - } - - private static > Statement>, Count> count( - EntitySchema schema, - Collection> parts - ) { - return new CountAllStatement<>(schema, ObjectSchema.of(Count.class), parts, YqlStatement::predicateFrom); - } - - protected static YqlPredicate predicateFrom(Collection> parts) { - return parts.stream() - .filter(p -> p instanceof YqlPredicate) - .map(YqlPredicate.class::cast) - .reduce(YqlPredicate.alwaysTrue(), (p1, p2) -> p1.and(p2)); - } - protected static Stream> mergeParts(Stream> origParts) { return origParts .collect(groupingBy(YqlStatementPart::getType)) @@ -175,14 +152,6 @@ private static List> combine(List>) first.combine((List) items.subList(1, items.size())); } - public static > Statement deleteAll(Class type) { - return new DeleteAllStatement<>(type); - } - - public static > Statement delete(Class type) { - return new DeleteByIdStatement<>(type); - } - @Override public boolean isPreparable() { return true; 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 c577327a..f098836e 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,9 @@ 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.CountAllStatement; +import tech.ydb.yoj.repository.ydb.statement.DeleteAllStatement; +import tech.ydb.yoj.repository.ydb.statement.DeleteByIdStatement; import tech.ydb.yoj.repository.ydb.statement.FindInStatement; import tech.ydb.yoj.repository.ydb.statement.FindStatement; import tech.ydb.yoj.repository.ydb.statement.FindYqlStatement; @@ -182,7 +185,7 @@ public List findAll(Class viewType) { @Override public void deleteAll() { - executor.pendingExecute(YqlStatement.deleteAll(type), null); + executor.pendingExecute(new DeleteAllStatement<>(schema), null); } @Override @@ -390,7 +393,8 @@ public static > List... parts) { List> partsList = asList(parts); - return executor.execute(YqlStatement.count(type, partsList), partsList).get(0).getCount(); + var statement = new CountAllStatement<>(schema, partsList); + return executor.execute(statement, partsList).get(0).getCount(); } public List find(Class viewType, YqlStatementPart part, YqlStatementPart... otherParts) { @@ -455,7 +459,7 @@ public T save(T t) { @Override public void delete(Entity.Id id) { - executor.pendingExecute(YqlStatement.delete(type), id); + executor.pendingExecute(new DeleteByIdStatement<>(schema), id); executor.getTransactionLocal().firstLevelCache().putEmpty(id); executor.getTransactionLocal().projectionCache().delete(id); } diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/yql/YqlPredicate.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/yql/YqlPredicate.java index 6383d3db..b5ef9751 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/yql/YqlPredicate.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/yql/YqlPredicate.java @@ -76,6 +76,16 @@ public static void setUseLegacyRel(boolean value) { YqlPredicate.useLegacyRel.set(value); } + public static YqlPredicate from(Collection> parts) { + YqlPredicate predicate = YqlPredicate.alwaysTrue(); + for (YqlStatementPart p : parts) { + if (p instanceof YqlPredicate yqlPredicate) { + predicate = predicate.and(yqlPredicate); + } + } + return predicate; + } + public static FieldPredicateBuilder where(@NonNull String fieldPath) { return new FieldPredicateBuilder(fieldPath, UnaryOperator.identity()); } 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 f6fa46e0..aba17246 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 @@ -9,6 +9,8 @@ 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.DeleteAllStatement; +import tech.ydb.yoj.repository.ydb.statement.DeleteByIdStatement; import tech.ydb.yoj.repository.ydb.statement.FindYqlStatement; import tech.ydb.yoj.repository.ydb.statement.Statement; import tech.ydb.yoj.repository.ydb.statement.YqlStatement; @@ -141,7 +143,7 @@ private QueriesMerger createMerger() { } private > YdbRepository.Query deleteAll(Class clazz) { - return new YdbRepository.Query<>(YqlStatement.deleteAll(clazz), null); + return new YdbRepository.Query<>(new DeleteAllStatement<>(EntitySchema.of(clazz)), null); } @SuppressWarnings("unchecked") @@ -163,7 +165,7 @@ private > YdbRepository.Query find(T p) { @SuppressWarnings("unchecked") private > YdbRepository.Query delete(T p) { - return new YdbRepository.Query<>(YqlStatement.delete((Class) p.getClass()), p.getId()); + return new YdbRepository.Query<>(new DeleteByIdStatement<>(EntitySchema.of((Class) p.getClass())), p.getId()); } private ArrayList getProjects() { diff --git a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/DeleteByIdStatementTest.java b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/DeleteByIdStatementTest.java index d9390c32..701d7a33 100644 --- a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/DeleteByIdStatementTest.java +++ b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/DeleteByIdStatementTest.java @@ -1,6 +1,7 @@ package tech.ydb.yoj.repository.ydb.statement; import org.junit.Test; +import tech.ydb.yoj.repository.db.EntitySchema; import static org.assertj.core.api.Assertions.assertThat; @@ -24,7 +25,7 @@ public void testDelete() { txManager.tx(() -> { var db = getTestDb(); - var deleteStatement = new DeleteByIdStatement<>(TestEntity.class); + var deleteStatement = new DeleteByIdStatement<>(EntitySchema.of(TestEntity.class)); db.pendingExecute(deleteStatement, ENTITY_1.getId()); db.pendingExecute(deleteStatement, ENTITY_3.getId());