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
8 changes: 8 additions & 0 deletions src/main/java/com/oath/halodb/HaloDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public byte[] get(byte[] key) throws HaloDBException {
}
}

public int size(byte[] key) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 'size' the right name? there is already a size method that returns the number of elements. To avoid confusion, we should consider a different name. Perhaps entrySize or valueSize ?

Also, the public API in HaloDB.java should document the expected behavior -- something like Returns the size of the value if present, or -1 if not present.

return dbInternal.size(key);
}

public boolean put(byte[] key, byte[] value) throws HaloDBException {
try {
return dbInternal.put(key, value);
Expand All @@ -56,6 +60,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
15 changes: 14 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 All @@ -410,6 +414,15 @@ long size() {
return inMemoryIndex.size();
}

int size(byte[] key) {
InMemoryIndexMetaData metaData = inMemoryIndex.get(key);
if (metaData == null) {
return -1;
}

return metaData.getValueSize();
}

void setIOErrorFlag() throws IOException {
DBMetaData metaData = new DBMetaData(dbDirectory);
metaData.loadFromFileIfExists();
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