-
Notifications
You must be signed in to change notification settings - Fork 3
/
mantaray.go
259 lines (211 loc) · 5.88 KB
/
mantaray.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package manifest
import (
"bytes"
"context"
"errors"
"fmt"
"math"
"github.com/FavorLabs/favorX/pkg/boson"
"github.com/FavorLabs/favorX/pkg/file"
"github.com/FavorLabs/manifest/mantaray"
)
const (
// ManifestMantarayContentType represents content type used for noting that
// specific file should be processed as mantaray manifest.
ManifestMantarayContentType = "application/manifest-mantaray+octet-stream"
)
type mantarayManifest struct {
trie *mantaray.Node
ls file.LoadSaver
}
// NewMantarayManifest creates a new mantaray-based manifest.
func NewMantarayManifest(
ls file.LoadSaver,
encrypted bool,
) (Interface, error) {
mm := &mantarayManifest{
trie: mantaray.New(),
ls: ls,
}
// use empty obfuscation key if not encrypting
if !encrypted {
// NOTE: it will be copied to all trie nodes
mm.trie.SetObfuscationKey(mantaray.ZeroObfuscationKey)
}
return mm, nil
}
// NewMantarayManifestReference loads existing mantaray-based manifest.
func NewMantarayManifestReference(
reference boson.Address,
ls file.LoadSaver,
) (Interface, error) {
return &mantarayManifest{
trie: mantaray.NewNodeRef(reference.Bytes()),
ls: ls,
}, nil
}
func (m *mantarayManifest) Type() string {
return ManifestMantarayContentType
}
func (m *mantarayManifest) Add(ctx context.Context, path string, entry Entry) error {
p := []byte(path)
e := entry.Reference().Bytes()
return m.trie.Add(ctx, p, e, entry.Metadata(), m.ls)
}
func (m *mantarayManifest) Remove(ctx context.Context, path string) error {
p := []byte(path)
err := m.trie.Remove(ctx, p, m.ls)
if err != nil {
if errors.Is(err, mantaray.ErrNotFound) {
return ErrNotFound
}
return err
}
return nil
}
func (m *mantarayManifest) Move(ctx context.Context, source boson.Address, path, newPath string, created bool) error {
p := []byte(path)
np := []byte(newPath)
root := m.trie
if !source.IsZero() && !bytes.Equal(source.Bytes(), m.trie.Reference()) {
root = mantaray.NewNodeRef(source.Bytes())
}
err := root.Move(ctx, m.trie, p, np, created, m.ls)
if err != nil {
if errors.Is(err, mantaray.ErrNotFound) {
return ErrNotFound
}
return err
}
return nil
}
func (m *mantarayManifest) Copy(ctx context.Context, source boson.Address, path, newPath string, created bool) error {
p := []byte(path)
np := []byte(newPath)
root := m.trie
if !source.IsZero() && !bytes.Equal(source.Bytes(), m.trie.Reference()) {
root = mantaray.NewNodeRef(source.Bytes())
}
err := root.Copy(ctx, m.trie, p, np, created, m.ls)
if err != nil {
if errors.Is(err, mantaray.ErrNotFound) {
return ErrNotFound
}
return err
}
return nil
}
func (m *mantarayManifest) Lookup(ctx context.Context, path string) (Entry, error) {
p := []byte(path)
node, err := m.trie.LookupNode(ctx, p, m.ls)
if err != nil {
if errors.Is(err, mantaray.ErrNotFound) {
return nil, ErrNotFound
}
return nil, err
}
if !node.IsValueType() {
return nil, ErrNotFound
}
address := boson.NewAddress(node.Entry())
entry := NewEntry(address, node.Metadata(), node.Index())
return entry, nil
}
func (m *mantarayManifest) HasPrefix(ctx context.Context, prefix string) (bool, error) {
p := []byte(prefix)
return m.trie.HasPrefix(ctx, p, m.ls)
}
func (m *mantarayManifest) Store(ctx context.Context, storeSizeFn ...StoreSizeFunc) (boson.Address, error) {
var ls mantaray.LoadSaver
if len(storeSizeFn) > 0 {
ls = &mantarayLoadSaver{
ls: m.ls,
storeSizeFn: storeSizeFn,
}
} else {
ls = m.ls
}
err := m.trie.Save(ctx, ls)
if err != nil {
return boson.ZeroAddress, fmt.Errorf("manifest save error: %w", err)
}
address := boson.NewAddress(m.trie.Reference())
return address, nil
}
func (m *mantarayManifest) IterateDirectories(ctx context.Context, path []byte, level int, fn NodeIterFunc) error {
reference := boson.NewAddress(m.trie.Reference())
if boson.ZeroAddress.Equal(reference) {
return ErrMissingReference
}
var uLevel uint
if level > 0 && level < math.MaxInt32 {
uLevel = uint(level)
} else {
uLevel = mantaray.MaxLevel
}
err := m.trie.WalkLevel(ctx, path, m.ls, uLevel, mantaray.WalkLevelFunc(fn))
if err != nil {
return fmt.Errorf("manifest walk level: %w", err)
}
return nil
}
func (m *mantarayManifest) IterateAddresses(ctx context.Context, fn boson.AddressIterFunc) error {
reference := boson.NewAddress(m.trie.Reference())
if boson.ZeroAddress.Equal(reference) {
return ErrMissingReference
}
emptyAddr := boson.NewAddress([]byte{31: 0})
walker := func(path []byte, node *mantaray.Node, err error) error {
if err != nil {
return err
}
if node != nil {
if node.Reference() != nil {
ref := boson.NewAddress(node.Reference())
err = fn(ref)
if err != nil {
return err
}
}
if node.IsValueType() && len(node.Entry()) > 0 {
entry := boson.NewAddress(node.Entry())
// The following comparison to the emptyAddr is
// a dirty hack which prevents the walker to
// fail when it encounters an empty address
// (e.g.: during the unpin traversal operation
// for manifest). This workaround should be
// removed after the manifest serialization bug
// is fixed.
if entry.Equal(emptyAddr) {
return nil
}
if err = fn(entry); err != nil {
return err
}
}
}
return nil
}
err := m.trie.WalkNode(ctx, []byte{}, m.ls, walker)
if err != nil {
return fmt.Errorf("manifest iterate addresses: %w", err)
}
return nil
}
type mantarayLoadSaver struct {
ls file.LoadSaver
storeSizeFn []StoreSizeFunc
}
func (ls *mantarayLoadSaver) Load(ctx context.Context, ref []byte, index int64) ([]byte, error) {
return ls.ls.Load(ctx, ref, index)
}
func (ls *mantarayLoadSaver) Save(ctx context.Context, data []byte) ([]byte, error) {
dataLen := int64(len(data))
for i := range ls.storeSizeFn {
err := ls.storeSizeFn[i](dataLen)
if err != nil {
return nil, fmt.Errorf("manifest store size func: %w", err)
}
}
return ls.ls.Save(ctx, data)
}