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

86 lines
2.2 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.database.analyzer.TextFieldsAnalyzer;
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) {
return Mono
2021-02-03 14:37:02 +01:00
.fromCallable(() -> new LLLocalKeyValueDatabase(name,
basePath.resolve("database_" + name),
columns,
new LinkedList<>(),
crashIfWalError,
lowMemory
))
.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,
Duration queryRefreshDebounceTime,
Duration commitDebounceTime,
boolean lowMemory) {
return Mono
.fromCallable(() -> {
if (instancesCount != 1) {
return new LLLocalMultiLuceneIndex(basePath.resolve("lucene"),
name,
instancesCount,
textFieldsAnalyzer,
queryRefreshDebounceTime,
commitDebounceTime,
lowMemory
);
} else {
return new LLLocalLuceneIndex(basePath.resolve("lucene"),
name,
textFieldsAnalyzer,
queryRefreshDebounceTime,
commitDebounceTime,
lowMemory
);
}
})
.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
}
}