-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
204 lines (132 loc) · 3.44 KB
/
query.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package query
import (
"sync"
//
"github.com/golangdaddy/tarantula/graph"
)
type Client struct {
DB *graph.Database
Queries map[string]*Query
sync.RWMutex
}
func NewClient(db *graph.Database) *Client {
return &Client{
DB: db,
Queries: map[string]*Query{},
}
}
type QueryExec struct {
Query *Query
Lists [][]*graph.Link
Results []*graph.Vertex
sync.RWMutex
}
type Query struct {
DB *graph.Database
Name string
Cursors Cursors
Traversals []*Traversal
TraversalCount int
Limit int64
Export bool
}
func (client *Client) Query(name string) *Query {
client.RLock()
query := client.Queries[name]
client.RUnlock()
return query
}
func (client *Client) NewQuery(name string) *Query {
q := &Query{
DB: client.DB,
Name: name,
Traversals: []*Traversal{},
Limit: -1,
}
client.Lock()
client.Queries[name] = q
client.Unlock()
return q
}
func (query *Query) lastTraversal() *Traversal {
x := len(query.Traversals)
if x == 0 { panic("THIS QUERY OBJECT HAS NO TRAVERSALS TO FILTER") }
return query.Traversals[x - 1]
}
func (query *Query) Class(class *graph.Class) *Query {
traversal := query.lastTraversal()
traversal.Class = class
return query
}
func (query *Query) Filter(filters map[string]interface{}) *Query {
traversal := query.lastTraversal()
traversal.Filters = filters
return query
}
func (query *Query) Exported() *Query {
query.Export = true
return query
}
type Traversal struct {
Predicate *graph.Predicate
Phase bool
Filters map[string]interface{}
Class *graph.Class
}
func (traversal *Traversal) Exec(vtx *graph.Vertex) (bool, []*graph.Link) {
db := traversal.Predicate.DB
var ok bool
var array []*graph.Link
if traversal.Phase {
if traversal.Predicate.Sync() {
array = traversal.Predicate.In.ActiveEdges(vtx.Uid, -1)
db.Log.Debug("IN QUERY SYNC")
db.Log.DebugJSON(array)
} else {
ok, array = db.Client.TraverseEdges(vtx.Uid, traversal.Predicate, traversal.Phase, -1); if !ok { db.Log.Debug("TRAVERSE QUIT 2"); return false, nil }
db.Log.Debug("IN QUERY SLOW")
db.Log.DebugJSON(array)
}
} else {
if traversal.Predicate.Sync() {
array = traversal.Predicate.Out.ActiveEdges(vtx.Uid, -1)
db.Log.Debug("OUT QUERY SYNC")
db.Log.DebugJSON(array)
} else {
ok, array = db.Client.TraverseEdges(vtx.Uid, traversal.Predicate, traversal.Phase, -1); if !ok { db.Log.Debug("TRAVERSE QUIT 3"); return false, nil }
db.Log.Debug("OUT QUERY SLOW")
db.Log.DebugJSON(array)
}
}
return true, array
}
func newTraversal(predicate *graph.Predicate, phase bool) *Traversal {
return &Traversal{
Predicate: predicate,
Phase: true,
Filters: map[string]interface{}{},
}
}
func (query *Query) updateTraversals(traversal *Traversal) *Query {
query.Traversals = append(query.Traversals, traversal)
query.TraversalCount = len(query.Traversals)
cl := len(query.Cursors)
cursor := &Cursor{
Stage: len(query.Cursors),
Traversal: traversal,
}
if cl > 0 {
cursor.Parent = query.Cursors[cl - 1]
}
query.Cursors = append(query.Cursors, cursor)
return query
}
func (query *Query) Traverse(predicate *graph.Predicate, phase bool) *Query {
return query.updateTraversals(newTraversal(predicate, phase))
}
func (query *Query) In(predicate *graph.Predicate) *Query {
return query.updateTraversals(newTraversal(predicate, true))
}
func (query *Query) Out(predicate *graph.Predicate) *Query {
return query.updateTraversals(newTraversal(predicate, false))
}