reflector.go/dht/store.go

64 lines
1.3 KiB
Go
Raw Normal View History

2018-03-07 22:15:58 +01:00
package dht
2018-03-08 01:49:33 +01:00
import "sync"
2018-03-07 22:15:58 +01:00
type peer struct {
2018-04-28 02:16:12 +02:00
contact Contact
//<lastPublished>,
//<originallyPublished>
// <originalPublisherID>
2018-03-07 22:15:58 +01:00
}
type peerStore struct {
2018-04-04 17:43:27 +02:00
// map of blob hashes to (map of node IDs to bools)
hashes map[Bitmap]map[Bitmap]bool
2018-04-28 02:16:12 +02:00
// stores the peers themselves, so they can be updated in one place
peers map[Bitmap]peer
lock sync.RWMutex
2018-03-07 22:15:58 +01:00
}
func newPeerStore() *peerStore {
return &peerStore{
2018-04-28 02:16:12 +02:00
hashes: make(map[Bitmap]map[Bitmap]bool),
peers: make(map[Bitmap]peer),
2018-03-07 22:15:58 +01:00
}
}
2018-04-28 02:16:12 +02:00
func (s *peerStore) Upsert(blobHash Bitmap, contact Contact) {
2018-03-07 22:15:58 +01:00
s.lock.Lock()
defer s.lock.Unlock()
2018-04-28 02:16:12 +02:00
2018-04-05 17:35:57 +02:00
if _, ok := s.hashes[blobHash]; !ok {
s.hashes[blobHash] = make(map[Bitmap]bool)
2018-03-07 22:15:58 +01:00
}
2018-04-28 02:16:12 +02:00
s.hashes[blobHash][contact.id] = true
s.peers[contact.id] = peer{contact: contact}
2018-03-07 22:15:58 +01:00
}
2018-04-28 02:16:12 +02:00
func (s *peerStore) Get(blobHash Bitmap) []Contact {
2018-03-07 22:15:58 +01:00
s.lock.RLock()
defer s.lock.RUnlock()
2018-04-28 02:16:12 +02:00
var contacts []Contact
2018-04-05 17:35:57 +02:00
if ids, ok := s.hashes[blobHash]; ok {
for id := range ids {
2018-04-28 02:16:12 +02:00
peer, ok := s.peers[id]
if !ok {
panic("node id in IDs list, but not in nodeInfo")
}
2018-04-28 02:16:12 +02:00
contacts = append(contacts, peer.contact)
2018-03-07 22:15:58 +01:00
}
}
2018-04-28 02:16:12 +02:00
return contacts
}
func (s *peerStore) RemoveTODO(contact Contact) {
// TODO: remove peer from everywhere
2018-03-07 22:15:58 +01:00
}
2018-04-05 17:35:57 +02:00
func (s *peerStore) CountStoredHashes() int {
s.lock.RLock()
defer s.lock.RUnlock()
2018-04-05 17:35:57 +02:00
return len(s.hashes)
}