Repository navigation
Expand file tree
/
Copy pathtrove.go
More file actions
379 lines (324 loc) · 10.6 KB
/
Copy pathtrove.go
File metadata and controls
379 lines (324 loc) · 10.6 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package trove
import (
"context"
"fmt"
"io"
"sort"
"github.com/xraph/trove/cas"
"github.com/xraph/trove/driver"
"github.com/xraph/trove/internal"
"github.com/xraph/trove/middleware"
"github.com/xraph/trove/stream"
"github.com/xraph/trove/vfs"
)
// Trove is the root handle for object storage operations.
// It wraps one or more storage drivers, routing operations through
// the configured backend(s).
type Trove struct {
config Config
router *internal.Router
driver driver.Driver // primary driver (alias for router.Default())
pool *stream.Pool // default streaming pool
resolver *middleware.Resolver // middleware pipeline resolver
cas *cas.CAS // content-addressable storage (nil if disabled)
vfsBucket string // VFS default bucket (empty = use config.DefaultBucket)
}
// Open creates a new Trove instance with the given default driver and options.
//
// Example:
//
// t, err := trove.Open(localdriver.New(),
// trove.WithDefaultBucket("primary"),
// trove.WithChunkSize(8 * 1024 * 1024),
// )
func Open(drv driver.Driver, opts ...Option) (*Trove, error) {
if drv == nil {
return nil, ErrNilDriver
}
t := &Trove{
config: DefaultConfig(),
router: internal.NewRouter(drv),
driver: drv,
resolver: middleware.NewResolver(),
}
for _, opt := range opts {
if err := opt(t); err != nil {
return nil, fmt.Errorf("trove: apply option: %w", err)
}
}
// Initialize the default streaming pool if not set by options.
if t.pool == nil {
t.pool = stream.NewPool("default", stream.PoolConfig{
MaxStreams: t.config.PoolSize,
ChunkSize: t.config.ChunkSize,
})
}
return t, nil
}
// --- Object Operations ---
// Put stores an object from a reader. Data passes through the write
// middleware pipeline (e.g., encryption, compression) before reaching the driver.
func (t *Trove) Put(ctx context.Context, bucket, key string, r io.Reader, opts ...driver.PutOption) (*driver.ObjectInfo, error) {
if bucket == "" {
bucket = t.config.DefaultBucket
}
if bucket == "" {
return nil, ErrBucketEmpty
}
if key == "" {
return nil, ErrKeyEmpty
}
// Resolve the write middleware pipeline.
pipeline := t.resolver.ResolveWrite(ctx, bucket, key)
if len(pipeline) > 0 {
r = t.applyWritePipeline(ctx, r, key, pipeline)
}
drv := t.router.Resolve(bucket, key)
return drv.Put(ctx, bucket, key, r, opts...)
}
// Get retrieves an object, returning a ReadCloser for the content.
// Data passes through the read middleware pipeline (e.g., decryption,
// decompression) after being retrieved from the driver.
func (t *Trove) Get(ctx context.Context, bucket, key string, opts ...driver.GetOption) (*driver.ObjectReader, error) {
if bucket == "" {
bucket = t.config.DefaultBucket
}
if bucket == "" {
return nil, ErrBucketEmpty
}
if key == "" {
return nil, ErrKeyEmpty
}
drv := t.router.Resolve(bucket, key)
obj, err := drv.Get(ctx, bucket, key, opts...)
if err != nil {
return nil, err
}
// Resolve the read middleware pipeline.
pipeline := t.resolver.ResolveRead(ctx, bucket, key)
if len(pipeline) > 0 {
wrapped, wrapErr := t.applyReadPipeline(ctx, obj.ReadCloser, obj.Info, pipeline)
if wrapErr != nil {
_ = obj.Close()
return nil, wrapErr
}
obj.ReadCloser = wrapped
}
return obj, nil
}
// Delete removes an object.
func (t *Trove) Delete(ctx context.Context, bucket, key string, opts ...driver.DeleteOption) error {
if bucket == "" {
bucket = t.config.DefaultBucket
}
if bucket == "" {
return ErrBucketEmpty
}
if key == "" {
return ErrKeyEmpty
}
drv := t.router.Resolve(bucket, key)
return drv.Delete(ctx, bucket, key, opts...)
}
// Head returns object metadata without content.
func (t *Trove) Head(ctx context.Context, bucket, key string) (*driver.ObjectInfo, error) {
if bucket == "" {
bucket = t.config.DefaultBucket
}
if bucket == "" {
return nil, ErrBucketEmpty
}
if key == "" {
return nil, ErrKeyEmpty
}
drv := t.router.Resolve(bucket, key)
return drv.Head(ctx, bucket, key)
}
// List returns objects matching the given options.
func (t *Trove) List(ctx context.Context, bucket string, opts ...driver.ListOption) (*driver.ObjectIterator, error) {
if bucket == "" {
bucket = t.config.DefaultBucket
}
if bucket == "" {
return nil, ErrBucketEmpty
}
drv := t.router.Resolve(bucket, "")
return drv.List(ctx, bucket, opts...)
}
// Copy copies an object within or across buckets.
func (t *Trove) Copy(ctx context.Context, srcBucket, srcKey, dstBucket, dstKey string, opts ...driver.CopyOption) (*driver.ObjectInfo, error) {
if srcBucket == "" || dstBucket == "" {
return nil, ErrBucketEmpty
}
if srcKey == "" || dstKey == "" {
return nil, ErrKeyEmpty
}
// Use the source driver for the copy operation.
drv := t.router.Resolve(srcBucket, srcKey)
return drv.Copy(ctx, srcBucket, srcKey, dstBucket, dstKey, opts...)
}
// --- Bucket Operations ---
// CreateBucket creates a new bucket on the default driver.
func (t *Trove) CreateBucket(ctx context.Context, name string, opts ...driver.BucketOption) error {
if name == "" {
return ErrBucketEmpty
}
return t.driver.CreateBucket(ctx, name, opts...)
}
// DeleteBucket removes a bucket from the default driver.
func (t *Trove) DeleteBucket(ctx context.Context, name string) error {
if name == "" {
return ErrBucketEmpty
}
return t.driver.DeleteBucket(ctx, name)
}
// ListBuckets returns all buckets from the default driver.
func (t *Trove) ListBuckets(ctx context.Context) ([]driver.BucketInfo, error) {
return t.driver.ListBuckets(ctx)
}
// --- Accessors ---
// Backend returns a new Trove handle pinned to the named backend.
// All operations on the returned handle bypass routing and go directly
// to the specified backend.
func (t *Trove) Backend(name string) (*Trove, error) {
drv := t.router.Backend(name)
if drv == nil {
return nil, fmt.Errorf("%w: %q", ErrBackendNotFound, name)
}
return &Trove{
config: t.config,
router: internal.NewRouter(drv), // pinned to single backend
driver: drv,
pool: t.pool, // share the parent's streaming pool
resolver: t.resolver, // share the parent's middleware pipeline
cas: t.cas, // share the parent's CAS
}, nil
}
// Driver returns the primary (default) storage driver.
func (t *Trove) Driver() driver.Driver {
return t.driver
}
// DriverFor returns the driver that serves bucket and key, after routing.
// Driver returns only the default; an object routed elsewhere is served by
// this one.
func (t *Trove) DriverFor(bucket, key string) driver.Driver {
return t.router.Resolve(bucket, key)
}
// Backends returns the names of the backends registered with WithBackend,
// sorted. The default driver has no name and is never in the list, so an
// empty list means every operation goes to the default. A handle returned
// by Backend is pinned to one driver and returns an empty list.
func (t *Trove) Backends() []string {
names := t.router.Backends()
sort.Strings(names)
return names
}
// Config returns a copy of the current configuration.
func (t *Trove) Config() Config {
return t.config
}
// Pool returns the default streaming pool.
func (t *Trove) Pool() *stream.Pool {
return t.pool
}
// Stream creates a new managed stream within the default pool. The stream
// provides chunked transfer with backpressure, resumability, and lifecycle hooks.
func (t *Trove) Stream(ctx context.Context, bucket, key string, dir stream.Direction, opts ...stream.Option) (*stream.Stream, error) {
if bucket == "" {
bucket = t.config.DefaultBucket
}
if bucket == "" {
return nil, ErrBucketEmpty
}
if key == "" {
return nil, ErrKeyEmpty
}
return t.pool.Acquire(ctx, dir, bucket, key, opts...)
}
// Resolver returns the middleware pipeline resolver.
func (t *Trove) Resolver() *middleware.Resolver {
return t.resolver
}
// CAS returns the content-addressable storage engine, or nil if CAS is not enabled.
func (t *Trove) CAS() *cas.CAS {
return t.cas
}
// VFS returns a virtual filesystem view of the given bucket.
// If no bucket is specified, it uses the VFS bucket (set via WithVFS)
// or falls back to the default bucket.
func (t *Trove) VFS(bucket ...string) *vfs.FS {
b := t.vfsBucket
if len(bucket) > 0 && bucket[0] != "" {
b = bucket[0]
}
if b == "" {
b = t.config.DefaultBucket
}
return vfs.New(t, b)
}
// UseMiddleware registers a middleware at runtime. Thread-safe.
func (t *Trove) UseMiddleware(reg middleware.Registration) {
t.resolver.Register(reg)
}
// RemoveMiddleware removes all registrations matching the given middleware
// name and scope. If scope is nil, removes all registrations with that name.
func (t *Trove) RemoveMiddleware(name string, scope middleware.Scope) {
t.resolver.Remove(name, scope)
}
// Health checks the health of the Trove by pinging its primary driver.
func (t *Trove) Health(ctx context.Context) error {
return t.driver.Ping(ctx)
}
// Close gracefully shuts down the streaming pool and all drivers.
func (t *Trove) Close(ctx context.Context) error {
if t.pool != nil {
_ = t.pool.Close()
}
return t.router.CloseAll(func(drv driver.Driver) error {
return drv.Close(ctx)
})
}
// --- Middleware Pipeline Helpers ---
// applyWritePipeline wraps the reader through the write middleware chain.
// Write middleware wraps writers, so we create a pipe, wrap the writer side,
// copy data through, and return the reader side.
func (t *Trove) applyWritePipeline(ctx context.Context, r io.Reader, key string, pipeline []middleware.WriteMiddleware) io.Reader {
// Create a buffer to hold the transformed data.
pr, pw := io.Pipe()
go func() {
// Build the writer chain: innermost writer is the pipe writer.
var w io.WriteCloser = pw
// Apply middleware in reverse order so the first middleware in the
// pipeline is the outermost wrapper (data flows through it first).
for i := len(pipeline) - 1; i >= 0; i-- {
wrapped, err := pipeline[i].WrapWriter(ctx, w, key)
if err != nil {
_ = pw.CloseWithError(fmt.Errorf("trove: middleware %d wrap writer: %w", i, err))
return
}
w = wrapped
}
// Copy original data through the middleware chain.
if _, err := io.Copy(w, r); err != nil {
_ = pw.CloseWithError(err)
return
}
// Close the writer chain to flush/finalize middleware.
if err := w.Close(); err != nil {
_ = pw.CloseWithError(err)
return
}
}()
return pr
}
// applyReadPipeline wraps the reader through the read middleware chain.
func (t *Trove) applyReadPipeline(ctx context.Context, r io.ReadCloser, info *driver.ObjectInfo, pipeline []middleware.ReadMiddleware) (io.ReadCloser, error) {
var err error
for _, mw := range pipeline {
r, err = mw.WrapReader(ctx, r, info)
if err != nil {
return nil, fmt.Errorf("trove: middleware wrap reader: %w", err)
}
}
return r, nil
}