-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathput.go
195 lines (169 loc) · 5.53 KB
/
put.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
package pail
import (
"context"
"errors"
"fmt"
"slices"
"strings"
"github.com/ipld/go-ipld-prime"
"github.com/storacha/go-pail/block"
"github.com/storacha/go-pail/shard"
)
// Put a value (a CID) for the given key. If the key exists it's value is
// overwritten.
func Put(ctx context.Context, blocks block.Fetcher, root ipld.Link, key string, value ipld.Link) (ipld.Link, shard.Diff, error) {
shards := shard.NewFetcher(blocks)
rshard, err := shards.GetRoot(ctx, root)
if err != nil {
return nil, shard.Diff{}, err
}
if rshard.Value().KeyChars() != shard.KeyCharsASCII {
return nil, shard.Diff{}, fmt.Errorf("unsupported key character set: %s", rshard.Value().KeyChars())
}
if !shard.IsPrintableASCII(key) {
return nil, shard.Diff{}, errors.New("key contains non-ASCII characters")
}
if int64(len(key)) > rshard.Value().MaxKeySize() {
return nil, shard.Diff{}, fmt.Errorf("UTF-8 encoded key exceeds max size of %d bytes", rshard.Value().MaxKeySize())
}
path, err := traverse(ctx, shards, shard.AsBlock(rshard), key)
if err != nil {
return nil, shard.Diff{}, fmt.Errorf("traversing shard: %w", err)
}
target := path[len(path)-1]
skey := key[len(target.Value().Prefix()):]
entry := shard.NewEntry(skey, shard.NewValue(value, nil))
targetEntries := target.Value().Entries()[:]
var additions []shard.BlockView
for i, e := range targetEntries {
k := e.Key()
v := e.Value()
// is this just a replace?
if k == skey {
break
}
// do we need to shard this entry?
var shortest string
var longest string
if len(k) < len(skey) {
shortest = k
longest = skey
} else {
shortest = skey
longest = k
}
common := ""
for _, char := range shortest {
next := common + string(char)
if !strings.HasPrefix(longest, next) {
break
}
common = next
}
if common != "" {
var entries []shard.Entry
// if the existing entry key or new key is equal to the common prefix,
// then the existing value / new value needs to persist in the parent
// shard. Otherwise they persist in this new shard.
if common != skey {
entries = shard.PutEntry(
entries,
shard.NewEntry(skey[len(common):], shard.NewValue(value, nil)),
)
}
if common != k {
entries = shard.PutEntry(entries, shard.NewEntry(k[len(common):], v))
}
child, err := shard.MarshalBlock(shard.New(target.Value().Prefix()+common, entries))
if err != nil {
return nil, shard.Diff{}, err
}
additions = append(additions, child)
// create parent shards for each character of the common prefix
var commonChars []string
for _, c := range common {
commonChars = append(commonChars, string(c))
}
for i := len(commonChars) - 1; i > 0; i-- {
parentPrefix := target.Value().Prefix() + strings.Join(commonChars[0:i], "")
var parentValue shard.Value
// if the first iteration and the existing entry key is equal to the
// common prefix, then existing value needs to persist in this parent
if i == len(commonChars)-1 && common == k {
if v.Shard() != nil {
return nil, shard.Diff{}, errors.New("found a shard link when expecting a value")
}
parentValue = shard.NewValue(v.Value(), child.Link())
} else if i == len(commonChars)-1 && common == skey {
parentValue = shard.NewValue(value, child.Link())
} else {
parentValue = shard.NewValue(nil, child.Link())
}
parent, err := shard.MarshalBlock(
shard.New(
parentPrefix,
[]shard.Entry{shard.NewEntry(commonChars[i], parentValue)},
),
)
if err != nil {
return nil, shard.Diff{}, err
}
additions = append(additions, parent)
child = parent
}
// remove the sharded entry
targetEntries = slices.Delete(targetEntries, i, i+1)
// create the entry that will be added to target
if len(commonChars) == 1 && common == k {
entry = shard.NewEntry(commonChars[0], shard.NewValue(v.Value(), child.Link()))
} else if len(commonChars) == 1 && common == skey {
entry = shard.NewEntry(commonChars[0], shard.NewValue(value, child.Link()))
} else {
entry = shard.NewEntry(commonChars[0], shard.NewValue(nil, child.Link()))
}
break
}
}
var nshard shard.Shard
if target.Value().Prefix() == "" {
nshard = shard.NewRoot(shard.PutEntry(targetEntries, entry))
} else {
nshard = shard.New(target.Value().Prefix(), shard.PutEntry(targetEntries, entry))
}
child, err := shard.MarshalBlock(nshard)
if err != nil {
return nil, shard.Diff{}, err
}
// if no change in the target then we're done
if child.Link().String() == target.Link().String() {
return root, shard.Diff{}, nil
}
additions = append(additions, child)
// path is root -> target, so work backwards, propagating the new shard CID
for i := len(path) - 2; i >= 0; i-- {
parent := path[i]
key := child.Value().Prefix()[len(parent.Value().Prefix()):]
entries := parent.Value().Entries()[:]
for i, e := range entries {
if e.Key() == key {
if e.Value().Shard() == nil {
return nil, shard.Diff{}, fmt.Errorf("\"%s\" is not a shard link in: %s", key, parent.Link().String())
}
entries[i] = shard.NewEntry(key, shard.NewValue(e.Value().Value(), child.Link()))
break
}
}
var cshard shard.Shard
if parent.Value().Prefix() == "" {
cshard = shard.NewRoot(entries)
} else {
cshard = shard.New(parent.Value().Prefix(), entries)
}
child, err = shard.MarshalBlock(cshard)
if err != nil {
return nil, shard.Diff{}, err
}
additions = append(additions, child)
}
return additions[len(additions)-1].Link(), shard.Diff{Additions: additions, Removals: path}, nil
}