-
Notifications
You must be signed in to change notification settings - Fork 458
/
sorted_cid_set.go
215 lines (188 loc) · 5.09 KB
/
sorted_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package types
import (
"encoding/json"
"fmt"
"sort"
cid "github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/polydawn/refmt/obj/atlas"
)
func init() {
cbor.RegisterCborType(atlas.BuildEntry(SortedCidSet{}).Transform().
TransformMarshal(atlas.MakeMarshalTransformFunc(
func(s SortedCidSet) ([]cid.Cid, error) {
return s.s, nil
})).
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
func(s []cid.Cid) (SortedCidSet, error) {
for i := 0; i < len(s)-1; i++ {
// Note that this will also catch duplicates.
if !cidLess(s[i], s[i+1]) {
return SortedCidSet{}, fmt.Errorf(
"invalid serialization of SortedCidSet - %s not less than %s", s[i].String(), s[i+1].String())
}
}
return SortedCidSet{s: s}, nil
})).
Complete())
}
// SortedCidSet is a set of Cids that is maintained sorted. The externally visible effect as
// compared to cid.Set is that iteration is cheap and always in-order.
// Sort order is lexicographic ascending, by serialization of the cid.
// TODO: This should probably go into go-cid package - see https://github.com/ipfs/go-cid/issues/45.
type SortedCidSet struct {
s []cid.Cid // should be maintained sorted
}
// NewSortedCidSet returns a SortedCidSet with the specified items.
func NewSortedCidSet(ids ...cid.Cid) (res SortedCidSet) {
for _, id := range ids {
res.Add(id)
}
return
}
// Add adds a cid to the set. Returns true if the item was added (didn't already exist), false
// otherwise.
func (s *SortedCidSet) Add(id cid.Cid) bool {
idx := s.search(id)
if idx < len(s.s) && s.s[idx].Equals(id) {
return false
}
s.s = append(s.s, cid.Undef)
copy(s.s[idx+1:], s.s[idx:])
s.s[idx] = id
return true
}
// Has returns true if the set contains the specified cid.
func (s SortedCidSet) Has(id cid.Cid) bool {
idx := s.search(id)
return idx < len(s.s) && s.s[idx].Equals(id)
}
// Len returns the number of items in the set.
func (s SortedCidSet) Len() int {
return len(s.s)
}
// Empty returns true if the set is empty.
func (s SortedCidSet) Empty() bool {
return s.Len() == 0
}
// Remove removes a cid from the set. Returns true if the item was removed (did in fact exist in
// the set), false otherwise.
func (s *SortedCidSet) Remove(id cid.Cid) bool {
idx := s.search(id)
if idx < len(s.s) && s.s[idx].Equals(id) {
copy(s.s[idx:], s.s[idx+1:])
s.s = s.s[0 : len(s.s)-1]
return true
}
return false
}
// Clear removes all entries from the set.
func (s *SortedCidSet) Clear() {
s.s = s.s[:0]
}
// Iter returns an iterator that allows the caller to iterate the set in its sort order.
func (s SortedCidSet) Iter() SortedCidSetIterator {
return SortedCidSetIterator{
s: s.s,
i: 0,
}
}
// Equals returns true if the set contains the same items as another set.
func (s SortedCidSet) Equals(s2 SortedCidSet) bool {
if s.Len() != s2.Len() {
return false
}
i1 := s.Iter()
i2 := s2.Iter()
for i := 0; i < s.Len(); i++ {
if !i1.Value().Equals(i2.Value()) {
return false
}
}
return true
}
// Contains checks if s2 is a sub-tipset of s
func (s *SortedCidSet) Contains(s2 *SortedCidSet) bool {
for it := s2.Iter(); !it.Complete(); it.Next() {
if !s.Has(it.Value()) {
return false
}
}
return true
}
// String returns a string listing the cids in the set.
func (s SortedCidSet) String() string {
out := "{"
for it := s.Iter(); !it.Complete(); it.Next() {
out = fmt.Sprintf("%s %s", out, it.Value().String())
}
return out + " }"
}
// ToSlice returns a slice listing the cids in the set.
func (s SortedCidSet) ToSlice() []cid.Cid {
out := make([]cid.Cid, s.Len())
var i int
for it := s.Iter(); !it.Complete(); it.Next() {
out[i] = it.Value()
i++
}
return out
}
// MarshalJSON serializes the set to JSON.
func (s SortedCidSet) MarshalJSON() ([]byte, error) {
return json.Marshal(s.s)
}
// UnmarshalJSON parses JSON into the set.
func (s *SortedCidSet) UnmarshalJSON(b []byte) error {
var ts []cid.Cid
if err := json.Unmarshal(b, &ts); err != nil {
return err
}
for i := 0; i < len(ts)-1; i++ {
if !cidLess(ts[i], ts[i+1]) {
return fmt.Errorf("invalid input - cids not sorted")
}
}
s.s = ts
return nil
}
func (s SortedCidSet) search(id cid.Cid) int {
return sort.Search(len(s.s), func(i int) bool {
return !cidLess(s.s[i], id)
})
}
// SortedCidSetIterator is a iterator over a sorted collection of CIDs.
type SortedCidSetIterator struct {
s []cid.Cid
i int
}
// Complete returns true if the iterator has reached the end of the set.
func (si *SortedCidSetIterator) Complete() bool {
return si.i >= len(si.s)
}
// Next advances the iterator to the next item and returns true if there is such an item.
func (si *SortedCidSetIterator) Next() bool {
switch {
case si.i < len(si.s):
si.i++
return si.i < len(si.s)
case si.i == len(si.s):
return false
default:
panic("unreached")
}
}
// Value returns the current item for the iterator
func (si SortedCidSetIterator) Value() cid.Cid {
switch {
case si.i < len(si.s):
return si.s[si.i]
case si.i == len(si.s):
return cid.Undef
default:
panic("unreached")
}
}
func cidLess(c1, c2 cid.Cid) bool {
return c1.KeyString() < c2.KeyString()
}