git.frostfs.info/TrueCloudLab/frostfs-sdk-go@v0.0.0-20241022124111-5361f0ecebd3/object/lock.go (about) 1 package object 2 3 import ( 4 v2object "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object" 5 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs" 6 oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id" 7 ) 8 9 // Lock represents record with locked objects. It is compatible with 10 // FrostFS API V2 protocol. 11 // 12 // Lock instance can be written to the Object, see WriteLock/ReadLock. 13 type Lock v2object.Lock 14 15 // WriteLock writes Lock to the Object, and sets its type to TypeLock. 16 // The object must not be nil. 17 // 18 // See also ReadLock. 19 func WriteLock(obj *Object, l Lock) { 20 obj.SetType(TypeLock) 21 obj.SetPayload(l.Marshal()) 22 } 23 24 // ReadLock reads Lock from the Object. The lock must not be nil. 25 // Returns an error describing incorrect format. Makes sense only 26 // if object has TypeLock type. 27 // 28 // See also WriteLock. 29 func ReadLock(l *Lock, obj Object) error { 30 return l.Unmarshal(obj.Payload()) 31 } 32 33 // NumberOfMembers returns number of members in lock list. 34 func (x Lock) NumberOfMembers() int { 35 return (*v2object.Lock)(&x).NumberOfMembers() 36 } 37 38 // ReadMembers reads list of locked members. 39 // 40 // Buffer length must not be less than NumberOfMembers. 41 func (x Lock) ReadMembers(buf []oid.ID) { 42 var i int 43 44 (*v2object.Lock)(&x).IterateMembers(func(idV2 refs.ObjectID) { 45 _ = buf[i].ReadFromV2(idV2) 46 i++ 47 }) 48 } 49 50 // WriteMembers writes list of locked members. 51 func (x *Lock) WriteMembers(ids []oid.ID) { 52 var members []refs.ObjectID 53 54 if ids != nil { 55 members = make([]refs.ObjectID, len(ids)) 56 57 for i := range ids { 58 ids[i].WriteToV2(&members[i]) 59 } 60 } 61 62 (*v2object.Lock)(x).SetMembers(members) 63 } 64 65 // Marshal encodes the Lock into a FrostFS protocol binary format. 66 func (x Lock) Marshal() []byte { 67 return (*v2object.Lock)(&x).StableMarshal(nil) 68 } 69 70 // Unmarshal decodes the Lock from its FrostFS protocol binary representation. 71 func (x *Lock) Unmarshal(data []byte) error { 72 return (*v2object.Lock)(x).Unmarshal(data) 73 }