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
b3466c29
Unverified
Commit
b3466c29
authored
Aug 23, 2019
by
Steven Allen
Committed by
GitHub
Aug 23, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5 from ipfs/fix/odd-ends
other odds and ends
parents
371f9656
b9a35a59
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
17 deletions
+28
-17
README.md
README.md
+1
-1
bbloom.go
bbloom.go
+22
-14
bbloom_test.go
bbloom_test.go
+5
-2
No files found.
README.md
View file @
b3466c29
...
...
@@ -96,7 +96,7 @@ Json = bf.JSONMarshal()
bf
.
Mtx
.
Unlock
()
// restore a bloom filter from storage
bfNew
:=
bbloom
.
JSONUnmarshal
(
Json
)
bfNew
,
_
:=
bbloom
.
JSONUnmarshal
(
Json
)
isInNew
:=
bfNew
.
Has
([]
byte
(
"butter"
))
// should be true
isNotInNew
:=
bfNew
.
Has
([]
byte
(
"Butter"
))
// should be false
...
...
bbloom.go
View file @
b3466c29
...
...
@@ -21,10 +21,10 @@
package
bbloom
import
(
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"log"
"math"
"math/bits"
"sync"
...
...
@@ -81,13 +81,13 @@ func New(params ...float64) (bloomfilter *Bloom, err error) {
// NewWithBoolset
// takes a []byte slice and number of locs per entry
// returns the bloomfilter with a bitset populated according to the input []byte
func
NewWithBoolset
(
bs
*
[]
byte
,
locs
uint64
)
(
bloomfilter
*
Bloom
)
{
bloomfilter
,
err
:=
New
(
float64
(
len
(
*
bs
)
<<
3
),
float64
(
locs
))
func
NewWithBoolset
(
bs
[]
byte
,
locs
uint64
)
(
bloomfilter
*
Bloom
)
{
bloomfilter
,
err
:=
New
(
float64
(
len
(
bs
)
<<
3
),
float64
(
locs
))
if
err
!=
nil
{
panic
(
err
)
// Should never happen
}
for
i
:=
range
bloomfilter
.
bitset
{
bloomfilter
.
bitset
[
i
]
=
binary
.
BigEndian
.
Uint64
((
*
bs
)[
i
<<
3
:
])
bloomfilter
.
bitset
[
i
]
=
binary
.
BigEndian
.
Uint64
((
bs
)[
i
<<
3
:
])
}
return
bloomfilter
}
...
...
@@ -249,29 +249,37 @@ func (bl *Bloom) marshal() bloomJSONImExport {
// JSONMarshal
// returns JSON-object (type bloomJSONImExport) as []byte
func
(
bl
*
Bloom
)
JSONMarshal
()
(
[]
byte
,
error
)
{
func
(
bl
*
Bloom
)
JSONMarshal
()
[]
byte
{
data
,
err
:=
json
.
Marshal
(
bl
.
marshal
())
return
data
,
err
if
err
!=
nil
{
log
.
Fatal
(
"json.Marshal failed: "
,
err
)
}
return
data
}
// JSONMarshalTS is a thread-safe version of JSONMarshal
func
(
bl
*
Bloom
)
JSONMarshalTS
()
(
[]
byte
,
error
)
{
func
(
bl
*
Bloom
)
JSONMarshalTS
()
[]
byte
{
bl
.
Mtx
.
RLock
()
export
:=
bl
.
marshal
()
bl
.
Mtx
.
RUnlock
()
return
json
.
Marshal
(
export
)
data
,
err
:=
json
.
Marshal
(
export
)
if
err
!=
nil
{
log
.
Fatal
(
"json.Marshal failed: "
,
err
)
}
return
data
}
// JSONUnmarshal
// takes JSON-Object (type bloomJSONImExport) as []bytes
// returns bloom32 / bloom64 object
func
JSONUnmarshal
(
dbData
[]
byte
)
*
Bloom
{
func
JSONUnmarshal
(
dbData
[]
byte
)
(
*
Bloom
,
error
)
{
bloomImEx
:=
bloomJSONImExport
{}
json
.
Unmarshal
(
dbData
,
&
bloomImEx
)
buf
:=
bytes
.
NewBuffer
(
bloomImEx
.
FilterSet
)
bs
:=
buf
.
Bytes
()
bf
:=
NewWithBoolset
(
&
bs
,
bloomImEx
.
SetLocs
)
return
bf
err
:=
json
.
Unmarshal
(
dbData
,
&
bloomImEx
)
if
err
!=
nil
{
return
nil
,
err
}
bf
:=
NewWithBoolset
(
bloomImEx
.
FilterSet
,
bloomImEx
.
SetLocs
)
return
bf
,
nil
}
// FillRatio returns the fraction of bits set.
...
...
bbloom_test.go
View file @
b3466c29
...
...
@@ -69,13 +69,16 @@ func TestM_JSON(t *testing.T) {
}
}
J
son
,
err
:=
bf
.
JSONMarshal
()
j
son
:=
bf
.
JSONMarshal
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
// create new bloomfilter from bloomfilter's JSON representation
bf2
:=
JSONUnmarshal
(
Json
)
bf2
,
err
:=
JSONUnmarshal
(
json
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
cnt2
:=
0
for
i
:=
range
wordlist1
{
...
...
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