~tsileo/blobstash

ref: b178995e346d blobstash/pkg/stash/store/store.go -rw-r--r-- 9.6 KiB
b178995eThomas Sileo vendor: update deps 1 year, 2 months ago
                                                                                
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package store // import "a4.io/blobstash/pkg/stash/store"

import (
	"context"
	"fmt"
	"sort"
	"strconv"
	"strings"

	"a4.io/blobsfile"
	"a4.io/blobstash/pkg/blob"
	"a4.io/blobstash/pkg/blobstore"
	"a4.io/blobstash/pkg/vkv"
)

var sepCandidates = []string{":", "&", "*", "^", "#", ".", "-", "_", "+", "=", "%", "@", "!"}

type sortHelper struct {
	Item       interface{}
	IsFromRoot bool
}

type mergeCursor struct {
	rstart, sstart   string
	rcursor, scursor string
}

func (c *mergeCursor) Encode(nextKey func(string) string) string {
	rcursor := nextKey(c.rcursor)
	scursor := nextKey(c.scursor)

	var sep string
	// Find a separator that is not in the key
	for _, c := range sepCandidates {
		if !strings.Contains(rcursor, c) && !strings.Contains(scursor, c) {
			sep = c
			break
		}
	}
	return fmt.Sprintf("stash:%s%s%s%s", sep, rcursor, sep, scursor)
}

func parseCursor(start string) *mergeCursor {
	c := &mergeCursor{}
	// Check if the cursor is a "merge cursor", i.e. in the format:
	// "stash:<root cursor>:<stash cursor>"
	if strings.HasPrefix(start, "stash:") {
		sep := start[6:7]
		cdata := strings.Split(start[7:len(start)], sep)
		c.rstart = cdata[0]
		c.sstart = cdata[1]

		// Initialize the part of the merge cursor to the "start" cursor,
		// as it's possible the current range will return nothing either in
		// the root or the stash
		c.rcursor = c.rstart
		c.scursor = c.sstart
	} else {
		c.rstart = start
		c.sstart = start

		// Same here for the cursor
		c.rcursor = start
		c.scursor = start
	}
	return c
}

type DataContext interface {
	StashBlobStore() BlobStore
	BlobStore() BlobStore
	KvStore() KvStore
	BlobStoreProxy() BlobStore
	KvStoreProxy() KvStore
	Merge(context.Context) error
	Close() error
	Closed() bool
	Destroy() error
}

type KvStore interface {
	Put(ctx context.Context, key, ref string, data []byte, version int64) (*vkv.KeyValue, error)
	Get(ctx context.Context, key string, version int64) (*vkv.KeyValue, error)
	GetMetaBlob(ctx context.Context, key string, version int64) (string, error)
	Versions(ctx context.Context, key, start string, limit int) (*vkv.KeyValueVersions, string, error)
	Keys(ctx context.Context, start, end string, limit int) ([]*vkv.KeyValue, string, error)
	ReverseKeys(ctx context.Context, start, end string, limit int) ([]*vkv.KeyValue, string, error)
	Close() error
}

type KvStoreProxy struct {
	KvStore
	ReadSrc KvStore
}

func (p *KvStoreProxy) Put(ctx context.Context, key, ref string, data []byte, version int64) (*vkv.KeyValue, error) {
	if version > 0 {
		kv, err := p.ReadSrc.Get(ctx, key, version)
		switch err {
		case vkv.ErrNotFound:
			return p.KvStore.Put(ctx, key, ref, data, version)
		case nil:
			return kv, nil
		default:
		}
	}

	return p.KvStore.Put(ctx, key, ref, data, version)
}

func (p *KvStoreProxy) Get(ctx context.Context, key string, version int64) (*vkv.KeyValue, error) {
	kv, err := p.KvStore.Get(ctx, key, version)
	switch err {
	case nil:
		// The "latest" version is requested, we need to compare with the "root" kv store
		// to return the latest between the two
		if version <= 0 {
			rkv, rerr := p.ReadSrc.Get(ctx, key, version)
			if rerr != nil && rerr != vkv.ErrNotFound {
				return nil, err
			}
			if rerr == nil && rkv.Version > kv.Version {
				// The one from the "root" kv store is more recent, return it
				return rkv, nil
			}
		}
	case vkv.ErrNotFound:
		return p.ReadSrc.Get(ctx, key, version)
	default:
		return nil, err
	}
	return kv, nil
}

func (p *KvStoreProxy) GetMetaBlob(ctx context.Context, key string, version int64) (string, error) {
	h, err := p.KvStore.GetMetaBlob(ctx, key, version)
	switch err {
	case nil:
	case vkv.ErrNotFound:
		return p.ReadSrc.GetMetaBlob(ctx, key, version)
	default:
		return "", err
	}
	return h, nil
}

func (p *KvStoreProxy) Versions(ctx context.Context, key, start string, limit int) (*vkv.KeyValueVersions, string, error) {
	var tmp []*sortHelper
	var out []*vkv.KeyValue
	res := &vkv.KeyValueVersions{
		Key: key,
	}

	mcursor := parseCursor(start)

	versions, _, err := p.ReadSrc.Versions(ctx, key, mcursor.rstart, limit)
	if err != nil && err != vkv.ErrNotFound {
		return nil, "", err
	}

	// The key may not exist
	if err == nil {
		for _, kv := range versions.Versions {
			tmp = append(tmp, &sortHelper{kv, true})
		}
	}

	localVersions, _, err := p.KvStore.Versions(ctx, key, mcursor.sstart, limit)
	if err != nil && err != vkv.ErrNotFound {
		return nil, "", err
	}

	// The key may not exist
	if err == nil {
		for _, kv := range localVersions.Versions {
			tmp = append(tmp, &sortHelper{kv, false})
		}
	}

	// Sort everything
	sort.Slice(tmp, func(i, j int) bool {
		return tmp[i].Item.(*vkv.KeyValue).Version > tmp[j].Item.(*vkv.KeyValue).Version
	})

	// Slice it if it's too big
	if limit > 0 && len(tmp) > 0 && len(tmp) > limit {
		tmp = tmp[0:limit]
	}

	// Build the final result, and compute the "merge cursor"
	for _, sh := range tmp {
		kv := sh.Item.(*vkv.KeyValue)
		if sh.IsFromRoot {
			mcursor.rcursor = strconv.FormatInt(kv.Version, 10)
		} else {
			mcursor.scursor = strconv.FormatInt(kv.Version, 10)
		}
		out = append(out, kv)
	}

	res.Versions = out

	return res, mcursor.Encode(vkv.NextVersionCursor), nil
}

func (p *KvStoreProxy) ReverseKeys(ctx context.Context, start, end string, limit int) ([]*vkv.KeyValue, string, error) {
	var tmp []*sortHelper
	var out []*vkv.KeyValue

	mcursor := parseCursor(start)

	kvs, _, err := p.ReadSrc.ReverseKeys(ctx, mcursor.rstart, end, limit)
	if err != nil {
		return nil, "", err
	}

	for _, kv := range kvs {
		tmp = append(tmp, &sortHelper{kv, true})
	}

	localKvs, _, err := p.KvStore.ReverseKeys(ctx, mcursor.sstart, end, 0)
	if err != nil {
		return nil, "", err
	}

	for _, kv := range localKvs {
		tmp = append(tmp, &sortHelper{kv, false})
	}

	// Sort everything
	sort.Slice(tmp, func(i, j int) bool {
		return tmp[i].Item.(*vkv.KeyValue).Key > tmp[j].Item.(*vkv.KeyValue).Key
	})

	// Slice it if it's too big
	if len(tmp) > 0 && len(tmp) > limit {
		tmp = tmp[0:limit]
	}

	// Build the final result, and compute the "merge cursor"
	for _, sh := range tmp {
		kv := sh.Item.(*vkv.KeyValue)
		if sh.IsFromRoot {
			mcursor.rcursor = kv.Key
		} else {
			mcursor.scursor = kv.Key
		}
		out = append(out, kv)
	}

	return out, mcursor.Encode(vkv.NextKey), nil
}

func (p *KvStoreProxy) Keys(ctx context.Context, start, end string, limit int) ([]*vkv.KeyValue, string, error) {
	var tmp []*sortHelper
	var out []*vkv.KeyValue

	mcursor := parseCursor(start)

	kvs, _, err := p.ReadSrc.Keys(ctx, mcursor.rstart, end, limit)
	if err != nil {
		return nil, "", err
	}

	for _, kv := range kvs {
		tmp = append(tmp, &sortHelper{kv, true})
	}

	localKvs, _, err := p.KvStore.Keys(ctx, mcursor.sstart, end, 0)
	if err != nil {
		return nil, "", err
	}

	for _, kv := range localKvs {
		tmp = append(tmp, &sortHelper{kv, false})
	}

	// Sort everything
	sort.Slice(tmp, func(i, j int) bool {
		return tmp[i].Item.(*vkv.KeyValue).Key < tmp[j].Item.(*vkv.KeyValue).Key
	})

	// Slice it if it's too big
	if len(tmp) > 0 && len(tmp) > limit {
		tmp = tmp[0:limit]
	}

	// Build the final result, and compute the "merge cursor"
	for _, sh := range tmp {
		kv := sh.Item.(*vkv.KeyValue)
		if sh.IsFromRoot {
			mcursor.rcursor = kv.Key
		} else {
			mcursor.scursor = kv.Key
		}
		out = append(out, kv)
	}

	return out, mcursor.Encode(vkv.NextKey), nil
}

type BlobStore interface {
	Put(ctx context.Context, blob *blob.Blob) (bool, error)
	Get(ctx context.Context, hash string) ([]byte, error)
	Stat(ctx context.Context, hash string) (bool, error)
	Enumerate(ctx context.Context, start, end string, limit int) ([]*blob.SizedBlobRef, string, error)
	Close() error
}

type BlobStoreProxy struct {
	BlobStore
	ReadSrc BlobStore
}

func (p *BlobStoreProxy) Get(ctx context.Context, hash string) ([]byte, error) {
	data, err := p.BlobStore.Get(ctx, hash)
	switch err {
	case nil:
	case blobsfile.ErrBlobNotFound:
		return p.ReadSrc.Get(ctx, hash)
	default:
		return nil, err
	}
	return data, nil
}

func (p *BlobStoreProxy) Stat(ctx context.Context, hash string) (bool, error) {
	exists, err := p.BlobStore.Stat(ctx, hash)
	if err != nil {
		return false, err
	}
	if !exists {
		return p.ReadSrc.Stat(ctx, hash)
	}
	return exists, nil
}

func (p *BlobStoreProxy) Put(ctx context.Context, blob *blob.Blob) (bool, error) {
	existsSrc, err := p.ReadSrc.Stat(ctx, blob.Hash)
	if err != nil {
		return false, err
	}
	if existsSrc {
		return true, nil
	}
	return p.BlobStore.Put(ctx, blob)
}

func (p *BlobStoreProxy) Enumerate(ctx context.Context, start, end string, limit int) ([]*blob.SizedBlobRef, string, error) {
	// Here, we will need to merge two differents "enumerate results" into one
	var tmp []*sortHelper
	var out []*blob.SizedBlobRef

	mcursor := parseCursor(start)

	// Fetch the data from the "root" blobstore
	rootBlobs, _, err := p.ReadSrc.Enumerate(ctx, mcursor.rstart, end, limit)
	if err != nil {
		return nil, "", err
	}

	for _, blob := range rootBlobs {
		tmp = append(tmp, &sortHelper{blob, true})
	}

	// Fetch the data from the stash
	localBlobs, _, err := p.BlobStore.Enumerate(ctx, mcursor.sstart, end, 0)
	if err != nil {
		return nil, "", err
	}

	for _, blob := range localBlobs {
		tmp = append(tmp, &sortHelper{blob, false})
	}

	// Sort everything
	sort.Slice(tmp, func(i, j int) bool {
		return tmp[i].Item.(*blob.SizedBlobRef).Hash > tmp[j].Item.(*blob.SizedBlobRef).Hash
	})

	// Slice it if it's too big
	if len(tmp) > 0 && len(tmp) > limit {
		tmp = tmp[0:limit]
	}

	// Build the final result, and compute the "merge cursor"
	for _, sh := range tmp {
		b := sh.Item.(*blob.SizedBlobRef)
		if sh.IsFromRoot {
			mcursor.rcursor = b.Hash
		} else {
			mcursor.scursor = b.Hash
		}
		out = append(out, b)
	}

	return out, mcursor.Encode(blobstore.NextHexKey), nil
}