Skip to content

Commit

Permalink
Add hamming distance calculation to bloom filters
Browse files Browse the repository at this point in the history
  • Loading branch information
krl committed Apr 15, 2015
1 parent 2406334 commit 2c3f9f2
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Godeps/_workspace/src/github.com/steakknife/hamming/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions Godeps/_workspace/src/github.com/steakknife/hamming/hamming.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions blocks/bloom/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"errors"
// Non crypto hash, because speed
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/mtchavez/jenkins"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/steakknife/hamming"
"hash"
)

type Filter interface {
Add([]byte)
Find([]byte) bool
Merge(Filter) (Filter, error)
HammingDistance(Filter) (int, error)
}

func NewFilter(size int) Filter {
Expand Down Expand Up @@ -100,3 +102,23 @@ func (f *filter) Merge(o Filter) (Filter, error) {

return nfilt, nil
}

func (f *filter) HammingDistance(o Filter) (int, error) {
casfil, ok := o.(*filter)
if !ok {
return 0, errors.New("Unsupported filter type")
}

if len(f.filter) != len(casfil.filter) {
return 0, errors.New("filter lengths must match!")
}

acc := 0

// xor together
for i := 0; i < len(f.filter); i++ {
acc += hamming.Byte(f.filter[i], casfil.filter[i])
}

return acc, nil
}
14 changes: 14 additions & 0 deletions blocks/bloom/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,17 @@ func TestMerge(t *testing.T) {
}
}
}

func TestHamming(t *testing.T) {
f1 := NewFilter(128)
f2 := NewFilter(128)

f1.Add([]byte("no collision"))
f1.Add([]byte("collision? no!"))

dist, _ := f1.HammingDistance(f2)

if dist != 6 {
t.Fatal("Should have 6 bit difference")
}
}

0 comments on commit 2c3f9f2

Please sign in to comment.