diff --git a/blocks/bloom/filter.go b/blocks/bloom/filter.go deleted file mode 100644 index a414297cf..000000000 --- a/blocks/bloom/filter.go +++ /dev/null @@ -1,130 +0,0 @@ -// Package bloom implements a simple bloom filter. -package bloom - -import ( - "encoding/binary" - "errors" - // Non crypto hash, because speed - "gx/ipfs/QmeWQMDa5dSdP4n8WDeoY5z8L2EKVqF4ZvK4VEHsLqXsGu/hamming" - "hash" - - "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/mtchavez/jenkins" -) - -// A Filter represents a bloom filter. -type Filter interface { - Add([]byte) - Find([]byte) bool - Merge(Filter) (Filter, error) - HammingDistance(Filter) (int, error) -} - -// NewFilter creates a new bloom Filter with the given -// size. k (the number of hash functions), is hardcoded to 3. -func NewFilter(size int) Filter { - return &filter{ - hash: jenkins.New(), - filter: make([]byte, size), - k: 3, - } -} - -type filter struct { - filter []byte - hash hash.Hash32 - k int -} - -// BasicFilter calls NewFilter with a bloom filter size of -// 2048 bytes. -func BasicFilter() Filter { - return NewFilter(2048) -} - -func (f *filter) Add(bytes []byte) { - for _, bit := range f.getBitIndicies(bytes) { - f.setBit(bit) - } -} - -func (f *filter) getBitIndicies(bytes []byte) []uint32 { - indicies := make([]uint32, f.k) - - f.hash.Write(bytes) - b := make([]byte, 4) - - for i := 0; i < f.k; i++ { - res := f.hash.Sum32() - indicies[i] = res % (uint32(len(f.filter)) * 8) - - binary.LittleEndian.PutUint32(b, res) - f.hash.Write(b) - } - - f.hash.Reset() - - return indicies -} - -func (f *filter) Find(bytes []byte) bool { - for _, bit := range f.getBitIndicies(bytes) { - if !f.getBit(bit) { - return false - } - } - return true -} - -func (f *filter) setBit(i uint32) { - f.filter[i/8] |= (1 << byte(i%8)) -} - -func (f *filter) getBit(i uint32) bool { - return f.filter[i/8]&(1<" - } - var out string - for i, v := range e.Errors { - if v != nil { - out += fmt.Sprintf("%d: %s\n", i, v) - } - } - return out -} - -func New(errs ...error) *Error { - return &Error{errs} -} diff --git a/thirdparty/todocounter/counter.go b/thirdparty/todocounter/counter.go deleted file mode 100644 index 0df638204..000000000 --- a/thirdparty/todocounter/counter.go +++ /dev/null @@ -1,118 +0,0 @@ -package todocounter - -import ( - "sync" -) - -// Counter records things remaining to process. It is needed for complicated -// cases where multiple goroutines are spawned to process items, and they may -// generate more items to process. For example, say a query over a set of nodes -// may yield either a result value, or more nodes to query. Signaling is subtly -// complicated, because the queue may be empty while items are being processed, -// that will end up adding more items to the queue. -// -// Use Counter like this: -// -// todos := make(chan int, 10) -// ctr := todoctr.NewCounter() -// -// process := func(item int) { -// fmt.Println("processing %d\n...", item) -// -// // this task may randomly generate more tasks -// if rand.Intn(5) == 0 { -// todos<- item + 1 -// ctr.Increment(1) // increment counter for new task. -// } -// -// ctr.Decrement(1) // decrement one to signal the task being done. -// } -// -// // add some tasks. -// todos<- 1 -// todos<- 2 -// todos<- 3 -// todos<- 4 -// ctr.Increment(4) -// -// for { -// select { -// case item := <- todos: -// go process(item) -// case <-ctr.Done(): -// fmt.Println("done processing everything.") -// close(todos) -// } -// } -type Counter interface { - // Incrememnt adds a number of todos to track. - // If the counter is **below** zero, it panics. - Increment(i uint32) - - // Decrement removes a number of todos to track. - // If the count drops to zero, signals done and destroys the counter. - // If the count drops **below** zero, panics. It means you have tried to remove - // more things than you added, i.e. sync issues. - Decrement(i uint32) - - // Done returns a channel to wait upon. Use it in selects: - // - // select { - // case <-ctr.Done(): - // // done processing all items - // } - // - Done() <-chan struct{} -} - -type todoCounter struct { - count int32 - done chan struct{} - sync.RWMutex -} - -// NewSyncCounter constructs a new counter -func NewSyncCounter() Counter { - return &todoCounter{ - done: make(chan struct{}), - } -} - -func (c *todoCounter) Increment(i uint32) { - c.Lock() - defer c.Unlock() - - if c.count < 0 { - panic("counter already signaled done. use a new counter.") - } - - // increment count - c.count += int32(i) -} - -// Decrement removes a number of todos to track. -// If the count drops to zero, signals done and destroys the counter. -// If the count drops **below** zero, panics. It means you have tried to remove -// more things than you added, i.e. sync issues. -func (c *todoCounter) Decrement(i uint32) { - c.Lock() - defer c.Unlock() - - if c.count < 0 { - panic("counter already signaled done. probably have sync issues.") - } - - if int32(i) > c.count { - panic("decrement amount creater than counter. sync issues.") - } - - c.count -= int32(i) - if c.count == 0 { // done! signal it. - c.count-- // set it to -1 to prevent reuse - close(c.done) // a closed channel will always return nil - } -} - -func (c *todoCounter) Done() <-chan struct{} { - return c.done -}