/
get.go
72 lines (58 loc) · 1.52 KB
/
get.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
package blobovnicza
import (
"bytes"
"errors"
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"go.etcd.io/bbolt"
)
// GetPrm groups the parameters of Get operation.
type GetPrm struct {
addr oid.Address
}
// GetRes groups the resulting values of Get operation.
type GetRes struct {
obj []byte
}
// SetAddress sets the address of the requested object.
func (p *GetPrm) SetAddress(addr oid.Address) {
p.addr = addr
}
// Object returns binary representation of the requested object.
func (p GetRes) Object() []byte {
return p.obj
}
// special error for normal bbolt.Tx.ForEach interruption.
var errInterruptForEach = errors.New("interrupt for-each")
// Get reads an object from Blobovnicza by address.
//
// Returns any error encountered that
// did not allow to completely read the object.
//
// Returns an error of type apistatus.ObjectNotFound if the requested object is not
// presented in Blobovnicza.
func (b *Blobovnicza) Get(prm GetPrm) (GetRes, error) {
var (
data []byte
addrKey = addressKey(prm.addr)
)
if err := b.boltDB.View(func(tx *bbolt.Tx) error {
return tx.ForEach(func(_ []byte, buck *bbolt.Bucket) error {
data = buck.Get(addrKey)
if data == nil {
return nil
}
data = bytes.Clone(data)
return errInterruptForEach
})
}); err != nil && err != errInterruptForEach {
return GetRes{}, err
}
if data == nil {
var errNotFound apistatus.ObjectNotFound
return GetRes{}, errNotFound
}
return GetRes{
obj: data,
}, nil
}