Skip to content

Commit

Permalink
Query multiple tables using one entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergei Galiamichev committed Dec 18, 2024
1 parent 8088322 commit 6c30f74
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 10 deletions.
3 changes: 2 additions & 1 deletion databind/src/main/java/tech/ydb/yoj/ExperimentalApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
Expand All @@ -14,7 +15,7 @@
* Annotates <em>experimental features</em>. These features are not part of the stable YOJ API: they can change incompatibly,
* or even disappear entirely <em>in any release</em>.
*/
@Target({TYPE, FIELD, METHOD, PARAMETER, ANNOTATION_TYPE})
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, ANNOTATION_TYPE})
@Retention(SOURCE)
public @interface ExperimentalApi {
/**
Expand Down
1 change: 1 addition & 0 deletions repository-ydb-v2/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ java_test_suite(
"@java_contribs_stable//:tech_ydb_test_ydb_tests_common",
"@java_contribs_stable//:tech_ydb_ydb_auth_api",
"@java_contribs_stable//:tech_ydb_ydb_proto_api",
"@java_contribs_stable//:tech_ydb_ydb_sdk_common",
"@java_contribs_stable//:tech_ydb_ydb_sdk_core",
"@java_contribs_stable//:tech_ydb_ydb_sdk_scheme",
"@java_contribs_stable//:tech_ydb_ydb_sdk_table",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ public <T extends Entity<T>> Table<T> table(Class<T> c) {
return new YdbTable<>(c, this);
}

/**
* WARNING!!! This method won't work if id's in different tables have intersections. The restriction will be eliminated in future
* versions
*/
@Override
@ExperimentalApi(issue = "https://github.com/ydb-platform/yoj-project/issues/32")
public <T extends Entity<T>> Table<T> table(@NonNull Class<T> clazz, @NonNull String tableName) {
return new YdbTable<>(clazz, tableName, this);
}

@Override
public void commit() {
if (isBadSession) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import tech.ydb.yoj.databind.DbType;
import tech.ydb.yoj.repository.db.Entity;
import tech.ydb.yoj.repository.db.EntitySchema;
import tech.ydb.yoj.repository.db.TableDescriptor;
import tech.ydb.yoj.repository.ydb.YdbConfig;
import tech.ydb.yoj.repository.ydb.YdbRepository;
import tech.ydb.yoj.repository.ydb.client.YdbPaths;
Expand All @@ -42,7 +43,7 @@
public final class YdbSchemaCompatibilityChecker {
private static final Logger log = LoggerFactory.getLogger(YdbSchemaCompatibilityChecker.class);

private final List<Class<? extends Entity>> entities;
private final List<TableDescriptor<? extends Entity>> entities;
private final Config config;
private final YdbRepository repository;
private final YdbConfig repositoryConfig;
Expand All @@ -51,12 +52,23 @@ public final class YdbSchemaCompatibilityChecker {
private final List<String> canExecuteMessages = new ArrayList<>();
private final List<String> incompatibleMessages = new ArrayList<>();

public YdbSchemaCompatibilityChecker(Map<String, Class<? extends Entity>> entities, YdbRepository repository) {
this(entities, repository, Config.DEFAULT);
}

public YdbSchemaCompatibilityChecker(List<Class<? extends Entity>> entities, YdbRepository repository) {
this(entities, repository, Config.DEFAULT);
}

public YdbSchemaCompatibilityChecker(List<Class<? extends Entity>> entities, YdbRepository repository, Config config) {
this.entities = entities;
this.entities = toDescriptors(entities);
this.config = config;
this.repository = repository;
this.repositoryConfig = this.repository.getConfig();
}

public YdbSchemaCompatibilityChecker(Map<String, Class<? extends Entity>> entities, YdbRepository repository, Config config) {
this.entities = toDescriptors(entities);
this.config = config;
this.repository = repository;
this.repositoryConfig = this.repository.getConfig();
Expand Down Expand Up @@ -153,10 +165,10 @@ private Map<String, YdbSchemaOperations.Table> generateSchemeFromCode() {
}

@SuppressWarnings("unchecked")
private YdbSchemaOperations.Table tableForEntity(Class<? extends Entity> c) {
EntitySchema<?> schema = EntitySchema.of(c);
private YdbSchemaOperations.Table tableForEntity(TableDescriptor<? extends Entity<?>> c) {
EntitySchema<?> schema = EntitySchema.of(c.entityType());
return repository.getSchemaOperations()
.describeTable(schema.getName(), schema.flattenFields(), schema.flattenId(),
.describeTable(c.tableName(), schema.flattenFields(), schema.flattenId(),
schema.getGlobalIndexes(), schema.getTtlModifier());
}

Expand Down Expand Up @@ -466,6 +478,18 @@ private boolean containsPrefix(String globalName, Set<String> prefixes) {
.anyMatch(realName::startsWith);
}

private static List<TableDescriptor<? extends Entity>> toDescriptors(List<Class<? extends Entity>> entities) {
List<TableDescriptor<? extends Entity>> descriptors = new ArrayList<>();
entities.forEach(e -> descriptors.add(TableDescriptor.from(EntitySchema.of(e))));
return descriptors;
}

private static List<TableDescriptor<? extends Entity>> toDescriptors(Map<String, Class<? extends Entity>> entities) {
List<TableDescriptor<? extends Entity>> descriptors = new ArrayList<>();
entities.forEach((n, e) -> descriptors.add(TableDescriptor.from(EntitySchema.of(e), n)));
return descriptors;
}

@Value
@Builder
@With
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ abstract class BatchFindSpliterator<R, T extends Entity<T>, ID extends Entity.Id

protected abstract List<R> find(YqlStatementPart<?> part, YqlStatementPart<?>... otherParts);

BatchFindSpliterator(Class<T> entityType, int batchSize) {
this(entityType, null, batchSize);
}

BatchFindSpliterator(Class<T> entityType, ID partial, int batchSize) {
this.batchSize = batchSize;
this.idSchema = EntityIdSchema.ofEntity(entityType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ public YdbTable(Class<T> type, QueryExecutor executor) {
this.tableDescriptor = TableDescriptor.from(schema);
}

public YdbTable(Class<T> type, String name, QueryExecutor executor) {
this.type = type;
this.executor = new CheckingQueryExecutor(executor);
this.schema = EntitySchema.of(type);
this.tableDescriptor = TableDescriptor.from(schema, name);
}

protected YdbTable(QueryExecutor executor) {
this.type = resolveEntityType();
this.executor = new CheckingQueryExecutor(executor);
Expand Down
6 changes: 6 additions & 0 deletions repository/src/main/java/tech/ydb/yoj/repository/BaseDb.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.ydb.yoj.repository;

import tech.ydb.yoj.ExperimentalApi;
import tech.ydb.yoj.repository.db.Entity;
import tech.ydb.yoj.repository.db.Table;
import tech.ydb.yoj.repository.db.Tx;
Expand All @@ -11,4 +12,9 @@ static <T> T current(Class<T> type) {
}

<T extends Entity<T>> Table<T> table(Class<T> c);

@ExperimentalApi(issue = "https://github.com/ydb-platform/yoj-project/issues/32")
default <T extends Entity<T>> Table<T> table(Class<T> c, String name) {
return table(c);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.common.reflect.TypeToken;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
import tech.ydb.yoj.ExperimentalApi;
import tech.ydb.yoj.databind.expression.FilterExpression;
import tech.ydb.yoj.databind.expression.OrderExpression;
import tech.ydb.yoj.repository.BaseDb;
Expand All @@ -26,6 +28,11 @@ protected AbstractDelegatingTable() {
this.target = BaseDb.current(BaseDb.class).table(resolveEntityType());
}

@ExperimentalApi(issue = "https://github.com/ydb-platform/yoj-project/issues/32")
protected AbstractDelegatingTable(@NonNull String tableName) {
this.target = BaseDb.current(BaseDb.class).table(resolveEntityType(), tableName);
}

@SuppressWarnings("unchecked")
private Class<T> resolveEntityType() {
return (Class<T>) (new TypeToken<T>(getClass()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public static <E extends Entity<E>> TableDescriptor<E> from(EntitySchema<E> sche
return new TableDescriptor<>(schema.getType(), schema.getName());
}

public static <E extends Entity<E>> TableDescriptor<E> from(EntitySchema<E> schema, String name) {
return new TableDescriptor<>(schema.getType(), name);
}

public String toDebugString() {
String entityName = entityType.getSimpleName();
if (entityName.equals(tableName)) {
Expand Down

0 comments on commit 6c30f74

Please sign in to comment.