This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
/
set_multimap.go
177 lines (157 loc) · 4.51 KB
/
set_multimap.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
package market
import (
"reflect"
"github.com/filecoin-project/go-state-types/abi"
cid "github.com/ipfs/go-cid"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"
"github.com/filecoin-project/specs-actors/v7/actors/util/adt"
)
type SetMultimap struct {
mp *adt.Map
store adt.Store
innerBitwidth int
}
// Interprets a store as a HAMT-based map of HAMT-based sets with root `r`.
// Both inner and outer HAMTs are interpreted with branching factor 2^bitwidth.
func AsSetMultimap(s adt.Store, r cid.Cid, outerBitwidth, innerBitwidth int) (*SetMultimap, error) {
m, err := adt.AsMap(s, r, outerBitwidth)
if err != nil {
return nil, err
}
return &SetMultimap{mp: m, store: s, innerBitwidth: innerBitwidth}, nil
}
// Creates a new map backed by an empty HAMT and flushes it to the store.
// Both inner and outer HAMTs have branching factor 2^bitwidth.
func MakeEmptySetMultimap(s adt.Store, bitwidth int) (*SetMultimap, error) {
m, err := adt.MakeEmptyMap(s, bitwidth)
if err != nil {
return nil, err
}
return &SetMultimap{mp: m, store: s, innerBitwidth: bitwidth}, nil
}
// Writes a new empty map to the store and returns its CID.
func StoreEmptySetMultimap(s adt.Store, bitwidth int) (cid.Cid, error) {
mm, err := MakeEmptySetMultimap(s, bitwidth)
if err != nil {
return cid.Undef, err
}
return mm.Root()
}
// Returns the root cid of the underlying HAMT.
func (mm *SetMultimap) Root() (cid.Cid, error) {
return mm.mp.Root()
}
func (mm *SetMultimap) Put(epoch abi.ChainEpoch, v abi.DealID) error {
// Load the hamt under key, or initialize a new empty one if not found.
k := abi.UIntKey(uint64(epoch))
set, found, err := mm.get(k)
if err != nil {
return err
}
if !found {
set, err = adt.MakeEmptySet(mm.store, mm.innerBitwidth)
if err != nil {
return err
}
}
// Add to the set.
if err = set.Put(dealKey(v)); err != nil {
return xerrors.Errorf("failed to add key to set %v: %w", epoch, err)
}
src, err := set.Root()
if err != nil {
return xerrors.Errorf("failed to flush set root: %w", err)
}
// Store the new set root under key.
newSetRoot := cbg.CborCid(src)
err = mm.mp.Put(k, &newSetRoot)
if err != nil {
return xerrors.Errorf("failed to store set: %w", err)
}
return nil
}
func (mm *SetMultimap) PutMany(epoch abi.ChainEpoch, vs []abi.DealID) error {
// Load the hamt under key, or initialize a new empty one if not found.
k := abi.UIntKey(uint64(epoch))
set, found, err := mm.get(k)
if err != nil {
return err
}
if !found {
set, err = adt.MakeEmptySet(mm.store, mm.innerBitwidth)
if err != nil {
return err
}
}
// Add to the set.
for _, v := range vs {
if err = set.Put(dealKey(v)); err != nil {
return xerrors.Errorf("failed to add key to set %v: %w", epoch, err)
}
}
src, err := set.Root()
if err != nil {
return xerrors.Errorf("failed to flush set root: %w", err)
}
// Store the new set root under key.
newSetRoot := cbg.CborCid(src)
err = mm.mp.Put(k, &newSetRoot)
if err != nil {
return xerrors.Errorf("failed to store set: %w", err)
}
return nil
}
// Removes all values for a key.
func (mm *SetMultimap) RemoveAll(key abi.ChainEpoch) error {
if _, err := mm.mp.TryDelete(abi.UIntKey(uint64(key))); err != nil {
return xerrors.Errorf("failed to delete set key %v: %w", key, err)
}
return nil
}
// Iterates all entries for a key, iteration halts if the function returns an error.
func (mm *SetMultimap) ForEach(epoch abi.ChainEpoch, fn func(id abi.DealID) error) error {
set, found, err := mm.get(abi.UIntKey(uint64(epoch)))
if err != nil {
return err
}
if found {
return set.ForEach(func(k string) error {
v, err := parseDealKey(k)
if err != nil {
return err
}
return fn(v)
})
}
return nil
}
func (mm *SetMultimap) get(key abi.Keyer) (*adt.Set, bool, error) {
var setRoot cbg.CborCid
found, err := mm.mp.Get(key, &setRoot)
if err != nil {
return nil, false, xerrors.Errorf("failed to load set key: %v: %w", key, err)
}
var set *adt.Set
if found {
set, err = adt.AsSet(mm.store, cid.Cid(setRoot), mm.innerBitwidth)
if err != nil {
return nil, false, err
}
}
return set, found, nil
}
func dealKey(e abi.DealID) abi.Keyer {
return abi.UIntKey(uint64(e))
}
func parseDealKey(s string) (abi.DealID, error) {
key, err := abi.ParseUIntKey(s)
return abi.DealID(key), err
}
func init() {
// Check that DealID is indeed an unsigned integer to confirm that dealKey is making the right interpretation.
var e abi.DealID
if reflect.TypeOf(e).Kind() != reflect.Uint64 {
panic("incorrect sector number encoding")
}
}