-
Notifications
You must be signed in to change notification settings - Fork 23
/
data_collection.go
169 lines (136 loc) · 3.6 KB
/
data_collection.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
package kopia
import (
"context"
"io"
"github.com/alcionai/clues"
"github.com/kopia/kopia/fs"
"github.com/alcionai/corso/src/internal/common/readers"
"github.com/alcionai/corso/src/internal/data"
"github.com/alcionai/corso/src/pkg/fault"
"github.com/alcionai/corso/src/pkg/logger"
"github.com/alcionai/corso/src/pkg/path"
)
var (
_ data.RestoreCollection = &kopiaDataCollection{}
_ data.Item = &kopiaDataStream{}
_ data.ItemSize = &kopiaDataStream{}
)
type kopiaDataCollection struct {
path path.Path
dir fs.Directory
items []string
counter ByteCounter
expectedVersion readers.SerializationVersion
}
func (kdc *kopiaDataCollection) Items(
ctx context.Context,
errs *fault.Bus,
) <-chan data.Item {
var (
res = make(chan data.Item)
el = errs.Local()
loadCount = 0
)
go func() {
defer close(res)
for _, item := range kdc.items {
s, err := kdc.FetchItemByName(ctx, item)
if err != nil {
el.AddRecoverable(ctx, clues.WrapWC(ctx, err, "fetching item").
Label(fault.LabelForceNoBackupCreation))
continue
}
loadCount++
if loadCount%1000 == 0 {
logger.Ctx(ctx).Infow(
"loading items from kopia",
"loaded_items", loadCount)
}
res <- s
}
logger.Ctx(ctx).Infow(
"done loading items from kopia",
"loaded_items", loadCount)
}()
return res
}
func (kdc kopiaDataCollection) FullPath() path.Path {
return kdc.path
}
// Fetch returns the file with the given name from the collection as a
// data.Item. Returns a data.ErrNotFound error if the file isn't in the
// collection.
func (kdc kopiaDataCollection) FetchItemByName(
ctx context.Context,
name string,
) (data.Item, error) {
ctx = clues.Add(ctx, "item_name", clues.Hide(name))
if kdc.dir == nil {
return nil, clues.New("no snapshot directory")
}
if len(name) == 0 {
return nil, clues.WrapWC(ctx, ErrNoRestorePath, "unknown item")
}
e, err := kdc.dir.Child(ctx, encodeAsPath(name))
if err != nil {
if isErrEntryNotFound(err) {
err = clues.Stack(data.ErrNotFound, err)
}
return nil, clues.WrapWC(ctx, err, "getting item")
}
f, ok := e.(fs.File)
if !ok {
return nil, clues.NewWC(ctx, "object is not a file")
}
size := f.Size() - int64(readers.VersionFormatSize)
if size < 0 {
logger.Ctx(ctx).Infow("negative file size; resetting to 0", "file_size", size)
size = 0
}
if kdc.counter != nil {
kdc.counter.Count(size)
}
r, err := f.Open(ctx)
if err != nil {
return nil, clues.WrapWC(ctx, err, "opening file")
}
// TODO(ashmrtn): Remove this when individual services implement checks for
// version and deleted items.
rr, err := readers.NewVersionedRestoreReader(r)
if err != nil {
return nil, clues.StackWC(ctx, err)
}
if rr.Format().Version != kdc.expectedVersion {
return nil, clues.NewWC(ctx, "unexpected data format").
With(
"read_version", rr.Format().Version,
"expected_version", kdc.expectedVersion)
}
// This is a conservative check, but we shouldn't be seeing items that were
// deleted in flight during restores because there's no way to select them.
if rr.Format().DelInFlight {
return nil, clues.NewWC(ctx, "selected item marked as deleted in flight")
}
return &kopiaDataStream{
id: name,
reader: rr,
size: size,
}, nil
}
type kopiaDataStream struct {
reader io.ReadCloser
id string
size int64
}
func (kds kopiaDataStream) ToReader() io.ReadCloser {
return kds.reader
}
func (kds kopiaDataStream) ID() string {
return kds.id
}
func (kds kopiaDataStream) Deleted() bool {
return false
}
func (kds kopiaDataStream) Size() int64 {
return kds.size
}