-
Notifications
You must be signed in to change notification settings - Fork 30
/
entry_merge.go
58 lines (44 loc) · 1.29 KB
/
entry_merge.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
package codec
import (
"github.com/chrislusf/vasto/pb"
"github.com/chrislusf/vasto/util"
)
// Merge merges two []byte into one []byte based on the Entry format.
func Merge(a, b []byte) (mergedBytes []byte, merged bool) {
x, merged := MergeEntry(a, b)
return x.ToBytes(), merged
}
// MergeEntry merges two []byte into one Entry object.
func MergeEntry(a, b []byte) (mergedEntry *Entry, merged bool) {
if a == nil {
return FromBytes(b), true
}
x := FromBytes(a)
merged = x.MergeWith(b)
return x, merged
}
// MergeWith merges one Entry object with a matching []byte.
func (e *Entry) MergeWith(b []byte) (merged bool) {
if b == nil {
return true
}
y := FromBytes(b)
switch y.OpAndDataType {
case OpAndDataType(pb.OpAndDataType_BYTES):
e.Value = append(e.Value, y.Value...)
case OpAndDataType(pb.OpAndDataType_FLOAT64):
result := util.BytesToFloat64(e.Value) + util.BytesToFloat64(y.Value)
e.Value = util.Float64ToBytes(result)
case OpAndDataType(pb.OpAndDataType_MAX_FLOAT64):
left, right := util.BytesToFloat64(e.Value), util.BytesToFloat64(y.Value)
if left < right {
e.Value = y.Value
}
case OpAndDataType(pb.OpAndDataType_MIN_FLOAT64):
left, right := util.BytesToFloat64(e.Value), util.BytesToFloat64(y.Value)
if left > right {
e.Value = y.Value
}
}
return true
}