remove some dead code
License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
Steven Allen committed
Sep 4, 2017 at 20:15 UTC
db6f058946a5c9b1cd86bab59c7b7ffeb79e90dd
9 files changed
-572
blocks/bloom/filter.go
deleted
-130
@@ -1,130 +0,0 @@
1
-// Package bloom implements a simple bloom filter.
2
-package bloom
3
-
4
-import (
5
- "encoding/binary"
6
- "errors"
7
- // Non crypto hash, because speed
8
- "gx/ipfs/QmeWQMDa5dSdP4n8WDeoY5z8L2EKVqF4ZvK4VEHsLqXsGu/hamming"
9
- "hash"
10
-
11
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/mtchavez/jenkins"
12
-)
13
-
14
-// A Filter represents a bloom filter.
15
-type Filter interface {
16
- Add([]byte)
17
- Find([]byte) bool
18
- Merge(Filter) (Filter, error)
19
- HammingDistance(Filter) (int, error)
20
-}
21
-
22
-// NewFilter creates a new bloom Filter with the given
23
-// size. k (the number of hash functions), is hardcoded to 3.
24
-func NewFilter(size int) Filter {
25
- return &filter{
26
- hash: jenkins.New(),
27
- filter: make([]byte, size),
28
- k: 3,
29
- }
30
-}
31
-
32
-type filter struct {
33
- filter []byte
34
- hash hash.Hash32
35
- k int
36
-}
37
-
38
-// BasicFilter calls NewFilter with a bloom filter size of
39
-// 2048 bytes.
40
-func BasicFilter() Filter {
41
- return NewFilter(2048)
42
-}
43
-
44
-func (f *filter) Add(bytes []byte) {
45
- for _, bit := range f.getBitIndicies(bytes) {
46
- f.setBit(bit)
47
- }
48
-}
49
-
50
-func (f *filter) getBitIndicies(bytes []byte) []uint32 {
51
- indicies := make([]uint32, f.k)
52
-
53
- f.hash.Write(bytes)
54
- b := make([]byte, 4)
55
-
56
- for i := 0; i < f.k; i++ {
57
- res := f.hash.Sum32()
58
- indicies[i] = res % (uint32(len(f.filter)) * 8)
59
-
60
- binary.LittleEndian.PutUint32(b, res)
61
- f.hash.Write(b)
62
- }
63
-
64
- f.hash.Reset()
65
-
66
- return indicies
67
-}
68
-
69
-func (f *filter) Find(bytes []byte) bool {
70
- for _, bit := range f.getBitIndicies(bytes) {
71
- if !f.getBit(bit) {
72
- return false
73
- }
74
- }
75
- return true
76
-}
77
-
78
-func (f *filter) setBit(i uint32) {
79
- f.filter[i/8] |= (1 << byte(i%8))
80
-}
81
-
82
-func (f *filter) getBit(i uint32) bool {
83
- return f.filter[i/8]&(1<<byte(i%8)) != 0
84
-}
85
-
86
-func (f *filter) Merge(o Filter) (Filter, error) {
87
- casfil, ok := o.(*filter)
88
- if !ok {
89
- return nil, errors.New("Unsupported filter type")
90
- }
91
-
92
- if len(casfil.filter) != len(f.filter) {
93
- return nil, errors.New("filter lengths must match")
94
- }
95
-
96
- if casfil.k != f.k {
97
- return nil, errors.New("filter k-values must match")
98
- }
99
-
100
- nfilt := new(filter)
101
- nfilt.hash = f.hash
102
- nfilt.filter = make([]byte, len(f.filter))
103
- nfilt.k = f.k
104
-
105
- for i, v := range f.filter {
106
- nfilt.filter[i] = v | casfil.filter[i]
107
- }
108
-
109
- return nfilt, nil
110
-}
111
-
112
-func (f *filter) HammingDistance(o Filter) (int, error) {
113
- casfil, ok := o.(*filter)
114
- if !ok {
115
- return 0, errors.New("Unsupported filter type")
116
- }
117
-
118
- if len(f.filter) != len(casfil.filter) {
119
- return 0, errors.New("filter lengths must match")
120
- }
121
-
122
- acc := 0
123
-
124
- // xor together
125
- for i := 0; i < len(f.filter); i++ {
126
- acc += hamming.Byte(f.filter[i], casfil.filter[i])
127
- }
128
-
129
- return acc, nil
130
-}
blocks/bloom/filter.proto
deleted
-10
@@ -1,10 +0,0 @@
1
-package bloom;
2
-
3
-message PackedFilter {
4
- enum HashType {
5
-
6
- }
7
- optional bool compressed;
8
- optional bytes data;
9
- repeated HashType hashes;
10
-}
blocks/bloom/filter_test.go
deleted
-102
@@ -1,102 +0,0 @@
1
-package bloom
2
-
3
-import (
4
- "encoding/binary"
5
- "fmt"
6
- "testing"
7
-)
8
-
9
-func TestBasicFilter(t *testing.T) {
10
- f := BasicFilter().(*filter)
11
-
12
- if len(f.filter) != 2048 {
13
- t.Fatal("basic filter should have length 2048, has:", len(f.filter))
14
- }
15
-}
16
-
17
-func TestFilter(t *testing.T) {
18
- f := NewFilter(128)
19
-
20
- keys := [][]byte{
21
- []byte("hello"),
22
- []byte("fish"),
23
- []byte("ipfsrocks"),
24
- []byte("i want ipfs socks"),
25
- }
26
-
27
- f.Add(keys[0])
28
- if !f.Find(keys[0]) {
29
- t.Fatal("Failed to find single inserted key!")
30
- }
31
-
32
- f.Add(keys[1])
33
- if !f.Find(keys[1]) {
34
- t.Fatal("Failed to find key!")
35
- }
36
-
37
- f.Add(keys[2])
38
- f.Add(keys[3])
39
-
40
- for _, k := range keys {
41
- if !f.Find(k) {
42
- t.Fatal("Couldnt find one of three keys")
43
- }
44
- }
45
-
46
- if f.Find([]byte("beep boop")) {
47
- t.Fatal("Got false positive! Super unlikely!")
48
- }
49
-
50
- fmt.Println(f)
51
-}
52
-
53
-func TestMerge(t *testing.T) {
54
-
55
- f1 := NewFilter(128)
56
- f2 := NewFilter(128)
57
-
58
- fbork := NewFilter(32)
59
-
60
- _, err := f1.Merge(fbork)
61
-
62
- if err == nil {
63
- t.Fatal("Merge should fail on filters with different lengths")
64
- }
65
-
66
- b := make([]byte, 4)
67
-
68
- var i uint32
69
- for i = 0; i < 10; i++ {
70
- binary.LittleEndian.PutUint32(b, i)
71
- f1.Add(b)
72
- }
73
-
74
- for i = 10; i < 20; i++ {
75
- binary.LittleEndian.PutUint32(b, i)
76
- f2.Add(b)
77
- }
78
-
79
- merged, _ := f1.Merge(f2)
80
-
81
- for i = 0; i < 20; i++ {
82
- binary.LittleEndian.PutUint32(b, i)
83
-
84
- if !merged.Find(b) {
85
- t.Fatal("Could not find all keys in merged filter")
86
- }
87
- }
88
-}
89
-
90
-func TestHamming(t *testing.T) {
91
- f1 := NewFilter(128)
92
- f2 := NewFilter(128)
93
-
94
- f1.Add([]byte("no collision"))
95
- f1.Add([]byte("collision? no!"))
96
-
97
- dist, _ := f1.HammingDistance(f2)
98
-
99
- if dist != 6 {
100
- t.Fatal("Should have 6 bit difference")
101
- }
102
-}
blocks/set/set.go
deleted
-65
@@ -1,65 +0,0 @@
1
-// Package set defines the BlockSet interface which provides
2
-// abstraction for sets of Cids.
3
-// It provides a default implementation using cid.Set.
4
-package set
5
-
6
-import (
7
- cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
8
-
9
- "github.com/ipfs/go-ipfs/blocks/bloom"
10
-)
11
-
12
-// BlockSet represents a mutable set of blocks CIDs.
13
-type BlockSet interface {
14
- AddBlock(*cid.Cid)
15
- RemoveBlock(*cid.Cid)
16
- HasKey(*cid.Cid) bool
17
- // GetBloomFilter creates and returns a bloom filter to which
18
- // all the CIDs in the set have been added.
19
- GetBloomFilter() bloom.Filter
20
- GetKeys() []*cid.Cid
21
-}
22
-
23
-// SimpleSetFromKeys returns a default implementation of BlockSet
24
-// using cid.Set. The given keys are added to the set.
25
-func SimpleSetFromKeys(keys []*cid.Cid) BlockSet {
26
- sbs := &simpleBlockSet{blocks: cid.NewSet()}
27
- for _, k := range keys {
28
- sbs.AddBlock(k)
29
- }
30
- return sbs
31
-}
32
-
33
-// NewSimpleBlockSet returns a new empty default implementation
34
-// of BlockSet using cid.Set.
35
-func NewSimpleBlockSet() BlockSet {
36
- return &simpleBlockSet{blocks: cid.NewSet()}
37
-}
38
-
39
-type simpleBlockSet struct {
40
- blocks *cid.Set
41
-}
42
-
43
-func (b *simpleBlockSet) AddBlock(k *cid.Cid) {
44
- b.blocks.Add(k)
45
-}
46
-
47
-func (b *simpleBlockSet) RemoveBlock(k *cid.Cid) {
48
- b.blocks.Remove(k)
49
-}
50
-
51
-func (b *simpleBlockSet) HasKey(k *cid.Cid) bool {
52
- return b.blocks.Has(k)
53
-}
54
-
55
-func (b *simpleBlockSet) GetBloomFilter() bloom.Filter {
56
- f := bloom.BasicFilter()
57
- for _, k := range b.blocks.Keys() {
58
- f.Add(k.Bytes())
59
- }
60
- return f
61
-}
62
-
63
-func (b *simpleBlockSet) GetKeys() []*cid.Cid {
64
- return b.blocks.Keys()
65
-}
blocks/set/set_test.go
deleted
-78
@@ -1,78 +0,0 @@
1
-package set
2
-
3
-import (
4
- "testing"
5
-
6
- bu "github.com/ipfs/go-ipfs/blocks/blocksutil"
7
-
8
- cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
9
-)
10
-
11
-const (
12
- tAdd int = 1 << iota
13
- tRemove
14
- tReAdd
15
-)
16
-
17
-func exampleKeys() []*cid.Cid {
18
- res := make([]*cid.Cid, 1<<8)
19
- gen := bu.NewBlockGenerator()
20
- for i := uint64(0); i < 1<<8; i++ {
21
- res[i] = gen.Next().Cid()
22
- }
23
- return res
24
-}
25
-func checkSet(set BlockSet, keySlice []*cid.Cid, t *testing.T) {
26
- for i, key := range keySlice {
27
- if i&tReAdd == 0 {
28
- if !set.HasKey(key) {
29
- t.Error("key should be in the set")
30
- }
31
- } else if i&tRemove == 0 {
32
- if set.HasKey(key) {
33
- t.Error("key shouldn't be in the set")
34
- }
35
- } else if i&tAdd == 0 {
36
- if !set.HasKey(key) {
37
- t.Error("key should be in the set")
38
- }
39
- }
40
- }
41
-}
42
-
43
-func TestSetWorks(t *testing.T) {
44
- set := NewSimpleBlockSet()
45
- keys := exampleKeys()
46
-
47
- for i, key := range keys {
48
- if i&tAdd == 0 {
49
- set.AddBlock(key)
50
- }
51
- }
52
- for i, key := range keys {
53
- if i&tRemove == 0 {
54
- set.RemoveBlock(key)
55
- }
56
- }
57
- for i, key := range keys {
58
- if i&tReAdd == 0 {
59
- set.AddBlock(key)
60
- }
61
- }
62
-
63
- checkSet(set, keys, t)
64
- addedKeys := set.GetKeys()
65
-
66
- newSet := SimpleSetFromKeys(addedKeys)
67
- // same check works on a new set
68
- checkSet(newSet, keys, t)
69
-
70
- bloom := set.GetBloomFilter()
71
-
72
- for _, key := range addedKeys {
73
- if !bloom.Find(key.Bytes()) {
74
- t.Error("bloom doesn't contain expected key")
75
- }
76
- }
77
-
78
-}
routing/mock/dht.go
deleted
-37
@@ -1,37 +0,0 @@
1
-package mockrouting
2
-
3
-import (
4
- context "context"
5
- dht "gx/ipfs/QmNV315eTphFgCttWPrT5ARNsiPNRLGFWHRJZyXyqvmjD6/go-libp2p-kad-dht"
6
- ds "gx/ipfs/QmVSase1JP7cq9QkPT46oNwdp9pT6kBkG3oqS14y3QcZjG/go-datastore"
7
- sync "gx/ipfs/QmVSase1JP7cq9QkPT46oNwdp9pT6kBkG3oqS14y3QcZjG/go-datastore/sync"
8
- "gx/ipfs/QmWRCn8vruNAzHx8i6SAXinuheRitKEGu8c7m26stKvsYx/go-testutil"
9
- mocknet "gx/ipfs/QmbRT4BwPQEx4CPCd8LKYL46tFWYneGswQnHFdsuiczJRL/go-libp2p/p2p/net/mock"
10
-)
11
-
12
-type mocknetserver struct {
13
- mn mocknet.Mocknet
14
-}
15
-
16
-func NewDHTNetwork(mn mocknet.Mocknet) Server {
17
- return &mocknetserver{
18
- mn: mn,
19
- }
20
-}
21
-
22
-func (rs *mocknetserver) Client(p testutil.Identity) Client {
23
- return rs.ClientWithDatastore(context.TODO(), p, ds.NewMapDatastore())
24
-}
25
-
26
-func (rs *mocknetserver) ClientWithDatastore(ctx context.Context, p testutil.Identity, ds ds.Datastore) Client {
27
-
28
- // FIXME AddPeer doesn't appear to be idempotent
29
-
30
- host, err := rs.mn.AddPeer(p.PrivateKey(), p.Address())
31
- if err != nil {
32
- panic("FIXME")
33
- }
34
- return dht.NewDHT(ctx, host, sync.MutexWrap(ds))
35
-}
36
-
37
-var _ Server = &mocknetserver{}
thirdparty/iter/iter.go
deleted
-5
@@ -1,5 +0,0 @@
1
-package iter
2
-
3
-func N(n int) []struct{} {
4
- return make([]struct{}, n)
5
-}
thirdparty/multierr/multierr.go
deleted
-27
@@ -1,27 +0,0 @@
1
-package multierr
2
-
3
-import (
4
- "fmt"
5
-)
6
-
7
-// Error contains a set of errors. Used to return multiple errors, as in listen.
8
-type Error struct {
9
- Errors []error
10
-}
11
-
12
-func (e *Error) Error() string {
13
- if e == nil {
14
- return "<nil error>"
15
- }
16
- var out string
17
- for i, v := range e.Errors {
18
- if v != nil {
19
- out += fmt.Sprintf("%d: %s\n", i, v)
20
- }
21
- }
22
- return out
23
-}
24
-
25
-func New(errs ...error) *Error {
26
- return &Error{errs}
27
-}
thirdparty/todocounter/counter.go
deleted
-118
@@ -1,118 +0,0 @@
1
-package todocounter
2
-
3
-import (
4
- "sync"
5
-)
6
-
7
-// Counter records things remaining to process. It is needed for complicated
8
-// cases where multiple goroutines are spawned to process items, and they may
9
-// generate more items to process. For example, say a query over a set of nodes
10
-// may yield either a result value, or more nodes to query. Signaling is subtly
11
-// complicated, because the queue may be empty while items are being processed,
12
-// that will end up adding more items to the queue.
13
-//
14
-// Use Counter like this:
15
-//
16
-// todos := make(chan int, 10)
17
-// ctr := todoctr.NewCounter()
18
-//
19
-// process := func(item int) {
20
-// fmt.Println("processing %d\n...", item)
21
-//
22
-// // this task may randomly generate more tasks
23
-// if rand.Intn(5) == 0 {
24
-// todos<- item + 1
25
-// ctr.Increment(1) // increment counter for new task.
26
-// }
27
-//
28
-// ctr.Decrement(1) // decrement one to signal the task being done.
29
-// }
30
-//
31
-// // add some tasks.
32
-// todos<- 1
33
-// todos<- 2
34
-// todos<- 3
35
-// todos<- 4
36
-// ctr.Increment(4)
37
-//
38
-// for {
39
-// select {
40
-// case item := <- todos:
41
-// go process(item)
42
-// case <-ctr.Done():
43
-// fmt.Println("done processing everything.")
44
-// close(todos)
45
-// }
46
-// }
47
-type Counter interface {
48
- // Incrememnt adds a number of todos to track.
49
- // If the counter is **below** zero, it panics.
50
- Increment(i uint32)
51
-
52
- // Decrement removes a number of todos to track.
53
- // If the count drops to zero, signals done and destroys the counter.
54
- // If the count drops **below** zero, panics. It means you have tried to remove
55
- // more things than you added, i.e. sync issues.
56
- Decrement(i uint32)
57
-
58
- // Done returns a channel to wait upon. Use it in selects:
59
- //
60
- // select {
61
- // case <-ctr.Done():
62
- // // done processing all items
63
- // }
64
- //
65
- Done() <-chan struct{}
66
-}
67
-
68
-type todoCounter struct {
69
- count int32
70
- done chan struct{}
71
- sync.RWMutex
72
-}
73
-
74
-// NewSyncCounter constructs a new counter
75
-func NewSyncCounter() Counter {
76
- return &todoCounter{
77
- done: make(chan struct{}),
78
- }
79
-}
80
-
81
-func (c *todoCounter) Increment(i uint32) {
82
- c.Lock()
83
- defer c.Unlock()
84
-
85
- if c.count < 0 {
86
- panic("counter already signaled done. use a new counter.")
87
- }
88
-
89
- // increment count
90
- c.count += int32(i)
91
-}
92
-
93
-// Decrement removes a number of todos to track.
94
-// If the count drops to zero, signals done and destroys the counter.
95
-// If the count drops **below** zero, panics. It means you have tried to remove
96
-// more things than you added, i.e. sync issues.
97
-func (c *todoCounter) Decrement(i uint32) {
98
- c.Lock()
99
- defer c.Unlock()
100
-
101
- if c.count < 0 {
102
- panic("counter already signaled done. probably have sync issues.")
103
- }
104
-
105
- if int32(i) > c.count {
106
- panic("decrement amount creater than counter. sync issues.")
107
- }
108
-
109
- c.count -= int32(i)
110
- if c.count == 0 { // done! signal it.
111
- c.count-- // set it to -1 to prevent reuse
112
- close(c.done) // a closed channel will always return nil
113
- }
114
-}
115
-
116
-func (c *todoCounter) Done() <-chan struct{} {
117
- return c.done
118
-}