-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple.go
167 lines (134 loc) · 3.94 KB
/
simple.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
package manifest
import (
"context"
"errors"
"fmt"
"github.com/FavorLabs/favorX/pkg/boson"
"github.com/FavorLabs/favorX/pkg/file"
"github.com/FavorLabs/manifest/simple"
)
const (
// ManifestSimpleContentType represents content type used for noting that
// specific file should be processed as 'simple' manifest
ManifestSimpleContentType = "application/manifest-simple+json"
)
type simpleManifest struct {
manifest simple.Manifest
reference boson.Address
ls file.LoadSaver
}
// NewSimpleManifest creates a new simple manifest.
func NewSimpleManifest(ls file.LoadSaver) (Interface, error) {
return &simpleManifest{
manifest: simple.NewManifest(),
ls: ls,
}, nil
}
// NewSimpleManifestReference loads existing simple manifest.
func NewSimpleManifestReference(ref boson.Address, l file.LoadSaver) (Interface, error) {
m := &simpleManifest{
manifest: simple.NewManifest(),
reference: ref,
ls: l,
}
err := m.load(context.Background(), ref, 0)
return m, err
}
func (m *simpleManifest) Type() string {
return ManifestSimpleContentType
}
func (m *simpleManifest) Add(_ context.Context, path string, entry Entry) error {
e := entry.Reference().String()
return m.manifest.Add(path, e, entry.Metadata())
}
func (m *simpleManifest) Remove(_ context.Context, path string) error {
err := m.manifest.Remove(path)
if err != nil {
if errors.Is(err, simple.ErrNotFound) {
return ErrNotFound
}
return err
}
return nil
}
func (m *simpleManifest) Move(_ context.Context, _ boson.Address, _, _ string, _ bool) error {
return nil
}
func (m *simpleManifest) Copy(_ context.Context, _ boson.Address, _, _ string, _ bool) error {
return nil
}
func (m *simpleManifest) Lookup(_ context.Context, path string) (Entry, error) {
n, err := m.manifest.Lookup(path)
if err != nil {
return nil, ErrNotFound
}
address, err := boson.ParseHexAddress(n.Reference())
if err != nil {
return nil, fmt.Errorf("parse boson address: %w", err)
}
entry := NewEntry(address, n.Metadata(), 0)
return entry, nil
}
func (m *simpleManifest) HasPrefix(_ context.Context, prefix string) (bool, error) {
return m.manifest.HasPrefix(prefix), nil
}
func (m *simpleManifest) Store(ctx context.Context, storeSizeFn ...StoreSizeFunc) (boson.Address, error) {
data, err := m.manifest.MarshalBinary()
if err != nil {
return boson.ZeroAddress, fmt.Errorf("manifest marshal error: %w", err)
}
if len(storeSizeFn) > 0 {
dataLen := int64(len(data))
for i := range storeSizeFn {
err = storeSizeFn[i](dataLen)
if err != nil {
return boson.ZeroAddress, fmt.Errorf("manifest store size func: %w", err)
}
}
}
ref, err := m.ls.Save(ctx, data)
if err != nil {
return boson.ZeroAddress, fmt.Errorf("manifest save error: %w", err)
}
m.reference = boson.NewAddress(ref)
return m.reference, nil
}
func (m *simpleManifest) IterateDirectories(ctx context.Context, path []byte, level int, fn NodeIterFunc) error {
return nil
}
func (m *simpleManifest) IterateAddresses(ctx context.Context, fn boson.AddressIterFunc) error {
if boson.ZeroAddress.Equal(m.reference) {
return ErrMissingReference
}
// NOTE: making it behave same for all manifest implementation
err := fn(m.reference)
if err != nil {
return fmt.Errorf("manifest iterate addresses: %w", err)
}
walker := func(path string, entry simple.Entry, err error) error {
if err != nil {
return err
}
ref, err := boson.ParseHexAddress(entry.Reference())
if err != nil {
return err
}
return fn(ref)
}
err = m.manifest.WalkEntry("", walker)
if err != nil {
return fmt.Errorf("manifest iterate addresses: %w", err)
}
return nil
}
func (m *simpleManifest) load(ctx context.Context, reference boson.Address, index int64) error {
buf, err := m.ls.Load(ctx, reference.Bytes(), index)
if err != nil {
return fmt.Errorf("manifest load error: %w", err)
}
err = m.manifest.UnmarshalBinary(buf)
if err != nil {
return fmt.Errorf("manifest unmarshal error: %w", err)
}
return nil
}