-
Notifications
You must be signed in to change notification settings - Fork 348
/
manager.go
173 lines (155 loc) · 6.03 KB
/
manager.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
package committed
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/treeverse/lakefs/pkg/logging"
"github.com/treeverse/lakefs/pkg/graveler"
)
type committedManager struct {
metaRangeManager MetaRangeManager
logger logging.Logger
}
func NewCommittedManager(m MetaRangeManager) graveler.CommittedManager {
return &committedManager{metaRangeManager: m, logger: logging.Default()}
}
func (c *committedManager) Exists(ctx context.Context, ns graveler.StorageNamespace, id graveler.MetaRangeID) (bool, error) {
return c.metaRangeManager.Exists(ctx, ns, id)
}
func (c *committedManager) Get(ctx context.Context, ns graveler.StorageNamespace, rangeID graveler.MetaRangeID, key graveler.Key) (*graveler.Value, error) {
it, err := c.metaRangeManager.NewMetaRangeIterator(ctx, ns, rangeID)
if err != nil {
return nil, err
}
valIt := NewValueIterator(it)
defer valIt.Close()
valIt.SeekGE(key)
// return the next value
if !valIt.Next() {
// error or not found
if err := valIt.Err(); err != nil {
return nil, err
}
return nil, graveler.ErrNotFound
}
// compare the key we found
rec := valIt.Value()
if !bytes.Equal(rec.Key, key) {
return nil, graveler.ErrNotFound
}
return rec.Value, nil
}
func (c *committedManager) List(ctx context.Context, ns graveler.StorageNamespace, rangeID graveler.MetaRangeID) (graveler.ValueIterator, error) {
it, err := c.metaRangeManager.NewMetaRangeIterator(ctx, ns, rangeID)
if err != nil {
return nil, err
}
return NewValueIterator(it), nil
}
func (c *committedManager) WriteMetaRange(ctx context.Context, ns graveler.StorageNamespace, it graveler.ValueIterator, metadata graveler.Metadata) (*graveler.MetaRangeID, error) {
writer := c.metaRangeManager.NewWriter(ctx, ns, metadata)
defer func() {
if err := writer.Abort(); err != nil {
c.logger.Errorf("Aborting write to meta range: %w", err)
}
}()
for it.Next() {
if err := writer.WriteRecord(*it.Value()); err != nil {
return nil, fmt.Errorf("writing record: %w", err)
}
}
if err := it.Err(); err != nil {
return nil, fmt.Errorf("getting value from iterator: %w", err)
}
id, err := writer.Close()
if err != nil {
return nil, fmt.Errorf("closing writer: %w", err)
}
return id, nil
}
func (c *committedManager) Diff(ctx context.Context, ns graveler.StorageNamespace, left, right graveler.MetaRangeID) (graveler.DiffIterator, error) {
leftIt, err := c.metaRangeManager.NewMetaRangeIterator(ctx, ns, left)
if err != nil {
return nil, err
}
rightIt, err := c.metaRangeManager.NewMetaRangeIterator(ctx, ns, right)
if err != nil {
return nil, err
}
return NewDiffValueIterator(ctx, leftIt, rightIt), nil
}
func (c *committedManager) diffWithRanges(ctx context.Context, ns graveler.StorageNamespace, left, right graveler.MetaRangeID) (DiffIterator, error) {
leftIt, err := c.metaRangeManager.NewMetaRangeIterator(ctx, ns, left)
if err != nil {
return nil, err
}
rightIt, err := c.metaRangeManager.NewMetaRangeIterator(ctx, ns, right)
if err != nil {
return nil, err
}
return NewDiffIterator(ctx, leftIt, rightIt), nil
}
func (c *committedManager) Merge(ctx context.Context, ns graveler.StorageNamespace, destination, source, base graveler.MetaRangeID) (graveler.MetaRangeID, graveler.DiffSummary, error) {
diffIt, err := c.diffWithRanges(ctx, ns, destination, source)
if err != nil {
return "", graveler.DiffSummary{}, fmt.Errorf("diff: %w", err)
}
defer diffIt.Close()
baseIt, err := c.metaRangeManager.NewMetaRangeIterator(ctx, ns, base)
if err != nil {
return "", graveler.DiffSummary{}, fmt.Errorf("get base iterator: %w", err)
}
defer baseIt.Close()
patchIterator := NewMergeIterator(ctx, diffIt, baseIt)
defer patchIterator.Close()
return c.applyOnDiffWithRanges(ctx, ns, destination, patchIterator)
}
func (c *committedManager) applyOnDiffWithRanges(ctx context.Context, ns graveler.StorageNamespace, rangeID graveler.MetaRangeID, diffs Iterator) (graveler.MetaRangeID, graveler.DiffSummary, error) {
mwWriter := c.metaRangeManager.NewWriter(ctx, ns, nil)
defer func() {
err := mwWriter.Abort()
if err != nil {
c.logger.WithError(err).Error("Abort failed after Apply")
}
}()
metaRangeIterator, err := c.metaRangeManager.NewMetaRangeIterator(ctx, ns, rangeID)
if err != nil {
return "", graveler.DiffSummary{}, fmt.Errorf("get metarange ns=%s id=%s: %w", ns, rangeID, err)
}
defer metaRangeIterator.Close()
summary, err := Apply(ctx, mwWriter, metaRangeIterator, diffs, &ApplyOptions{})
if err != nil {
if !errors.Is(err, graveler.ErrUserVisible) {
err = fmt.Errorf("apply ns=%s id=%s: %w", ns, rangeID, err)
}
return "", graveler.DiffSummary{}, err
}
newID, err := mwWriter.Close()
if newID == nil {
return "", graveler.DiffSummary{}, fmt.Errorf("close writer ns=%s id=%s: %w", ns, rangeID, err)
}
return *newID, summary, err
}
func (c *committedManager) Apply(ctx context.Context, ns graveler.StorageNamespace, rangeID graveler.MetaRangeID, diffs graveler.ValueIterator) (graveler.MetaRangeID, graveler.DiffSummary, error) {
return c.applyOnDiffWithRanges(ctx, ns, rangeID, NewIteratorWrapper(diffs))
}
func (c *committedManager) Compare(ctx context.Context, ns graveler.StorageNamespace, destination, source, base graveler.MetaRangeID) (graveler.DiffIterator, error) {
diffIt, err := c.Diff(ctx, ns, destination, source)
if err != nil {
return nil, fmt.Errorf("diff: %w", err)
}
baseIt, err := c.metaRangeManager.NewMetaRangeIterator(ctx, ns, base)
if err != nil {
return nil, fmt.Errorf("get base iterator: %w", err)
}
return NewCompareValueIterator(ctx, NewDiffIteratorWrapper(diffIt), baseIt), nil
}
func (c *committedManager) GetMetaRange(ctx context.Context, ns graveler.StorageNamespace, id graveler.MetaRangeID) (graveler.MetaRangeInfo, error) {
uri, err := c.metaRangeManager.GetMetaRangeURI(ctx, ns, id)
return graveler.MetaRangeInfo{Address: uri}, err
}
func (c *committedManager) GetRange(ctx context.Context, ns graveler.StorageNamespace, id graveler.RangeID) (graveler.RangeInfo, error) {
uri, err := c.metaRangeManager.GetRangeURI(ctx, ns, id)
return graveler.RangeInfo{Address: uri}, err
}