Skip to content

Commit

Permalink
Remove static count(), delete() and deleteAll() methods from YqlState…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
Alexander Lavrukov committed Dec 4, 2024
1 parent b11a0fa commit 05b48e8
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ENTITY extends Entity<ENTITY>> extends PredicateStatement<Collection<? extends YqlStatementPart<?>>, ENTITY, Count> {
private final Collection<? extends YqlStatementPart<?>> parts;
public class CountAllStatement<ENTITY extends Entity<ENTITY>> extends PredicateStatement<Collection<? extends YqlStatementPart<?>>, ENTITY, Count> {
private final List<YqlStatementPart<?>> parts;

public CountAllStatement(
EntitySchema<ENTITY> schema,
Schema<Count> resultSchema,
Collection<? extends YqlStatementPart<?>> parts,
Function<Collection<? extends YqlStatementPart<?>>, YqlPredicate> predicateFrom
) {
super(schema, resultSchema, parts, predicateFrom);
this.parts = parts;
}

public CountAllStatement(
EntitySchema<ENTITY> schema,
Schema<Count> resultSchema,
Collection<? extends YqlStatementPart<?>> parts,
Function<Collection<? extends YqlStatementPart<?>>, YqlPredicate> predicateFrom,
String tableName
) {
super(schema, resultSchema, parts, predicateFrom, tableName);
public CountAllStatement(EntitySchema<ENTITY> schema, List<YqlStatementPart<?>> parts) {
super(schema, ObjectSchema.of(Count.class), parts, YqlPredicate::from);
this.parts = parts;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<PARAMS, ENTITY extends Entity<ENTITY>> extends YqlStatement<PARAMS, ENTITY, ENTITY> {
public DeleteAllStatement(@NonNull Class<ENTITY> type) {
super(EntitySchema.of(type), EntitySchema.of(type));
}

public DeleteAllStatement(@NonNull EntitySchema<ENTITY> schema, String tableName) {
super(schema, schema, tableName);
public class DeleteAllStatement<PARAMS, ENTITY extends Entity<ENTITY>> extends YqlStatement<PARAMS, ENTITY, ENTITY> {
public DeleteAllStatement(EntitySchema<ENTITY> schema) {
super(schema, schema);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IN, T extends Entity<T>> extends MultipleVarsYqlStatement.Simple<IN, T> {
DeleteByIdStatement(Class<T> type) {
super(type);
}

DeleteByIdStatement(Class<T> type, String tableName) {
super(type, tableName);
public DeleteByIdStatement(EntitySchema<T> schema) {
super(schema);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,7 +41,7 @@ private FindStatement(
@NonNull List<YqlStatementPart<?>> parts,
boolean distinct
) {
super(schema, outSchema, parts, YqlStatement::predicateFrom);
super(schema, outSchema, parts, YqlPredicate::from);
this.distinct = distinct;
this.parts = parts;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,9 @@ public Simple(@NonNull Class<ENTITY> type) {
public Simple(@NonNull Class<ENTITY> type, String tableName) {
super(EntitySchema.of(type), EntitySchema.of(type), tableName);
}

public Simple(EntitySchema<ENTITY> schema) {
super(schema, schema);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -138,27 +136,6 @@ public String getDeclaration(String name, String type) {
return String.format("DECLARE %s AS %s;\n", name, type);
}

public static <ENTITY extends Entity<ENTITY>> Statement<Collection<? extends YqlStatementPart<?>>, Count> count(
Class<ENTITY> entityType,
Collection<? extends YqlStatementPart<?>> parts
) {
return count(EntitySchema.of(entityType), parts);
}

private static <ENTITY extends Entity<ENTITY>> Statement<Collection<? extends YqlStatementPart<?>>, Count> count(
EntitySchema<ENTITY> schema,
Collection<? extends YqlStatementPart<?>> parts
) {
return new CountAllStatement<>(schema, ObjectSchema.of(Count.class), parts, YqlStatement::predicateFrom);
}

protected static YqlPredicate predicateFrom(Collection<? extends YqlStatementPart<?>> parts) {
return parts.stream()
.filter(p -> p instanceof YqlPredicate)
.map(YqlPredicate.class::cast)
.reduce(YqlPredicate.alwaysTrue(), (p1, p2) -> p1.and(p2));
}

protected static Stream<? extends YqlStatementPart<?>> mergeParts(Stream<? extends YqlStatementPart<?>> origParts) {
return origParts
.collect(groupingBy(YqlStatementPart::getType))
Expand All @@ -175,14 +152,6 @@ private static List<? extends YqlStatementPart<?>> combine(List<? extends YqlSta
return (List<? extends YqlStatementPart<?>>) first.combine((List) items.subList(1, items.size()));
}

public static <PARAMS, ENTITY extends Entity<ENTITY>> Statement<PARAMS, ENTITY> deleteAll(Class<ENTITY> type) {
return new DeleteAllStatement<>(type);
}

public static <PARAMS, ENTITY extends Entity<ENTITY>> Statement<PARAMS, ENTITY> delete(Class<ENTITY> type) {
return new DeleteByIdStatement<>(type);
}

@Override
public boolean isPreparable() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -182,7 +185,7 @@ public <V extends View> List<V> findAll(Class<V> viewType) {

@Override
public void deleteAll() {
executor.pendingExecute(YqlStatement.deleteAll(type), null);
executor.pendingExecute(new DeleteAllStatement<>(schema), null);
}

@Override
Expand Down Expand Up @@ -390,7 +393,8 @@ public static <T extends Entity<T>> List<YqlStatementPart<? extends YqlStatement

public long count(YqlStatementPart<?>... parts) {
List<YqlStatementPart<?>> 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 <V extends View> List<V> find(Class<V> viewType, YqlStatementPart<?> part, YqlStatementPart<?>... otherParts) {
Expand Down Expand Up @@ -455,7 +459,7 @@ public T save(T t) {

@Override
public void delete(Entity.Id<T> id) {
executor.pendingExecute(YqlStatement.delete(type), id);
executor.pendingExecute(new DeleteByIdStatement<>(schema), id);
executor.getTransactionLocal().firstLevelCache().putEmpty(id);
executor.getTransactionLocal().projectionCache().delete(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ public static void setUseLegacyRel(boolean value) {
YqlPredicate.useLegacyRel.set(value);
}

public static YqlPredicate from(Collection<? extends YqlStatementPart<?>> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -141,7 +143,7 @@ private QueriesMerger createMerger() {
}

private <T extends Entity<T>> YdbRepository.Query<?> deleteAll(Class<T> clazz) {
return new YdbRepository.Query<>(YqlStatement.deleteAll(clazz), null);
return new YdbRepository.Query<>(new DeleteAllStatement<>(EntitySchema.of(clazz)), null);
}

@SuppressWarnings("unchecked")
Expand All @@ -163,7 +165,7 @@ private <T extends Entity<T>> YdbRepository.Query<?> find(T p) {

@SuppressWarnings("unchecked")
private <T extends Entity<T>> YdbRepository.Query<?> delete(T p) {
return new YdbRepository.Query<>(YqlStatement.delete((Class<T>) p.getClass()), p.getId());
return new YdbRepository.Query<>(new DeleteByIdStatement<>(EntitySchema.of((Class<T>) p.getClass())), p.getId());
}

private ArrayList<Project> getProjects() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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());
Expand Down

0 comments on commit 05b48e8

Please sign in to comment.