CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/disk/LLLocalDatabaseConnection.java

113 lines
2.9 KiB
Java
Raw Normal View History

2020-12-07 22:15:18 +01:00
package it.cavallium.dbengine.database.disk;
2021-05-03 21:41:51 +02:00
import io.netty.buffer.ByteBufAllocator;
import it.cavallium.dbengine.database.Column;
import it.cavallium.dbengine.database.LLDatabaseConnection;
import it.cavallium.dbengine.database.LLLuceneIndex;
import it.cavallium.dbengine.lucene.analyzer.TextFieldsAnalyzer;
import it.cavallium.dbengine.lucene.analyzer.TextFieldsSimilarity;
2021-05-03 18:07:18 +02:00
import it.cavallium.dbengine.netty.JMXNettyMonitoringManager;
2020-12-07 22:15:18 +01:00
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.LinkedList;
import java.util.List;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
2020-12-07 22:15:18 +01:00
public class LLLocalDatabaseConnection implements LLDatabaseConnection {
2021-05-03 18:07:18 +02:00
static {
2021-05-03 21:41:51 +02:00
JMXNettyMonitoringManager.initialize();
2021-05-03 18:07:18 +02:00
}
2021-05-03 21:41:51 +02:00
private final ByteBufAllocator allocator;
2020-12-07 22:15:18 +01:00
private final Path basePath;
private final boolean crashIfWalError;
2021-05-03 21:41:51 +02:00
public LLLocalDatabaseConnection(ByteBufAllocator allocator, Path basePath, boolean crashIfWalError) {
this.allocator = allocator;
2020-12-07 22:15:18 +01:00
this.basePath = basePath;
this.crashIfWalError = crashIfWalError;
}
2021-05-03 21:41:51 +02:00
@Override
public ByteBufAllocator getAllocator() {
return allocator;
}
2020-12-07 22:15:18 +01:00
@Override
public Mono<LLDatabaseConnection> connect() {
return Mono
.<LLDatabaseConnection>fromCallable(() -> {
if (Files.notExists(basePath)) {
Files.createDirectories(basePath);
}
return this;
})
.subscribeOn(Schedulers.boundedElastic());
2020-12-07 22:15:18 +01:00
}
@Override
public Mono<LLLocalKeyValueDatabase> getDatabase(String name,
List<Column> columns,
boolean lowMemory,
boolean inMemory) {
return Mono
2021-05-03 21:41:51 +02:00
.fromCallable(() -> new LLLocalKeyValueDatabase(
allocator,
name,
basePath.resolve("database_" + name),
columns,
new LinkedList<>(),
crashIfWalError,
lowMemory,
inMemory
))
.subscribeOn(Schedulers.boundedElastic());
2020-12-07 22:15:18 +01:00
}
@Override
public Mono<LLLuceneIndex> getLuceneIndex(String name,
2020-12-07 22:15:18 +01:00
int instancesCount,
TextFieldsAnalyzer textFieldsAnalyzer,
TextFieldsSimilarity textFieldsSimilarity,
2020-12-07 22:15:18 +01:00
Duration queryRefreshDebounceTime,
Duration commitDebounceTime,
boolean lowMemory,
boolean inMemory) {
return Mono
.fromCallable(() -> {
if (instancesCount != 1) {
return new LLLocalMultiLuceneIndex(basePath.resolve("lucene"),
name,
instancesCount,
textFieldsAnalyzer,
textFieldsSimilarity,
queryRefreshDebounceTime,
commitDebounceTime,
lowMemory,
inMemory
);
} else {
return new LLLocalLuceneIndex(basePath.resolve("lucene"),
name,
textFieldsAnalyzer,
textFieldsSimilarity,
queryRefreshDebounceTime,
commitDebounceTime,
lowMemory,
inMemory,
null
);
}
})
.subscribeOn(Schedulers.boundedElastic());
2020-12-07 22:15:18 +01:00
}
@Override
public Mono<Void> disconnect() {
return Mono.empty();
2020-12-07 22:15:18 +01:00
}
}