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 #24: can the add() method return a boolean indicating a 'fresh' insert #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

thomascharbonnel
Copy link

No description provided.

Copy link
Owner

@alexandrnikitin alexandrnikitin left a comment

Choose a reason for hiding this comment

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

Hi Thomas, Thank you for the PR. I left few comments.

*
* @param x the T item to add
* @return true if the item was not in the filter before, false otherwise
*/
def add(x: T): Unit = {
Copy link
Owner

Choose a reason for hiding this comment

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

still return Unit

Copy link
Author

Choose a reason for hiding this comment

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

Ooops


var i = 0
while (i < numberOfHashes) {
val computedHash = hash1 + i * hash2
if (!bits.get((computedHash & Long.MaxValue) % numberOfBits))
Copy link
Owner

Choose a reason for hiding this comment

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

It's an additional read op that I really want to avoid. It can be done inside UnsafeBitArray but still.
What if it will be a separate method for AddAndGet semantic (needs better name for sure). WDYT?

Copy link
Author

Choose a reason for hiding this comment

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

Why not for the separate method, addAndGet is not that bad (that's my way of saying that I can't think of anything clearer).
Do you think it's better to go the naive way

def addAndGet(x: T): Boolean = {
  val was_set = mightContain(x)
  add(x)
  !was_set
}

or C&P?

Copy link
Owner

Choose a reason for hiding this comment

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

I think the best way is a separate method in BloomFilter class and another one in UnsafeBitArray (it has the read operation there already)

UnsafeBitArray{
  def setAndGet?(index: Long): Boolean = {
    val offset = ptr + (index >>> 6) * 8L
    val long = unsafe.getLong(offset)
    if (bit not set in long) {
        unsafe.putLong(offset, long | (1L << index))
        true
    } else false
  }
}

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.

2 participants