-
Notifications
You must be signed in to change notification settings - Fork 1
/
triples.go
172 lines (144 loc) · 2.82 KB
/
triples.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
// Copyright 2014 Comcast Cable Communications Management, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tinygraph
import (
"fmt"
)
type Triple struct {
S []byte
P []byte
O []byte
V []byte
}
func (t *Triple) Copy() *Triple {
return &Triple{t.S, t.P, t.O, t.V}
}
func TripleFromStrings(args ...string) *Triple {
var s []byte
var p []byte
var o []byte
var v []byte
n := len(args)
if 0 < n {
s = []byte(args[0])
}
if 1 < n {
p = []byte(args[1])
}
if 2 < n {
o = []byte(args[2])
}
if 3 < n {
v = []byte(args[3])
}
return &Triple{s, p, o, v}
}
// Not func(t *Triple) so Otto can find this method.
func (t Triple) Strings() []string {
acc := make([]string, 0, 4)
acc = append(acc, string(t.S))
acc = append(acc, string(t.P))
acc = append(acc, string(t.O))
acc = append(acc, string(t.V))
return acc
}
// Not func(t *Triple) so Otto can find this method.
func (t Triple) String() string {
return "<'" +
string(t.S) + "','" +
string(t.P) + "','" +
string(t.O) + "','" +
string(t.V) + "'>"
}
func TripleFromBytes(k []byte, v []byte) *Triple {
start := 0
at := 0
for 0 < k[at] {
at++
}
s := k[start:at]
at++
start = at
for 0 < k[at] {
at++
}
p := k[start:at]
at++
start = at
for 0 < k[at] {
at++
}
o := k[start:at]
at++
return &Triple{s, p, o, v}
}
func (t *Triple) Key() []byte {
k := make([]byte, 0, len(t.S)+len(t.P)+len(t.O)+3)
k = append(k, t.S...)
k = append(k, byte(0))
k = append(k, t.P...)
k = append(k, byte(0))
k = append(k, t.O...)
k = append(k, byte(0))
return k
}
func (t *Triple) Val() []byte {
return t.V
}
func (t *Triple) StartKey() []byte {
k := make([]byte, 0, len(t.S)+len(t.P)+len(t.O)+3)
if len(t.S) == 0 {
return k
}
k = append(k, t.S...)
k = append(k, byte(0))
k = append(k, t.P...)
k = append(k, byte(0))
k = append(k, t.O...)
k = append(k, byte(0))
return k
}
func (t *Triple) KeyPrefix() []byte {
prefix := t.StartKey()
i := len(prefix) - 1
for 0 <= i && prefix[i] == 0 {
i--
}
return prefix[0 : i+2]
}
type Index byte
const (
SPO = iota
OPS
PSO
)
// Does not copy!
func (t *Triple) Permute(index Index) *Triple {
switch index {
case SPO:
case OPS:
s := t.S
t.S = t.O
t.O = s
case PSO:
s := t.S
t.S = t.P
t.P = s
}
return t
}
func PrintTriple(t *Triple) bool {
fmt.Println(t.Strings())
return true
}