-
Notifications
You must be signed in to change notification settings - Fork 2
/
mantaray.go
201 lines (163 loc) · 4.55 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
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package manifest
import (
"context"
"errors"
"fmt"
"github.com/ethsana/sana/pkg/file"
"github.com/ethsana/sana/pkg/manifest/mantaray"
"github.com/ethsana/sana/pkg/swarm"
)
const (
// ManifestMantarayContentType represents content type used for noting that
// specific file should be processed as mantaray manifest.
ManifestMantarayContentType = "application/sana-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 swarm.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) 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 := swarm.NewAddress(node.Entry())
entry := NewEntry(address, node.Metadata())
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) (swarm.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 swarm.ZeroAddress, fmt.Errorf("manifest save error: %w", err)
}
address := swarm.NewAddress(m.trie.Reference())
return address, nil
}
func (m *mantarayManifest) IterateAddresses(ctx context.Context, fn swarm.AddressIterFunc) error {
reference := swarm.NewAddress(m.trie.Reference())
if swarm.ZeroAddress.Equal(reference) {
return ErrMissingReference
}
emptyAddr := swarm.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 := swarm.NewAddress(node.Reference())
err = fn(ref)
if err != nil {
return err
}
}
if node.IsValueType() && len(node.Entry()) > 0 {
entry := swarm.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) ([]byte, error) {
return ls.ls.Load(ctx, ref)
}
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)
}