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

98 lines
2.5 KiB
Java
Raw Normal View History

2020-12-07 22:15:18 +01:00
package it.cavallium.dbengine.database.disk;
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;
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 {
private final Path basePath;
private final boolean crashIfWalError;
public LLLocalDatabaseConnection(Path basePath, boolean crashIfWalError) {
this.basePath = basePath;
this.crashIfWalError = crashIfWalError;
}
@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-02-03 14:37:02 +01:00
.fromCallable(() -> new LLLocalKeyValueDatabase(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
}
}