Unverified Commit b3466c29 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #5 from ipfs/fix/odd-ends

other odds and ends
parents 371f9656 b9a35a59
......@@ -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
......
......@@ -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.
......
......@@ -69,13 +69,16 @@ func TestM_JSON(t *testing.T) {
}
}
Json, err := bf.JSONMarshal()
json := 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 {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment