diff options
| author | 2026-03-25 14:30:31 +0000 | |
|---|---|---|
| committer | 2026-03-25 14:30:31 +0000 | |
| commit | bfa0a3f5f18b752a6ebd3d5b37411c6871f7bb17 (patch) | |
| tree | 8ee2479273e2b34d284c30703c2be48efe197556 /object/store/chain | |
| parent | *: Resort import order (diff) | |
| signature | No signature | |
*: objectstore -> object/store
Diffstat (limited to 'object/store/chain')
| -rw-r--r-- | object/store/chain/bytes.go | 46 | ||||
| -rw-r--r-- | object/store/chain/chain.go | 14 | ||||
| -rw-r--r-- | object/store/chain/close.go | 8 | ||||
| -rw-r--r-- | object/store/chain/header.go | 28 | ||||
| -rw-r--r-- | object/store/chain/new.go | 13 | ||||
| -rw-r--r-- | object/store/chain/reader.go | 47 | ||||
| -rw-r--r-- | object/store/chain/refresh.go | 17 | ||||
| -rw-r--r-- | object/store/chain/size.go | 27 |
8 files changed, 200 insertions, 0 deletions
diff --git a/object/store/chain/bytes.go b/object/store/chain/bytes.go new file mode 100644 index 00000000..d02b30bd --- /dev/null +++ b/object/store/chain/bytes.go @@ -0,0 +1,46 @@ +package chain + +import ( + "errors" + "fmt" + + objectid "codeberg.org/lindenii/furgit/object/id" + "codeberg.org/lindenii/furgit/object/store" + objecttype "codeberg.org/lindenii/furgit/object/type" +) + +// ReadBytesFull reads a full serialized object from the first backend that has it. +func (chain *Chain) ReadBytesFull(id objectid.ObjectID) ([]byte, error) { + for i, backend := range chain.backends { + full, err := backend.ReadBytesFull(id) + if err == nil { + return full, nil + } + + if errors.Is(err, objectstore.ErrObjectNotFound) { + continue + } + + return nil, fmt.Errorf("objectstore: backend %d read bytes full: %w", i, err) + } + + return nil, objectstore.ErrObjectNotFound +} + +// ReadBytesContent reads an object's type and content bytes from the first backend that has it. +func (chain *Chain) ReadBytesContent(id objectid.ObjectID) (objecttype.Type, []byte, error) { + for i, backend := range chain.backends { + ty, content, err := backend.ReadBytesContent(id) + if err == nil { + return ty, content, nil + } + + if errors.Is(err, objectstore.ErrObjectNotFound) { + continue + } + + return objecttype.TypeInvalid, nil, fmt.Errorf("objectstore: backend %d read bytes content: %w", i, err) + } + + return objecttype.TypeInvalid, nil, objectstore.ErrObjectNotFound +} diff --git a/object/store/chain/chain.go b/object/store/chain/chain.go new file mode 100644 index 00000000..1a7f4201 --- /dev/null +++ b/object/store/chain/chain.go @@ -0,0 +1,14 @@ +// Package chain provides a wrapper object storage backend to query a chain of +// backends. +package chain + +import ( + "codeberg.org/lindenii/furgit/object/store" +) + +// Chain queries multiple object databases in order. +// +// Chain borrows its backend stores. +type Chain struct { + backends []objectstore.Store +} diff --git a/object/store/chain/close.go b/object/store/chain/close.go new file mode 100644 index 00000000..6bd74565 --- /dev/null +++ b/object/store/chain/close.go @@ -0,0 +1,8 @@ +package chain + +// Close releases wrapper-local resources. +// +// Chain borrows its backends, so Close does not close them. +// +// Repeated calls to Close are undefined behavior. +func (chain *Chain) Close() error { return nil } diff --git a/object/store/chain/header.go b/object/store/chain/header.go new file mode 100644 index 00000000..e66a5aae --- /dev/null +++ b/object/store/chain/header.go @@ -0,0 +1,28 @@ +package chain + +import ( + "errors" + "fmt" + + objectid "codeberg.org/lindenii/furgit/object/id" + "codeberg.org/lindenii/furgit/object/store" + objecttype "codeberg.org/lindenii/furgit/object/type" +) + +// ReadHeader reads object header data from the first backend that has it. +func (chain *Chain) ReadHeader(id objectid.ObjectID) (objecttype.Type, int64, error) { + for i, backend := range chain.backends { + ty, size, err := backend.ReadHeader(id) + if err == nil { + return ty, size, nil + } + + if errors.Is(err, objectstore.ErrObjectNotFound) { + continue + } + + return objecttype.TypeInvalid, 0, fmt.Errorf("objectstore: backend %d read header: %w", i, err) + } + + return objecttype.TypeInvalid, 0, objectstore.ErrObjectNotFound +} diff --git a/object/store/chain/new.go b/object/store/chain/new.go new file mode 100644 index 00000000..2cc79b0d --- /dev/null +++ b/object/store/chain/new.go @@ -0,0 +1,13 @@ +package chain + +import "codeberg.org/lindenii/furgit/object/store" + +// New creates an ordered object database chain. +// +// The provided backends must be non-nil and distinct. +// Chain borrows the provided backends and does not close them in Close. +func New(backends ...objectstore.Store) *Chain { + return &Chain{ + backends: append([]objectstore.Store(nil), backends...), + } +} diff --git a/object/store/chain/reader.go b/object/store/chain/reader.go new file mode 100644 index 00000000..8d650503 --- /dev/null +++ b/object/store/chain/reader.go @@ -0,0 +1,47 @@ +package chain + +import ( + "errors" + "fmt" + "io" + + objectid "codeberg.org/lindenii/furgit/object/id" + "codeberg.org/lindenii/furgit/object/store" + objecttype "codeberg.org/lindenii/furgit/object/type" +) + +// ReadReaderFull reads a full serialized object stream from the first backend that has it. +func (chain *Chain) ReadReaderFull(id objectid.ObjectID) (io.ReadCloser, error) { + for i, backend := range chain.backends { + reader, err := backend.ReadReaderFull(id) + if err == nil { + return reader, nil + } + + if errors.Is(err, objectstore.ErrObjectNotFound) { + continue + } + + return nil, fmt.Errorf("objectstore: backend %d read reader full: %w", i, err) + } + + return nil, objectstore.ErrObjectNotFound +} + +// ReadReaderContent reads an object's type, declared content length, and content stream from the first backend that has it. +func (chain *Chain) ReadReaderContent(id objectid.ObjectID) (objecttype.Type, int64, io.ReadCloser, error) { + for i, backend := range chain.backends { + ty, size, reader, err := backend.ReadReaderContent(id) + if err == nil { + return ty, size, reader, nil + } + + if errors.Is(err, objectstore.ErrObjectNotFound) { + continue + } + + return objecttype.TypeInvalid, 0, nil, fmt.Errorf("objectstore: backend %d read reader content: %w", i, err) + } + + return objecttype.TypeInvalid, 0, nil, objectstore.ErrObjectNotFound +} diff --git a/object/store/chain/refresh.go b/object/store/chain/refresh.go new file mode 100644 index 00000000..c47352dc --- /dev/null +++ b/object/store/chain/refresh.go @@ -0,0 +1,17 @@ +package chain + +import "errors" + +// Refresh forwards refresh calls to all backends. +func (chain *Chain) Refresh() error { + var errs []error + + for _, backend := range chain.backends { + err := backend.Refresh() + if err != nil { + errs = append(errs, err) + } + } + + return errors.Join(errs...) +} diff --git a/object/store/chain/size.go b/object/store/chain/size.go new file mode 100644 index 00000000..bd35139f --- /dev/null +++ b/object/store/chain/size.go @@ -0,0 +1,27 @@ +package chain + +import ( + "errors" + "fmt" + + objectid "codeberg.org/lindenii/furgit/object/id" + "codeberg.org/lindenii/furgit/object/store" +) + +// ReadSize reads object content length from the first backend that has it. +func (chain *Chain) ReadSize(id objectid.ObjectID) (int64, error) { + for i, backend := range chain.backends { + size, err := backend.ReadSize(id) + if err == nil { + return size, nil + } + + if errors.Is(err, objectstore.ErrObjectNotFound) { + continue + } + + return 0, fmt.Errorf("objectstore: backend %d read size: %w", i, err) + } + + return 0, objectstore.ErrObjectNotFound +} |
