rockserver/src/main/java/it/cavallium/rockserver/core/client/EmbeddedConnection.java

149 lines
4.3 KiB
Java
Raw Normal View History

2023-12-06 00:43:35 +01:00
package it.cavallium.rockserver.core.client;
import it.cavallium.rockserver.core.common.RequestType;
import it.cavallium.rockserver.core.common.RequestType.RequestGet;
import it.cavallium.rockserver.core.common.RequestType.RequestPut;
2023-12-06 00:43:35 +01:00
import it.cavallium.rockserver.core.common.ColumnSchema;
import it.cavallium.rockserver.core.common.RocksDBAPI;
import it.cavallium.rockserver.core.common.RocksDBAPICommand;
import it.cavallium.rockserver.core.common.RocksDBAsyncAPI;
2023-12-06 00:43:35 +01:00
import it.cavallium.rockserver.core.common.RocksDBException;
import it.cavallium.rockserver.core.common.RocksDBSyncAPI;
2023-12-06 00:43:35 +01:00
import it.cavallium.rockserver.core.impl.EmbeddedDB;
import java.io.IOException;
2023-12-08 23:50:27 +01:00
import java.lang.foreign.Arena;
2023-12-06 00:43:35 +01:00
import java.lang.foreign.MemorySegment;
import java.net.URI;
import java.nio.file.Path;
2023-12-07 00:14:28 +01:00
import java.util.Optional;
2024-03-29 02:44:11 +01:00
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
2024-03-29 02:44:11 +01:00
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
2023-12-09 01:02:08 +01:00
import org.jetbrains.annotations.NotNull;
2023-12-06 00:43:35 +01:00
import org.jetbrains.annotations.Nullable;
public class EmbeddedConnection extends BaseConnection implements RocksDBAPI {
2023-12-06 00:43:35 +01:00
private final EmbeddedDB db;
2023-12-07 00:14:28 +01:00
public static final URI PRIVATE_MEMORY_URL = URI.create("memory://private");
2024-03-29 02:44:11 +01:00
private final ExecutorService exeuctor;
2023-12-06 00:43:35 +01:00
2023-12-07 00:14:28 +01:00
public EmbeddedConnection(@Nullable Path path, String name, @Nullable Path embeddedConfig) {
2023-12-06 00:43:35 +01:00
super(name);
2023-12-07 00:14:28 +01:00
this.db = new EmbeddedDB(path, name, embeddedConfig);
2024-03-29 02:44:11 +01:00
this.exeuctor = Executors.newVirtualThreadPerTaskExecutor();
2023-12-06 00:43:35 +01:00
}
@Override
public void close() throws IOException {
db.close();
super.close();
}
@Override
public URI getUrl() {
2023-12-07 00:14:28 +01:00
return Optional.ofNullable(db.getPath()).map(Path::toUri).orElse(PRIVATE_MEMORY_URL);
2023-12-06 00:43:35 +01:00
}
@Override
public RocksDBSyncAPI getSyncApi() {
return this;
}
@Override
public RocksDBAsyncAPI getAsyncApi() {
return this;
}
2023-12-06 00:43:35 +01:00
@Override
public long openTransaction(long timeoutMs) {
return db.openTransaction(timeoutMs);
}
@Override
2023-12-07 16:54:12 +01:00
public boolean closeTransaction(long transactionId, boolean commit) {
return db.closeTransaction(transactionId, commit);
2023-12-06 00:43:35 +01:00
}
2023-12-09 17:45:47 +01:00
@Override
public void closeFailedUpdate(long updateId) throws RocksDBException {
db.closeFailedUpdate(updateId);
}
2023-12-06 00:43:35 +01:00
@Override
2023-12-09 01:02:08 +01:00
public long createColumn(String name, @NotNull ColumnSchema schema) {
2023-12-06 00:43:35 +01:00
return db.createColumn(name, schema);
}
@Override
public void deleteColumn(long columnId) throws RocksDBException {
db.deleteColumn(columnId);
}
@Override
2023-12-09 01:02:08 +01:00
public long getColumnId(@NotNull String name) {
2023-12-06 00:43:35 +01:00
return db.getColumnId(name);
}
@Override
public <R> R requestSync(RocksDBAPICommand<R> req) {
return req.handleSync(this);
}
@Override
public <R> CompletionStage<R> requestAsync(RocksDBAPICommand<R> req) {
2024-03-29 02:44:11 +01:00
return CompletableFuture.supplyAsync(() -> req.handleSync(this), exeuctor);
}
2023-12-06 00:43:35 +01:00
@Override
2023-12-08 23:50:27 +01:00
public <T> T put(Arena arena,
2023-12-09 17:45:47 +01:00
long transactionOrUpdateId,
2023-12-06 00:43:35 +01:00
long columnId,
2023-12-09 01:02:08 +01:00
@NotNull MemorySegment @NotNull [] keys,
@NotNull MemorySegment value,
RequestPut<? super MemorySegment, T> requestType) throws RocksDBException {
return db.put(arena, transactionOrUpdateId, columnId, keys, value, requestType);
2023-12-06 00:43:35 +01:00
}
@Override
2023-12-08 23:50:27 +01:00
public <T> T get(Arena arena,
2023-12-09 17:56:30 +01:00
long transactionOrUpdateId,
2023-12-07 16:54:12 +01:00
long columnId,
2023-12-09 01:02:08 +01:00
MemorySegment @NotNull [] keys,
RequestGet<? super MemorySegment, T> requestType) throws RocksDBException {
return db.get(arena, transactionOrUpdateId, columnId, keys, requestType);
2023-12-06 00:43:35 +01:00
}
@Override
2023-12-08 23:50:27 +01:00
public long openIterator(Arena arena,
long transactionId,
2023-12-06 00:43:35 +01:00
long columnId,
2023-12-09 01:02:08 +01:00
@NotNull MemorySegment @NotNull [] startKeysInclusive,
2023-12-06 00:43:35 +01:00
@Nullable MemorySegment[] endKeysExclusive,
boolean reverse,
long timeoutMs) throws RocksDBException {
2023-12-08 23:50:27 +01:00
return db.openIterator(arena, transactionId, columnId, startKeysInclusive, endKeysExclusive, reverse, timeoutMs);
2023-12-06 00:43:35 +01:00
}
@Override
public void closeIterator(long iteratorId) throws RocksDBException {
db.closeIterator(iteratorId);
}
@Override
2023-12-09 01:02:08 +01:00
public void seekTo(Arena arena, long iterationId, MemorySegment @NotNull [] keys) throws RocksDBException {
2023-12-08 23:50:27 +01:00
db.seekTo(arena, iterationId, keys);
2023-12-06 00:43:35 +01:00
}
@Override
2023-12-08 23:50:27 +01:00
public <T> T subsequent(Arena arena,
long iterationId,
2023-12-06 00:43:35 +01:00
long skipCount,
long takeCount,
@NotNull RequestType.RequestIterate<? super MemorySegment, T> requestType) throws RocksDBException {
return db.subsequent(arena, iterationId, skipCount, takeCount, requestType);
2023-12-06 00:43:35 +01:00
}
}