-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
core: add support for bloom filters #99
Conversation
core/bloom.go
Outdated
func setBit(buf []byte, b int) { | ||
if b < 0 { | ||
return | ||
} | ||
|
||
idx, offset := b/8, 7-b%8 | ||
if idx < 0 || idx >= len(buf) { | ||
return | ||
} | ||
|
||
buf[idx] = buf[idx] | 1<<offset | ||
} | ||
|
||
// isBitSet checks if the bit at index `b` is set to "1" or not in `buf`. | ||
func isBitSet(buf []byte, b int) bool { | ||
if b < 0 { | ||
return false | ||
} | ||
|
||
idx, offset := b/8, 7-b%8 | ||
if idx >= len(buf) { | ||
return false | ||
} | ||
|
||
if buf[idx]&(1<<offset) == 1<<offset { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} |
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.
There is a util directory where you can put byte level utils; also we have something similar for varint
and bytelist
. Check that implementation once and see if we need to converge.
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, at the moment, I don't think there's any code which can be converged. Bytelist holds some extra metadata and we might not need any of it for bloom bytes. Although I can move some logic to bloom_utils to separate the logic.
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.
Such beautifully written code. Loved reading through it 🥇
@@ -24,6 +24,9 @@ var OBJ_ENCODING_QREF uint8 = 1 | |||
var OBJ_ENCODING_STACKINT uint8 = 2 | |||
var OBJ_ENCODING_STACKREF uint8 = 3 | |||
|
|||
var OBJ_TYPE_BITSET uint8 = 1 << 5 // 00100000 |
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.
This is a great decision; as we cannot use go strings as byte arrays easily.
} else { | ||
return false | ||
} |
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.
don't need an else here; just a blanket return false
This PR attempts to add bloom filters in dice db (See issue: #36).
The commands supported are mentioned below:
Returns "OK" in case of successful bloom filter creation and relevant error if not.
Returns "1" in case of successful addition to the filter and "0" if the element (or some other elements which set the same bits) was already added before. It also returns "-1" in case of errors. Moreover, if the filter does not exist, it will create one with defaults.
Returns "1" is the element may (or may not) exist in the filter and "0" is the element surely doesn't exist. It also returns "-1" in case of errors.
Returns the parameters and metadata of the filter.
Sufficient unit tests for most of the functions have been added in this PR.
Future work (not necessary in this PR)