Skip to content

Commit 2697bfd

Browse files
author
Alexander Lavrukov
committed
SchemaOperations
1 parent 26f2caf commit 2697bfd

File tree

10 files changed

+158
-191
lines changed

10 files changed

+158
-191
lines changed

repository-inmemory/src/main/java/tech/ydb/yoj/repository/test/inmemory/InMemoryRepository.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ public class InMemoryRepository implements Repository {
2121
@Getter(AccessLevel.PACKAGE)
2222
private volatile InMemoryStorage storage = new InMemoryStorage();
2323

24-
@Override
25-
public void dropDb() {
26-
storage.dropDb();
27-
}
28-
2924
@Override
3025
public String makeSnapshot() {
3126
String snapshotId = UUID.randomUUID().toString();
@@ -38,25 +33,29 @@ public void loadSnapshot(String id) {
3833
storage = snapshots.get(id).createSnapshot();
3934
}
4035

41-
@Override
42-
public Set<TableDescriptor<?>> tables() {
43-
return Set.copyOf(storage.tables());
44-
}
45-
4636
@Override
4737
public RepositoryTransaction startTransaction(TxOptions options) {
4838
return new InMemoryRepositoryTransaction(options, this);
4939
}
5040

51-
public <T extends Entity<T>> SchemaOperations<T> schema(TableDescriptor<T> tableDescriptor) {
52-
return new SchemaOperations<T>() {
41+
public SchemaOperations getSchemaOperations() {
42+
return new SchemaOperations() {
5343
@Override
54-
public void create() {
44+
public void createTablespace() {
45+
}
46+
47+
@Override
48+
public void removeTablespace() {
49+
storage.dropDb();
50+
}
51+
52+
@Override
53+
public <T extends Entity<T>> void createTable(TableDescriptor<T> tableDescriptor) {
5554
storage.createTable(tableDescriptor);
5655
}
5756

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

6766
@Override
68-
public boolean exists() {
67+
public <T extends Entity<T>> boolean hasTable(TableDescriptor<T> tableDescriptor) {
6968
return storage.containsTable(tableDescriptor);
7069
}
7170
};
7271
}
72+
73+
@Override
74+
public void shutdown() {
75+
}
7376
}

repository-test/src/main/java/tech/ydb/yoj/repository/test/RepositoryTest.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import tech.ydb.yoj.repository.db.SchemaOperations;
2424
import tech.ydb.yoj.repository.db.StdTxManager;
2525
import tech.ydb.yoj.repository.db.Table;
26+
import tech.ydb.yoj.repository.db.TableDescriptor;
2627
import tech.ydb.yoj.repository.db.Tx;
2728
import tech.ydb.yoj.repository.db.TxManager;
2829
import tech.ydb.yoj.repository.db.TxOptions;
@@ -144,17 +145,21 @@ public void tearDown() {
144145

145146
@Test
146147
public void schema() {
147-
SchemaOperations<Simple> schema = repository.schema(Simple.class);
148-
schema.create();
149-
schema.create(); // second create doesn't fail
150-
schema.drop();
151-
assertThatExceptionOfType(DropTableException.class).isThrownBy(schema::drop); // second drop fails
148+
SchemaOperations schema = repository.getSchemaOperations();
149+
TableDescriptor<Simple> tableDescriptor = TableDescriptor.from(EntitySchema.of(Simple.class));
150+
schema.createTable(tableDescriptor);
151+
schema.createTable(tableDescriptor); // second create doesn't fail
152+
schema.dropTable(tableDescriptor);
153+
assertThatExceptionOfType(DropTableException.class)
154+
.isThrownBy(() -> schema.dropTable(tableDescriptor)); // second drop fails
152155
}
153156

154157
@Test
155158
public void multiLevelDirectorySchema() {
156-
SchemaOperations<MultiLevelDirectory> schema = repository.schema(MultiLevelDirectory.class);
157-
schema.create();
159+
TableDescriptor<MultiLevelDirectory> tableDescriptor = TableDescriptor.from(
160+
EntitySchema.of(MultiLevelDirectory.class)
161+
);
162+
repository.getSchemaOperations().createTable(tableDescriptor);
158163
}
159164

160165
@Test
@@ -163,7 +168,7 @@ public void snapshotWithSubfolders() {
163168

164169
checkEmpty(txManager);
165170

166-
String initSnapshotId = repository.makeSnapshot();
171+
String initSnapshotId = repository.getSchemaOperations().makeSnapshot();
167172

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

180185
// make ne snapshot and load initial
181-
String snapshotWithDataId = repository.makeSnapshot();
182-
repository.loadSnapshot(initSnapshotId);
186+
String snapshotWithDataId = repository.getSchemaOperations().makeSnapshot();
187+
repository.getSchemaOperations().loadSnapshot(initSnapshotId);
183188

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

187192
// load snapshot created after inserts and check entities present
188-
repository.loadSnapshot(snapshotWithDataId);
193+
repository.getSchemaOperations().loadSnapshot(snapshotWithDataId);
189194
checkNotEmpty(txManager);
190195
}
191196

repository-test/src/main/java/tech/ydb/yoj/repository/test/RepositoryTestSupport.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@ public void setUp() {
2525

2626
@After
2727
public void tearDown() {
28-
clearDb(this.repository);
29-
}
30-
31-
public static void clearDb(Repository repo) {
32-
Set<TableDescriptor<?>> tableDescriptors = repo.tables();
33-
new StdTxManager(repo).tx(() -> {
34-
for (TableDescriptor<?> tableDescriptor : tableDescriptors) {
35-
Tx.Current.get().getRepositoryTransaction().table(tableDescriptor).deleteAll();
36-
}
37-
});
28+
this.repository.getSchemaOperations().removeTablespace();
3829
}
3930
}

repository-test/src/main/java/tech/ydb/yoj/repository/test/entity/TestEntities.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import lombok.NonNull;
44
import tech.ydb.yoj.repository.db.Entity;
5+
import tech.ydb.yoj.repository.db.EntitySchema;
56
import tech.ydb.yoj.repository.db.Repository;
7+
import tech.ydb.yoj.repository.db.SchemaOperations;
68
import tech.ydb.yoj.repository.db.TableDescriptor;
79
import tech.ydb.yoj.repository.test.sample.model.Book;
810
import tech.ydb.yoj.repository.test.sample.model.Bubble;
@@ -73,11 +75,14 @@ private TestEntities() {
7375

7476
@SuppressWarnings("unchecked")
7577
public static Repository init(@NonNull Repository repository) {
76-
repository.createTablespace();
77-
ALL.forEach(entityClass -> repository.schema(entityClass).create());
78+
SchemaOperations schemaOperations = repository.getSchemaOperations();
79+
80+
schemaOperations.createTablespace();
81+
82+
ALL.forEach(entityClass -> schemaOperations.createTable(TableDescriptor.from(EntitySchema.of(entityClass))));
7883

7984
for (TableDescriptor<?> tableDescriptor : ALL_TABLE_DESCRIPTORS) {
80-
repository.schema(tableDescriptor).create();
85+
schemaOperations.createTable(tableDescriptor);
8186
}
8287

8388
return repository;

repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/YdbRepository.java

Lines changed: 1 addition & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ public class YdbRepository implements Repository {
5757
private final GrpcTransport transport;
5858
private final CloseableMemoizer<SessionClient> sessionClient;
5959

60-
private final ConcurrentMap<String, TableDescriptor<?>> entityClassesByTableName;
61-
6260
public YdbRepository(@NonNull YdbConfig config) {
6361
this(config, NopAuthProvider.INSTANCE);
6462
}
@@ -88,7 +86,6 @@ public YdbRepository(@NonNull YdbConfig config, @NonNull GrpcTransport transport
8886
}
8987

9088
public YdbRepository(@NonNull YdbConfig config, @NonNull Settings repositorySettings, @NonNull GrpcTransport transport) {
91-
this.entityClassesByTableName = new ConcurrentHashMap<>();
9289
this.transport = transport;
9390
this.sessionClient = MoreSuppliers.memoizeCloseable(() -> new SessionClient(config, repositorySettings, transport));
9491
}
@@ -174,6 +171,7 @@ public SessionManager getSessionManager() {
174171
return sessionClient.get().sessionManager;
175172
}
176173

174+
@Override
177175
public YdbSchemaOperations getSchemaOperations() {
178176
return sessionClient.get().schemaOperations;
179177
}
@@ -219,112 +217,11 @@ public void shutdown() {
219217
Exceptions.closeAll(sessionClient, transport);
220218
}
221219

222-
@Override
223-
public void createTablespace() {
224-
getSchemaOperations().createTablespace();
225-
}
226-
227-
@Override
228-
public Set<TableDescriptor<?>> tables() {
229-
return getSchemaOperations().getTableNames().stream()
230-
.map(entityClassesByTableName::get)
231-
.filter(Objects::nonNull)
232-
.collect(toUnmodifiableSet());
233-
}
234-
235220
@Override
236221
public RepositoryTransaction startTransaction(TxOptions options) {
237222
return new YdbRepositoryTransaction<>(this, options);
238223
}
239224

240-
@Override
241-
public String makeSnapshot() {
242-
YdbSchemaOperations schemaOperations = getSchemaOperations();
243-
244-
String snapshotPath = schemaOperations.getTablespace() + ".snapshot-" + UUID.randomUUID() + "/";
245-
schemaOperations.snapshot(snapshotPath);
246-
return snapshotPath;
247-
}
248-
249-
@Override
250-
public void loadSnapshot(String id) {
251-
YdbSchemaOperations schemaOperations = getSchemaOperations();
252-
253-
String current = schemaOperations.getTablespace();
254-
255-
schemaOperations.getTableNames().forEach(schemaOperations::dropTable);
256-
schemaOperations.getDirectoryNames().stream()
257-
.filter(name -> !schemaOperations.isSnapshotDirectory(name))
258-
.forEach(schemaOperations::removeDirectoryRecursive);
259-
260-
schemaOperations.setTablespace(id);
261-
schemaOperations.snapshot(current);
262-
schemaOperations.setTablespace(current);
263-
264-
// NB: We use getSessionManager() method to allow mocking YdbRepository
265-
sessionClient.reset();
266-
}
267-
268-
@Override
269-
public void dropDb() {
270-
try {
271-
getSchemaOperations().removeTablespace();
272-
entityClassesByTableName.clear();
273-
} catch (Exception e) {
274-
log.error("Could not drop all tables from tablespace", e);
275-
}
276-
}
277-
278-
@Override
279-
public <T extends Entity<T>> SchemaOperations<T> schema(TableDescriptor<T> tableDescriptor) {
280-
EntitySchema<T> schema = EntitySchema.of(tableDescriptor.entityType());
281-
return new SchemaOperations<>() {
282-
@Override
283-
public void create() {
284-
String tableName = tableDescriptor.tableName();
285-
getSchemaOperations().createTable(
286-
tableName,
287-
schema.flattenFields(),
288-
schema.flattenId(),
289-
extractHint(),
290-
schema.getGlobalIndexes(),
291-
schema.getTtlModifier(),
292-
schema.getChangefeeds()
293-
);
294-
entityClassesByTableName.put(tableName, tableDescriptor);
295-
}
296-
297-
private YdbTableHint extractHint() {
298-
try {
299-
Field ydbTableHintField = tableDescriptor.entityType().getDeclaredField("ydbTableHint");
300-
ydbTableHintField.setAccessible(true);
301-
return (YdbTableHint) ydbTableHintField.get(null);
302-
} catch (NoSuchFieldException | IllegalAccessException ignored) {
303-
return null;
304-
}
305-
}
306-
307-
@Override
308-
public void drop() {
309-
String tableName = tableDescriptor.tableName();
310-
getSchemaOperations().dropTable(tableName);
311-
entityClassesByTableName.remove(tableName);
312-
}
313-
314-
@Override
315-
public boolean exists() {
316-
String tableName = tableDescriptor.tableName();
317-
boolean exists = getSchemaOperations().hasTable(tableName);
318-
if (exists) {
319-
entityClassesByTableName.put(tableName, tableDescriptor);
320-
} else {
321-
entityClassesByTableName.remove(tableName);
322-
}
323-
return exists;
324-
}
325-
};
326-
}
327-
328225
@Value
329226
public static class Query<PARAMS> {
330227
Statement<PARAMS, ?> statement;

0 commit comments

Comments
 (0)