diff options
author | 2023-03-01 09:44:54 +0000 | |
---|---|---|
committer | 2023-03-01 10:44:54 +0100 | |
commit | 87c5c4297284791ca60ee9eccc4d685b05013272 (patch) | |
tree | 04572ef24e5943eead505219e1f8097a9829a550 /internal/storage/storage.go | |
parent | [chore] Improve unsupported_grant_type error (#1572) (diff) | |
download | gotosocial-87c5c4297284791ca60ee9eccc4d685b05013272.tar.xz |
[chore/performance] simplify storage driver to use storage.Storage directly (#1576)
* simply use storage.Storage, removing wrapping KVStore as we don't need KV store locking functionality
Signed-off-by: kim <grufwub@gmail.com>
* fix missing unwrapped function
Signed-off-by: kim <grufwub@gmail.com>
* add code comment
Signed-off-by: kim <grufwub@gmail.com>
* linter, please take my offering in peace
Signed-off-by: kim <grufwub@gmail.com>
---------
Signed-off-by: kim <grufwub@gmail.com>
Diffstat (limited to 'internal/storage/storage.go')
-rw-r--r-- | internal/storage/storage.go | 49 |
1 files changed, 45 insertions, 4 deletions
diff --git a/internal/storage/storage.go b/internal/storage/storage.go index 00dcc088c..d9fc6d930 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -21,6 +21,7 @@ package storage import ( "context" "fmt" + "io" "mime" "net/url" "path" @@ -28,7 +29,6 @@ import ( "codeberg.org/gruf/go-bytesize" "codeberg.org/gruf/go-cache/v3/ttl" - "codeberg.org/gruf/go-store/v2/kv" "codeberg.org/gruf/go-store/v2/storage" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" @@ -54,7 +54,6 @@ var ErrAlreadyExists = storage.ErrAlreadyExists // Driver wraps a kv.KVStore to also provide S3 presigned GET URLs. type Driver struct { // Underlying storage - *kv.KVStore Storage storage.Storage // S3-only parameters @@ -63,6 +62,50 @@ type Driver struct { PresignedCache *ttl.Cache[string, PresignedURL] } +// Get returns the byte value for key in storage. +func (d *Driver) Get(ctx context.Context, key string) ([]byte, error) { + return d.Storage.ReadBytes(ctx, key) +} + +// GetStream returns an io.ReadCloser for the value bytes at key in the storage. +func (d *Driver) GetStream(ctx context.Context, key string) (io.ReadCloser, error) { + return d.Storage.ReadStream(ctx, key) +} + +// Put writes the supplied value bytes at key in the storage +func (d *Driver) Put(ctx context.Context, key string, value []byte) (int, error) { + return d.Storage.WriteBytes(ctx, key, value) +} + +// PutStream writes the bytes from supplied reader at key in the storage +func (d *Driver) PutStream(ctx context.Context, key string, r io.Reader) (int64, error) { + return d.Storage.WriteStream(ctx, key, r) +} + +// Remove attempts to remove the supplied key (and corresponding value) from storage. +func (d *Driver) Delete(ctx context.Context, key string) error { + return d.Storage.Remove(ctx, key) +} + +// Has checks if the supplied key is in the storage. +func (d *Driver) Has(ctx context.Context, key string) (bool, error) { + return d.Storage.Stat(ctx, key) +} + +// WalkKeys walks the keys in the storage. +func (d *Driver) WalkKeys(ctx context.Context, walk func(context.Context, string) error) error { + return d.Storage.WalkKeys(ctx, storage.WalkKeysOptions{ + WalkFn: func(ctx context.Context, entry storage.Entry) error { + return walk(ctx, entry.Key) + }, + }) +} + +// Close will close the storage, releasing any file locks. +func (d *Driver) Close() error { + return d.Storage.Close() +} + // URL will return a presigned GET object URL, but only if running on S3 storage with proxying disabled. func (d *Driver) URL(ctx context.Context, key string) *PresignedURL { // Check whether S3 *without* proxying is enabled @@ -128,7 +171,6 @@ func NewFileStorage() (*Driver, error) { } return &Driver{ - KVStore: kv.New(disk), Storage: disk, }, nil } @@ -163,7 +205,6 @@ func NewS3Storage() (*Driver, error) { presignedCache.Start(urlCacheExpiryFrequency) return &Driver{ - KVStore: kv.New(s3), Proxy: config.GetStorageS3Proxy(), Bucket: config.GetStorageS3BucketName(), Storage: s3, |