Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: contains_key bugfix #909

Merged
merged 1 commit into from
Nov 28, 2022
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
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