2021-07-10 20:52:01 +02:00
|
|
|
package it.cavallium.dbengine.database.memory;
|
|
|
|
|
2022-07-23 02:42:48 +02:00
|
|
|
import static it.cavallium.dbengine.lucene.LuceneUtils.luceneScheduler;
|
|
|
|
|
2021-10-30 11:13:46 +02:00
|
|
|
import io.micrometer.core.instrument.MeterRegistry;
|
2022-10-02 03:09:50 +02:00
|
|
|
import io.netty5.buffer.BufferAllocator;
|
2021-07-10 20:52:01 +02:00
|
|
|
import it.cavallium.dbengine.database.LLDatabaseConnection;
|
|
|
|
import it.cavallium.dbengine.database.LLKeyValueDatabase;
|
|
|
|
import it.cavallium.dbengine.database.LLLuceneIndex;
|
|
|
|
import it.cavallium.dbengine.database.disk.LLLocalLuceneIndex;
|
2022-04-06 02:41:32 +02:00
|
|
|
import it.cavallium.dbengine.database.disk.LLTempHugePqEnv;
|
2021-10-13 12:25:32 +02:00
|
|
|
import it.cavallium.dbengine.lucene.LuceneHacks;
|
2022-07-23 02:42:48 +02:00
|
|
|
import it.cavallium.dbengine.lucene.LuceneUtils;
|
2022-03-05 15:46:40 +01:00
|
|
|
import it.cavallium.dbengine.rpc.current.data.ByteBuffersDirectory;
|
2022-03-02 12:34:30 +01:00
|
|
|
import it.cavallium.dbengine.rpc.current.data.Column;
|
|
|
|
import it.cavallium.dbengine.rpc.current.data.DatabaseOptions;
|
2022-03-05 15:46:40 +01:00
|
|
|
import it.cavallium.dbengine.rpc.current.data.IndicizerAnalyzers;
|
|
|
|
import it.cavallium.dbengine.rpc.current.data.IndicizerSimilarities;
|
|
|
|
import it.cavallium.dbengine.rpc.current.data.LuceneIndexStructure;
|
|
|
|
import it.cavallium.dbengine.rpc.current.data.LuceneOptions;
|
|
|
|
import it.cavallium.dbengine.rpc.current.data.LuceneOptionsBuilder;
|
2021-07-10 20:52:01 +02:00
|
|
|
import java.util.List;
|
2021-11-21 12:31:23 +01:00
|
|
|
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-07-10 20:52:01 +02:00
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
import reactor.core.scheduler.Schedulers;
|
|
|
|
|
|
|
|
public class LLMemoryDatabaseConnection implements LLDatabaseConnection {
|
|
|
|
|
2021-11-21 12:31:23 +01:00
|
|
|
private final AtomicBoolean connected = new AtomicBoolean();
|
2021-08-31 09:14:46 +02:00
|
|
|
private final BufferAllocator allocator;
|
2021-10-30 11:13:46 +02:00
|
|
|
private final MeterRegistry meterRegistry;
|
2022-04-06 02:41:32 +02:00
|
|
|
private final AtomicReference<LLTempHugePqEnv> env = new AtomicReference<>();
|
2021-07-10 20:52:01 +02:00
|
|
|
|
2021-10-30 11:13:46 +02:00
|
|
|
public LLMemoryDatabaseConnection(BufferAllocator allocator, MeterRegistry meterRegistry) {
|
2021-07-10 20:52:01 +02:00
|
|
|
this.allocator = allocator;
|
2021-10-30 11:13:46 +02:00
|
|
|
this.meterRegistry = meterRegistry;
|
2021-07-10 20:52:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-31 09:14:46 +02:00
|
|
|
public BufferAllocator getAllocator() {
|
2021-07-10 20:52:01 +02:00
|
|
|
return allocator;
|
|
|
|
}
|
|
|
|
|
2021-10-30 11:13:46 +02:00
|
|
|
@Override
|
|
|
|
public MeterRegistry getMeterRegistry() {
|
|
|
|
return meterRegistry;
|
|
|
|
}
|
|
|
|
|
2021-07-10 20:52:01 +02:00
|
|
|
@Override
|
|
|
|
public Mono<LLDatabaseConnection> connect() {
|
2021-11-21 12:31:23 +01:00
|
|
|
return Mono
|
|
|
|
.<LLDatabaseConnection>fromCallable(() -> {
|
|
|
|
if (!connected.compareAndSet(false, true)) {
|
|
|
|
throw new IllegalStateException("Already connected");
|
|
|
|
}
|
2022-04-06 02:41:32 +02:00
|
|
|
var prev = env.getAndSet(new LLTempHugePqEnv());
|
2021-11-21 12:31:23 +01:00
|
|
|
if (prev != null) {
|
|
|
|
throw new IllegalStateException("Env was already set");
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
})
|
|
|
|
.subscribeOn(Schedulers.boundedElastic());
|
2021-07-10 20:52:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<LLKeyValueDatabase> getDatabase(String name,
|
|
|
|
List<Column> columns,
|
|
|
|
DatabaseOptions databaseOptions) {
|
|
|
|
return Mono
|
|
|
|
.<LLKeyValueDatabase>fromCallable(() -> new LLMemoryKeyValueDatabase(
|
|
|
|
allocator,
|
2021-10-30 11:13:46 +02:00
|
|
|
meterRegistry,
|
2021-07-10 20:52:01 +02:00
|
|
|
name,
|
|
|
|
columns
|
|
|
|
))
|
|
|
|
.subscribeOn(Schedulers.boundedElastic());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-03-05 15:46:40 +01:00
|
|
|
public Mono<? extends LLLuceneIndex> getLuceneIndex(String clusterName,
|
|
|
|
LuceneIndexStructure indexStructure,
|
2021-07-10 20:52:01 +02:00
|
|
|
IndicizerAnalyzers indicizerAnalyzers,
|
|
|
|
IndicizerSimilarities indicizerSimilarities,
|
2022-03-05 15:46:40 +01:00
|
|
|
LuceneOptions luceneOptions,
|
2021-10-13 00:23:56 +02:00
|
|
|
@Nullable LuceneHacks luceneHacks) {
|
2022-02-26 22:51:22 +01:00
|
|
|
var memoryLuceneOptions = LuceneOptionsBuilder
|
2022-03-05 15:46:40 +01:00
|
|
|
.builder(luceneOptions)
|
2022-02-26 22:51:22 +01:00
|
|
|
.directoryOptions(new ByteBuffersDirectory())
|
|
|
|
.build();
|
2021-07-10 20:52:01 +02:00
|
|
|
return Mono
|
2021-11-21 12:31:23 +01:00
|
|
|
.<LLLuceneIndex>fromCallable(() -> {
|
|
|
|
var env = this.env.get();
|
|
|
|
return new LLLocalLuceneIndex(env,
|
|
|
|
meterRegistry,
|
2021-12-30 17:28:06 +01:00
|
|
|
clusterName,
|
2022-03-05 15:46:40 +01:00
|
|
|
0,
|
2021-11-21 12:31:23 +01:00
|
|
|
indicizerAnalyzers,
|
|
|
|
indicizerSimilarities,
|
2022-02-26 22:51:22 +01:00
|
|
|
memoryLuceneOptions,
|
2022-03-05 15:46:40 +01:00
|
|
|
luceneHacks,
|
|
|
|
null
|
2021-11-21 12:31:23 +01:00
|
|
|
);
|
|
|
|
})
|
2022-07-23 02:42:48 +02:00
|
|
|
.transform(LuceneUtils::scheduleLucene);
|
2021-07-10 20:52:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Void> disconnect() {
|
2021-11-21 12:31:23 +01:00
|
|
|
return Mono.<Void>fromCallable(() -> {
|
|
|
|
if (connected.compareAndSet(true, false)) {
|
|
|
|
var env = this.env.get();
|
|
|
|
if (env != null) {
|
|
|
|
env.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}).subscribeOn(Schedulers.boundedElastic());
|
2021-07-10 20:52:01 +02:00
|
|
|
}
|
|
|
|
}
|