bucket.go 1.59 KB
Newer Older
1 2 3 4
package dht

import (
	"container/list"
5
	"sync"
6 7 8

	peer "github.com/jbenet/go-ipfs/peer"
)
Jeromy's avatar
Jeromy committed
9

10
// Bucket holds a list of peers.
11 12 13 14 15
type Bucket struct {
	lk   sync.RWMutex
	list *list.List
}

16
func newBucket() *Bucket {
17 18 19 20
	b := new(Bucket)
	b.list = list.New()
	return b
}
21

22
func (b *Bucket) find(id peer.ID) *list.Element {
23 24 25
	b.lk.RLock()
	defer b.lk.RUnlock()
	for e := b.list.Front(); e != nil; e = e.Next() {
26
		if e.Value.(peer.Peer).ID().Equal(id) {
27 28 29 30 31 32
			return e
		}
	}
	return nil
}

33
func (b *Bucket) moveToFront(e *list.Element) {
34 35 36
	b.lk.Lock()
	b.list.MoveToFront(e)
	b.lk.Unlock()
37 38
}

39
func (b *Bucket) pushFront(p peer.Peer) {
40 41 42
	b.lk.Lock()
	b.list.PushFront(p)
	b.lk.Unlock()
43 44
}

45
func (b *Bucket) popBack() peer.Peer {
46 47 48 49
	b.lk.Lock()
	defer b.lk.Unlock()
	last := b.list.Back()
	b.list.Remove(last)
50
	return last.Value.(peer.Peer)
51 52
}

53
func (b *Bucket) len() int {
54 55 56
	b.lk.RLock()
	defer b.lk.RUnlock()
	return b.list.Len()
57 58
}

59
// Split splits a buckets peers into two buckets, the methods receiver will have
60 61 62
// peers with CPL equal to cpl, the returned bucket will have peers with CPL
// greater than cpl (returned bucket has closer peers)
func (b *Bucket) Split(cpl int, target ID) *Bucket {
63 64 65
	b.lk.Lock()
	defer b.lk.Unlock()

66
	out := list.New()
67
	newbuck := newBucket()
68 69
	newbuck.list = out
	e := b.list.Front()
70
	for e != nil {
71
		peerID := ConvertPeerID(e.Value.(peer.Peer).ID())
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72
		peerCPL := commonPrefixLen(peerID, target)
73
		if peerCPL > cpl {
74 75 76
			cur := e
			out.PushBack(e.Value)
			e = e.Next()
77
			b.list.Remove(cur)
78 79 80 81
			continue
		}
		e = e.Next()
	}
82
	return newbuck
83 84 85
}

func (b *Bucket) getIter() *list.Element {
86
	return b.list.Front()
87
}