github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/vecmt/vector.go (about) 1 package vecmt 2 3 import ( 4 "encoding/binary" 5 6 "github.com/unicornultrafoundation/go-helios/native/idx" 7 "github.com/unicornultrafoundation/go-helios/vecfc" 8 9 "github.com/unicornultrafoundation/go-u2u/native" 10 ) 11 12 /* 13 * Use binary form for optimization, to avoid serialization. As a result, DB cache works as elements cache. 14 */ 15 16 type ( 17 // HighestBeforeTime is a vector of highest events (their CreationTime) which are observed by source event 18 HighestBeforeTime []byte 19 20 HighestBefore struct { 21 VSeq *vecfc.HighestBeforeSeq 22 VTime *HighestBeforeTime 23 } 24 ) 25 26 // NewHighestBefore creates new HighestBefore vector. 27 func NewHighestBefore(size idx.Validator) *HighestBefore { 28 return &HighestBefore{ 29 VSeq: vecfc.NewHighestBeforeSeq(size), 30 VTime: NewHighestBeforeTime(size), 31 } 32 } 33 34 // NewHighestBeforeTime creates new HighestBeforeTime vector. 35 func NewHighestBeforeTime(size idx.Validator) *HighestBeforeTime { 36 b := make(HighestBeforeTime, size*8) 37 return &b 38 } 39 40 // Get i's position in the byte-encoded vector clock 41 func (b HighestBeforeTime) Get(i idx.Validator) native.Timestamp { 42 for i >= b.Size() { 43 return 0 44 } 45 return native.Timestamp(binary.LittleEndian.Uint64(b[i*8 : (i+1)*8])) 46 } 47 48 // Set i's position in the byte-encoded vector clock 49 func (b *HighestBeforeTime) Set(i idx.Validator, time native.Timestamp) { 50 for i >= b.Size() { 51 // append zeros if exceeds size 52 *b = append(*b, []byte{0, 0, 0, 0, 0, 0, 0, 0}...) 53 } 54 binary.LittleEndian.PutUint64((*b)[i*8:(i+1)*8], uint64(time)) 55 } 56 57 // Size of the vector clock 58 func (b HighestBeforeTime) Size() idx.Validator { 59 return idx.Validator(len(b) / 8) 60 }