Skip to content

Commit

Permalink
Remove static findIn() method from YqlStatement
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Lavrukov committed Dec 4, 2024
1 parent 0bee8ca commit b21b4bd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,75 +127,54 @@ public final class FindInStatement<IN, T extends Entity<T>, RESULT> extends Mult
* @param orderBy order by expression for result sorting, order fields must be present
* in the {@code resultSchema}
*/
protected FindInStatement(
public static <ID extends Entity.Id<T>, T extends Entity<T>, RESULT> FindInStatement<Set<ID>, T, RESULT> from(
EntitySchema<T> schema,
Schema<RESULT> resultSchema,
Iterable<? extends Entity.Id<T>> ids,
Iterable<ID> ids,
@Nullable FilterExpression<T> filter,
@Nullable OrderExpression<T> orderBy,
@Nullable Integer limit
) {
this(schema, resultSchema, ids, filter, orderBy, limit, schema.getName());
}

protected FindInStatement(
EntitySchema<T> schema,
Schema<RESULT> resultSchema,
Iterable<? extends Entity.Id<T>> ids,
@Nullable FilterExpression<T> filter,
@Nullable OrderExpression<T> orderBy,
@Nullable Integer limit,
String tableName
) {
super(schema, resultSchema, tableName);

this.orderBy = orderBy;
this.limit = limit;
var keySchema = schema.getIdSchema();
var keyFields = collectKeyFieldsFromIds(schema.getIdSchema(), ids);

indexName = null;
keySchema = schema.getIdSchema();
keyFields = collectKeyFieldsFromIds(schema.getIdSchema(), ids);
predicate = (filter == null) ? null : new PredicateClause<>(schema, YqlListingQuery.toYqlPredicate(filter));

validateOrderByFields();
return new FindInStatement<>(schema, resultSchema, keySchema, keyFields, null, filter, orderBy, limit);
}

/**
* Creates new {@code FindInStatement} instance with index usage and pagination.
*/
protected <V> FindInStatement(
public static <K, T extends Entity<T>, RESULT> FindInStatement<Set<K>, T, RESULT> from(
EntitySchema<T> schema,
Schema<RESULT> resultSchema,
String indexName,
Iterable<V> keys,
Iterable<K> keys,
@Nullable FilterExpression<T> filter,
@Nullable OrderExpression<T> orderBy,
@Nullable Integer limit
) {
this(schema, resultSchema, indexName, keys, filter, orderBy, limit, schema.getName());
Schema<K> keySchema = getKeySchemaFromValues(keys);
Set<String> keyFields = collectKeyFieldsFromKeys(schema, indexName, keySchema, keys);

return new FindInStatement<>(schema, resultSchema, keySchema, keyFields, indexName, filter, orderBy, limit);
}

protected <V> FindInStatement(
private <PARAMS> FindInStatement(
EntitySchema<T> schema,
Schema<RESULT> resultSchema,
String indexName,
Iterable<V> keys,
Schema<PARAMS> keySchema,
Set<String> keyFields,
@Nullable String indexName,
@Nullable FilterExpression<T> filter,
@Nullable OrderExpression<T> orderBy,
@Nullable Integer limit,
String tableName
@Nullable Integer limit

) {
super(schema, resultSchema, tableName);
super(schema, resultSchema);

this.indexName = indexName;
this.orderBy = orderBy;
this.limit = limit;

Schema<V> schemaFromValues = getKeySchemaFromValues(keys);

keySchema = schemaFromValues;
keyFields = collectKeyFieldsFromKeys(schema, indexName, schemaFromValues, keys);
predicate = (filter == null) ? null : new PredicateClause<>(schema, YqlListingQuery.toYqlPredicate(filter));
this.keySchema = keySchema;
this.keyFields = keyFields;
this.predicate = (filter == null) ? null : new PredicateClause<>(schema, YqlListingQuery.toYqlPredicate(filter));

validateOrderByFields();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.google.protobuf.NullValue;
import lombok.NonNull;
import tech.ydb.proto.ValueProtos;
import tech.ydb.yoj.databind.expression.FilterExpression;
import tech.ydb.yoj.databind.expression.OrderExpression;
import tech.ydb.yoj.databind.schema.ObjectSchema;
import tech.ydb.yoj.databind.schema.Schema;
import tech.ydb.yoj.repository.db.Entity;
Expand Down Expand Up @@ -92,60 +90,6 @@ public static <ENTITY extends Entity<ENTITY>, VIEW extends View, ID extends Enti
return new FindRangeStatement<>(EntitySchema.of(type), ViewSchema.of(viewType), range);
}

public static <PARAMS, ENTITY extends Entity<ENTITY>, ID extends Entity.Id<ENTITY>> Statement<PARAMS, ID> findIdsIn(
Class<ENTITY> type,
Iterable<ID> ids,
FilterExpression<ENTITY> filter,
OrderExpression<ENTITY> orderBy,
Integer limit
) {
return new FindInStatement<>(EntitySchema.of(type), EntityIdSchema.ofEntity(type), ids, filter, orderBy, limit);
}

public static <PARAMS, ENTITY extends Entity<ENTITY>> Statement<PARAMS, ENTITY> findIn(
Class<ENTITY> type,
Iterable<? extends Entity.Id<ENTITY>> ids,
FilterExpression<ENTITY> filter,
OrderExpression<ENTITY> orderBy,
Integer limit
) {
return new FindInStatement<>(EntitySchema.of(type), EntitySchema.of(type), ids, filter, orderBy, limit);
}

public static <PARAMS, ENTITY extends Entity<ENTITY>, VIEW extends View> Statement<PARAMS, VIEW> findIn(
Class<ENTITY> type,
Class<VIEW> viewType,
Iterable<? extends Entity.Id<ENTITY>> ids,
FilterExpression<ENTITY> filter,
OrderExpression<ENTITY> orderBy,
Integer limit
) {
return new FindInStatement<>(EntitySchema.of(type), ViewSchema.of(viewType), ids, filter, orderBy, limit);
}

public static <PARAMS, ENTITY extends Entity<ENTITY>, K> Statement<PARAMS, ENTITY> findIn(
Class<ENTITY> type,
String indexName,
Iterable<K> keys,
FilterExpression<ENTITY> filter,
OrderExpression<ENTITY> orderBy,
Integer limit
) {
return new FindInStatement<>(EntitySchema.of(type), EntitySchema.of(type), indexName, keys, filter, orderBy, limit);
}

public static <PARAMS, ENTITY extends Entity<ENTITY>, VIEW extends View, K> Statement<PARAMS, VIEW> findIn(
Class<ENTITY> type,
Class<VIEW> viewType,
String indexName,
Iterable<K> keys,
FilterExpression<ENTITY> filter,
OrderExpression<ENTITY> orderBy,
Integer limit
) {
return new FindInStatement<>(EntitySchema.of(type), ViewSchema.of(viewType), indexName, keys, filter, orderBy, limit);
}

public static <PARAMS, ENTITY extends Entity<ENTITY>> Statement<PARAMS, ENTITY> findAll(
Class<ENTITY> type
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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.FindInStatement;
import tech.ydb.yoj.repository.ydb.statement.FindStatement;
import tech.ydb.yoj.repository.ydb.statement.FindYqlStatement;
import tech.ydb.yoj.repository.ydb.statement.Statement;
Expand Down Expand Up @@ -328,31 +329,37 @@ public <ID extends Entity.Id<T>> List<T> findUncached(Set<ID> ids, @Nullable Fil
if (ids.isEmpty()) {
return List.of();
}
return executor.execute(YqlStatement.findIn(type, ids, filter, orderBy, limit), ids);
var statement = FindInStatement.from(schema, schema, ids, filter, orderBy, limit);
return executor.execute(statement, ids);
}

@Override
public <V extends View, ID extends Id<T>> List<V> find(Class<V> viewType, Set<ID> ids, @Nullable FilterExpression<T> filter, @Nullable OrderExpression<T> orderBy, @Nullable Integer limit) {
if (ids.isEmpty()) {
return List.of();
}
return executor.execute(YqlStatement.findIn(type, viewType, ids, filter, orderBy, limit), ids);
ViewSchema<V> viewSchema = ViewSchema.of(viewType);
var statement = FindInStatement.from(schema, viewSchema, ids, filter, orderBy, limit);
return executor.execute(statement, ids);
}

@Override
public <K> List<T> find(String indexName, Set<K> keys, @Nullable FilterExpression<T> filter, @Nullable OrderExpression<T> orderBy, @Nullable Integer limit) {
if (keys.isEmpty()) {
return List.of();
}
return postLoad(executor.execute(YqlStatement.findIn(type, indexName, keys, filter, orderBy, limit), keys));
var statement = FindInStatement.from(schema, schema, indexName, keys, filter, orderBy, limit);
return postLoad(executor.execute(statement, keys));
}

@Override
public <V extends View, K> List<V> find(Class<V> viewType, String indexName, Set<K> keys, @Nullable FilterExpression<T> filter, @Nullable OrderExpression<T> orderBy, @Nullable Integer limit) {
if (keys.isEmpty()) {
return List.of();
}
return executor.execute(YqlStatement.findIn(type, viewType, indexName, keys, filter, orderBy, limit), keys);
ViewSchema<V> viewSchema = ViewSchema.of(viewType);
var statement = FindInStatement.from(schema, viewSchema, indexName, keys, filter, orderBy, limit);
return executor.execute(statement, keys);
}

public static <T extends Entity<T>> List<YqlStatementPart<? extends YqlStatementPart<?>>> buildStatementParts(
Expand Down Expand Up @@ -416,7 +423,10 @@ public <ID extends Id<T>> List<ID> findIds(Set<ID> partialIds) {
if (partialIds.isEmpty()) {
return List.of();
}
return executor.execute(YqlStatement.findIdsIn(type, partialIds, null, defaultOrder(type), null), partialIds);
OrderExpression<T> order = defaultOrder(type);
EntityIdSchema<ID> idSchema = EntityIdSchema.ofEntity(type);
var statement = FindInStatement.from(schema, idSchema, partialIds, null, order, null);
return executor.execute(statement, partialIds);
}

@Override
Expand Down
Loading

0 comments on commit b21b4bd

Please sign in to comment.