Skip to content

Commit

Permalink
fix: contains_key bugfix (#909)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengjiachun committed Nov 28, 2022
1 parent 67621e5 commit ee41b61
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,12 @@ public void containsKey(final byte[] key, final KVStoreClosure closure) {
boolean exists = false;
Holder<byte[]> valueHolder = new Holder<>();
if (this.db.keyMayExist(key, valueHolder)) {
exists = valueHolder.getValue() != null;
if (valueHolder.getValue() != null) {
// the key is found in memory
exists = true;
} else {
exists = this.db.get(key) != null;
}
}
setSuccess(closure, exists);
} catch (final Exception e) {
Expand Down Expand Up @@ -1431,7 +1436,7 @@ void ingestSstFiles(final EnumMap<SstColumnFamily, File> sstFileTable) {
}
}

RocksDBBackupInfo backupDB(final String backupDBPath) throws IOException {
public RocksDBBackupInfo backupDB(final String backupDBPath) throws IOException {
final Timer.Context timeCtx = getTimeContext("BACKUP_DB");
FileUtils.forceMkdir(new File(backupDBPath));
final Lock writeLock = this.readWriteLock.writeLock();
Expand All @@ -1457,7 +1462,7 @@ RocksDBBackupInfo backupDB(final String backupDBPath) throws IOException {
}
}

void restoreBackup(final String backupDBPath, final RocksDBBackupInfo rocksBackupInfo) {
public void restoreBackup(final String backupDBPath, final RocksDBBackupInfo rocksBackupInfo) {
final Timer.Context timeCtx = getTimeContext("RESTORE_BACKUP");
final Lock writeLock = this.readWriteLock.writeLock();
writeLock.lock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alipay.sofa.jraft.rhea;

import java.lang.reflect.Field;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -46,4 +47,8 @@ public static <T> T getByName(final Object obj, final String name, final Class<T
throw new RuntimeException(e);
}
}

public static String mkTempDir() {
return Paths.get(System.getProperty("java.io.tmpdir", "/tmp"), "rheakv_test_" + System.nanoTime()).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

import com.alipay.sofa.jraft.rhea.TestUtil;
import com.alipay.sofa.jraft.rhea.storage.RocksDBBackupInfo;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -183,7 +185,7 @@ public void getLocalIteratorTest() {
* Test method: {@link RocksRawKVStore#containsKey(byte[], KVStoreClosure)}
*/
@Test
public void containsKeyTest() {
public void containsKeyTest() throws Exception {
final byte[] key = makeKey("contains_key_test");
Boolean isContains = new SyncKVStore<Boolean>() {
@Override
Expand All @@ -195,6 +197,19 @@ public void execute(RawKVStore kvStore, KVStoreClosure closure) {

final byte[] value = makeValue("contains_key_test_value");
this.kvStore.put(key, value, null);

isContains = new SyncKVStore<Boolean>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.containsKey(key, closure);
}
}.apply(this.kvStore);
assertTrue(isContains);

final String snapshotPath = TestUtil.mkTempDir();
final RocksDBBackupInfo backupInfo = this.kvStore.backupDB(snapshotPath);
this.kvStore.restoreBackup(snapshotPath, backupInfo);

isContains = new SyncKVStore<Boolean>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
Expand Down

0 comments on commit ee41b61

Please sign in to comment.