-
Notifications
You must be signed in to change notification settings - Fork 13
/
batch.go
306 lines (268 loc) · 7.64 KB
/
batch.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
Copyright © 2020 FairOS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package collection
import (
"context"
"errors"
"fmt"
"path/filepath"
"strconv"
"sync/atomic"
"time"
)
const (
maxManifestDepth = 3
)
// Batch is to be used in KV table or a Document database
type Batch struct {
idx *Index
memDb *Manifest
manifestStack []*Manifest
storageCount uint64
}
// NewBatch creates a new batch index to be used in a KV table or a Document database.
func NewBatch(idx *Index) (*Batch, error) {
return &Batch{
idx: idx,
}, nil
}
// PutNumber inserts index as a number.
func (b *Batch) PutNumber(key float64, refValue []byte, apnd, memory bool) error {
stringKey := fmt.Sprintf("%020.20g", key)
return b.Put(stringKey, refValue, apnd, memory)
}
// Put creates an index entry given a key string and value.
func (b *Batch) Put(key string, value []byte, apnd, memory bool) error {
if b.idx.isReadOnlyFeed() { // skipcq: TCV-001
return ErrReadOnlyIndex
}
if b.memDb == nil {
manifest := &Manifest{
Name: b.idx.name,
IdxType: b.idx.indexType,
CreationTime: time.Now().Unix(),
dirtyFlag: true,
}
b.memDb = manifest
}
ctx := context.Background()
stringKey := key
if b.idx.indexType == NumberIndex {
i, err := strconv.ParseInt(stringKey, 10, 64)
if err != nil { // skipcq: TCV-001
return ErrKVKeyNotANumber
}
stringKey = fmt.Sprintf("%020d", i)
} else if b.idx.indexType == BytesIndex {
ref, err := b.idx.client.UploadBlob(value, 0, true)
if err != nil { // skipcq: TCV-001
return err
}
value = ref
}
return b.idx.addOrUpdateStringEntry(ctx, b.memDb, stringKey, b.idx.indexType, value, memory, apnd)
}
// Get extracts an index value from an index given a key.
func (b *Batch) Get(key string) ([][]byte, error) {
if b.memDb == nil {
return nil, ErrEntryNotFound
}
if len(b.memDb.Entries) > 0 {
stringKey := key
if b.idx.indexType == NumberIndex { // skipcq: TCV-001
i, err := strconv.ParseInt(stringKey, 10, 64)
if err != nil {
return nil, ErrKVKeyNotANumber
}
stringKey = fmt.Sprintf("%020d", i)
}
_, manifest, i, err := b.idx.findManifest(nil, b.memDb, stringKey)
if err != nil {
return nil, err
}
return manifest.Entries[i].Ref, nil // skipcq: TCV-001
}
return nil, ErrEntryNotFound // skipcq: TCV-001
}
// DelNumber deletes a number index key and value.
// skipcq: TCV-001
func (b *Batch) DelNumber(key float64) ([][]byte, error) {
stringKey := fmt.Sprintf("%020.20g", key)
return b.Del(stringKey)
}
// Del deletes a index entry.
func (b *Batch) Del(key string) ([][]byte, error) {
if b.idx.isReadOnlyFeed() { // skipcq: TCV-001
return nil, ErrReadOnlyIndex
}
if !b.idx.mutable { // skipcq: TCV-001
return nil, ErrCannotModifyImmutableIndex
}
if b.memDb == nil { // skipcq: TCV-001
return nil, ErrEntryNotFound
}
if len(b.memDb.Entries) > 0 {
stringKey := key
if b.idx.indexType == NumberIndex { // skipcq: TCV-001
i, err := strconv.ParseInt(stringKey, 10, 64)
if err != nil {
return nil, ErrKVKeyNotANumber
}
stringKey = fmt.Sprintf("%020d", i)
}
parentManifest, manifest, i, err := b.idx.findManifest(nil, b.memDb, stringKey)
if err != nil { // skipcq: TCV-001
return nil, err
}
deletedRef := manifest.Entries[i].Ref
if parentManifest != nil && len(manifest.Entries) == 1 && manifest.Entries[0].Name == "" { // skipcq: TCV-001
// then we have to remove the intermediate node in the parent Manifest
// so that the entire branch goes kaboom
parentEntryKey := filepath.Base(manifest.Name)
for i, entry := range parentManifest.Entries {
if entry.EType == intermediateEntry && entry.Name == parentEntryKey {
deletedRef = entry.Ref
parentManifest.Entries = append(parentManifest.Entries[:i], parentManifest.Entries[i+1:]...)
break
}
}
return deletedRef, nil
}
manifest.Entries = append(manifest.Entries[:i], manifest.Entries[i+1:]...)
return deletedRef, nil
}
return nil, ErrEntryNotFound // skipcq: TCV-001
}
// Write commits the raw index file in to the Swarm network.
func (b *Batch) Write(podFile string) (*Manifest, error) {
if b.idx.isReadOnlyFeed() { // skipcq: TCV-001
return nil, ErrReadOnlyIndex
}
if b.memDb == nil { // skipcq: TCV-001
return nil, ErrEntryNotFound
}
if b.memDb.dirtyFlag {
diskManifest, err := b.idx.loadManifest(b.memDb.Name, b.idx.encryptionPassword)
if err != nil && errors.Is(err, ErrNoManifestFound) { // skipcq: TCV-001
return nil, err
}
diskManifest.PodFile = podFile
b.memDb.PodFile = podFile
b.idx.podFile = podFile
return b.mergeAndWriteManifest(diskManifest, b.memDb)
}
return b.memDb, nil // skipcq: TCV-001
}
func (b *Batch) mergeAndWriteManifest(diskManifest, memManifest *Manifest) (*Manifest, error) {
// merge the mem manifest with the disk version
if memManifest.dirtyFlag {
for _, dirtyEntry := range memManifest.Entries {
diskManifest.dirtyFlag = true
b.idx.addEntryToManifestSortedLexicographically(diskManifest, dirtyEntry)
if dirtyEntry.EType == intermediateEntry && dirtyEntry.Manifest != nil { // skipcq: TCV-001
err := b.storeMemoryManifest(dirtyEntry.Manifest, 0)
if err != nil {
return nil, err
}
dirtyEntry.Manifest = nil
}
}
diskManifest.Mutable = memManifest.Mutable
for _, dirtyEntry := range diskManifest.Entries {
dirtyEntry.Manifest = nil
}
if diskManifest.dirtyFlag {
// save th disk manifest
err := b.idx.updateManifest(diskManifest, b.idx.encryptionPassword)
if err != nil { // skipcq: TCV-001
return nil, err
}
}
err := b.emptyManifestStack()
if err != nil { // skipcq: TCV-001
return nil, err
}
return diskManifest, nil
}
return diskManifest, nil
}
func (b *Batch) emptyManifestStack() error {
var tempStack []*Manifest
// copy the data to tempStack
tempStack = append(tempStack, b.manifestStack...)
b.manifestStack = nil
for _, man := range tempStack { // skipcq: TCV-001
err := b.storeMemoryManifest(man, 0)
if err != nil {
return err
}
}
if len(b.manifestStack) > 0 { // skipcq: TCV-001
return b.emptyManifestStack()
}
return nil
}
// skipcq: TCV-001
func (b *Batch) storeMemoryManifest(manifest *Manifest, depth int) error {
/*
var wg sync.WaitGroup
errC := make(chan error)
wgDone := make(chan bool)
*/
// store any branches in this manifest
for _, entry := range manifest.Entries {
if entry.EType == intermediateEntry && entry.Manifest != nil {
if depth >= maxManifestDepth {
// process later
b.manifestStack = append(b.manifestStack, entry.Manifest)
entry.Manifest = nil
return nil
}
// wg.Add(1)
// go func() {
// defer func() {
// wg.Done()
// }()
err := b.storeMemoryManifest(entry.Manifest, depth+1)
if err != nil {
return err
}
// }()
}
}
// go func() {
// wg.Wait()
// close(wgDone)
// }()
//
// select {
// case <-wgDone:
// break
// case err := <-errC:
// close(errC)
// return err
// }
// store this manifest
// go func() {
err := b.idx.storeManifest(manifest, b.idx.encryptionPassword)
if err != nil {
return err
}
atomic.AddUint64(&b.storageCount, 1)
count := atomic.LoadUint64(&b.storageCount)
if count%100 == 0 {
fmt.Println(count)
}
// }()
return nil
}