Skip to content
Open
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
19 changes: 19 additions & 0 deletions java/rocksjni/sst_file_writerjni.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,25 @@ void Java_org_rocksdb_SstFileWriter_delete__JJ(JNIEnv *env, jclass /*jcls*/,
}
}

/*
* Class: org_rocksdb_SstFileWriter
* Method: deleteDirect
* Signature: (JLjava/nio/ByteBuffer;IIL)V
*/
void Java_org_rocksdb_SstFileWriter_deleteDirect(JNIEnv *env, jclass /*jcls*/,
jlong jdb_handle, jobject jkey,
jint jkey_off, jint jkey_len) {
auto *writer = reinterpret_cast<ROCKSDB_NAMESPACE::SstFileWriter *>(jdb_handle);
auto Delete = [&env, &writer](ROCKSDB_NAMESPACE::Slice &key) {
ROCKSDB_NAMESPACE::Status s = writer->Delete(key);
if (s.ok()) {
return;
}
ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
};
ROCKSDB_NAMESPACE::JniUtil::k_op_direct(Delete, env, jkey, jkey_off, jkey_len);
}

/*
* Class: org_rocksdb_SstFileWriter
* Method: finish
Expand Down
17 changes: 17 additions & 0 deletions java/src/main/java/org/rocksdb/SstFileWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ public void delete(final Slice key) throws RocksDBException {
delete(nativeHandle_, key.getNativeHandle());
}

/**
* Add a Delete key with value to currently opened file.
*
* @param key the specified key to be inserted.
*
* @throws RocksDBException thrown if error happens in underlying
* native library.
*/
public void delete(final ByteBuffer key) throws RocksDBException {
assert key.isDirect();
deleteDirect(nativeHandle_, key, key.position(), key.remaining());
key.position(key.limit());
}

/**
* Add a deletion key to currently opened file.
*
Expand Down Expand Up @@ -227,6 +241,9 @@ private static native void delete(final long handle, final long keyHandle)

private static native void delete(final long handle, final byte[] key) throws RocksDBException;

private static native void deleteDirect(long handle, ByteBuffer key, int keyOffset, int keyLength)
throws RocksDBException;

private static native void finish(final long handle) throws RocksDBException;

@Override
Expand Down
16 changes: 15 additions & 1 deletion java/src/test/java/org/rocksdb/SstFileWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class SstFileWriterTest {

@Rule public TemporaryFolder parentFolder = new TemporaryFolder();

enum OpType { PUT, PUT_BYTES, PUT_DIRECT, MERGE, MERGE_BYTES, DELETE, DELETE_BYTES }
enum OpType { PUT, PUT_BYTES, PUT_DIRECT, MERGE, MERGE_BYTES, DELETE, DELETE_BYTES, DELETE_DIRECT }

static class KeyValueWithOp {
KeyValueWithOp(final String key, final String value, final OpType opType) {
Expand Down Expand Up @@ -114,6 +114,9 @@ private File newSstFile(final List<KeyValueWithOp> keyValues,
case DELETE_BYTES:
sstFileWriter.delete(keyBytes);
break;
case DELETE_DIRECT:
sstFileWriter.delete(keyDirect);
break;
default:
fail("Unsupported op type");
}
Expand Down Expand Up @@ -146,6 +149,7 @@ public void generateSstFileWithJavaComparator()
keyValues.add(new KeyValueWithOp("key3", "value3", OpType.MERGE));
keyValues.add(new KeyValueWithOp("key4", "value4", OpType.MERGE));
keyValues.add(new KeyValueWithOp("key5", "", OpType.DELETE));
keyValues.add(new KeyValueWithOp("key6", "", OpType.DELETE_DIRECT));

newSstFile(keyValues, true);
}
Expand All @@ -159,6 +163,7 @@ public void generateSstFileWithNativeComparator()
keyValues.add(new KeyValueWithOp("key3", "value3", OpType.MERGE));
keyValues.add(new KeyValueWithOp("key4", "value4", OpType.MERGE));
keyValues.add(new KeyValueWithOp("key5", "", OpType.DELETE));
keyValues.add(new KeyValueWithOp("key6", "", OpType.DELETE_DIRECT));

newSstFile(keyValues, false);
}
Expand All @@ -173,6 +178,7 @@ public void ingestSstFile() throws RocksDBException, IOException {
keyValues.add(new KeyValueWithOp("key5", "value5", OpType.MERGE_BYTES));
keyValues.add(new KeyValueWithOp("key6", "", OpType.DELETE));
keyValues.add(new KeyValueWithOp("key7", "", OpType.DELETE));
keyValues.add(new KeyValueWithOp("key8", "", OpType.DELETE_DIRECT));


final File sstFile = newSstFile(keyValues, false);
Expand All @@ -185,6 +191,8 @@ public void ingestSstFile() throws RocksDBException, IOException {
final RocksDB db = RocksDB.open(options, dbFolder.getAbsolutePath());
final IngestExternalFileOptions ingestExternalFileOptions =
new IngestExternalFileOptions()) {
db.put("key8".getBytes(), "value8".getBytes());
assertThat(db.get("key8".getBytes())).isEqualTo("value8".getBytes());
db.ingestExternalFile(
Collections.singletonList(sstFile.getAbsolutePath()), ingestExternalFileOptions);

Expand All @@ -195,6 +203,7 @@ public void ingestSstFile() throws RocksDBException, IOException {
assertThat(db.get("key5".getBytes())).isEqualTo("value5".getBytes());
assertThat(db.get("key6".getBytes())).isEqualTo(null);
assertThat(db.get("key7".getBytes())).isEqualTo(null);
assertThat(db.get("key8".getBytes())).isEqualTo(null);
}
}

Expand All @@ -205,6 +214,7 @@ public void ingestSstFile_cf() throws RocksDBException, IOException {
keyValues.add(new KeyValueWithOp("key2", "value2", OpType.PUT));
keyValues.add(new KeyValueWithOp("key3", "value3", OpType.MERGE));
keyValues.add(new KeyValueWithOp("key4", "", OpType.DELETE));
keyValues.add(new KeyValueWithOp("key5", "", OpType.DELETE_DIRECT));

final File sstFile = newSstFile(keyValues, false);
final File dbFolder = parentFolder.newFolder(DB_DIRECTORY_NAME);
Expand All @@ -222,6 +232,8 @@ public void ingestSstFile_cf() throws RocksDBException, IOException {
.setMergeOperator(stringAppendOperator);
final ColumnFamilyHandle cf_handle = db.createColumnFamily(
new ColumnFamilyDescriptor("new_cf".getBytes(), cf_opts))) {
db.put(cf_handle, "key5".getBytes(), "value5".getBytes());
assertThat(db.get(cf_handle, "key5".getBytes())).isEqualTo("value5".getBytes());
db.ingestExternalFile(cf_handle, Collections.singletonList(sstFile.getAbsolutePath()),
ingestExternalFileOptions);

Expand All @@ -233,6 +245,8 @@ public void ingestSstFile_cf() throws RocksDBException, IOException {
"key3".getBytes())).isEqualTo("value3".getBytes());
assertThat(db.get(cf_handle,
"key4".getBytes())).isEqualTo(null);
assertThat(db.get(cf_handle,
"key5".getBytes())).isEqualTo(null);
}
}
}
Expand Down