/
safe_cid_set.go
97 lines (83 loc) · 1.74 KB
/
safe_cid_set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package blockstore
import (
"sync"
"github.com/ipfs/go-cid"
)
// Set is a implementation of a set of Cids, that is, a structure
// to which holds a single copy of every Cids that is added to it.
type Set struct {
set map[cid.Cid]struct{}
lk sync.Mutex
}
// NewSet initializes and returns a new Set.
func NewSet() *Set {
return &Set{set: make(map[cid.Cid]struct{}), lk: sync.Mutex{}}
}
// Add puts a Cid in the Set.
func (s *Set) Add(c cid.Cid) {
s.lk.Lock()
defer s.lk.Unlock()
s.set[c] = struct{}{}
}
// Add puts a Cid in the Set.
func (s *Set) add(c cid.Cid) {
s.set[c] = struct{}{}
}
// Has returns if the Set contains a given Cid.
func (s *Set) Has(c cid.Cid) bool {
s.lk.Lock()
defer s.lk.Unlock()
_, ok := s.set[c]
return ok
}
// Has returns if the Set contains a given Cid.
func (s *Set) has(c cid.Cid) bool {
_, ok := s.set[c]
return ok
}
// Remove deletes a Cid from the Set.
func (s *Set) Remove(c cid.Cid) {
s.lk.Lock()
defer s.lk.Unlock()
delete(s.set, c)
}
// Len returns how many elements the Set has.
func (s *Set) Len() int {
s.lk.Lock()
defer s.lk.Unlock()
return len(s.set)
}
// Keys returns the Cids in the set.
func (s *Set) Keys() []cid.Cid {
s.lk.Lock()
defer s.lk.Unlock()
out := make([]cid.Cid, 0, len(s.set))
for k := range s.set {
out = append(out, k)
}
return out
}
// Visit adds a Cid to the set only if it is
// not in it already.
func (s *Set) Visit(c cid.Cid) bool {
s.lk.Lock()
defer s.lk.Unlock()
if !s.has(c) {
s.add(c)
return true
}
return false
}
// ForEach allows to run a custom function on each
// Cid in the set.
func (s *Set) ForEach(f func(c cid.Cid) error) error {
s.lk.Lock()
defer s.lk.Unlock()
for c := range s.set {
err := f(c)
if err != nil {
return err
}
}
return nil
}