Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<!-- AssertJ for testing -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.27.6</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/tidesdb/TidesDBIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public void seekToLast() throws TidesDBException {
*/
public void seek(byte[] key) throws TidesDBException {
checkNotFreed();
if (key == null) {
throw new IllegalArgumentException("Key cannot be null");
if (key == null || key.length == 0) {
throw new IllegalArgumentException("Key cannot be null or empty");
}
nativeSeek(nativeHandle, key);
}
Expand All @@ -79,8 +79,8 @@ public void seek(byte[] key) throws TidesDBException {
*/
public void seekForPrev(byte[] key) throws TidesDBException {
checkNotFreed();
if (key == null) {
throw new IllegalArgumentException("Key cannot be null");
if (key == null || key.length == 0) {
throw new IllegalArgumentException("Key cannot be null or empty");
}
nativeSeekForPrev(nativeHandle, key);
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/tidesdb/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public void put(ColumnFamily cf, byte[] key, byte[] value, long ttl) throws Tide
if (cf == null) {
throw new IllegalArgumentException("Column family cannot be null");
}
if (key == null) {
throw new IllegalArgumentException("Key cannot be null");
if (key == null || key.length == 0) {
throw new IllegalArgumentException("Key cannot be null or empty");
}
if (value == null) {
throw new IllegalArgumentException("Value cannot be null");
Expand Down Expand Up @@ -85,8 +85,8 @@ public byte[] get(ColumnFamily cf, byte[] key) throws TidesDBException {
if (cf == null) {
throw new IllegalArgumentException("Column family cannot be null");
}
if (key == null) {
throw new IllegalArgumentException("Key cannot be null");
if (key == null || key.length == 0) {
throw new IllegalArgumentException("Key cannot be null or empty");
}
return nativeGet(nativeHandle, cf.getNativeHandle(), key);
}
Expand All @@ -103,7 +103,7 @@ public void delete(ColumnFamily cf, byte[] key) throws TidesDBException {
if (cf == null) {
throw new IllegalArgumentException("Column family cannot be null");
}
if (key == null) {
if (key == null || key.length == 0) {
throw new IllegalArgumentException("Key cannot be null");
}
nativeDelete(nativeHandle, cf.getNativeHandle(), key);
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/com/tidesdb/TidesDBTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,47 @@ void testCustomColumnFamilyConfig() throws TidesDBException {
assertEquals("custom_cf", cf.getName());
}
}

@Test
@Order(13)
void testTransactionPutGetDeleteBadKey() throws TidesDBException {
Config config = Config.builder(tempDir.resolve("testdb3").toString())
.numFlushThreads(2)
.numCompactionThreads(2)
.logLevel(LogLevel.INFO)
.blockCacheSize(64 * 1024 * 1024)
.maxOpenSSTables(256)
.build();

try (TidesDB db = TidesDB.open(config)) {
ColumnFamilyConfig cfConfig = ColumnFamilyConfig.defaultConfig();
db.createColumnFamily("test_cf", cfConfig);

ColumnFamily cf = db.getColumnFamily("test_cf");

byte[] key = new byte[0]; // Bad key (empty)
byte[] value = "value".getBytes(StandardCharsets.UTF_8);

assertThrows(IllegalArgumentException.class, () -> {
try (Transaction txn = db.beginTransaction()) {
txn.put(cf, key, value);
}
});

assertThrows(IllegalArgumentException.class, () -> {
try (Transaction txn = db.beginTransaction()) {
byte[] result = txn.get(cf, key);
assertNotNull(result);
assertArrayEquals(value, result);
}
});

assertThrows(IllegalArgumentException.class, () -> {
try (Transaction txn = db.beginTransaction()) {
txn.delete(cf, key);
txn.commit();
}
});
}
}
}
Loading