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 (#104)

Co-authored-by: Alexander Lavrukov <[email protected]>
  • Loading branch information
2 people authored and nvamelichev committed Dec 6, 2024
1 parent 5e0f06d commit 0a72816
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
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.UpsertYqlStatement;
import tech.ydb.yoj.repository.ydb.statement.YqlStatement;

import java.util.ArrayList;
Expand Down Expand Up @@ -158,12 +161,12 @@ private MergingState doTransition(MergingState state, Statement.QueryType nextQu

@SuppressWarnings("unchecked")
private static YdbRepository.Query convertInsertToUpsert(YdbRepository.Query<?> query) {
return new YdbRepository.Query<>(YqlStatement.save(getEntityClass(query)), query.getValues().get(0));
return new YdbRepository.Query<>(new UpsertYqlStatement<>(EntitySchema.of(getEntityClass(query))), query.getValues().get(0));
}

@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
@@ -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 InsertYqlStatement<PARAMS, ENTITY extends Entity<ENTITY>> extends MultipleVarsYqlStatement.Simple<PARAMS, ENTITY> {
public InsertYqlStatement(Class<ENTITY> type) {
super(type);
}

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

@Override
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 @@ -24,12 +24,8 @@ public final class UpdateByIdStatement<ENTITY extends Entity<ENTITY>, ID extends

private final Set<YqlStatementParam> idParams;

public UpdateByIdStatement(Class<ENTITY> type, UpdateModel.ById<ID> model) {
this(type, model, EntitySchema.of(type).getName());
}

public UpdateByIdStatement(Class<ENTITY> type, UpdateModel.ById<ID> model, String tableName) {
super(EntitySchema.of(type), EntitySchema.of(type), tableName);
public UpdateByIdStatement(EntitySchema<ENTITY> schema, UpdateModel.ById<ID> model) {
super(schema, schema);
this.idParams = schema.flattenId().stream()
.map(c -> YqlStatementParam.required(YqlType.of(c), c.getName()))
.collect(toUnmodifiableSet());
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 UpsertYqlStatement<IN, T extends Entity<T>> extends MultipleVarsYqlStatement.Simple<IN, T> {
public UpsertYqlStatement(Class<T> type) {
super(type);
}

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

@Override
Expand Down
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 @@ -55,25 +53,6 @@ public YqlStatement(@NonNull EntitySchema<ENTITY> schema, @NonNull Schema<RESULT
this.tableName = tableName;
}

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

public static <ENTITY extends Entity<ENTITY>, ID extends Entity.Id<ENTITY>> Statement<UpdateModel.ById<ID>, ?> update(
Class<ENTITY> type,
UpdateModel.ById<ID> model
) {
return new UpdateByIdStatement<>(type, model);
}

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

public static <ENTITY extends Entity<ENTITY>, ID extends Entity.Id<ENTITY>> Statement<Range<ID>, ENTITY> findRange(
Class<ENTITY> type,
Range<ID> range
Expand Down Expand Up @@ -138,27 +117,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 +133,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,12 +24,18 @@
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;
import tech.ydb.yoj.repository.ydb.statement.InsertYqlStatement;
import tech.ydb.yoj.repository.ydb.statement.Statement;
import tech.ydb.yoj.repository.ydb.statement.UpdateByIdStatement;
import tech.ydb.yoj.repository.ydb.statement.UpdateInStatement;
import tech.ydb.yoj.repository.ydb.statement.UpdateModel;
import tech.ydb.yoj.repository.ydb.statement.UpsertYqlStatement;
import tech.ydb.yoj.repository.ydb.statement.YqlStatement;
import tech.ydb.yoj.repository.ydb.yql.YqlLimit;
import tech.ydb.yoj.repository.ydb.yql.YqlListingQuery;
Expand Down Expand Up @@ -182,7 +188,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 +396,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 @@ -432,13 +439,13 @@ public <ID extends Id<T>> List<ID> findIds(Set<ID> partialIds) {
@Override
public void update(Entity.Id<T> id, Changeset changeset) {
UpdateModel.ById<Id<T>> model = new UpdateModel.ById<>(id, changeset.toMap());
executor.pendingExecute(YqlStatement.update(type, model), model);
executor.pendingExecute(new UpdateByIdStatement<>(schema, model), model);
}

@Override
public T insert(T t) {
T entityToSave = t.preSave();
executor.pendingExecute(YqlStatement.insert(type), entityToSave);
executor.pendingExecute(new InsertYqlStatement<>(schema), entityToSave);
executor.getTransactionLocal().firstLevelCache().put(entityToSave);
executor.getTransactionLocal().projectionCache().save(entityToSave);
return t;
Expand All @@ -447,15 +454,15 @@ public T insert(T t) {
@Override
public T save(T t) {
T entityToSave = t.preSave();
executor.pendingExecute(YqlStatement.save(type), entityToSave);
executor.pendingExecute(new UpsertYqlStatement<>(schema), entityToSave);
executor.getTransactionLocal().firstLevelCache().put(entityToSave);
executor.getTransactionLocal().projectionCache().save(entityToSave);
return 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 All @@ -477,7 +484,7 @@ public <ID extends Id<T>> void migrate(ID id) {
}
T rawEntity = foundRaw.get(0);
T entityToSave = rawEntity.postLoad().preSave();
executor.pendingExecute(YqlStatement.save(type), entityToSave);
executor.pendingExecute(new UpsertYqlStatement<>(schema), entityToSave);
executor.getTransactionLocal().projectionCache().save(entityToSave);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public static void setUseLegacyRel(boolean value) {
YqlPredicate.useLegacyRel.set(value);
}

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

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 @@ -24,7 +24,7 @@
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;
import tech.ydb.yoj.repository.ydb.statement.UpsertYqlStatement;

import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -324,7 +324,7 @@ private List<Complex> createComplexesList() {
private CompletableFuture<Result<DataQueryResult>> convertEntity(List<Complex> complexes) {
ValueProtos.ResultSet.Builder builder = ValueProtos.ResultSet.newBuilder();
complexes.stream()
.map(complex -> YqlStatement.save(Complex.class).toQueryParameters(complex))
.map(complex -> new UpsertYqlStatement<>(EntitySchema.of(Complex.class)).toQueryParameters(complex))
.map(map -> YdbConverter.convertToParams(map).values().get(MultipleVarsYqlStatement.listName))
.peek(value -> {
if (builder.getColumnsCount() == 0) {
Expand Down
Loading

0 comments on commit 0a72816

Please sign in to comment.