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;
|
2021-02-04 22:42:57 +01:00
|
|
|
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;
|
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,
|
2021-02-04 22:42:57 +01:00
|
|
|
TextFieldsSimilarity textFieldsSimilarity,
|
2020-12-07 22:15:18 +01:00
|
|
|
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,
|
2021-02-04 22:42:57 +01:00
|
|
|
textFieldsSimilarity,
|
2021-01-31 12:43:28 +01:00
|
|
|
queryRefreshDebounceTime,
|
|
|
|
commitDebounceTime,
|
|
|
|
lowMemory
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return new LLLocalLuceneIndex(basePath.resolve("lucene"),
|
|
|
|
name,
|
|
|
|
textFieldsAnalyzer,
|
2021-02-04 22:42:57 +01:00
|
|
|
textFieldsSimilarity,
|
2021-01-31 12:43:28 +01:00
|
|
|
queryRefreshDebounceTime,
|
|
|
|
commitDebounceTime,
|
2021-02-04 22:42:57 +01:00
|
|
|
lowMemory,
|
|
|
|
null
|
2021-01-31 12:43:28 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.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
|
|
|
}
|
|
|
|
}
|