-
Notifications
You must be signed in to change notification settings - Fork 5
/
comparator.go
175 lines (146 loc) · 3.72 KB
/
comparator.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package origins
// Comparator is a function type that compares two facts for the purposes of sorting.
// It returns true if the first fact should come before the second fact.
type Comparator func(f1, f2 *Fact) bool
const (
compTrue int8 = iota - 1
compEqual
compFalse
)
// identComparator compares two Ident values by domain then local name.
// This comparator is used as the basis for other identity-based comparators and
// therefore returns compTrue, compFalse or compEqual with whether the left is less than the
// right.
func identComparator(i1, i2 *Ident) int8 {
// Compare pointer values. No change.
if i1 == i2 {
return compEqual
}
// First check does not pass.
if i1.Domain > i2.Domain {
return compFalse
} else if i1.Domain < i2.Domain {
return compTrue
}
if i1.Name > i2.Name {
return compFalse
} else if i1.Name < i2.Name {
return compTrue
}
return compEqual
}
// IdentComparator compares to identities.
func IdentComparator(id1, id2 *Ident) bool {
if identComparator(id1, id2) == compTrue {
return true
}
return false
}
// EntityComparator compares two entity identities.
func EntityComparator(f1, f2 *Fact) bool {
return IdentComparator(f1.Entity, f2.Entity)
}
// AttributeComparator compares two attribute identities.
func AttributeComparator(f1, f2 *Fact) bool {
return IdentComparator(f1.Attribute, f2.Attribute)
}
// ValueComparator compares two value identities.
func ValueComparator(f1, f2 *Fact) bool {
return IdentComparator(f1.Value, f2.Value)
}
// TransactionComparator compares two value identities.
func TransactionComparator(f1, f2 *Fact) bool {
return f1.Transaction < f2.Transaction
}
// TimeComparator compares two times.
func TimeComparator(f1, f2 *Fact) bool {
return f1.Time.Before(f2.Time)
}
// EAVTComparator compares two facts using an entity-attribute-value-time sort.
func EAVTComparator(f1, f2 *Fact) bool {
switch identComparator(f1.Entity, f2.Entity) {
case compTrue:
return true
case compFalse:
return false
}
switch identComparator(f1.Attribute, f2.Attribute) {
case compTrue:
return true
case compFalse:
return false
}
switch identComparator(f1.Value, f2.Value) {
case compTrue:
return true
case compFalse:
return false
}
return TransactionComparator(f1, f2)
}
// AEVTComparator compares two facts using an attribute-entity-value-time sort.
func AEVTComparator(f1, f2 *Fact) bool {
switch identComparator(f1.Attribute, f2.Attribute) {
case compTrue:
return true
case compFalse:
return false
}
switch identComparator(f1.Entity, f2.Entity) {
case compTrue:
return true
case compFalse:
return false
}
switch identComparator(f1.Value, f2.Value) {
case compTrue:
return true
case compFalse:
return false
}
return TransactionComparator(f1, f2)
}
// AVETComparator compares two facts using an attribute-value-entity-time sort.
func AVETComparator(f1, f2 *Fact) bool {
switch identComparator(f1.Attribute, f2.Attribute) {
case compTrue:
return true
case compFalse:
return false
}
switch identComparator(f1.Value, f2.Value) {
case compTrue:
return true
case compFalse:
return false
}
switch identComparator(f1.Entity, f2.Entity) {
case compTrue:
return true
case compFalse:
return false
}
return TransactionComparator(f1, f2)
}
// VAETComparator compares two facts using an value-attribute-entity-time sort.
func VAETComparator(f1, f2 *Fact) bool {
switch identComparator(f1.Value, f2.Value) {
case compTrue:
return true
case compFalse:
return false
}
switch identComparator(f1.Attribute, f2.Attribute) {
case compTrue:
return true
case compFalse:
return false
}
switch identComparator(f1.Entity, f2.Entity) {
case compTrue:
return true
case compFalse:
return false
}
return TransactionComparator(f1, f2)
}