CavalliumDBEngine/src/test/java/it/cavallium/dbengine/LocalTemporaryDbGenerator.java

139 lines
4.9 KiB
Java
Raw Normal View History

2021-09-06 01:15:13 +02:00
package it.cavallium.dbengine;
2021-12-12 23:40:30 +01:00
import static it.cavallium.dbengine.DbTestUtils.MAX_IN_MEMORY_RESULT_ENTRIES;
2021-09-06 01:15:13 +02:00
import static it.cavallium.dbengine.DbTestUtils.ensureNoLeaks;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
2022-03-02 12:34:30 +01:00
import it.cavallium.data.generator.nativedata.Nullableboolean;
2022-03-09 02:29:38 +01:00
import it.cavallium.data.generator.nativedata.Nullabledouble;
2022-03-02 12:34:30 +01:00
import it.cavallium.data.generator.nativedata.Nullableint;
import it.cavallium.data.generator.nativedata.Nullablelong;
2021-09-06 01:15:13 +02:00
import it.cavallium.dbengine.DbTestUtils.TempDb;
import it.cavallium.dbengine.DbTestUtils.TestAllocator;
import it.cavallium.dbengine.client.IndicizerAnalyzers;
import it.cavallium.dbengine.client.IndicizerSimilarities;
2022-03-02 12:34:30 +01:00
import it.cavallium.dbengine.database.ColumnUtils;
2021-09-06 01:15:13 +02:00
import it.cavallium.dbengine.database.LLKeyValueDatabase;
import it.cavallium.dbengine.database.disk.LLLocalDatabaseConnection;
import it.cavallium.dbengine.lucene.LuceneHacks;
import it.cavallium.dbengine.lucene.LuceneUtils;
import it.cavallium.dbengine.lucene.analyzer.TextFieldsAnalyzer;
import it.cavallium.dbengine.lucene.analyzer.TextFieldsSimilarity;
2022-03-05 15:46:40 +01:00
import it.cavallium.dbengine.rpc.current.data.ByteBuffersDirectory;
2022-03-02 12:34:30 +01:00
import it.cavallium.dbengine.rpc.current.data.DatabaseOptions;
2022-03-05 15:46:40 +01:00
import it.cavallium.dbengine.rpc.current.data.LuceneOptions;
2021-09-06 01:15:13 +02:00
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
2021-09-06 01:15:13 +02:00
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicInteger;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
public class LocalTemporaryDbGenerator implements TemporaryDbGenerator {
private static final AtomicInteger dbId = new AtomicInteger(0);
2021-12-12 23:40:30 +01:00
private static final LuceneOptions LUCENE_OPTS = new LuceneOptions(Map.of(),
Duration.ofSeconds(5),
Duration.ofSeconds(5),
false,
2022-02-26 22:51:22 +01:00
new ByteBuffersDirectory(),
2022-03-09 02:29:38 +01:00
Nullableboolean.empty(),
Nullabledouble.empty(),
Nullableint.empty(),
Nullableboolean.empty(),
Nullableboolean.empty(),
2021-12-12 23:40:30 +01:00
true,
MAX_IN_MEMORY_RESULT_ENTRIES
);
2021-09-06 01:15:13 +02:00
@Override
public Mono<TempDb> openTempDb(TestAllocator allocator) {
boolean canUseNettyDirect = DbTestUtils.computeCanUseNettyDirect();
return Mono.defer(() -> {
var wrkspcPath = Path.of("/tmp/.cache/tempdb-" + dbId.incrementAndGet() + "/");
return Mono
.<LLKeyValueDatabase>fromCallable(() -> {
if (Files.exists(wrkspcPath)) {
Files.walk(wrkspcPath).sorted(Comparator.reverseOrder()).forEach(file -> {
try {
Files.delete(file);
} catch (IOException ex) {
throw new CompletionException(ex);
}
});
}
Files.createDirectories(wrkspcPath);
return null;
})
.subscribeOn(Schedulers.boundedElastic())
2022-03-05 15:46:40 +01:00
.then(new LLLocalDatabaseConnection(allocator.allocator(),
new SimpleMeterRegistry(),
wrkspcPath,
true,
null
).connect())
.flatMap(conn -> {
SwappableLuceneSearcher searcher = new SwappableLuceneSearcher();
var luceneHacks = new LuceneHacks(() -> searcher, () -> searcher);
return Mono.zip(
conn.getDatabase("testdb",
2022-03-02 12:34:30 +01:00
List.of(ColumnUtils.dictionary("testmap"), ColumnUtils.special("ints"), ColumnUtils.special("longs")),
2022-01-12 16:18:31 +01:00
new DatabaseOptions(List.of(),
Map.of(),
true,
false,
false,
true,
canUseNettyDirect,
false,
2022-03-02 12:34:30 +01:00
Nullableint.of(-1),
Nullablelong.empty(),
Nullablelong.empty(),
Nullableboolean.empty()
2022-01-12 16:18:31 +01:00
)
),
2022-03-05 15:46:40 +01:00
conn.getLuceneIndex("testluceneindex1",
LuceneUtils.singleStructure(),
2022-01-11 22:23:07 +01:00
IndicizerAnalyzers.of(TextFieldsAnalyzer.ICUCollationKey),
IndicizerSimilarities.of(TextFieldsSimilarity.Boolean),
LUCENE_OPTS,
luceneHacks
),
conn.getLuceneIndex("testluceneindex16",
LuceneUtils.shardsStructure(3),
2022-01-11 22:23:07 +01:00
IndicizerAnalyzers.of(TextFieldsAnalyzer.ICUCollationKey),
IndicizerSimilarities.of(TextFieldsSimilarity.Boolean),
LUCENE_OPTS,
luceneHacks
),
Mono.just(searcher)
)
.map(tuple -> new TempDb(allocator, conn, tuple.getT1(), tuple.getT2(), tuple.getT3(), tuple.getT4(), wrkspcPath));
});
2021-09-06 01:15:13 +02:00
});
}
@Override
public Mono<Void> closeTempDb(TempDb tempDb) {
return tempDb.db().close().then(tempDb.connection().disconnect()).then(Mono.fromCallable(() -> {
ensureNoLeaks(tempDb.allocator().allocator(), false, false);
2021-09-06 01:15:13 +02:00
if (Files.exists(tempDb.path())) {
Files.walk(tempDb.path()).sorted(Comparator.reverseOrder()).forEach(file -> {
try {
Files.delete(file);
} catch (IOException ex) {
throw new CompletionException(ex);
}
});
}
return null;
}).subscribeOn(Schedulers.boundedElastic())).then();
}
}