2020-12-07 22:15:18 +01:00
|
|
|
package it.cavallium.dbengine.database.disk;
|
|
|
|
|
2021-01-31 12:43:28 +01:00
|
|
|
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;
|
2021-01-30 21:30:18 +01:00
|
|
|
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
|
2021-01-31 12:43:28 +01:00
|
|
|
public Mono<LLDatabaseConnection> connect() {
|
2021-01-30 21:30:18 +01:00
|
|
|
return Mono
|
2021-01-31 12:43:28 +01:00
|
|
|
.<LLDatabaseConnection>fromCallable(() -> {
|
2021-01-30 21:30:18 +01:00
|
|
|
if (Files.notExists(basePath)) {
|
|
|
|
Files.createDirectories(basePath);
|
|
|
|
}
|
2021-01-31 12:43:28 +01:00
|
|
|
return this;
|
2021-01-30 21:30:18 +01:00
|
|
|
})
|
|
|
|
.subscribeOn(Schedulers.boundedElastic());
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-31 12:43:28 +01:00
|
|
|
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,
|
2021-01-31 12:43:28 +01:00
|
|
|
basePath.resolve("database_" + name),
|
|
|
|
columns,
|
|
|
|
new LinkedList<>(),
|
|
|
|
crashIfWalError,
|
|
|
|
lowMemory
|
|
|
|
))
|
|
|
|
.subscribeOn(Schedulers.boundedElastic());
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-31 12:43:28 +01:00
|
|
|
public Mono<LLLuceneIndex> getLuceneIndex(String name,
|
2020-12-07 22:15:18 +01:00
|
|
|
int instancesCount,
|
|
|
|
TextFieldsAnalyzer textFieldsAnalyzer,
|
|
|
|
Duration queryRefreshDebounceTime,
|
|
|
|
Duration commitDebounceTime,
|
2021-01-31 12:43:28 +01:00
|
|
|
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
|
2021-01-30 21:30:18 +01:00
|
|
|
public Mono<Void> disconnect() {
|
|
|
|
return Mono.empty();
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
}
|