/
store.go
124 lines (102 loc) · 2.9 KB
/
store.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
package blobs
import (
"context"
"fmt"
"io"
"github.com/apple/foundationdb/bindings/go/src/fdb"
"github.com/apple/foundationdb/bindings/go/src/fdb/directory"
)
// Upload token returned when uploading and used when commiting an upload.
type UploadToken struct {
dir directory.DirectorySubspace
}
// The store type.
type Store struct {
db fdb.Database
blobsDir directory.DirectorySubspace
removedDir directory.DirectorySubspace
uploadsDir directory.DirectorySubspace
chunkSize int
chunksPerTransaction int
systemTime SystemTime
idGenerator IdGenerator
}
// NewStore constructs a new blob store with the given FoundationDB instance, a
// namespace ns the blobs are stored under and a list of options.
func NewStore(db fdb.Database, ns string, opts ...Option) (*Store, error) {
dir, err := directory.CreateOrOpen(db, []string{"fdb-blobs", ns}, nil)
if err != nil {
return nil, err
}
blobsDir, err := dir.CreateOrOpen(db, []string{"blobs"}, nil)
if err != nil {
return nil, err
}
uploadsDir, err := dir.CreateOrOpen(db, []string{"uploads"}, nil)
if err != nil {
return nil, err
}
removedDir, err := dir.CreateOrOpen(db, []string{"removed"}, nil)
if err != nil {
return nil, err
}
store := &Store{
db: db,
blobsDir: blobsDir,
uploadsDir: uploadsDir,
removedDir: removedDir,
chunkSize: 10000,
chunksPerTransaction: 100,
systemTime: realClock{},
idGenerator: UlidIdGenerator{},
}
for _, opt := range opts {
err := opt(store)
if err != nil {
return store, err
}
}
return store, nil
}
func (store *Store) openBlobDir(id Id) (directory.DirectorySubspace, error) {
blobDir, err := store.blobsDir.Open(store.db, []string{string(id)}, nil)
if err != nil {
return blobDir, fmt.Errorf("%w: %q", BlobNotFoundError, id)
}
return blobDir, nil
}
// Returns a blob instance for the given id.
func (store *Store) Blob(id Id) (*Blob, error) {
blobDir, err := store.openBlobDir(id)
if err != nil {
return nil, err
}
data, err := store.db.ReadTransact(func(tr fdb.ReadTransaction) (any, error) {
return tr.Get(blobDir.Sub("chunkSize")).Get()
})
if err != nil {
return nil, err
}
chunkSize := int(decodeUInt64(data.([]byte)))
blob := &Blob{
db: store.db,
dir: blobDir,
chunkSize: chunkSize,
chunksPerTransaction: store.chunksPerTransaction,
}
return blob, err
}
// Creates and returns a new blob with the content of the given reader r.
func (store *Store) Create(ctx context.Context, r io.Reader) (*Blob, error) {
token, err := store.Upload(ctx, r)
if err != nil {
return nil, err
}
id, err := store.db.Transact(func(tr fdb.Transaction) (any, error) {
return store.CommitUpload(tr, token)
})
if err != nil {
return nil, err
}
return store.Blob(id.(Id))
}