Skip to content

HaloDB.size and HaloDB.contains method#27

Open
akhodakivskiy wants to merge 2 commits into
yahoo:masterfrom
akhodakivskiy:db-size
Open

HaloDB.size and HaloDB.contains method#27
akhodakivskiy wants to merge 2 commits into
yahoo:masterfrom
akhodakivskiy:db-size

Conversation

@akhodakivskiy
Copy link
Copy Markdown

@akhodakivskiy akhodakivskiy commented Feb 22, 2019

Adding two features:

  • check value size associated with a given key without reading entire value from the disk. The size is stored in the memory index.
  • test if a key is stored in the database.

@wangtao724
Copy link
Copy Markdown
Contributor

@akhodakivskiy Do you still need this feature. If so, please update and rebase

@akhodakivskiy
Copy link
Copy Markdown
Author

Rebased. Thanks!

@akhodakivskiy akhodakivskiy changed the title HaloDB.size method HaloDB.size and HaloDB.contains method Nov 30, 2019
int size(byte[] key) {
InMemoryIndexMetaData metaData = inMemoryIndex.get(key);
if (metaData == null) {
return 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

perhaps this should be -1 since 0 is a valid value size. Then -1 indicates the key is not present, and 0 indicates a value of size 0.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good point, fixed. Method HaloDB.get also returns 0 for keys that are not stored. I was following the same logic. But I agree that it should return -1 for abscent keys.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Interesting... HaloDB.get returns a byte[], but HaloDBInternal has another get method that takes a ByteBuffer and reads the value into it (or truncating it if the value is larger than the buffer). This returns the number of bytes copied into the buffer, and 0 when not present.

However, this method is not used anywhere. Its not tested, its not used by the public facing API, it seems to be dead (and untested!) code.

Copy link
Copy Markdown

@scottcarey scottcarey left a comment

Choose a reason for hiding this comment

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

Oops, I meant to comment, not review.

}
}

public int size(byte[] key) {
Copy link
Copy Markdown

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.

darkfrog26 added a commit to outr/HaloDB that referenced this pull request Jun 4, 2026
Ports yahoo#27: check key existence and read a value's size straight from
the in-memory index with no disk access (valueSize returns -1 when absent). The
upstream size(byte[]) is named valueSize here to avoid overloading size() (record
count).
@darkfrog26
Copy link
Copy Markdown

Thanks for this! Upstream looks unmaintained, so I've started a maintained fork — outr/HaloDB (com.outr:halodb-revive on Maven Central). I've ported contains/value-size lookups into it. Sharing in case it helps anyone who finds this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants