Skip to content

Commit

Permalink
Remove unused @Dynamic annotation
Browse files Browse the repository at this point in the history
Better support for multiple tables
with the same Schema is underway.
  • Loading branch information
Alexander Lavrukov committed Dec 3, 2024
1 parent f8860f0 commit 85a8360
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 56 deletions.
74 changes: 29 additions & 45 deletions databind/src/main/java/tech/ydb/yoj/databind/schema/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import tech.ydb.yoj.databind.schema.reflect.StdReflector;

import javax.annotation.Nullable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
import java.time.Duration;
Expand Down Expand Up @@ -94,7 +92,7 @@ protected Schema(@NonNull SchemaKey<T> key, @NonNull Reflector reflector) {
this.reflectType = reflector.reflectRootType(type);

this.schemaKey = key;
this.staticName = type.isAnnotationPresent(Dynamic.class) ? null : namingStrategy.getNameForClass(type);
this.staticName = namingStrategy.getNameForClass(type);

this.fields = reflectType.getFields().stream().map(this::newRootJavaField).toList();
recurseFields(this.fields)
Expand All @@ -106,6 +104,32 @@ protected Schema(@NonNull SchemaKey<T> key, @NonNull Reflector reflector) {
this.ttlModifier = prepareTtlModifier(extractTtlModifier(type));
this.changefeeds = prepareChangefeeds(collectChangefeeds(type));
}

protected Schema(Schema<?> schema, String subSchemaFieldPath) {
JavaField subSchemaField = schema.getField(subSchemaFieldPath);

@SuppressWarnings("unchecked") ReflectType<T> rt = (ReflectType<T>) subSchemaField.field.getReflectType();
this.reflectType = rt;

this.schemaKey = schema.schemaKey.withClazz(reflectType.getRawType());

this.staticName = schema.staticName;
this.globalIndexes = schema.globalIndexes;

if (subSchemaField.fields != null) {
this.fields = subSchemaField.fields.stream().map(this::newRootJavaField).toList();
} else {
if (subSchemaField.getCustomValueTypeInfo() != null) {
var dummyField = new JavaField(new DummyCustomValueSubField(subSchemaField), subSchemaField, __ -> true);
dummyField.setName(subSchemaField.getName());
this.fields = List.of(dummyField);
} else {
this.fields = List.of();
}
}
this.ttlModifier = schema.ttlModifier;
this.changefeeds = schema.changefeeds;
}

private void validateFieldNames() {
flattenFields().stream().collect(toMap(JavaField::getName, Function.identity(), ((x, y) -> {
Expand Down Expand Up @@ -187,32 +211,6 @@ private List<Changefeed> prepareChangefeeds(List<tech.ydb.yoj.databind.schema.Ch
.toList();
}

protected Schema(Schema<?> schema, String subSchemaFieldPath) {
JavaField subSchemaField = schema.getField(subSchemaFieldPath);

@SuppressWarnings("unchecked") ReflectType<T> rt = (ReflectType<T>) subSchemaField.field.getReflectType();
reflectType = rt;

schemaKey = schema.schemaKey.withClazz(reflectType.getRawType());

staticName = schema.staticName;
globalIndexes = schema.globalIndexes;

if (subSchemaField.fields != null) {
fields = subSchemaField.fields.stream().map(this::newRootJavaField).toList();
} else {
if (subSchemaField.getCustomValueTypeInfo() != null) {
var dummyField = new JavaField(new DummyCustomValueSubField(subSchemaField), subSchemaField, __ -> true);
dummyField.setName(subSchemaField.getName());
fields = List.of(dummyField);
} else {
fields = List.of();
}
}
ttlModifier = schema.ttlModifier;
changefeeds = schema.changefeeds;
}

private static Stream<JavaField> recurseFields(Collection<JavaField> fields) {
return fields == null
? Stream.empty()
Expand Down Expand Up @@ -278,11 +276,7 @@ public final NamingStrategy getNamingStrategy() {
* @return the table name for data binding
*/
public final String getName() {
return staticName != null ? staticName : getNamingStrategy().getNameForClass(getType());
}

public final boolean isDynamic() {
return staticName == null;
return staticName;
}

public final List<JavaField> flattenFields() {
Expand Down Expand Up @@ -386,9 +380,7 @@ public final String toString() {
schemaName = getClass().getName();
}

return schemaName
+ (isDynamic() ? ", dynamic" : " \"" + staticName + "\"")
+ " [type=" + getType().getName() + "]";
return schemaName + " \"" + staticName + "\" [type=" + getType().getName() + "]";
}

private static final class DummyCustomValueSubField implements ReflectField {
Expand Down Expand Up @@ -816,12 +808,4 @@ public static class Changefeed {

boolean initialScan;
}

/**
* Annotation for schemas with dynamic names (the {@link NamingStrategy} can return different names
* for different invocations.)
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface Dynamic {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,16 @@ public <T extends Entity<T>> SchemaOperations<T> schema(Class<T> c) {
@Override
public void create() {
String tableName = schema.getName();
getSchemaOperations().createTable(tableName, schema.flattenFields(), schema.flattenId(),
extractHint(), schema.getGlobalIndexes(), schema.getTtlModifier(), schema.getChangefeeds());
if (!schema.isDynamic()) {
entityClassesByTableName.put(tableName, c);
}
getSchemaOperations().createTable(
tableName,
schema.flattenFields(),
schema.flattenId(),
extractHint(),
schema.getGlobalIndexes(),
schema.getTtlModifier(),
schema.getChangefeeds()
);
entityClassesByTableName.put(tableName, c);
}

private YdbTableHint extractHint() {
Expand All @@ -283,12 +288,10 @@ public void drop() {
public boolean exists() {
String tableName = schema.getName();
boolean exists = getSchemaOperations().hasTable(tableName);
if (!schema.isDynamic()) {
if (exists) {
entityClassesByTableName.put(tableName, c);
} else {
entityClassesByTableName.remove(tableName);
}
if (exists) {
entityClassesByTableName.put(tableName, c);
} else {
entityClassesByTableName.remove(tableName);
}
return exists;
}
Expand Down

0 comments on commit 85a8360

Please sign in to comment.