lbry.go/dht/store.go

63 lines
1.4 KiB
Go
Raw Normal View History

2018-03-07 16:15:58 -05:00
package dht
2018-06-14 11:48:02 -04:00
import (
"sync"
2019-10-10 05:07:33 +02:00
"github.com/lbryio/lbry.go/v2/dht/bits"
2018-06-14 11:48:02 -04:00
)
2018-03-07 16:15:58 -05:00
2018-05-22 12:16:01 -04:00
// TODO: expire stored data after tExpire time
type contactStore struct {
2018-04-04 11:43:27 -04:00
// map of blob hashes to (map of node IDs to bools)
2018-06-14 11:48:02 -04:00
hashes map[bits.Bitmap]map[bits.Bitmap]bool
2018-04-27 20:16:12 -04:00
// stores the peers themselves, so they can be updated in one place
2018-06-14 11:48:02 -04:00
contacts map[bits.Bitmap]Contact
2018-05-01 16:18:38 -04:00
lock sync.RWMutex
2018-03-07 16:15:58 -05:00
}
func newStore() *contactStore {
return &contactStore{
2018-06-14 11:48:02 -04:00
hashes: make(map[bits.Bitmap]map[bits.Bitmap]bool),
contacts: make(map[bits.Bitmap]Contact),
2018-03-07 16:15:58 -05:00
}
}
func (s *contactStore) Upsert(blobHash bits.Bitmap, contact Contact) {
2018-03-07 16:15:58 -05:00
s.lock.Lock()
defer s.lock.Unlock()
2018-04-27 20:16:12 -04:00
2018-04-05 11:35:57 -04:00
if _, ok := s.hashes[blobHash]; !ok {
2018-06-14 11:48:02 -04:00
s.hashes[blobHash] = make(map[bits.Bitmap]bool)
2018-03-07 16:15:58 -05:00
}
s.hashes[blobHash][contact.ID] = true
s.contacts[contact.ID] = contact
2018-03-07 16:15:58 -05:00
}
2018-06-14 11:48:02 -04:00
func (s *contactStore) Get(blobHash bits.Bitmap) []Contact {
2018-03-07 16:15:58 -05:00
s.lock.RLock()
defer s.lock.RUnlock()
2018-04-27 20:16:12 -04:00
var contacts []Contact
2018-04-05 11:35:57 -04:00
if ids, ok := s.hashes[blobHash]; ok {
for id := range ids {
2018-05-01 16:18:38 -04:00
contact, ok := s.contacts[id]
if !ok {
panic("node id in IDs list, but not in nodeInfo")
}
contacts = append(contacts, contact)
2018-03-07 16:15:58 -05:00
}
}
2018-04-27 20:16:12 -04:00
return contacts
}
func (s *contactStore) RemoveTODO(contact Contact) {
2018-04-27 20:16:12 -04:00
// TODO: remove peer from everywhere
2018-03-07 16:15:58 -05:00
}
func (s *contactStore) CountStoredHashes() int {
s.lock.RLock()
defer s.lock.RUnlock()
2018-04-05 11:35:57 -04:00
return len(s.hashes)
}