Partial commit

This commit is contained in:
Andrea Cavalli 2024-10-18 18:03:17 +02:00
parent 397b9e0353
commit 9e06f9b9c2
13 changed files with 619 additions and 491 deletions

View File

@ -151,6 +151,11 @@
<artifactId>reactive-streams</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>3.6.4</version>
</dependency>
<dependency>
<groupId>org.lz4</groupId>

View File

@ -30,8 +30,9 @@ module rockserver.core {
requires io.netty.transport.classes.epoll;
requires org.reactivestreams;
requires io.netty.transport.unix.common;
requires reactor.core;
exports it.cavallium.rockserver.core.client;
exports it.cavallium.rockserver.core.client;
exports it.cavallium.rockserver.core.common;
exports it.cavallium.rockserver.core.config;
opens it.cavallium.rockserver.core.resources;

View File

@ -15,9 +15,14 @@ import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Stream;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
public class EmbeddedConnection extends BaseConnection implements RocksDBAPI {
@ -83,17 +88,18 @@ public class EmbeddedConnection extends BaseConnection implements RocksDBAPI {
}
@Override
public <R> R requestSync(RocksDBAPICommand<R> req) {
public <R, RS> RS requestSync(RocksDBAPICommand<R, RS, ?> req) {
return req.handleSync(this);
}
@Override
public <R> CompletableFuture<R> requestAsync(RocksDBAPICommand<R> req) {
if (req instanceof RocksDBAPICommand.PutBatch putBatch) {
//noinspection unchecked
return (CompletableFuture<R>) this.putBatchAsync(putBatch.columnId(), putBatch.batchPublisher(), putBatch.mode());
}
return CompletableFuture.supplyAsync(() -> req.handleSync(this), exeuctor);
@SuppressWarnings("unchecked")
@Override
public <R, RS, RA> RA requestAsync(RocksDBAPICommand<R, RS, RA> req) {
return (RA) switch (req) {
case RocksDBAPICommand.RocksDBAPICommandSingle.PutBatch putBatch -> this.putBatchAsync(putBatch.columnId(), putBatch.batchPublisher(), putBatch.mode());
case RocksDBAPICommand.RocksDBAPICommandSingle<?> _ -> CompletableFuture.supplyAsync(() -> req.handleSync(this), exeuctor);
case RocksDBAPICommand.RocksDBAPICommandStream<?> _ -> throw RocksDBException.of(RocksDBException.RocksDBErrorType.NOT_IMPLEMENTED, "The request of type " + req.getClass().getName() + " is not implemented in class " + this.getClass().getName());
};
}
@Override
@ -170,7 +176,7 @@ public class EmbeddedConnection extends BaseConnection implements RocksDBAPI {
}
@Override
public <T> T getRange(Arena arena, long transactionId, long columnId, @Nullable Keys startKeysInclusive, @Nullable Keys endKeysExclusive, boolean reverse, RequestType.@NotNull RequestGetRange<? super KV, T> requestType, long timeoutMs) throws RocksDBException {
return db.getRange(arena, transactionId, columnId, startKeysInclusive, endKeysExclusive, reverse, requestType, timeoutMs);
public <T> T reduceRange(Arena arena, long transactionId, long columnId, @Nullable Keys startKeysInclusive, @Nullable Keys endKeysExclusive, boolean reverse, RequestType.@NotNull RequestGetRange<? super KV, T> requestType, long timeoutMs) throws RocksDBException {
return db.reduceRange(arena, transactionId, columnId, startKeysInclusive, endKeysExclusive, reverse, requestType, timeoutMs);
}
}

View File

@ -61,6 +61,7 @@ import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;
import static it.cavallium.rockserver.core.common.Utils.toMemorySegment;
@ -133,12 +134,19 @@ public class GrpcConnection extends BaseConnection implements RocksDBAPI {
return this;
}
@Override
public <R> R requestSync(RocksDBAPICommand<R> req) {
var asyncResponse = req.handleAsync(this);
return asyncResponse
.toCompletableFuture()
.join();
@SuppressWarnings("unchecked")
@Override
public <R, RS, RA> RS requestSync(RocksDBAPICommand<R, RS, RA> req) {
return (RS) switch (req) {
case RocksDBAPICommand.RocksDBAPICommandSingle<?> _ -> {
var asyncResponse = (CompletableFuture<R>) req.handleAsync(this);
yield asyncResponse.join();
}
case RocksDBAPICommand.RocksDBAPICommandStream<?> _ -> {
var asyncResponse = (Publisher<R>) req.handleAsync(this);
yield Flux.from(asyncResponse).toStream();
}
};
}
@Override
@ -508,7 +516,7 @@ public class GrpcConnection extends BaseConnection implements RocksDBAPI {
@SuppressWarnings("unchecked")
@Override
public <T> CompletableFuture<T> getRangeAsync(Arena arena, long transactionId, long columnId, @NotNull Keys startKeysInclusive, @Nullable Keys endKeysExclusive, boolean reverse, RequestType.RequestGetRange<? super it.cavallium.rockserver.core.common.KV, T> requestType, long timeoutMs) throws RocksDBException {
public <T> CompletableFuture<T> reduceRangeAsync(Arena arena, long transactionId, long columnId, @NotNull Keys startKeysInclusive, @Nullable Keys endKeysExclusive, boolean reverse, RequestType.RequestGetRange<? super it.cavallium.rockserver.core.common.KV, T> requestType, long timeoutMs) throws RocksDBException {
var request = GetRangeRequest.newBuilder()
.setTransactionId(transactionId)
.setColumnId(columnId)
@ -526,6 +534,11 @@ public class GrpcConnection extends BaseConnection implements RocksDBAPI {
};
}
@Override
public <T> Publisher<T> getRangeStream(Arena arena, long transactionId, long columnId, @Nullable Keys startKeysInclusive, @Nullable Keys endKeysExclusive, boolean reverse, RequestType.RequestGetRange<? super it.cavallium.rockserver.core.common.KV, T> requestType, long timeoutMs) throws RocksDBException {
// todo: implement
}
private static it.cavallium.rockserver.core.common.Delta<MemorySegment> mapDelta(Delta x) {
return new it.cavallium.rockserver.core.common.Delta<>(
x.hasPrevious() ? mapByteString(x.getPrevious()) : null,

View File

@ -7,432 +7,491 @@ import java.lang.foreign.MemorySegment;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.stream.Stream;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.reactivestreams.Publisher;
public sealed interface RocksDBAPICommand<R> {
public sealed interface RocksDBAPICommand<RESULT_ITEM_TYPE, SYNC_RESULT, ASYNC_RESULT> {
R handleSync(RocksDBSyncAPI api);
CompletionStage<R> handleAsync(RocksDBAsyncAPI api);
SYNC_RESULT handleSync(RocksDBSyncAPI api);
ASYNC_RESULT handleAsync(RocksDBAsyncAPI api);
/**
* Open a transaction
* <p>
* Returns the transaction id
*
* @param timeoutMs timeout in milliseconds
*/
record OpenTransaction(long timeoutMs) implements RocksDBAPICommand<Long> {
sealed interface RocksDBAPICommandSingle<R> extends RocksDBAPICommand<R, R, CompletableFuture<R>> {
@Override
public Long handleSync(RocksDBSyncAPI api) {
return api.openTransaction(timeoutMs);
}
@Override
public CompletionStage<Long> handleAsync(RocksDBAsyncAPI api) {
return api.openTransactionAsync(timeoutMs);
}
/**
* Open a transaction
* <p>
* Returns the transaction id
*
* @param timeoutMs timeout in milliseconds
*/
record OpenTransaction(long timeoutMs) implements RocksDBAPICommandSingle<Long> {
}
/**
* Close a transaction
* <p>
* Returns true if committed, if false, you should try again
*
* @param transactionId transaction id to close
* @param commit true to commit the transaction, false to rollback it
*/
record CloseTransaction(long transactionId, boolean commit) implements RocksDBAPICommand<Boolean> {
@Override
public Boolean handleSync(RocksDBSyncAPI api) {
return api.closeTransaction(transactionId, commit);
}
@Override
public CompletionStage<Boolean> handleAsync(RocksDBAsyncAPI api) {
return api.closeTransactionAsync(transactionId, commit);
}
}
/**
* Close a failed update, discarding all changes
*
* @param updateId update id to close
*/
record CloseFailedUpdate(long updateId) implements RocksDBAPICommand<Void> {
@Override
public Void handleSync(RocksDBSyncAPI api) {
api.closeFailedUpdate(updateId);
return null;
}
@Override
public CompletionStage<Void> handleAsync(RocksDBAsyncAPI api) {
return api.closeFailedUpdateAsync(updateId);
}
}
/**
* Create a column
* <p>
* Returns the column id
*
* @param name column name
* @param schema column key-value schema
*/
record CreateColumn(String name, @NotNull ColumnSchema schema) implements RocksDBAPICommand<Long> {
@Override
public Long handleSync(RocksDBSyncAPI api) {
return api.createColumn(name, schema);
}
@Override
public CompletionStage<Long> handleAsync(RocksDBAsyncAPI api) {
return api.createColumnAsync(name, schema);
}
}
/**
* Delete a column
* @param columnId column id
*/
record DeleteColumn(long columnId) implements RocksDBAPICommand<Void> {
@Override
public Void handleSync(RocksDBSyncAPI api) {
api.deleteColumn(columnId);
return null;
}
@Override
public CompletionStage<Void> handleAsync(RocksDBAsyncAPI api) {
return api.deleteColumnAsync(columnId);
}
}
/**
* Get column id by name
* <p>
* Returns the column id
*
* @param name column name
*/
record GetColumnId(@NotNull String name) implements RocksDBAPICommand<Long> {
@Override
public Long handleSync(RocksDBSyncAPI api) {
return api.getColumnId(name);
}
@Override
public CompletionStage<Long> handleAsync(RocksDBAsyncAPI api) {
return api.getColumnIdAsync(name);
}
}
/**
* Put an element into the specified position
* @param arena arena
* @param transactionOrUpdateId transaction id, update id, or 0
* @param columnId column id
* @param keys column keys, or empty array if not needed
* @param value value, or null if not needed
* @param requestType the request type determines which type of data will be returned.
*/
record Put<T>(Arena arena,
long transactionOrUpdateId,
long columnId,
Keys keys,
@NotNull MemorySegment value,
RequestPut<? super MemorySegment, T> requestType) implements RocksDBAPICommand<T> {
@Override
public T handleSync(RocksDBSyncAPI api) {
return api.put(arena, transactionOrUpdateId, columnId, keys, value, requestType);
}
@Override
public CompletionStage<T> handleAsync(RocksDBAsyncAPI api) {
return api.putAsync(arena, transactionOrUpdateId, columnId, keys, value, requestType);
}
@Override
public String toString() {
var sb = new StringBuilder("PUT");
if (transactionOrUpdateId != 0) {
sb.append(" tx:").append(transactionOrUpdateId);
@Override
public Long handleSync(RocksDBSyncAPI api) {
return api.openTransaction(timeoutMs);
}
sb.append(" column:").append(columnId);
sb.append(" keys:").append(keys);
sb.append(" value:").append(Utils.toPrettyString(value));
sb.append(" expected:").append(requestType.getRequestTypeId());
return sb.toString();
}
}
/**
* Put multiple elements into the specified positions
* @param arena arena
* @param transactionOrUpdateId transaction id, update id, or 0
* @param columnId column id
* @param keys multiple lists of column keys
* @param values multiple values, or null if not needed
* @param requestType the request type determines which type of data will be returned.
*/
record PutMulti<T>(Arena arena, long transactionOrUpdateId, long columnId,
@NotNull List<Keys> keys,
@NotNull List<@NotNull MemorySegment> values,
RequestPut<? super MemorySegment, T> requestType) implements RocksDBAPICommand<List<T>> {
@Override
public List<T> handleSync(RocksDBSyncAPI api) {
return api.putMulti(arena, transactionOrUpdateId, columnId, keys, values, requestType);
}
@Override
public CompletionStage<List<T>> handleAsync(RocksDBAsyncAPI api) {
return api.putMultiAsync(arena, transactionOrUpdateId, columnId, keys, values, requestType);
}
@Override
public String toString() {
var sb = new StringBuilder("PUT_MULTI");
if (transactionOrUpdateId != 0) {
sb.append(" tx:").append(transactionOrUpdateId);
@Override
public CompletableFuture<Long> handleAsync(RocksDBAsyncAPI api) {
return api.openTransactionAsync(timeoutMs);
}
sb.append(" column:").append(columnId);
sb.append(" expected:").append(requestType.getRequestTypeId());
sb.append(" multi:[");
for (int i = 0; i < keys.size(); i++) {
if (i > 0) sb.append(",");
sb.append(" keys:").append(keys.get(i));
sb.append(" value:").append(Utils.toPrettyString(values.get(i)));
}
/**
* Close a transaction
* <p>
* Returns true if committed, if false, you should try again
*
* @param transactionId transaction id to close
* @param commit true to commit the transaction, false to rollback it
*/
record CloseTransaction(long transactionId, boolean commit) implements RocksDBAPICommandSingle<Boolean> {
@Override
public Boolean handleSync(RocksDBSyncAPI api) {
return api.closeTransaction(transactionId, commit);
}
sb.append("]");
return sb.toString();
}
}
/**
* Put multiple elements into the specified positions
* @param columnId column id
* @param batchPublisher publisher of batches of keys and values
* @param mode put batch mode
*/
record PutBatch(long columnId,
@NotNull org.reactivestreams.Publisher<@NotNull KVBatch> batchPublisher,
@NotNull PutBatchMode mode) implements RocksDBAPICommand<Void> {
@Override
public Void handleSync(RocksDBSyncAPI api) {
api.putBatch(columnId, batchPublisher, mode);
return null;
}
@Override
public CompletionStage<Void> handleAsync(RocksDBAsyncAPI api) {
return api.putBatchAsync(columnId, batchPublisher, mode);
}
@Override
public String toString() {
var sb = new StringBuilder("PUT_BATCH");
sb.append(" column:").append(columnId);
sb.append(" mode:").append(mode);
sb.append(" batch:[...]");
return sb.toString();
}
}
/**
* Get an element from the specified position
* @param arena arena
* @param transactionOrUpdateId transaction id, update id for retry operations, or 0
* @param columnId column id
* @param keys column keys, or empty array if not needed
* @param requestType the request type determines which type of data will be returned.
*/
record Get<T>(Arena arena,
long transactionOrUpdateId,
long columnId,
Keys keys,
RequestGet<? super MemorySegment, T> requestType) implements RocksDBAPICommand<T> {
@Override
public T handleSync(RocksDBSyncAPI api) {
return api.get(arena, transactionOrUpdateId, columnId, keys, requestType);
}
@Override
public CompletionStage<T> handleAsync(RocksDBAsyncAPI api) {
return api.getAsync(arena, transactionOrUpdateId, columnId, keys, requestType);
}
@Override
public String toString() {
var sb = new StringBuilder("GET");
if (transactionOrUpdateId != 0) {
sb.append(" tx:").append(transactionOrUpdateId);
@Override
public CompletableFuture<Boolean> handleAsync(RocksDBAsyncAPI api) {
return api.closeTransactionAsync(transactionId, commit);
}
sb.append(" column:").append(columnId);
sb.append(" keys:").append(keys);
sb.append(" expected:").append(requestType.getRequestTypeId());
return sb.toString();
}
/**
* Close a failed update, discarding all changes
*
* @param updateId update id to close
*/
record CloseFailedUpdate(long updateId) implements RocksDBAPICommandSingle<Void> {
@Override
public Void handleSync(RocksDBSyncAPI api) {
api.closeFailedUpdate(updateId);
return null;
}
@Override
public CompletableFuture<Void> handleAsync(RocksDBAsyncAPI api) {
return api.closeFailedUpdateAsync(updateId);
}
}
/**
* Create a column
* <p>
* Returns the column id
*
* @param name column name
* @param schema column key-value schema
*/
record CreateColumn(String name, @NotNull ColumnSchema schema) implements RocksDBAPICommandSingle<Long> {
@Override
public Long handleSync(RocksDBSyncAPI api) {
return api.createColumn(name, schema);
}
@Override
public CompletableFuture<Long> handleAsync(RocksDBAsyncAPI api) {
return api.createColumnAsync(name, schema);
}
}
/**
* Delete a column
* @param columnId column id
*/
record DeleteColumn(long columnId) implements RocksDBAPICommandSingle<Void> {
@Override
public Void handleSync(RocksDBSyncAPI api) {
api.deleteColumn(columnId);
return null;
}
@Override
public CompletableFuture<Void> handleAsync(RocksDBAsyncAPI api) {
return api.deleteColumnAsync(columnId);
}
}
/**
* Get column id by name
* <p>
* Returns the column id
*
* @param name column name
*/
record GetColumnId(@NotNull String name) implements RocksDBAPICommandSingle<Long> {
@Override
public Long handleSync(RocksDBSyncAPI api) {
return api.getColumnId(name);
}
@Override
public CompletableFuture<Long> handleAsync(RocksDBAsyncAPI api) {
return api.getColumnIdAsync(name);
}
}
/**
* Put an element into the specified position
* @param arena arena
* @param transactionOrUpdateId transaction id, update id, or 0
* @param columnId column id
* @param keys column keys, or empty array if not needed
* @param value value, or null if not needed
* @param requestType the request type determines which type of data will be returned.
*/
record Put<T>(Arena arena,
long transactionOrUpdateId,
long columnId,
Keys keys,
@NotNull MemorySegment value,
RequestPut<? super MemorySegment, T> requestType) implements RocksDBAPICommandSingle<T> {
@Override
public T handleSync(RocksDBSyncAPI api) {
return api.put(arena, transactionOrUpdateId, columnId, keys, value, requestType);
}
@Override
public CompletableFuture<T> handleAsync(RocksDBAsyncAPI api) {
return api.putAsync(arena, transactionOrUpdateId, columnId, keys, value, requestType);
}
@Override
public String toString() {
var sb = new StringBuilder("PUT");
if (transactionOrUpdateId != 0) {
sb.append(" tx:").append(transactionOrUpdateId);
}
sb.append(" column:").append(columnId);
sb.append(" keys:").append(keys);
sb.append(" value:").append(Utils.toPrettyString(value));
sb.append(" expected:").append(requestType.getRequestTypeId());
return sb.toString();
}
}
/**
* Put multiple elements into the specified positions
* @param arena arena
* @param transactionOrUpdateId transaction id, update id, or 0
* @param columnId column id
* @param keys multiple lists of column keys
* @param values multiple values, or null if not needed
* @param requestType the request type determines which type of data will be returned.
*/
record PutMulti<T>(Arena arena, long transactionOrUpdateId, long columnId,
@NotNull List<Keys> keys,
@NotNull List<@NotNull MemorySegment> values,
RequestPut<? super MemorySegment, T> requestType) implements RocksDBAPICommandSingle<List<T>> {
@Override
public List<T> handleSync(RocksDBSyncAPI api) {
return api.putMulti(arena, transactionOrUpdateId, columnId, keys, values, requestType);
}
@Override
public CompletableFuture<List<T>> handleAsync(RocksDBAsyncAPI api) {
return api.putMultiAsync(arena, transactionOrUpdateId, columnId, keys, values, requestType);
}
@Override
public String toString() {
var sb = new StringBuilder("PUT_MULTI");
if (transactionOrUpdateId != 0) {
sb.append(" tx:").append(transactionOrUpdateId);
}
sb.append(" column:").append(columnId);
sb.append(" expected:").append(requestType.getRequestTypeId());
sb.append(" multi:[");
for (int i = 0; i < keys.size(); i++) {
if (i > 0) sb.append(",");
sb.append(" keys:").append(keys.get(i));
sb.append(" value:").append(Utils.toPrettyString(values.get(i)));
}
sb.append("]");
return sb.toString();
}
}
/**
* Put multiple elements into the specified positions
* @param columnId column id
* @param batchPublisher publisher of batches of keys and values
* @param mode put batch mode
*/
record PutBatch(long columnId,
@NotNull org.reactivestreams.Publisher<@NotNull KVBatch> batchPublisher,
@NotNull PutBatchMode mode) implements RocksDBAPICommandSingle<Void> {
@Override
public Void handleSync(RocksDBSyncAPI api) {
api.putBatch(columnId, batchPublisher, mode);
return null;
}
@Override
public CompletableFuture<Void> handleAsync(RocksDBAsyncAPI api) {
return api.putBatchAsync(columnId, batchPublisher, mode);
}
@Override
public String toString() {
var sb = new StringBuilder("PUT_BATCH");
sb.append(" column:").append(columnId);
sb.append(" mode:").append(mode);
sb.append(" batch:[...]");
return sb.toString();
}
}
/**
* Get an element from the specified position
* @param arena arena
* @param transactionOrUpdateId transaction id, update id for retry operations, or 0
* @param columnId column id
* @param keys column keys, or empty array if not needed
* @param requestType the request type determines which type of data will be returned.
*/
record Get<T>(Arena arena,
long transactionOrUpdateId,
long columnId,
Keys keys,
RequestGet<? super MemorySegment, T> requestType) implements RocksDBAPICommandSingle<T> {
@Override
public T handleSync(RocksDBSyncAPI api) {
return api.get(arena, transactionOrUpdateId, columnId, keys, requestType);
}
@Override
public CompletableFuture<T> handleAsync(RocksDBAsyncAPI api) {
return api.getAsync(arena, transactionOrUpdateId, columnId, keys, requestType);
}
@Override
public String toString() {
var sb = new StringBuilder("GET");
if (transactionOrUpdateId != 0) {
sb.append(" tx:").append(transactionOrUpdateId);
}
sb.append(" column:").append(columnId);
sb.append(" keys:").append(keys);
sb.append(" expected:").append(requestType.getRequestTypeId());
return sb.toString();
}
}
/**
* Open an iterator
* <p>
* Returns the iterator id
*
* @param arena arena
* @param transactionId transaction id, or 0
* @param columnId column id
* @param startKeysInclusive start keys, inclusive. [] means "the beginning"
* @param endKeysExclusive end keys, exclusive. Null means "the end"
* @param reverse if true, seek in reverse direction
* @param timeoutMs timeout in milliseconds
*/
record OpenIterator(Arena arena,
long transactionId,
long columnId,
Keys startKeysInclusive,
@Nullable Keys endKeysExclusive,
boolean reverse,
long timeoutMs) implements RocksDBAPICommandSingle<Long> {
@Override
public Long handleSync(RocksDBSyncAPI api) {
return api.openIterator(arena, transactionId, columnId, startKeysInclusive, endKeysExclusive, reverse, timeoutMs);
}
@Override
public CompletableFuture<Long> handleAsync(RocksDBAsyncAPI api) {
return api.openIteratorAsync(arena,
transactionId,
columnId,
startKeysInclusive,
endKeysExclusive,
reverse,
timeoutMs
);
}
}
/**
* Close an iterator
* @param iteratorId iterator id
*/
record CloseIterator(long iteratorId) implements RocksDBAPICommandSingle<Void> {
@Override
public Void handleSync(RocksDBSyncAPI api) {
api.closeIterator(iteratorId);
return null;
}
@Override
public CompletableFuture<Void> handleAsync(RocksDBAsyncAPI api) {
return api.closeIteratorAsync(iteratorId);
}
}
/**
* Seek to the specific element during an iteration, or the subsequent one if not found
* @param arena arena
* @param iterationId iteration id
* @param keys keys, inclusive. [] means "the beginning"
*/
record SeekTo(Arena arena, long iterationId, Keys keys) implements RocksDBAPICommandSingle<Void> {
@Override
public Void handleSync(RocksDBSyncAPI api) {
api.seekTo(arena, iterationId, keys);
return null;
}
@Override
public CompletableFuture<Void> handleAsync(RocksDBAsyncAPI api) {
return api.seekToAsync(arena, iterationId, keys);
}
}
/**
* Get the subsequent element during an iteration
* @param arena arena
* @param iterationId iteration id
* @param skipCount number of elements to skip
* @param takeCount number of elements to take
* @param requestType the request type determines which type of data will be returned.
*/
record Subsequent<T>(Arena arena,
long iterationId,
long skipCount,
long takeCount,
@NotNull RequestType.RequestIterate<? super MemorySegment, T> requestType)
implements RocksDBAPICommandSingle<T> {
@Override
public T handleSync(RocksDBSyncAPI api) {
return api.subsequent(arena, iterationId, skipCount, takeCount, requestType);
}
@Override
public CompletableFuture<T> handleAsync(RocksDBAsyncAPI api) {
return api.subsequentAsync(arena, iterationId, skipCount, takeCount, requestType);
}
}
/**
* Reduce values in a range
* <p>
* Returns the result
*
* @param arena arena
* @param transactionId transaction id, or 0
* @param columnId column id
* @param startKeysInclusive start keys, inclusive. [] means "the beginning"
* @param endKeysExclusive end keys, exclusive. Null means "the end"
* @param reverse if true, seek in reverse direction
* @param requestType the request type determines which type of data will be returned.
* @param timeoutMs timeout in milliseconds
*/
record ReduceRange<T>(Arena arena,
long transactionId,
long columnId,
@Nullable Keys startKeysInclusive,
@Nullable Keys endKeysExclusive,
boolean reverse,
RequestType.RequestGetRange<? super KV, T> requestType,
long timeoutMs) implements RocksDBAPICommandSingle<T> {
@Override
public T handleSync(RocksDBSyncAPI api) {
return api.reduceRange(arena,
transactionId,
columnId,
startKeysInclusive,
endKeysExclusive,
reverse,
requestType,
timeoutMs
);
}
@Override
public CompletableFuture<T> handleAsync(RocksDBAsyncAPI api) {
return api.reduceRangeAsync(arena,
transactionId,
columnId,
startKeysInclusive,
endKeysExclusive,
reverse,
requestType,
timeoutMs
);
}
}
}
/**
* Open an iterator
* <p>
* Returns the iterator id
*
* @param arena arena
* @param transactionId transaction id, or 0
* @param columnId column id
* @param startKeysInclusive start keys, inclusive. [] means "the beginning"
* @param endKeysExclusive end keys, exclusive. Null means "the end"
* @param reverse if true, seek in reverse direction
* @param timeoutMs timeout in milliseconds
*/
record OpenIterator(Arena arena,
long transactionId,
long columnId,
Keys startKeysInclusive,
@Nullable Keys endKeysExclusive,
boolean reverse,
long timeoutMs) implements RocksDBAPICommand<Long> {
sealed interface RocksDBAPICommandStream<R> extends RocksDBAPICommand<R, Stream<R>, Publisher<R>> {
/**
* Get some values in a range
* <p>
* Returns the result
*
* @param arena arena
* @param transactionId transaction id, or 0
* @param columnId column id
* @param startKeysInclusive start keys, inclusive. [] means "the beginning"
* @param endKeysExclusive end keys, exclusive. Null means "the end"
* @param reverse if true, seek in reverse direction
* @param requestType the request type determines which type of data will be returned.
* @param timeoutMs timeout in milliseconds
*/
record GetRange<T>(Arena arena,
long transactionId,
long columnId,
@Nullable Keys startKeysInclusive,
@Nullable Keys endKeysExclusive,
boolean reverse,
RequestType.RequestGetRange<? super KV, T> requestType,
long timeoutMs) implements RocksDBAPICommandStream<T> {
@Override
public Stream<T> handleSync(RocksDBSyncAPI api) {
return api.getRange(arena,
transactionId,
columnId,
startKeysInclusive,
endKeysExclusive,
reverse,
requestType,
timeoutMs
);
}
@Override
public Publisher<T> handleAsync(RocksDBAsyncAPI api) {
return api.getRangeAsync(arena,
transactionId,
columnId,
startKeysInclusive,
endKeysExclusive,
reverse,
requestType,
timeoutMs
);
}
@Override
public Long handleSync(RocksDBSyncAPI api) {
return api.openIterator(arena, transactionId, columnId, startKeysInclusive, endKeysExclusive, reverse, timeoutMs);
}
@Override
public CompletionStage<Long> handleAsync(RocksDBAsyncAPI api) {
return api.openIteratorAsync(arena,
transactionId,
columnId,
startKeysInclusive,
endKeysExclusive,
reverse,
timeoutMs
);
}
}
/**
* Close an iterator
* @param iteratorId iterator id
*/
record CloseIterator(long iteratorId) implements RocksDBAPICommand<Void> {
@Override
public Void handleSync(RocksDBSyncAPI api) {
api.closeIterator(iteratorId);
return null;
}
@Override
public CompletionStage<Void> handleAsync(RocksDBAsyncAPI api) {
return api.closeIteratorAsync(iteratorId);
}
}
/**
* Seek to the specific element during an iteration, or the subsequent one if not found
* @param arena arena
* @param iterationId iteration id
* @param keys keys, inclusive. [] means "the beginning"
*/
record SeekTo(Arena arena, long iterationId, Keys keys) implements
RocksDBAPICommand<Void> {
@Override
public Void handleSync(RocksDBSyncAPI api) {
api.seekTo(arena, iterationId, keys);
return null;
}
@Override
public CompletionStage<Void> handleAsync(RocksDBAsyncAPI api) {
return api.seekToAsync(arena, iterationId, keys);
}
}
/**
* Get the subsequent element during an iteration
* @param arena arena
* @param iterationId iteration id
* @param skipCount number of elements to skip
* @param takeCount number of elements to take
* @param requestType the request type determines which type of data will be returned.
*/
record Subsequent<T>(Arena arena,
long iterationId,
long skipCount,
long takeCount,
@NotNull RequestType.RequestIterate<? super MemorySegment, T> requestType)
implements RocksDBAPICommand<T> {
@Override
public T handleSync(RocksDBSyncAPI api) {
return api.subsequent(arena, iterationId, skipCount, takeCount, requestType);
}
@Override
public CompletionStage<T> handleAsync(RocksDBAsyncAPI api) {
return api.subsequentAsync(arena, iterationId, skipCount, takeCount, requestType);
}
}
/**
* Get some values in a range
* <p>
* Returns the result
*
* @param arena arena
* @param transactionId transaction id, or 0
* @param columnId column id
* @param startKeysInclusive start keys, inclusive. [] means "the beginning"
* @param endKeysExclusive end keys, exclusive. Null means "the end"
* @param reverse if true, seek in reverse direction
* @param requestType the request type determines which type of data will be returned.
* @param timeoutMs timeout in milliseconds
*/
record GetRange<T>(Arena arena,
long transactionId,
long columnId,
@Nullable Keys startKeysInclusive,
@Nullable Keys endKeysExclusive,
boolean reverse,
RequestType.RequestGetRange<? super KV, T> requestType,
long timeoutMs) implements RocksDBAPICommand<T> {
@Override
public T handleSync(RocksDBSyncAPI api) {
return api.getRange(arena,
transactionId,
columnId,
startKeysInclusive,
endKeysExclusive,
reverse,
requestType,
timeoutMs
);
}
@Override
public CompletableFuture<T> handleAsync(RocksDBAsyncAPI api) {
return api.getRangeAsync(arena,
transactionId,
columnId,
startKeysInclusive,
endKeysExclusive,
reverse,
requestType,
timeoutMs
);
}
}
}

View File

@ -2,20 +2,21 @@ package it.cavallium.rockserver.core.common;
import it.cavallium.rockserver.core.common.RequestType.RequestGet;
import it.cavallium.rockserver.core.common.RequestType.RequestPut;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.CloseFailedUpdate;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.CloseIterator;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.CloseTransaction;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.CreateColumn;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.DeleteColumn;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.Get;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.GetColumnId;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.GetRange;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.OpenTransaction;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.Put;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.PutMulti;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.PutBatch;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.SeekTo;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.Subsequent;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.CloseFailedUpdate;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.CloseIterator;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.CloseTransaction;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.CreateColumn;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.DeleteColumn;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.Get;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.GetColumnId;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.ReduceRange;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.OpenIterator;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.OpenTransaction;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.Put;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.PutMulti;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.PutBatch;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.SeekTo;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.Subsequent;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.util.List;
@ -100,7 +101,7 @@ public interface RocksDBAsyncAPI extends RocksDBAsyncAPIRequestHandler {
@Nullable Keys endKeysExclusive,
boolean reverse,
long timeoutMs) throws RocksDBException {
return requestAsync(new RocksDBAPICommand.OpenIterator(arena,
return requestAsync(new OpenIterator(arena,
transactionId,
columnId,
startKeysInclusive,
@ -129,16 +130,16 @@ public interface RocksDBAsyncAPI extends RocksDBAsyncAPIRequestHandler {
return requestAsync(new Subsequent<>(arena, iterationId, skipCount, takeCount, requestType));
}
/** See: {@link GetRange}. */
default <T> CompletableFuture<T> getRangeAsync(Arena arena,
long transactionId,
long columnId,
@Nullable Keys startKeysInclusive,
@Nullable Keys endKeysExclusive,
boolean reverse,
RequestType.RequestGetRange<? super KV, T> requestType,
long timeoutMs) throws RocksDBException {
return requestAsync(new RocksDBAPICommand.GetRange<>(arena,
/** See: {@link ReduceRange}. */
default <T> CompletableFuture<T> reduceRangeAsync(Arena arena,
long transactionId,
long columnId,
@Nullable Keys startKeysInclusive,
@Nullable Keys endKeysExclusive,
boolean reverse,
RequestType.RequestGetRange<? super KV, T> requestType,
long timeoutMs) throws RocksDBException {
return requestAsync(new ReduceRange<>(arena,
transactionId,
columnId,
startKeysInclusive,
@ -148,4 +149,17 @@ public interface RocksDBAsyncAPI extends RocksDBAsyncAPIRequestHandler {
timeoutMs
));
}
/** See: {@link GetRange}. */
default <T> Publisher<T> getRangeAsync(Arena arena,
long transactionId,
long columnId,
@Nullable Keys startKeysInclusive,
@Nullable Keys endKeysExclusive,
boolean reverse,
RequestType.RequestGetRange<? super KV, T> requestType,
long timeoutMs) throws RocksDBException {
throw RocksDBException.of(RocksDBException.RocksDBErrorType.NOT_IMPLEMENTED,
"GetRangeStream is not implemented");
}
}

View File

@ -1,10 +1,20 @@
package it.cavallium.rockserver.core.common;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import java.util.concurrent.CompletableFuture;
public interface RocksDBAsyncAPIRequestHandler {
default <R> CompletableFuture<R> requestAsync(RocksDBAPICommand<R> req) {
return CompletableFuture.failedFuture(new UnsupportedOperationException("Unsupported request type: " + req));
@SuppressWarnings("unchecked")
default <R, RS, RA> RA requestAsync(RocksDBAPICommand<R, RS, RA> req) {
return (RA) switch (req) {
case RocksDBAPICommand.RocksDBAPICommandStream<?> _ ->
(Publisher<R>) subscriber ->
subscriber.onError(new UnsupportedOperationException("Unsupported request type: " + req));
case RocksDBAPICommand.RocksDBAPICommandSingle<?> _ ->
CompletableFuture.<R>failedFuture(new UnsupportedOperationException("Unsupported request type: " + req));
};
}
}

View File

@ -2,24 +2,27 @@ package it.cavallium.rockserver.core.common;
import it.cavallium.rockserver.core.common.RequestType.RequestGet;
import it.cavallium.rockserver.core.common.RequestType.RequestPut;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.CloseFailedUpdate;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.CloseIterator;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.CloseTransaction;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.CreateColumn;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.DeleteColumn;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.Get;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.GetColumnId;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.GetRange;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.OpenIterator;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.OpenTransaction;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.Put;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.PutMulti;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.PutBatch;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.SeekTo;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.Subsequent;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.CloseFailedUpdate;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.CloseIterator;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.CloseTransaction;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.CreateColumn;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.DeleteColumn;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.Get;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.GetColumnId;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.ReduceRange;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.OpenIterator;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.OpenTransaction;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.Put;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.PutMulti;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.PutBatch;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.SeekTo;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandSingle.Subsequent;
import it.cavallium.rockserver.core.common.RocksDBAPICommand.RocksDBAPICommandStream.GetRange;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.util.List;
import java.util.stream.Stream;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -121,15 +124,27 @@ public interface RocksDBSyncAPI extends RocksDBSyncAPIRequestHandler {
return requestSync(new Subsequent<>(arena, iterationId, skipCount, takeCount, requestType));
}
/** See: {@link ReduceRange}. */
default <T> T reduceRange(Arena arena,
long transactionId,
long columnId,
@Nullable Keys startKeysInclusive,
@Nullable Keys endKeysExclusive,
boolean reverse,
@NotNull RequestType.RequestGetRange<? super KV, T> requestType,
long timeoutMs) throws RocksDBException {
return requestSync(new ReduceRange<>(arena, transactionId, columnId, startKeysInclusive, endKeysExclusive, reverse, requestType, timeoutMs));
}
/** See: {@link GetRange}. */
default <T> T getRange(Arena arena,
long transactionId,
long columnId,
@Nullable Keys startKeysInclusive,
@Nullable Keys endKeysExclusive,
boolean reverse,
@NotNull RequestType.RequestGetRange<? super KV, T> requestType,
long timeoutMs) throws RocksDBException {
default <T> Stream<T> getRange(Arena arena,
long transactionId,
long columnId,
@Nullable Keys startKeysInclusive,
@Nullable Keys endKeysExclusive,
boolean reverse,
@NotNull RequestType.RequestGetRange<? super KV, T> requestType,
long timeoutMs) throws RocksDBException {
return requestSync(new GetRange<>(arena, transactionId, columnId, startKeysInclusive, endKeysExclusive, reverse, requestType, timeoutMs));
}
}

View File

@ -2,7 +2,7 @@ package it.cavallium.rockserver.core.common;
public interface RocksDBSyncAPIRequestHandler {
default <R> R requestSync(RocksDBAPICommand<R> req) {
default <R, RS, RA> RS requestSync(RocksDBAPICommand<R, RS, RA> req) {
throw new UnsupportedOperationException("Unsupported request type: " + req);
}
}

View File

@ -949,14 +949,14 @@ public class EmbeddedDB implements RocksDBSyncAPI, Closeable {
@SuppressWarnings("unchecked")
@Override
public <T> T getRange(Arena arena,
long transactionId,
long columnId,
@Nullable Keys startKeysInclusive,
@Nullable Keys endKeysExclusive,
boolean reverse,
RequestType.@NotNull RequestGetRange<? super KV, T> requestType,
long timeoutMs) throws it.cavallium.rockserver.core.common.RocksDBException {
public <T> T reduceRange(Arena arena,
long transactionId,
long columnId,
@Nullable Keys startKeysInclusive,
@Nullable Keys endKeysExclusive,
boolean reverse,
RequestType.@NotNull RequestGetRange<? super KV, T> requestType,
long timeoutMs) throws it.cavallium.rockserver.core.common.RocksDBException {
ops.beginOp();
try {
var col = getColumn(columnId);

View File

@ -706,7 +706,7 @@ public class GrpcServer extends Server {
try {
try (var arena = Arena.ofConfined()) {
it.cavallium.rockserver.core.common.FirstAndLast<it.cavallium.rockserver.core.common.KV> firstAndLast
= api.getRange(arena,
= api.reduceRange(arena,
request.getTransactionId(),
request.getColumnId(),
mapKeys(arena, request.getStartKeysInclusiveCount(), request::getStartKeysInclusive),

View File

@ -125,4 +125,5 @@ service RocksDBService {
rpc subsequentExists(SubsequentRequest) returns (PreviousPresence);
rpc subsequentMultiGet(SubsequentRequest) returns (stream KV);
rpc getRangeFirstAndLast(GetRangeRequest) returns (FirstAndLast);
rpc getRangeStream(GetRangeRequest) returns (stream KV);
}

View File

@ -384,23 +384,27 @@ abstract class EmbeddedDBTest {
}
@Test
void getRangeFirstAndLast() {
void reduceRangeFirstAndLast() {
var firstKey = getKVSequenceFirst().keys();
var lastKey = getKVSequenceLast().keys();
var prevLastKV = getKVSequence().get(getKVSequence().size() - 2);
if (getSchemaVarKeys().isEmpty()) {
FirstAndLast<KV> firstAndLast = db.getRange(arena, 0, colId, firstKey, lastKey, false, RequestType.firstAndLast(), 1000);
FirstAndLast<KV> firstAndLast = db.reduceRange(arena, 0, colId, firstKey, lastKey, false, RequestType.firstAndLast(), 1000);
Assertions.assertNull(firstAndLast.first(), "First should be empty because the db is empty");
Assertions.assertNull(firstAndLast.last(), "Last should be empty because the db is empty");
fillSomeKeys();
firstAndLast = db.getRange(arena, 0, colId, firstKey, lastKey, false, RequestType.firstAndLast(), 1000);
firstAndLast = db.reduceRange(arena, 0, colId, firstKey, lastKey, false, RequestType.firstAndLast(), 1000);
Assertions.assertEquals(getKVSequenceFirst(), firstAndLast.first(), "First key mismatch");
Assertions.assertEquals(prevLastKV, firstAndLast.last(), "Last key mismatch");
firstAndLast = db.reduceRange(arena, 0, colId, firstKey, firstKey, false, RequestType.firstAndLast(), 1000);
Assertions.assertNull(firstAndLast.first(), "First should be empty because the range is empty");
Assertions.assertNull(firstAndLast.last(), "Last should be empty because the range is empty");
} else {
Assertions.assertThrowsExactly(RocksDBException.class, () -> {
db.getRange(arena, 0, colId, firstKey, lastKey, false, RequestType.firstAndLast(), 1000);
db.reduceRange(arena, 0, colId, firstKey, lastKey, false, RequestType.firstAndLast(), 1000);
});
}
}