-
Notifications
You must be signed in to change notification settings - Fork 14
/
lock.go
78 lines (65 loc) · 1.93 KB
/
lock.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
73
74
75
76
77
78
package object
import (
v2object "github.com/nspcc-dev/neofs-api-go/v2/object"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
)
// Lock represents record with locked objects. It is compatible with
// NeoFS API V2 protocol.
//
// Lock instance can be written to the [Object], see WriteLock/ReadLock.
type Lock v2object.Lock
// WriteLock writes [Lock] to the [Object], and sets its type to [TypeLock].
//
// See also ReadLock.
func (o *Object) WriteLock(l Lock) {
o.SetType(TypeLock)
o.SetPayload(l.Marshal())
}
// ReadLock reads [Lock] from the [Object]. The lock must not be nil.
// Returns an error describing incorrect format. Makes sense only
// if object has [TypeLock] type.
//
// See also [Object.WriteLock].
func (o *Object) ReadLock(l *Lock) error {
return l.Unmarshal(o.Payload())
}
// NumberOfMembers returns number of members in lock list.
func (x Lock) NumberOfMembers() int {
return (*v2object.Lock)(&x).NumberOfMembers()
}
// ReadMembers reads list of locked members.
//
// Buffer length must not be less than [Lock.NumberOfMembers].
func (x Lock) ReadMembers(buf []oid.ID) {
var i int
(*v2object.Lock)(&x).IterateMembers(func(idV2 refs.ObjectID) {
_ = buf[i].ReadFromV2(idV2)
i++
})
}
// WriteMembers writes list of locked members.
//
// See also [Lock.ReadMembers].
func (x *Lock) WriteMembers(ids []oid.ID) {
var members []refs.ObjectID
if ids != nil {
members = make([]refs.ObjectID, len(ids))
for i := range ids {
ids[i].WriteToV2(&members[i])
}
}
(*v2object.Lock)(x).SetMembers(members)
}
// Marshal encodes the [Lock] into a NeoFS protocol binary format.
//
// See also [Lock.Unmarshal].
func (x Lock) Marshal() []byte {
return (*v2object.Lock)(&x).StableMarshal(nil)
}
// Unmarshal decodes the [Lock] from its NeoFS protocol binary representation.
//
// See also [Lock.Marshal].
func (x *Lock) Unmarshal(data []byte) error {
return (*v2object.Lock)(x).Unmarshal(data)
}