Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#645] improvement(core): Refine some code logic in `RocksDB` (#644)

Qi Yub786e3abad39
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/kv/KvBackend.java b/core/src/main/java/com/datastrato/gravitino/storage/kv/KvBackend.java
index 6fd1c79924..6819bcf588 100644
--- a/core/src/main/java/com/datastrato/gravitino/storage/kv/KvBackend.java
+++ b/core/src/main/java/com/datastrato/gravitino/storage/kv/KvBackend.java
@@ -51,9 +51,7 @@ public interface KvBackend extends Closeable {
* @return True if the key-value pair was successfully deleted, false if the key was not found.
* @throws IOException If an I/O exception occurs during deletion.
*/
- default boolean delete(byte[] key) throws IOException {
- return false;
- }
+ boolean delete(byte[] key) throws IOException;
/**
* Delete the key-value pair associated with the given {@link KvRangeScan}
@@ -76,6 +74,9 @@ public interface KvBackend extends Closeable {
/**
* Executes a transactional operation on the KV backend.
*
+ * <p>NOTE: Some backends may not support transactions, in which case this method should throw an
+ * UnsupportedOperationException.
+ *
* @param executable The executable operation to perform transactionally.
* @param <R> The type of the result.
* @param <E> The type of exception that the executable may throw.
@@ -83,6 +84,8 @@ public interface KvBackend extends Closeable {
* @throws E If the executable throws an exception.
* @throws IOException If an I/O exception occurs during the transaction.
*/
- <R, E extends Exception> R executeInTransaction(Executable<R, E> executable)
- throws E, IOException;
+ default <R, E extends Exception> R executeInTransaction(Executable<R, E> executable)
+ throws E, IOException {
+ throw new UnsupportedOperationException("Transaction not supported");
+ }
}
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/kv/KvRangeScan.java b/core/src/main/java/com/datastrato/gravitino/storage/kv/KvRangeScan.java
index c2c9a985a3..61dbffddf6 100644
--- a/core/src/main/java/com/datastrato/gravitino/storage/kv/KvRangeScan.java
+++ b/core/src/main/java/com/datastrato/gravitino/storage/kv/KvRangeScan.java
@@ -5,6 +5,8 @@
package com.datastrato.gravitino.storage.kv;
+import com.datastrato.gravitino.utils.Bytes;
+import com.google.common.base.Preconditions;
import lombok.Builder;
import lombok.Data;
@@ -28,9 +30,18 @@ public class KvRangeScan {
* @param endInclusive True if the end key is inclusive, false otherwise.
* @param limit The maximum number of results to retrieve.
*/
- @Builder
public KvRangeScan(
byte[] start, byte[] end, boolean startInclusive, boolean endInclusive, int limit) {
+ Preconditions.checkArgument(start != null, "start cannot be null");
+ Preconditions.checkArgument(end != null, "start cannot be null");
+ Preconditions.checkArgument(
+ Bytes.wrap(start).compareTo(end) <= 0, "start must be less than or equal to end");
+
+ if (limit == 0) {
+ limit = Integer.MAX_VALUE;
+ }
+ Preconditions.checkArgument(limit > 0, "limit must be greater than 0");
+
this.start = start;
this.end = end;
this.startInclusive = startInclusive;
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/kv/RocksDBKvBackend.java b/core/src/main/java/com/datastrato/gravitino/storage/kv/RocksDBKvBackend.java
index 61c177ee56..9274dd92dd 100644
--- a/core/src/main/java/com/datastrato/gravitino/storage/kv/RocksDBKvBackend.java
+++ b/core/src/main/java/com/datastrato/gravitino/storage/kv/RocksDBKvBackend.java
@@ -93,7 +93,7 @@ public class RocksDBKvBackend implements KvBackend {
handlePutWithTransaction(key, value, overwrite, tx);
} catch (EntityAlreadyExistsException e) {
throw e;
- } catch (Throwable e) {
+ } catch (Exception e) {
throw new IOException(e);
}
}