Skip to content

Commit

Permalink
HaloDB.contains method
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Khodakivskiy committed May 30, 2020
1 parent 741b4f7 commit aed0d27
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/main/java/com/oath/halodb/HaloDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public void delete(byte[] key) throws HaloDBException {
}
}

public boolean contains(byte[] key) {
return dbInternal.contains(key);
}

public void close() throws HaloDBException {
try {
dbInternal.close();
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/oath/halodb/HaloDBInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,17 @@ boolean clearSnapshot() {
return false;
}

return true;
return true;
} else {
logger.info("snapshot not existed");
return true;
}
}

boolean contains(byte[] key) {
return inMemoryIndex.containsKey(key);
}

void delete(byte[] key) throws IOException {
writeLock.lock();
try {
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/oath/halodb/HaloDBTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,33 @@ public void testPutUpdateAndGetDB(HaloDBOptions options) throws HaloDBException
});
}

@Test(dataProvider = "Options")
public void testPutAndContainsDB(HaloDBOptions options) throws HaloDBException {
String directory = TestUtils.getTestDirectory("HaloDBTest", "testPutAndGetDB");

options.setCompactionDisabled(true);

HaloDB db = getTestDB(directory, options);

int noOfRecords = 10_000;
List<Record> records = TestUtils.insertRandomRecordsOfSize(db, noOfRecords, 128);

List<byte[]> missingKeys = new ArrayList<>();
for (int i = 0; i < noOfRecords; i++) {
missingKeys.add(TestUtils.generateRandomByteArray(64));
}

List<Record> actual = new ArrayList<>();
db.newIterator().forEachRemaining(actual::add);

Assert.assertTrue(actual.containsAll(records) && records.containsAll(actual));

records.forEach(record -> Assert.assertTrue(db.contains(record.getKey())));

missingKeys.forEach(key -> Assert.assertFalse(db.contains(key)));
}


@Test(dataProvider = "Options")
public void testCreateCloseAndOpenDB(HaloDBOptions options) throws HaloDBException {

Expand Down

0 comments on commit aed0d27

Please sign in to comment.