This repository has been archived by the owner on Jul 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_linked_data.go
363 lines (324 loc) · 11.6 KB
/
client_linked_data.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
/*
* Copyright (C) 2017-2018 GIG Technology NV and Contributors
*
* 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 client
import (
"errors"
"io"
"github.com/threefoldtech/0-stor/client/datastor/pipeline"
"github.com/threefoldtech/0-stor/client/metastor"
"github.com/threefoldtech/0-stor/client/metastor/metatypes"
log "github.com/sirupsen/logrus"
)
// NOTE:
// the functionality provided in this file is to be considered EXPERIMENTAL,
// and might be moved or changed in a future milestone.
// See https://github.com/threefoldtech/0-stor/issues/424 for more information.
var (
// ErrInvalidTraverseIterator is an error returned when (meta)data
// of an iterator is requested, while that iterator is in an invalid state.
ErrInvalidTraverseIterator = errors.New(
"TraverseIterator is invalid: did you call (TraverseIterator).Next?")
// ErrInvalidEpochRange is an error returned when,
// during the creation of a traverse iterator,
// the given epoch range is invalid (e.g. start > end).
ErrInvalidEpochRange = errors.New(
"cannot create traverse iterator: epoch range is invalid")
)
// TraverseIterator defines the interface of an iterator,
// which is returned by a client traverse function.
type TraverseIterator interface {
// Next moves the iterator one (valid) position forward,
// returning false if the iterator has been exhausted.
//
// Next has to be called before any (meta)data can be fetched or read.
Next() bool
// PeekNextKey returns the next key in line.
// Note that due to the specified epoch range it
// might mean that the data of this key will never be available,
// in case the creation time of the linked (meta)data is
// not within the specified time range.
//
// False is returned in case the iterator has been exhausted,
// and thus no next key is lined up.
PeekNextKey() ([]byte, bool)
// GetMetadata returns the current (and already fetched) metadata.
//
// An error is returned in case no metadata is available,
// due to the iterator being in an invalid state.
GetMetadata() (*metatypes.Metadata, error)
// ReadData reads the data available for the current metadata,
// and writes it to the specified writer.
//
// An error is returned in case the iterator is in an invalid state,
// and thus no data is available to be read.
ReadData(w io.Writer) error
}
// WriteLinked writes the data to a 0-stor cluster,
// storing the metadata using the internal metastor client,
// as well as linking the metadata created for this data,
// to the metadata linked to the given previous key.
//
// This method is to be considered EXPERIMENTAL,
// and might be moved or changed in a future milestone.
// See https://github.com/threefoldtech/0-stor/issues/424 for more information.
func (c *Client) WriteLinked(key, prevKey []byte, r io.Reader) (meta, prevMeta *metatypes.Metadata, err error) {
if len(key) == 0 {
return nil, nil, ErrNilKey // ensure a key is given
}
if len(prevKey) == 0 {
// ensure a prevKey is given
// this is not optional here,
// if you don't want prevKey you should use the Write method
return nil, nil, ErrNilKey
}
// ensure the prevmetadata exists, and lock it until we've updated it,
// however we want to make sure that we only create+store the current metadata once
prevMeta, err = c.metastorClient.UpdateMetadata(prevKey,
func(prevMetadata metatypes.Metadata) (*metatypes.Metadata, error) {
// create the current metadata, should it not be created yet
if meta == nil {
// process and write the data
chunks, err := c.dataPipeline.Write(r)
if err != nil {
return nil, err
}
// create new metadata, as we'll overwrite either way
now := EpochNow()
meta = &metatypes.Metadata{
Key: key,
CreationEpoch: now,
LastWriteEpoch: now,
PreviousKey: prevKey,
}
// set/update chunks and size in metadata
meta.Chunks = chunks
for _, chunk := range chunks {
meta.Size += chunk.Size
}
// store current metadata
err = c.metastorClient.SetMetadata(*meta)
if err != nil {
return nil, err
}
}
// do the actual update of the prevMetadata
prevMetadata.NextKey = key
return &prevMetadata, nil
})
return meta, prevMeta, err
}
// Traverse traverses the stored (meta)data,
// which is chained together using the (*Client).WriteLinked method.
// It starts searching from a given startKey and will iterate through all (meta)data,
// which has a registered CreationEpoch in the given inclusive epoch range.
//
// Both the fromEpoch and toEpoch input parameters are optional and do not have to be given.
// Any value of 0 or less can be given as to not specify them. Not giving an epoch limit,
// simply means that this limit won't be used/enforced.
//
// An error will be returned in case no (valid) startKey is given,
// or in case the given epoch range is invalid (fromEpoch > toEpoch).
//
// The returned TraverseIterator is only valid,
// as long as the client which created and owns that iterator is valid (e.g. not closed).
// This traverse iterator is NOT /THREAD-SAFE/.
//
// This method is to be considered EXPERIMENTAL,
// and might be moved or changed in a future milestone.
// See https://github.com/threefoldtech/0-stor/issues/424 for more information.
func (c *Client) Traverse(startKey []byte, fromEpoch, toEpoch int64) (TraverseIterator, error) {
if len(startKey) == 0 {
return nil, ErrNilKey
}
if fromEpoch <= 0 {
fromEpoch = -1
}
if toEpoch <= 0 {
toEpoch = -1
}
if toEpoch != -1 && fromEpoch > toEpoch {
return nil, ErrInvalidEpochRange
}
return &forwardTraverseIterator{
traverseIteratorState: traverseIteratorState{
dataPipeline: c.dataPipeline,
},
nextKey: startKey,
fromEpoch: fromEpoch,
toEpoch: toEpoch,
metaClient: c.metastorClient,
}, nil
}
// TraversePostOrder traverses the stored (meta)data, backwards,
// which is chained together using the (*Client).WriteLinked method.
// It starts searching from a given startKey and will iterate through all (meta)data,
// which has a registered CreationEpoch in the given inclusive epoch range.
//
// Both the fromEpoch and toEpoch input parameters are optional and do not have to be given.
// Any value of 0 or less can be given as to not specify them. Not giving an epoch limit,
// simply means that this limit won't be used/enforced.
//
// As this method traverses backwards, the startKey is expected
// to be the newest data as the given fromEpoch should be the most recent time in this chain.
//
// An error will be returned in case no (valid) startKey is given,
// or in case the given epoch range is invalid (toEpoch > fromEpoch).
//
// The returned TraverseIterator is only valid,
// as long as the client which created and owns that iterator is valid (e.g. not closed).
// This traverse iterator is NOT /THREAD-SAFE/.
//
// This method is to be considered EXPERIMENTAL,
// and might be moved or changed in a future milestone.
// See https://github.com/threefoldtech/0-stor/issues/424 for more information.
func (c *Client) TraversePostOrder(startKey []byte, fromEpoch, toEpoch int64) (TraverseIterator, error) {
if len(startKey) == 0 {
return nil, ErrNilKey
}
if fromEpoch <= 0 {
fromEpoch = -1
}
if toEpoch <= 0 {
toEpoch = -1
}
if fromEpoch != -1 && toEpoch > fromEpoch {
return nil, ErrInvalidEpochRange
}
return &backwardTraverseIterator{
traverseIteratorState: traverseIteratorState{
dataPipeline: c.dataPipeline,
},
previousKey: startKey,
fromEpoch: fromEpoch,
toEpoch: toEpoch,
metaClient: c.metastorClient,
}, nil
}
// traverseIteratorState is the core of both iterators defined in this file.
// It defines the (meta)data fetcher methods, as implementations to become a TraverseIterator.
// The actual traverse iterator type will encapsulate this method, to provide the
// required Next method, to complete the implementation.
//
// The state contains a static dataPipeline, provided at construction time,
// and shared with the Client owner. After that client closes,
// this traverse iterator should no longer be used, as the dataPipeline will no longer function.
//
// It also contains a cached metadata structure pointer,
// which contains the current metadata state, the iterator is on.
// If this metadata is nil, the iterator is to be considered invalid
// (e.g. Next was never called (successfully)).
type traverseIteratorState struct {
md *metatypes.Metadata
dataPipeline pipeline.Pipeline
}
// Getmetadata implements TraverseIterator.GetMetadata
func (state *traverseIteratorState) GetMetadata() (*metatypes.Metadata, error) {
if state.md == nil {
return nil, ErrInvalidTraverseIterator
}
return state.md, nil
}
// ReadData implements TraverseIterator.ReadData
func (state *traverseIteratorState) ReadData(w io.Writer) error {
if w == nil {
panic("TraverseIterator: ReadData: required io.Writer is nil")
}
if state.md == nil {
return ErrInvalidTraverseIterator
}
return state.dataPipeline.Read(state.md.Chunks, w)
}
// forwardTraverseIterator contains the logic and state
// to move a traverse iterator forward (in time).
type forwardTraverseIterator struct {
traverseIteratorState
nextKey []byte
fromEpoch, toEpoch int64
metaClient *metastor.Client
}
// Next implements TraverseIterator.Next
func (it *forwardTraverseIterator) Next() bool {
for it.nextKey != nil {
md, err := it.metaClient.GetMetadata(it.nextKey)
if err != nil {
log.Errorf("error while fetching metdata for key %s: %v",
it.nextKey, err)
return false
}
if it.toEpoch != -1 && md.CreationEpoch > it.toEpoch {
// we've exhausted the iterator
it.nextKey = nil
return false
}
it.nextKey = md.NextKey
if it.fromEpoch != -1 && md.CreationEpoch < it.fromEpoch {
continue
}
it.traverseIteratorState.md = md
return true
}
return false
}
// PeekNextKey implements TraverseIterator.PeekNextKey
func (it *forwardTraverseIterator) PeekNextKey() ([]byte, bool) {
if it.nextKey == nil {
return nil, false
}
return it.nextKey, true
}
// backwardTraverseIterator contains the logic and state
// to move a traverse iterator backward (in time).
type backwardTraverseIterator struct {
traverseIteratorState
previousKey []byte
fromEpoch, toEpoch int64
metaClient *metastor.Client
}
// Next implements TraverseIterator.Next
func (it *backwardTraverseIterator) Next() bool {
for it.previousKey != nil {
md, err := it.metaClient.GetMetadata(it.previousKey)
if err != nil {
log.Errorf("error while fetching metdata for key %s: %v",
it.previousKey, err)
return false
}
if it.toEpoch != -1 && md.CreationEpoch < it.toEpoch {
// we've exhausted the iterator
it.previousKey = nil
return false
}
it.previousKey = md.PreviousKey
if it.fromEpoch != -1 && md.CreationEpoch > it.fromEpoch {
continue
}
it.traverseIteratorState.md = md
return true
}
return false
}
// PeekNextKey implements TraverseIterator.PeekNextKey
func (it *backwardTraverseIterator) PeekNextKey() ([]byte, bool) {
if it.previousKey == nil {
return nil, false
}
return it.previousKey, true
}
// Ensure our iterators adhere to the TraverseIterator interface.
var (
_ TraverseIterator = (*forwardTraverseIterator)(nil)
_ TraverseIterator = (*backwardTraverseIterator)(nil)
)