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 @@ -21,11 +21,6 @@ public class InMemoryRepository implements Repository {
@Getter(AccessLevel.PACKAGE)
private volatile InMemoryStorage storage = new InMemoryStorage();

@Override
public void dropDb() {
storage.dropDb();
}

@Override
public String makeSnapshot() {
String snapshotId = UUID.randomUUID().toString();
Expand All @@ -38,25 +33,29 @@ public void loadSnapshot(String id) {
storage = snapshots.get(id).createSnapshot();
}

@Override
public Set<TableDescriptor<?>> tables() {
return Set.copyOf(storage.tables());
}

@Override
public RepositoryTransaction startTransaction(TxOptions options) {
return new InMemoryRepositoryTransaction(options, this);
}

public <T extends Entity<T>> SchemaOperations<T> schema(TableDescriptor<T> tableDescriptor) {
return new SchemaOperations<T>() {
public SchemaOperations getSchemaOperations() {
return new SchemaOperations() {
@Override
public void create() {
public void createTablespace() {
}

@Override
public void removeTablespace() {
storage.dropDb();
}

@Override
public <T extends Entity<T>> void createTable(TableDescriptor<T> tableDescriptor) {
storage.createTable(tableDescriptor);
}

@Override
public void drop() {
public <T extends Entity<T>> void dropTable(TableDescriptor<T> tableDescriptor) {
if (!storage.dropTable(tableDescriptor)) {
throw new DropTableException(String.format("Can't drop table %s: table doesn't exist",
tableDescriptor.toDebugString())
Expand All @@ -65,9 +64,13 @@ public void drop() {
}

@Override
public boolean exists() {
public <T extends Entity<T>> boolean hasTable(TableDescriptor<T> tableDescriptor) {
return storage.containsTable(tableDescriptor);
}
};
}

@Override
public void shutdown() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import tech.ydb.yoj.repository.db.SchemaOperations;
import tech.ydb.yoj.repository.db.StdTxManager;
import tech.ydb.yoj.repository.db.Table;
import tech.ydb.yoj.repository.db.TableDescriptor;
import tech.ydb.yoj.repository.db.Tx;
import tech.ydb.yoj.repository.db.TxManager;
import tech.ydb.yoj.repository.db.TxOptions;
Expand Down Expand Up @@ -144,17 +145,21 @@ public void tearDown() {

@Test
public void schema() {
SchemaOperations<Simple> schema = repository.schema(Simple.class);
schema.create();
schema.create(); // second create doesn't fail
schema.drop();
assertThatExceptionOfType(DropTableException.class).isThrownBy(schema::drop); // second drop fails
SchemaOperations schema = repository.getSchemaOperations();
TableDescriptor<Simple> tableDescriptor = TableDescriptor.from(EntitySchema.of(Simple.class));
schema.createTable(tableDescriptor);
schema.createTable(tableDescriptor); // second create doesn't fail
schema.dropTable(tableDescriptor);
assertThatExceptionOfType(DropTableException.class)
.isThrownBy(() -> schema.dropTable(tableDescriptor)); // second drop fails
}

@Test
public void multiLevelDirectorySchema() {
SchemaOperations<MultiLevelDirectory> schema = repository.schema(MultiLevelDirectory.class);
schema.create();
TableDescriptor<MultiLevelDirectory> tableDescriptor = TableDescriptor.from(
EntitySchema.of(MultiLevelDirectory.class)
);
repository.getSchemaOperations().createTable(tableDescriptor);
}

@Test
Expand All @@ -163,7 +168,7 @@ public void snapshotWithSubfolders() {

checkEmpty(txManager);

String initSnapshotId = repository.makeSnapshot();
String initSnapshotId = repository.getSchemaOperations().makeSnapshot();

txManager.tx(() -> {
TestEntityOperations db = BaseDb.current(TestEntityOperations.class);
Expand All @@ -178,14 +183,14 @@ public void snapshotWithSubfolders() {
checkNotEmpty(txManager);

// make ne snapshot and load initial
String snapshotWithDataId = repository.makeSnapshot();
repository.loadSnapshot(initSnapshotId);
String snapshotWithDataId = repository.getSchemaOperations().makeSnapshot();
repository.getSchemaOperations().loadSnapshot(initSnapshotId);

// must be empty after load of initial snapshot
checkEmpty(txManager);

// load snapshot created after inserts and check entities present
repository.loadSnapshot(snapshotWithDataId);
repository.getSchemaOperations().loadSnapshot(snapshotWithDataId);
checkNotEmpty(txManager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ public void setUp() {

@After
public void tearDown() {
clearDb(this.repository);
}

public static void clearDb(Repository repo) {
Set<TableDescriptor<?>> tableDescriptors = repo.tables();
new StdTxManager(repo).tx(() -> {
for (TableDescriptor<?> tableDescriptor : tableDescriptors) {
Tx.Current.get().getRepositoryTransaction().table(tableDescriptor).deleteAll();
}
});
this.repository.getSchemaOperations().removeTablespace();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import lombok.NonNull;
import tech.ydb.yoj.repository.db.Entity;
import tech.ydb.yoj.repository.db.EntitySchema;
import tech.ydb.yoj.repository.db.Repository;
import tech.ydb.yoj.repository.db.SchemaOperations;
import tech.ydb.yoj.repository.db.TableDescriptor;
import tech.ydb.yoj.repository.test.sample.model.Book;
import tech.ydb.yoj.repository.test.sample.model.Bubble;
Expand Down Expand Up @@ -73,11 +75,14 @@ private TestEntities() {

@SuppressWarnings("unchecked")
public static Repository init(@NonNull Repository repository) {
repository.createTablespace();
ALL.forEach(entityClass -> repository.schema(entityClass).create());
SchemaOperations schemaOperations = repository.getSchemaOperations();

schemaOperations.createTablespace();

ALL.forEach(entityClass -> schemaOperations.createTable(TableDescriptor.from(EntitySchema.of(entityClass))));

for (TableDescriptor<?> tableDescriptor : ALL_TABLE_DESCRIPTORS) {
repository.schema(tableDescriptor).create();
schemaOperations.createTable(tableDescriptor);
}

return repository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ public class YdbRepository implements Repository {
private final GrpcTransport transport;
private final CloseableMemoizer<SessionClient> sessionClient;

private final ConcurrentMap<String, TableDescriptor<?>> entityClassesByTableName;

public YdbRepository(@NonNull YdbConfig config) {
this(config, NopAuthProvider.INSTANCE);
}
Expand Down Expand Up @@ -88,7 +86,6 @@ public YdbRepository(@NonNull YdbConfig config, @NonNull GrpcTransport transport
}

public YdbRepository(@NonNull YdbConfig config, @NonNull Settings repositorySettings, @NonNull GrpcTransport transport) {
this.entityClassesByTableName = new ConcurrentHashMap<>();
this.transport = transport;
this.sessionClient = MoreSuppliers.memoizeCloseable(() -> new SessionClient(config, repositorySettings, transport));
}
Expand Down Expand Up @@ -174,6 +171,7 @@ public SessionManager getSessionManager() {
return sessionClient.get().sessionManager;
}

@Override
public YdbSchemaOperations getSchemaOperations() {
return sessionClient.get().schemaOperations;
}
Expand Down Expand Up @@ -219,112 +217,11 @@ public void shutdown() {
Exceptions.closeAll(sessionClient, transport);
}

@Override
public void createTablespace() {
getSchemaOperations().createTablespace();
}

@Override
public Set<TableDescriptor<?>> tables() {
return getSchemaOperations().getTableNames().stream()
.map(entityClassesByTableName::get)
.filter(Objects::nonNull)
.collect(toUnmodifiableSet());
}

@Override
public RepositoryTransaction startTransaction(TxOptions options) {
return new YdbRepositoryTransaction<>(this, options);
}

@Override
public String makeSnapshot() {
YdbSchemaOperations schemaOperations = getSchemaOperations();

String snapshotPath = schemaOperations.getTablespace() + ".snapshot-" + UUID.randomUUID() + "/";
schemaOperations.snapshot(snapshotPath);
return snapshotPath;
}

@Override
public void loadSnapshot(String id) {
YdbSchemaOperations schemaOperations = getSchemaOperations();

String current = schemaOperations.getTablespace();

schemaOperations.getTableNames().forEach(schemaOperations::dropTable);
schemaOperations.getDirectoryNames().stream()
.filter(name -> !schemaOperations.isSnapshotDirectory(name))
.forEach(schemaOperations::removeDirectoryRecursive);

schemaOperations.setTablespace(id);
schemaOperations.snapshot(current);
schemaOperations.setTablespace(current);

// NB: We use getSessionManager() method to allow mocking YdbRepository
sessionClient.reset();
}

@Override
public void dropDb() {
try {
getSchemaOperations().removeTablespace();
entityClassesByTableName.clear();
} catch (Exception e) {
log.error("Could not drop all tables from tablespace", e);
}
}

@Override
public <T extends Entity<T>> SchemaOperations<T> schema(TableDescriptor<T> tableDescriptor) {
EntitySchema<T> schema = EntitySchema.of(tableDescriptor.entityType());
return new SchemaOperations<>() {
@Override
public void create() {
String tableName = tableDescriptor.tableName();
getSchemaOperations().createTable(
tableName,
schema.flattenFields(),
schema.flattenId(),
extractHint(),
schema.getGlobalIndexes(),
schema.getTtlModifier(),
schema.getChangefeeds()
);
entityClassesByTableName.put(tableName, tableDescriptor);
}

private YdbTableHint extractHint() {
try {
Field ydbTableHintField = tableDescriptor.entityType().getDeclaredField("ydbTableHint");
ydbTableHintField.setAccessible(true);
return (YdbTableHint) ydbTableHintField.get(null);
} catch (NoSuchFieldException | IllegalAccessException ignored) {
return null;
}
}

@Override
public void drop() {
String tableName = tableDescriptor.tableName();
getSchemaOperations().dropTable(tableName);
entityClassesByTableName.remove(tableName);
}

@Override
public boolean exists() {
String tableName = tableDescriptor.tableName();
boolean exists = getSchemaOperations().hasTable(tableName);
if (exists) {
entityClassesByTableName.put(tableName, tableDescriptor);
} else {
entityClassesByTableName.remove(tableName);
}
return exists;
}
};
}

@Value
public static class Query<PARAMS> {
Statement<PARAMS, ?> statement;
Expand Down
Loading
Loading