-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.go
60 lines (49 loc) · 1.53 KB
/
vector.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
package vecmt
import (
"encoding/binary"
"github.com/corex-mn/corex-base/inter/idx"
"github.com/corex-mn/corex-base/vecfc"
"github.com/corex-mn/go-corex/inter"
)
/*
* Use binary form for optimization, to avoid serialization. As a result, DB cache works as elements cache.
*/
type (
// HighestBeforeTime is a vector of highest events (their CreationTime) which are observed by source event
HighestBeforeTime []byte
HighestBefore struct {
VSeq *vecfc.HighestBeforeSeq
VTime *HighestBeforeTime
}
)
// NewHighestBefore creates new HighestBefore vector.
func NewHighestBefore(size idx.Validator) *HighestBefore {
return &HighestBefore{
VSeq: vecfc.NewHighestBeforeSeq(size),
VTime: NewHighestBeforeTime(size),
}
}
// NewHighestBeforeTime creates new HighestBeforeTime vector.
func NewHighestBeforeTime(size idx.Validator) *HighestBeforeTime {
b := make(HighestBeforeTime, size*8)
return &b
}
// Get i's position in the byte-encoded vector clock
func (b HighestBeforeTime) Get(i idx.Validator) inter.Timestamp {
for i >= b.Size() {
return 0
}
return inter.Timestamp(binary.LittleEndian.Uint64(b[i*8 : (i+1)*8]))
}
// Set i's position in the byte-encoded vector clock
func (b *HighestBeforeTime) Set(i idx.Validator, time inter.Timestamp) {
for i >= b.Size() {
// append zeros if exceeds size
*b = append(*b, []byte{0, 0, 0, 0, 0, 0, 0, 0}...)
}
binary.LittleEndian.PutUint64((*b)[i*8:(i+1)*8], uint64(time))
}
// Size of the vector clock
func (b HighestBeforeTime) Size() idx.Validator {
return idx.Validator(len(b) / 8)
}