aboutsummaryrefslogtreecommitdiff
path: root/object/storer/chain/bytes.go
blob: d41f3b92597c2ff9df9675a80bf40b48dd4af67b (about) (plain) (blame)
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
package chain

import (
	"errors"
	"fmt"

	objectid "codeberg.org/lindenii/furgit/object/id"
	objectstorer "codeberg.org/lindenii/furgit/object/storer"
	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, objectstorer.ErrObjectNotFound) {
			continue
		}

		return nil, fmt.Errorf("objectstorer: backend %d read bytes full: %w", i, err)
	}

	return nil, objectstorer.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, objectstorer.ErrObjectNotFound) {
			continue
		}

		return objecttype.TypeInvalid, nil, fmt.Errorf("objectstorer: backend %d read bytes content: %w", i, err)
	}

	return objecttype.TypeInvalid, nil, objectstorer.ErrObjectNotFound
}