-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
68 lines (57 loc) · 1.88 KB
/
sync.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
// Copyright 2024 cloudeng llc. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
package stores
import (
"context"
"cloudeng.io/file/content"
)
// Sync represents a synchronous store for objects, ie. it implements
// content.ObjectStore. It uses an instance of content.FS to store and
// retrieve objects.
type Sync struct {
fs content.FS
}
// NewSync returns a new instance of Sync backed by the supplied
// content.FS and storing the specified objects encoded using the
// specified encodings.
func NewSync(fs content.FS) *Sync {
return &Sync{
fs: fs,
}
}
// EraseExisting deletes all contents of the store beneath root.
func (s *Sync) EraseExisting(ctx context.Context, root string) error {
return eraseExisting(ctx, s.fs, root)
}
func (s *Sync) FS() content.FS {
return s.fs
}
// Read retrieves the object type and serialized data at the specified prefix and name
// from the store. The caller is responsible for using the returned type to
// decode the data into an appropriate object.
func (s *Sync) Read(ctx context.Context, prefix, name string) (content.Type, []byte, error) {
return read(ctx, s.fs, s.fs.Join(prefix, name))
}
// Write stores the data at the specified prefix and name in the store.
func (s *Sync) Write(ctx context.Context, prefix, name string, data []byte) error {
return write(ctx, s.fs, prefix, name, data)
}
// ReadV calls ReadFunc for each object read from the store synchronously.
func (s *Sync) ReadV(ctx context.Context, prefix string, names []string, fn ReadFunc) error {
for _, name := range names {
typ, data, err := read(ctx, s.fs, s.fs.Join(prefix, name))
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if err := fn(ctx, prefix, name, typ, data, err); err != nil {
return err
}
}
return nil
}
func (s *Sync) Finish(context.Context) error {
return nil
}