-
Notifications
You must be signed in to change notification settings - Fork 342
/
fakes.go
680 lines (576 loc) · 16.3 KB
/
fakes.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
package testutil
import (
"bytes"
"context"
"fmt"
"sort"
"github.com/treeverse/lakefs/pkg/graveler"
"github.com/treeverse/lakefs/pkg/graveler/committed"
)
const DefaultBranchID = graveler.BranchID("main")
type AppliedData struct {
Values graveler.ValueIterator
MetaRangeID graveler.MetaRangeID
}
type CommittedFake struct {
ValuesByKey map[string]*graveler.Value
ValueIterator graveler.ValueIterator
DiffIterator graveler.DiffIterator
Err error
MetaRangeID graveler.MetaRangeID
DiffSummary graveler.DiffSummary
AppliedData AppliedData
}
type MetaRangeFake struct {
id graveler.MetaRangeID
}
func (t *MetaRangeFake) ID() graveler.MetaRangeID {
return t.id
}
func NewCommittedFake() *CommittedFake {
return &CommittedFake{}
}
func (c *CommittedFake) Exists(context.Context, graveler.StorageNamespace, graveler.MetaRangeID) (bool, error) {
if c.Err != nil {
return false, c.Err
}
return true, nil
}
func (c *CommittedFake) Get(_ context.Context, _ graveler.StorageNamespace, _ graveler.MetaRangeID, key graveler.Key) (*graveler.Value, error) {
if c.Err != nil {
return nil, c.Err
}
return c.ValuesByKey[string(key)], nil
}
func (c *CommittedFake) List(context.Context, graveler.StorageNamespace, graveler.MetaRangeID) (graveler.ValueIterator, error) {
if c.Err != nil {
return nil, c.Err
}
return c.ValueIterator, nil
}
func (c *CommittedFake) Diff(context.Context, graveler.StorageNamespace, graveler.MetaRangeID, graveler.MetaRangeID) (graveler.DiffIterator, error) {
if c.Err != nil {
return nil, c.Err
}
return c.DiffIterator, nil
}
func (c *CommittedFake) Compare(context.Context, graveler.StorageNamespace, graveler.MetaRangeID, graveler.MetaRangeID, graveler.MetaRangeID) (graveler.DiffIterator, error) {
if c.Err != nil {
return nil, c.Err
}
return c.DiffIterator, nil
}
func (c *CommittedFake) Merge(_ context.Context, _ graveler.StorageNamespace, _, _, _ graveler.MetaRangeID) (graveler.MetaRangeID, graveler.DiffSummary, error) {
if c.Err != nil {
return "", graveler.DiffSummary{}, c.Err
}
return c.MetaRangeID, c.DiffSummary, nil
}
func (c *CommittedFake) Apply(_ context.Context, _ graveler.StorageNamespace, metaRangeID graveler.MetaRangeID, values graveler.ValueIterator) (graveler.MetaRangeID, graveler.DiffSummary, error) {
if c.Err != nil {
return "", graveler.DiffSummary{}, c.Err
}
c.AppliedData.Values = values
c.AppliedData.MetaRangeID = metaRangeID
return c.MetaRangeID, c.DiffSummary, nil
}
func (c *CommittedFake) WriteMetaRange(ctx context.Context, ns graveler.StorageNamespace, it graveler.ValueIterator, metadata graveler.Metadata) (*graveler.MetaRangeID, error) {
if c.Err != nil {
return nil, c.Err
}
return &c.MetaRangeID, nil
}
func (c *CommittedFake) GetMetaRange(ctx context.Context, ns graveler.StorageNamespace, metaRangeID graveler.MetaRangeID) (graveler.MetaRangeInfo, error) {
return graveler.MetaRangeInfo{
Address: fmt.Sprintf("fake://prefix/%s(metarange)", metaRangeID),
}, nil
}
func (c *CommittedFake) GetRange(ctx context.Context, ns graveler.StorageNamespace, rangeID graveler.RangeID) (graveler.RangeInfo, error) {
return graveler.RangeInfo{
Address: fmt.Sprintf("fake://prefix/%s(range)", rangeID),
}, nil
}
type StagingFake struct {
Err error
DropErr error // specific error for drop call
Value *graveler.Value
ValueIterator graveler.ValueIterator
stagingToken graveler.StagingToken
LastSetValueRecord *graveler.ValueRecord
LastRemovedKey graveler.Key
DropCalled bool
SetErr error
}
func (s *StagingFake) DropByPrefix(context.Context, graveler.StagingToken, graveler.Key) error {
return nil
}
func (s *StagingFake) Drop(context.Context, graveler.StagingToken) error {
s.DropCalled = true
if s.DropErr != nil {
return s.DropErr
}
return nil
}
func (s *StagingFake) Get(context.Context, graveler.StagingToken, graveler.Key) (*graveler.Value, error) {
if s.Err != nil {
return nil, s.Err
}
return s.Value, nil
}
func (s *StagingFake) Set(_ context.Context, _ graveler.StagingToken, key graveler.Key, value *graveler.Value, _ bool) error {
if s.SetErr != nil {
return s.SetErr
}
s.LastSetValueRecord = &graveler.ValueRecord{
Key: key,
Value: value,
}
return nil
}
func (s *StagingFake) DropKey(_ context.Context, _ graveler.StagingToken, key graveler.Key) error {
if s.Err != nil {
return s.Err
}
s.LastRemovedKey = key
return nil
}
func (s *StagingFake) List(context.Context, graveler.StagingToken) (graveler.ValueIterator, error) {
if s.Err != nil {
return nil, s.Err
}
return s.ValueIterator, nil
}
func (s *StagingFake) Snapshot(context.Context, graveler.StagingToken) (graveler.StagingToken, error) {
if s.Err != nil {
return "", s.Err
}
return s.stagingToken, nil
}
func (s *StagingFake) ListSnapshot(context.Context, graveler.StagingToken, graveler.Key) (graveler.ValueIterator, error) {
if s.Err != nil {
return nil, s.Err
}
return s.ValueIterator, nil
}
type AddedCommitData struct {
Committer string
Message string
MetaRangeID graveler.MetaRangeID
Parents graveler.CommitParents
Metadata graveler.Metadata
}
type RefsFake struct {
ListRepositoriesRes graveler.RepositoryIterator
ListBranchesRes graveler.BranchIterator
RevParseRes map[graveler.Ref]graveler.Reference
ListTagsRes graveler.TagIterator
CommitIter graveler.CommitIterator
RefType graveler.ReferenceType
Branch *graveler.Branch
TagCommitID *graveler.CommitID
Err error
CommitErr error
AddedCommit AddedCommitData
CommitID graveler.CommitID
Commits map[graveler.CommitID]*graveler.Commit
}
func (m *RefsFake) CreateBareRepository(ctx context.Context, repositoryID graveler.RepositoryID, repository graveler.Repository) error {
panic("implement me")
}
func (m *RefsFake) ListCommits(ctx context.Context, repositoryID graveler.RepositoryID) (graveler.CommitIterator, error) {
return nil, nil
}
func (m *RefsFake) RevParse(ctx context.Context, repoID graveler.RepositoryID, ref graveler.Ref) (graveler.Reference, error) {
if m.RevParseRes != nil {
if res, ok := m.RevParseRes[ref]; ok {
return res, nil
}
}
var branch graveler.BranchID
if m.RefType == graveler.ReferenceTypeBranch {
branch = DefaultBranchID
}
return NewFakeReference(m.RefType, branch, m.CommitID), nil
}
func (m *RefsFake) GetRepository(context.Context, graveler.RepositoryID) (*graveler.Repository, error) {
return &graveler.Repository{}, nil
}
func (m *RefsFake) CreateRepository(context.Context, graveler.RepositoryID, graveler.Repository, graveler.StagingToken) error {
return nil
}
func (m *RefsFake) ListRepositories(context.Context) (graveler.RepositoryIterator, error) {
return m.ListRepositoriesRes, nil
}
func (m *RefsFake) DeleteRepository(context.Context, graveler.RepositoryID) error {
return nil
}
func (m *RefsFake) GetBranch(context.Context, graveler.RepositoryID, graveler.BranchID) (*graveler.Branch, error) {
return m.Branch, m.Err
}
func (m *RefsFake) SetBranch(context.Context, graveler.RepositoryID, graveler.BranchID, graveler.Branch) error {
return nil
}
func (m *RefsFake) DeleteBranch(context.Context, graveler.RepositoryID, graveler.BranchID) error {
return nil
}
func (m *RefsFake) ListBranches(context.Context, graveler.RepositoryID) (graveler.BranchIterator, error) {
return m.ListBranchesRes, nil
}
func (m *RefsFake) GetTag(context.Context, graveler.RepositoryID, graveler.TagID) (*graveler.CommitID, error) {
return m.TagCommitID, m.Err
}
func (m *RefsFake) CreateTag(context.Context, graveler.RepositoryID, graveler.TagID, graveler.CommitID) error {
return nil
}
func (m *RefsFake) DeleteTag(context.Context, graveler.RepositoryID, graveler.TagID) error {
return nil
}
func (m *RefsFake) ListTags(context.Context, graveler.RepositoryID) (graveler.TagIterator, error) {
return m.ListTagsRes, nil
}
func (m *RefsFake) GetCommit(_ context.Context, _ graveler.RepositoryID, id graveler.CommitID) (*graveler.Commit, error) {
if val, ok := m.Commits[id]; ok {
return val, nil
}
return nil, graveler.ErrCommitNotFound
}
func (m *RefsFake) AddCommit(_ context.Context, _ graveler.RepositoryID, commit graveler.Commit) (graveler.CommitID, error) {
if m.CommitErr != nil {
return "", m.CommitErr
}
m.AddedCommit = AddedCommitData{
Committer: commit.Committer,
Message: commit.Message,
MetaRangeID: commit.MetaRangeID,
Parents: commit.Parents,
Metadata: commit.Metadata,
}
return m.CommitID, nil
}
func (m *RefsFake) FindMergeBase(context.Context, graveler.RepositoryID, ...graveler.CommitID) (*graveler.Commit, error) {
return &graveler.Commit{}, nil
}
func (m *RefsFake) Log(context.Context, graveler.RepositoryID, graveler.CommitID) (graveler.CommitIterator, error) {
return m.CommitIter, nil
}
type diffIter struct {
current int
records []graveler.Diff
err error
}
func NewDiffIter(records []graveler.Diff) *diffIter {
return &diffIter{records: records, current: -1}
}
func (r *diffIter) Next() bool {
r.current++
return r.current < len(r.records)
}
func (r *diffIter) SeekGE(id graveler.Key) {
i := sort.Search(len(r.records), func(i int) bool {
return bytes.Compare(r.records[i].Key, id) >= 0
})
r.current = i - 1
}
func (r *diffIter) Value() *graveler.Diff {
if r.current < 0 || r.current >= len(r.records) {
return nil
}
return &r.records[r.current]
}
func (r *diffIter) Err() error {
return r.err
}
func (r *diffIter) Close() {}
type valueIteratorFake struct {
current int
records []graveler.ValueRecord
err error
}
func NewValueIteratorFake(records []graveler.ValueRecord) graveler.ValueIterator {
return &valueIteratorFake{records: records, current: -1}
}
func (r *valueIteratorFake) Next() bool {
r.current++
return r.current < len(r.records)
}
func (r *valueIteratorFake) SeekGE(id graveler.Key) {
for i, record := range r.records {
if bytes.Compare(record.Key, id) >= 0 {
r.current = i - 1
return
}
}
r.current = len(r.records)
}
func (r *valueIteratorFake) Value() *graveler.ValueRecord {
if r.current < 0 || r.current >= len(r.records) {
return nil
}
return &r.records[r.current]
}
func (r *valueIteratorFake) Err() error {
return r.err
}
func (r *valueIteratorFake) Close() {}
type committedValueIteratorFake struct {
current int
records []committed.Record
err error
}
func NewCommittedValueIteratorFake(records []committed.Record) *committedValueIteratorFake {
return &committedValueIteratorFake{records: records, current: -1}
}
func (r *committedValueIteratorFake) Next() bool {
r.current++
return r.current < len(r.records)
}
func (r *committedValueIteratorFake) SeekGE(id committed.Key) {
i := sort.Search(len(r.records), func(i int) bool {
return bytes.Compare(r.records[i].Key, id) >= 0
})
r.current = i - 1
}
func (r *committedValueIteratorFake) Value() *committed.Record {
if r.current < 0 || r.current >= len(r.records) {
return nil
}
return &r.records[r.current]
}
func (r *committedValueIteratorFake) Err() error {
return r.err
}
func (r *committedValueIteratorFake) Close() {}
type referenceFake struct {
refType graveler.ReferenceType
branch graveler.Branch
commitID graveler.CommitID
}
// NewFakeReference returns a referenceFake
// if branch parameter is empty branch record will be nil
func NewFakeReference(refType graveler.ReferenceType, branchID graveler.BranchID, commitID graveler.CommitID) *referenceFake {
var branch graveler.Branch
if branchID != "" {
branch = graveler.Branch{CommitID: commitID}
}
return &referenceFake{
refType: refType,
branch: branch,
commitID: commitID,
}
}
func (m *referenceFake) Type() graveler.ReferenceType {
return m.refType
}
func (m *referenceFake) Branch() graveler.Branch {
return m.branch
}
func (m *referenceFake) CommitID() graveler.CommitID {
return m.commitID
}
type RV struct {
R *committed.Range
V *graveler.ValueRecord
}
type FakeIterator struct {
RV []RV
idx int
rangeIdx int
err error
closed bool
readsByRange []int
}
func NewFakeIterator() *FakeIterator {
// Start with an empty record so the first `Next()` can skip it.
return &FakeIterator{RV: make([]RV, 1), idx: 0, rangeIdx: -1}
}
// ReadsByRange returns the number of Next operations performed inside each range
func (i *FakeIterator) ReadsByRange() []int {
return i.readsByRange
}
func (i *FakeIterator) nextKey() []byte {
if len(i.RV) <= i.idx+1 {
return nil
}
if i.RV[i.idx+1].V == nil {
return i.RV[i.idx+1].R.MinKey
}
return i.RV[i.idx+1].V.Key
}
func (i *FakeIterator) SetErr(err error) {
i.err = err
}
func (i *FakeIterator) AddRange(p *committed.Range) *FakeIterator {
i.RV = append(i.RV, RV{R: p})
i.readsByRange = append(i.readsByRange, 0)
return i
}
func (i *FakeIterator) AddValueRecords(vs ...*graveler.ValueRecord) *FakeIterator {
if len(i.RV) == 0 {
panic(fmt.Sprintf("cannot add ValueRecords %+v with no range", vs))
}
rng := i.RV[len(i.RV)-1].R
for _, v := range vs {
i.RV = append(i.RV, RV{R: rng, V: v})
}
return i
}
func (i *FakeIterator) Next() bool {
if i.err != nil || i.closed {
return false
}
if len(i.RV) <= i.idx+1 {
return false
}
i.idx++
if i.RV[i.idx].V == nil {
i.rangeIdx++
} else {
i.readsByRange[i.rangeIdx]++
}
return true
}
func (i *FakeIterator) NextRange() bool {
for {
if len(i.RV) <= i.idx+1 {
return false
}
i.idx++
if i.RV[i.idx].V == nil {
i.rangeIdx++
return true
}
}
}
func (i *FakeIterator) Value() (*graveler.ValueRecord, *committed.Range) {
if i.closed {
return nil, nil
}
return i.RV[i.idx].V, i.RV[i.idx].R
}
func (i *FakeIterator) SeekGE(id graveler.Key) {
i.idx = 0
i.rangeIdx = -1
for {
nextKey := i.nextKey()
if nextKey == nil || bytes.Compare(nextKey, id) >= 0 {
return
}
if !i.Next() {
return
}
}
}
func (i *FakeIterator) Err() error {
return i.err
}
func (i *FakeIterator) Close() {
i.closed = true
}
type DRV struct {
R *committed.RangeDiff
V *graveler.Diff
}
type FakeDiffIterator struct {
DRV []DRV
idx int
rangeIdx int
err error
closed bool
readsByRange []int
}
func NewFakeDiffIterator() *FakeDiffIterator {
// Start with an empty record so the first `Next()` can skip it.
return &FakeDiffIterator{DRV: make([]DRV, 1), idx: 0, rangeIdx: -1}
}
// ReadsByRange returns the number of Next operations performed inside each range
func (i *FakeDiffIterator) ReadsByRange() []int {
return i.readsByRange
}
func (i *FakeDiffIterator) nextKey() []byte {
for j := 1; len(i.DRV) > i.idx+j; j++ {
if i.DRV[i.idx+j].V == nil {
if i.DRV[i.idx+j].R == nil {
continue // in case we added an empty range header
}
return i.DRV[i.idx+j].R.Range.MinKey
}
return i.DRV[i.idx+j].V.Key
}
return nil
}
func (i *FakeDiffIterator) SetErr(err error) {
i.err = err
}
func (i *FakeDiffIterator) AddRange(p *committed.RangeDiff) *FakeDiffIterator {
i.DRV = append(i.DRV, DRV{R: p})
i.readsByRange = append(i.readsByRange, 0)
return i
}
func (i *FakeDiffIterator) AddValueRecords(vs ...*graveler.Diff) *FakeDiffIterator {
if len(i.DRV) == 0 {
panic(fmt.Sprintf("cannot add ValueRecords %+v with no range", vs))
}
rng := i.DRV[len(i.DRV)-1].R
for _, v := range vs {
i.DRV = append(i.DRV, DRV{R: rng, V: v})
}
return i
}
func (i *FakeDiffIterator) Next() bool {
if i.err != nil || i.closed {
return false
}
if len(i.DRV) <= i.idx+1 {
return false
}
i.idx++
if i.DRV[i.idx].V == nil {
i.rangeIdx++
if i.DRV[i.idx].R == nil {
return i.Next() // in case we added an empty range header
}
} else if i.DRV[i.idx].R != nil {
i.readsByRange[i.rangeIdx]++
}
return true
}
func (i *FakeDiffIterator) NextRange() bool {
for {
if len(i.DRV) <= i.idx+1 {
return false
}
i.idx++
if i.DRV[i.idx].V == nil {
i.rangeIdx++
if i.DRV[i.idx].R == nil {
return i.Next() // in case we added an empty range header
}
return true
}
}
}
func (i *FakeDiffIterator) Value() (*graveler.Diff, *committed.RangeDiff) {
if i.closed {
return nil, nil
}
return i.DRV[i.idx].V, i.DRV[i.idx].R
}
func (i *FakeDiffIterator) SeekGE(id graveler.Key) {
i.idx = 0
i.rangeIdx = -1
for {
nextKey := i.nextKey()
if nextKey == nil || bytes.Compare(nextKey, id) >= 0 {
return
}
if !i.Next() {
return
}
}
}
func (i *FakeDiffIterator) Err() error {
return i.err
}
func (i *FakeDiffIterator) Close() {
i.closed = true
}