Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public <T extends Entity<T>, ID extends Entity.Id<T>> void markRangeRead(TableDe
}

public <T extends Entity<T>, ID extends Entity.Id<T>> void markRangeRead(TableDescriptor<T> tableDescriptor, EntitySchema<T> schema, Map<String, Object> map) {
Range<ID> range = Range.create(schema.getIdSchema(), map);
Range<ID> range = schema.<ID>getIdSchema().newRangeInstance(map);
markRangeRead(tableDescriptor, range);
}

public <T extends Entity<T>, ID extends Entity.Id<T>> void markTableRead(TableDescriptor<T> tableDescriptor, EntitySchema<T> schema) {
Range<ID> range = Range.create(schema.getIdSchema(), Map.of());
Range<ID> range = schema.<ID>getIdSchema().newRangeInstance(Map.of());
markRangeRead(tableDescriptor, range);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package tech.ydb.yoj.repository.ydb.statement;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import tech.ydb.proto.ValueProtos;
import tech.ydb.yoj.databind.schema.Schema;
import tech.ydb.yoj.databind.schema.Schema.JavaField;
import tech.ydb.yoj.repository.db.Entity;
import tech.ydb.yoj.repository.db.EntitySchema;
import tech.ydb.yoj.repository.db.Range;
Expand Down Expand Up @@ -36,10 +37,10 @@ public FindRangeStatement(
.collect(toList());
}

private Stream<YqlStatementRangeParam> toParams(Set<String> names, FindRangeStatement.RangeBound rangeBound) {
private Stream<YqlStatementRangeParam> toParams(Set<JavaField> fields, FindRangeStatement.RangeBound rangeBound) {
return schema.flattenId().stream()
.filter(f -> names.contains(f.getName()))
.map(c -> new FindRangeStatement.YqlStatementRangeParam(YqlType.of(c), c.getName(), rangeBound));
.filter(fields::contains)
.map(c -> new FindRangeStatement.YqlStatementRangeParam(YqlType.of(c), c, rangeBound));
}

@Override
Expand All @@ -48,7 +49,7 @@ public Map<String, ValueProtos.TypedValue> toQueryParameters(Range<ID> parameter
.map(YqlStatementRangeParam.class::cast)
.collect(toMap(
YqlStatementParam::getVar,
p -> createTQueryParameter(p.getType(), p.rangeBound.map(parameters).get(p.rangeName), p.isOptional()))
p -> createTQueryParameter(p.getType(), p.rangeBound.map(parameters).get(p.field), p.isOptional()))
);
}

Expand All @@ -74,31 +75,32 @@ public String getQuery(String tablespace) {
private String predicationVars() {
return getParams().stream()
.map(YqlStatementRangeParam.class::cast)
.map(p -> "(" + escape(p.rangeName) + p.rangeBound.op + p.getVar() + ")")
.map(p -> "(" + escape(p.field.getName()) + p.rangeBound.op + p.getVar() + ")")
.collect(joining(" AND "));
}

@AllArgsConstructor
enum RangeBound {
@RequiredArgsConstructor
private enum RangeBound {
EQ("=", Range::getEqMap),
MAX("<=", Range::getMaxMap),
MIN(">=", Range::getMinMap);
String op;
Function<Range, Map<String, Object>> mapper;
MIN(">=", Range::getMinMap),
MAX("<=", Range::getMaxMap);

public Map<String, Object> map(Range range) {
private final String op;
private final Function<Range<?>, Map<JavaField, Object>> mapper;

public Map<JavaField, Object> map(Range<?> range) {
return mapper.apply(range);
}
}

static class YqlStatementRangeParam extends YqlStatementParam {
private static final class YqlStatementRangeParam extends YqlStatementParam {
private final RangeBound rangeBound;
private final String rangeName;
private final JavaField field;

YqlStatementRangeParam(YqlType type, String name, RangeBound rangeBound) {
super(type, rangeBound.name() + "_" + name, true);
private YqlStatementRangeParam(YqlType type, JavaField field, RangeBound rangeBound) {
super(type, rangeBound.name() + "_" + field.getName(), true);
this.rangeBound = rangeBound;
this.rangeName = name;
this.field = field;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.ydb.yoj.repository.ydb.table;

import tech.ydb.yoj.databind.schema.Schema;
import tech.ydb.yoj.databind.schema.Schema.JavaField;
import tech.ydb.yoj.repository.db.Entity;
import tech.ydb.yoj.repository.db.EntityIdSchema;
import tech.ydb.yoj.repository.db.Range;
Expand Down Expand Up @@ -43,11 +44,11 @@ abstract class BatchFindSpliterator<R, T extends Entity<T>, ID extends Entity.Id
this.top = YqlLimit.top(batchSize);
if (partial != null) {
Range<ID> range = Range.create(this.idSchema, partial);
Map<String, Object> eqMap = range.getEqMap();
Map<JavaField, Object> eqMap = range.getEqMap();
this.initialPartialPredicates = this.idSchema
.flattenFields().stream()
.filter(f -> eqMap.containsKey(f.getName()))
.map(f -> YqlPredicate.eq(f.getPath(), eqMap.get(f.getName())))
.filter(eqMap::containsKey)
.map(f -> YqlPredicate.eq(f.getPath(), eqMap.get(f)))
.collect(toList());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ public static <T extends Entity<T>> Comparator<Entity.Id<T>> getIdComparator(Cla
STRING, INTEGER, ENUM, BOOLEAN, TIMESTAMP, UUID, BYTE_ARRAY
);

private final EntitySchema<?> entitySchema;

private <E extends Entity<E>> EntityIdSchema(EntitySchema<E> entitySchema) {
super(entitySchema, ID_FIELD_NAME);
this.entitySchema = entitySchema;

var flattenedFields = flattenFields();

Expand Down Expand Up @@ -160,6 +163,50 @@ public static boolean isIdFieldName(@NonNull String name) {
return name.equals(ID_FIELD_NAME) || name.startsWith(ID_SUBFIELD_NAME_PREFIX);
}

public EntitySchema<?> getEntitySchema() {
return entitySchema;
}

public Class<? extends Entity<?>> getEntityType() {
return entitySchema.getType();
}

/**
* Creates a new {@link Range} representing all the IDs that have the ID prefix specified in {@code cells}.
* If {@code cells} do not contain any ID fields at all, a whole-table {@code Range} will be returned.
*
* @param cells ID prefix value map: <code>{@link JavaField#getName() ID field name} -> ID field value</code>
* @return {@code Range} for the specified ID prefix
* @throws IllegalArgumentException {@code cells} contain ID values but these do not represent an ID prefix.
* <br><em>E.g.</em>, for a four-column ID {@code (id_a, id_b, id_c, id_d)}
* the cells contain {@code (id_a, id_c)} but {@code id_b} is missing.
* @see Range#create(EntityIdSchema, Entity.Id)
* @see #newRangeInstance(Map, Map)
* @see #newInstance(Map)
*/
public Range<ID> newRangeInstance(Map<String, Object> cells) {
return Range.internalCreate(this, cells);
}

/**
* Creates a new {@link Range} representing all the IDs between the two specified {@code cells}.
* If {@code cells} do not contain any ID fields at all, a whole-table {@code Range} will be returned.
*
* @param minCellsInclusive min (inclusive) ID value map: <code>{@link JavaField#getName() ID field name} ->
* ID field value</code>
* @param maxCellsInclusive max (inclusive) ID value map: <code>{@link JavaField#getName() ID field name} ->
* ID field value</code>
* @return {@code Range} between the specified minimum and maximum ID values, inclusive
* @throws IllegalArgumentException ID represented by {@code minCellsInclusive} is greater than the ID
* represented by {@code maxCellsInclusive}
* @see Range#create(EntityIdSchema, Entity.Id, Entity.Id)
* @see #newRangeInstance(Map)
* @see #newInstance(Map)
*/
public Range<ID> newRangeInstance(Map<String, Object> minCellsInclusive, Map<String, Object> maxCellsInclusive) {
return Range.internalCreate(this, minCellsInclusive, maxCellsInclusive);
}

@Override
public int compare(@NonNull ID a, @NonNull ID b) {
Map<String, Object> idA = flatten(a);
Expand Down
Loading
Loading