2020-12-07 22:15:18 +01:00
|
|
|
package it.cavallium.dbengine.database.disk;
|
|
|
|
|
2021-10-30 11:13:46 +02:00
|
|
|
import io.micrometer.core.instrument.MeterRegistry;
|
2021-09-17 16:56:28 +02:00
|
|
|
import io.net5.buffer.api.BufferAllocator;
|
2021-05-28 16:04:59 +02:00
|
|
|
import it.cavallium.dbengine.client.IndicizerAnalyzers;
|
|
|
|
import it.cavallium.dbengine.client.IndicizerSimilarities;
|
2021-07-01 21:19:52 +02:00
|
|
|
import it.cavallium.dbengine.client.LuceneOptions;
|
2021-01-31 12:43:28 +01:00
|
|
|
import it.cavallium.dbengine.database.LLDatabaseConnection;
|
|
|
|
import it.cavallium.dbengine.database.LLLuceneIndex;
|
2021-10-13 12:25:32 +02:00
|
|
|
import it.cavallium.dbengine.lucene.LuceneHacks;
|
2021-05-03 18:07:18 +02:00
|
|
|
import it.cavallium.dbengine.netty.JMXNettyMonitoringManager;
|
2022-03-02 12:34:30 +01:00
|
|
|
import it.cavallium.dbengine.rpc.current.data.Column;
|
|
|
|
import it.cavallium.dbengine.rpc.current.data.DatabaseOptions;
|
2020-12-07 22:15:18 +01:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2021-10-16 14:35:04 +02:00
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
2021-10-13 00:23:56 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
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 {
|
|
|
|
|
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-10-16 14:35:04 +02:00
|
|
|
private final AtomicBoolean connected = new AtomicBoolean();
|
2021-08-29 23:18:03 +02:00
|
|
|
private final BufferAllocator allocator;
|
2021-10-30 11:13:46 +02:00
|
|
|
private final MeterRegistry meterRegistry;
|
2020-12-07 22:15:18 +01:00
|
|
|
private final Path basePath;
|
2022-03-02 12:34:30 +01:00
|
|
|
private final boolean inMemory;
|
2021-10-16 14:35:04 +02:00
|
|
|
private final AtomicReference<LLTempLMDBEnv> env = new AtomicReference<>();
|
2020-12-07 22:15:18 +01:00
|
|
|
|
2022-03-02 12:34:30 +01:00
|
|
|
public LLLocalDatabaseConnection(BufferAllocator allocator,
|
|
|
|
MeterRegistry meterRegistry,
|
|
|
|
Path basePath,
|
|
|
|
boolean inMemory) {
|
2021-05-03 21:41:51 +02:00
|
|
|
this.allocator = allocator;
|
2021-10-30 11:13:46 +02:00
|
|
|
this.meterRegistry = meterRegistry;
|
2020-12-07 22:15:18 +01:00
|
|
|
this.basePath = basePath;
|
2022-03-02 12:34:30 +01:00
|
|
|
this.inMemory = inMemory;
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 21:41:51 +02:00
|
|
|
@Override
|
2021-08-29 23:18:03 +02:00
|
|
|
public BufferAllocator getAllocator() {
|
2021-05-03 21:41:51 +02:00
|
|
|
return allocator;
|
|
|
|
}
|
|
|
|
|
2021-10-30 11:13:46 +02:00
|
|
|
public MeterRegistry getMeterRegistry() {
|
|
|
|
return meterRegistry;
|
|
|
|
}
|
|
|
|
|
2020-12-07 22:15:18 +01:00
|
|
|
@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-10-16 14:35:04 +02:00
|
|
|
if (!connected.compareAndSet(false, true)) {
|
|
|
|
throw new IllegalStateException("Already connected");
|
|
|
|
}
|
2021-01-30 21:30:18 +01:00
|
|
|
if (Files.notExists(basePath)) {
|
|
|
|
Files.createDirectories(basePath);
|
|
|
|
}
|
2021-10-16 14:35:04 +02:00
|
|
|
var prev = env.getAndSet(new LLTempLMDBEnv());
|
|
|
|
if (prev != null) {
|
|
|
|
throw new IllegalStateException("Env was already set");
|
|
|
|
}
|
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-04-30 19:15:04 +02:00
|
|
|
public Mono<LLLocalKeyValueDatabase> getDatabase(String name,
|
|
|
|
List<Column> columns,
|
2021-06-27 15:40:56 +02:00
|
|
|
DatabaseOptions databaseOptions) {
|
2021-01-31 12:43:28 +01:00
|
|
|
return Mono
|
2021-05-03 21:41:51 +02:00
|
|
|
.fromCallable(() -> new LLLocalKeyValueDatabase(
|
|
|
|
allocator,
|
2021-10-30 11:13:46 +02:00
|
|
|
meterRegistry,
|
2021-05-03 21:41:51 +02:00
|
|
|
name,
|
2022-03-02 12:34:30 +01:00
|
|
|
inMemory,
|
2021-01-31 12:43:28 +01:00
|
|
|
basePath.resolve("database_" + name),
|
|
|
|
columns,
|
|
|
|
new LinkedList<>(),
|
2021-06-27 15:40:56 +02:00
|
|
|
databaseOptions
|
2021-01-31 12:43:28 +01:00
|
|
|
))
|
|
|
|
.subscribeOn(Schedulers.boundedElastic());
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-30 17:28:06 +01:00
|
|
|
public Mono<LLLuceneIndex> getLuceneIndex(@Nullable String clusterName,
|
|
|
|
@Nullable String shardName,
|
2020-12-07 22:15:18 +01:00
|
|
|
int instancesCount,
|
2021-05-28 16:04:59 +02:00
|
|
|
IndicizerAnalyzers indicizerAnalyzers,
|
|
|
|
IndicizerSimilarities indicizerSimilarities,
|
2021-10-13 00:23:56 +02:00
|
|
|
LuceneOptions luceneOptions,
|
|
|
|
@Nullable LuceneHacks luceneHacks) {
|
2021-01-31 12:43:28 +01:00
|
|
|
return Mono
|
|
|
|
.fromCallable(() -> {
|
2021-11-21 12:31:23 +01:00
|
|
|
var env = this.env.get();
|
2021-12-30 17:28:06 +01:00
|
|
|
if (clusterName == null && shardName == null) {
|
|
|
|
throw new IllegalArgumentException("Shard name and/or cluster name must be set");
|
|
|
|
}
|
2021-01-31 12:43:28 +01:00
|
|
|
if (instancesCount != 1) {
|
2021-12-30 17:28:06 +01:00
|
|
|
if (shardName != null && !shardName.equals(clusterName)) {
|
|
|
|
throw new IllegalArgumentException("You shouldn't use a shard name for clustered instances");
|
|
|
|
}
|
2021-10-16 14:35:04 +02:00
|
|
|
Objects.requireNonNull(env, "Environment not set");
|
|
|
|
return new LLLocalMultiLuceneIndex(env,
|
2021-10-30 11:13:46 +02:00
|
|
|
meterRegistry,
|
2021-12-30 17:28:06 +01:00
|
|
|
clusterName,
|
2021-01-31 12:43:28 +01:00
|
|
|
instancesCount,
|
2021-05-28 16:04:59 +02:00
|
|
|
indicizerAnalyzers,
|
|
|
|
indicizerSimilarities,
|
2021-10-13 00:23:56 +02:00
|
|
|
luceneOptions,
|
|
|
|
luceneHacks
|
2021-01-31 12:43:28 +01:00
|
|
|
);
|
|
|
|
} else {
|
2022-02-26 22:51:22 +01:00
|
|
|
return new LLLocalLuceneIndex(env,
|
2021-10-30 11:13:46 +02:00
|
|
|
meterRegistry,
|
2021-12-30 17:28:06 +01:00
|
|
|
clusterName,
|
|
|
|
shardName,
|
2021-05-28 16:04:59 +02:00
|
|
|
indicizerAnalyzers,
|
|
|
|
indicizerSimilarities,
|
2021-10-13 00:23:56 +02:00
|
|
|
luceneOptions,
|
|
|
|
luceneHacks
|
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() {
|
2021-10-16 14:35:04 +02:00
|
|
|
return Mono.<Void>fromCallable(() -> {
|
|
|
|
if (connected.compareAndSet(true, false)) {
|
|
|
|
var env = this.env.get();
|
|
|
|
if (env != null) {
|
2021-10-16 14:59:38 +02:00
|
|
|
env.close();
|
2021-10-16 14:35:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}).subscribeOn(Schedulers.boundedElastic());
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
}
|