-
Notifications
You must be signed in to change notification settings - Fork 57
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still return Unit
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
}
}
No description provided.