Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
bbloom
Commits
cafad0a6
Commit
cafad0a6
authored
Jun 21, 2016
by
Jakub Sztandera
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change locks and make some functions private
parent
df7a9774
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
10 deletions
+14
-10
bbloom.go
bbloom.go
+14
-10
No files found.
bbloom.go
View file @
cafad0a6
...
...
@@ -97,7 +97,7 @@ type bloomJSONImExport struct {
//
// Bloom filter
type
Bloom
struct
{
Mtx
sync
.
Mutex
Mtx
sync
.
RW
Mutex
bitset
[]
uint64
sizeExp
uint64
size
uint64
...
...
@@ -127,7 +127,7 @@ type Bloom struct {
func
(
bl
*
Bloom
)
Add
(
entry
[]
byte
)
{
l
,
h
:=
bl
.
sipHash
(
entry
)
for
i
:=
uint64
(
0
);
i
<
(
*
bl
)
.
setLocs
;
i
++
{
(
*
bl
)
.
S
et
((
h
+
i
*
l
)
&
(
*
bl
)
.
size
)
bl
.
s
et
((
h
+
i
*
l
)
&
(
*
bl
)
.
size
)
}
}
...
...
@@ -146,7 +146,7 @@ func (bl Bloom) Has(entry []byte) bool {
l
,
h
:=
bl
.
sipHash
(
entry
)
res
:=
true
for
i
:=
uint64
(
0
);
i
<
bl
.
setLocs
;
i
++
{
res
=
res
&&
bl
.
I
sSet
((
h
+
i
*
l
)
&
bl
.
size
)
res
=
res
&&
bl
.
i
sSet
((
h
+
i
*
l
)
&
bl
.
size
)
// Branching here (early escape) is not worth it
// This is my conclusion from benchmarks
// if !res {
...
...
@@ -159,8 +159,8 @@ func (bl Bloom) Has(entry []byte) bool {
// HasTS
// Thread safe: Mutex.Lock the bloomfilter for the time of processing the entry
func
(
bl
*
Bloom
)
HasTS
(
entry
[]
byte
)
bool
{
bl
.
Mtx
.
Lock
()
defer
bl
.
Mtx
.
Unlock
()
bl
.
Mtx
.
R
Lock
()
defer
bl
.
Mtx
.
R
Unlock
()
return
bl
.
Has
(
entry
[
:
])
}
...
...
@@ -172,7 +172,7 @@ func (bl *Bloom) AddIfNotHas(entry []byte) (added bool) {
l
,
h
:=
bl
.
sipHash
(
entry
)
res
:=
true
for
i
:=
uint64
(
0
);
i
<
bl
.
setLocs
;
i
++
{
prev
:=
bl
.
G
etSet
((
h
+
i
*
l
)
&
bl
.
size
)
prev
:=
bl
.
g
etSet
((
h
+
i
*
l
)
&
bl
.
size
)
res
=
res
&&
prev
}
return
!
res
...
...
@@ -191,6 +191,8 @@ func (bl *Bloom) AddIfNotHasTS(entry []byte) (added bool) {
// Clear
// resets the Bloom filter
func
(
bl
*
Bloom
)
Clear
()
{
bl
.
Mtx
.
Lock
()
defer
bl
.
Mtx
.
Unlock
()
for
i
,
_
:=
range
(
*
bl
)
.
bitset
{
bl
.
bitset
[
i
]
=
0
}
...
...
@@ -198,12 +200,12 @@ func (bl *Bloom) Clear() {
// Set
// set the bit[idx] of bitsit
func
(
bl
*
Bloom
)
S
et
(
idx
uint64
)
{
func
(
bl
*
Bloom
)
s
et
(
idx
uint64
)
{
ptr
:=
unsafe
.
Pointer
(
uintptr
(
unsafe
.
Pointer
(
&
bl
.
bitset
[
idx
>>
6
]))
+
uintptr
((
idx
%
64
)
>>
3
))
*
(
*
uint8
)(
ptr
)
|=
mask
[
idx
%
8
]
}
func
(
bl
*
Bloom
)
G
etSet
(
idx
uint64
)
bool
{
func
(
bl
*
Bloom
)
g
etSet
(
idx
uint64
)
bool
{
ptr
:=
unsafe
.
Pointer
(
uintptr
(
unsafe
.
Pointer
(
&
bl
.
bitset
[
idx
>>
6
]))
+
uintptr
((
idx
%
64
)
>>
3
))
res
:=
*
(
*
uint8
)(
ptr
)
&
mask
[
idx
%
8
]
>
0
*
(
*
uint8
)(
ptr
)
|=
mask
[
idx
%
8
]
...
...
@@ -213,14 +215,16 @@ func (bl *Bloom) GetSet(idx uint64) bool {
// IsSet
// check if bit[idx] of bitset is set
// returns true/false
func
(
bl
*
Bloom
)
I
sSet
(
idx
uint64
)
bool
{
func
(
bl
*
Bloom
)
i
sSet
(
idx
uint64
)
bool
{
ptr
:=
unsafe
.
Pointer
(
uintptr
(
unsafe
.
Pointer
(
&
bl
.
bitset
[
idx
>>
6
]))
+
uintptr
((
idx
%
64
)
>>
3
))
return
*
(
*
uint8
)(
ptr
)
&
mask
[
idx
%
8
]
>
0
}
// JSONMarshal
// returns JSON-object (type bloomJSONImExport) as []byte
func
(
bl
Bloom
)
JSONMarshal
()
[]
byte
{
func
(
bl
*
Bloom
)
JSONMarshal
()
[]
byte
{
bl
.
Mtx
.
RLock
()
defer
bl
.
Mtx
.
RUnlock
()
bloomImEx
:=
bloomJSONImExport
{}
bloomImEx
.
SetLocs
=
uint64
(
bl
.
setLocs
)
bloomImEx
.
FilterSet
=
make
([]
byte
,
len
(
bl
.
bitset
)
<<
3
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment