5
5
6
6
# Fastest way I can think to convert the data into a dict
7
7
# TODO : Probably should be made more efficient...definitely should be made more efficient
8
- function _convertNodeToDict (abstractNode:: N ):: Dict{String, Any} where N <: AbstractCGNode
8
+ function _convertNodeToDict (abstractNode:: N ) where N <: AbstractCGNode
9
9
cp = deepcopy (abstractNode)
10
10
data = length (cp. data) != 0 ? JSON2. write (cp. data) : " {}"
11
11
ser = JSON2. read (JSON2. write (abstractNode), Dict{String, Any})
@@ -14,8 +14,7 @@ function _convertNodeToDict(abstractNode::N)::Dict{String, Any} where N <: Abstr
14
14
return ser
15
15
end
16
16
17
- # TODO : Refactor, #HACK :D (but it works!)
18
- function _convertDictToSession (dict:: Dict{String, Any} ):: Session
17
+ function _convertDictToSession (dict:: Dict{String, Any} )
19
18
data = JSON2. read (String (base64decode (dict[" data" ])), Dict{Symbol, String})
20
19
session = Session (
21
20
Symbol (dict[" id" ]),
@@ -28,8 +27,7 @@ function _convertDictToSession(dict::Dict{String, Any})::Session
28
27
dict[" lastUpdatedTimestamp" ])
29
28
return session
30
29
end
31
- # TODO : Refactor, #HACK :D (but it works!)
32
- function _convertDictToRobot (dict:: Dict{String, Any} ):: Robot
30
+ function _convertDictToRobot (dict:: Dict{String, Any} )
33
31
data = JSON2. read (String (base64decode (dict[" data" ])), Dict{Symbol, String})
34
32
robot = Robot (
35
33
Symbol (dict[" id" ]),
@@ -41,8 +39,7 @@ function _convertDictToRobot(dict::Dict{String, Any})::Robot
41
39
dict[" lastUpdatedTimestamp" ])
42
40
return robot
43
41
end
44
- # TODO : Refactor, #HACK :D (but it works!)
45
- function _convertDictToUser (dict:: Dict{String, Any} ):: User
42
+ function _convertDictToUser (dict:: Dict{String, Any} )
46
43
data = JSON2. read (String (base64decode (dict[" data" ])), Dict{Symbol, String})
47
44
user = User (
48
45
Symbol (dict[" id" ]),
@@ -54,83 +51,41 @@ function _convertDictToUser(dict::Dict{String, Any})::User
54
51
return user
55
52
end
56
53
57
- function createUser (dfg:: CloudGraphsDFG , user:: User ):: User
58
- Symbol (dfg. userId) != user. id && error (" DFG user ID must match user's ID" )
59
- ! isValidLabel (user) && error (" Node cannot have an ID '$(user. id) '." )
60
-
61
- props = _convertNodeToDict (user)
62
- # TODO : Switch to _queryNeo4j
63
- retNode = _createNode (dfg. neo4jInstance, [" USER" , String (user. id)], props, nothing )
64
- return user
65
- end
66
-
67
- function createRobot (dfg:: CloudGraphsDFG , robot:: Robot ):: Robot
68
- Symbol (dfg. robotId) != robot. id && error (" DFG robot ID must match robot's ID" )
69
- Symbol (dfg. userId) != robot. userId && error (" DFG user ID must match robot's user ID" )
70
- ! isValidLabel (robot) && error (" Node cannot have an ID '$(robot. id) '." )
71
-
72
- # Find the parent
73
- parents = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:USER:$(dfg. userId) )" )
74
- length (parents) == 0 && error (" Cannot find user '$(dfg. userId) '" )
75
- length (parents) > 1 && error (" Found multiple users '$(dfg. userId) '" )
76
-
77
- # Already exists?
78
- length (_getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:ROBOT:$(dfg. userId) :$(robot. id) )" )) != 0 &&
79
- error (" Robot '$(robot. id) ' already exists for user '$(robot. userId) '" )
80
-
81
- props = _convertNodeToDict (robot)
82
- # TODO : Switch to _queryNeo4j
83
- retNode = _createNode (dfg. neo4jInstance, [" ROBOT" , String (robot. userId), String (robot. id)], props, parents[1 ], :ROBOT )
84
- return robot
85
- end
86
-
87
- function createSession (dfg:: CloudGraphsDFG , session:: Session ):: Session
88
- Symbol (dfg. robotId) != session. robotId && error (" DFG robot ID must match session's robot ID" )
89
- Symbol (dfg. userId) != session. userId && error (" DFG user ID must match session's->robot's->user ID" )
90
- ! isValidLabel (session) && error (" Node cannot have an ID '$(session. id) '." )
91
-
92
- # Find the parent
93
- parents = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:ROBOT:$(dfg. robotId) :$(dfg. userId) )" )
94
- length (parents) == 0 && error (" Cannot find robot '$(dfg. robotId) ' for user '$(dfg. userId) '" )
95
- length (parents) > 1 && error (" Found multiple robots '$(dfg. robotId) ' for user '$(dfg. userId) '" )
96
-
97
- # Already exists?
98
- length (_getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:SESSION:$(session. userId) :$(session. robotId) :$(session. id) )" )) != 0 &&
99
- error (" Session '$(session. id) ' already exists for robot '$(session. robotId) ' and user '$(session. userId) '" )
100
-
101
- props = _convertNodeToDict (session)
102
- # TODO : Switch to _queryNeo4j
103
- retNode = _createNode (dfg. neo4jInstance, [" SESSION" , String (session. userId), String (session. robotId), String (session. id)], props, parents[1 ], :SESSION )
104
- return session
105
- end
106
-
107
54
"""
108
55
$(SIGNATURES)
109
- Shortcut method to create the user, robot, and session if it doesn't already exist.
56
+ Efficient shortcut method to create the user, robot, and session if it doesn't already exist.
110
57
"""
111
- function createDfgSessionIfNotExist (dfg:: CloudGraphsDFG ):: Session
58
+ function createDfgSessionIfNotExist (dfg:: CloudGraphsDFG )
112
59
strip (dfg. userId) == " " && error (" User ID is not populated in DFG." )
113
60
strip (dfg. robotId) == " " && error (" Robot ID is not populated in DFG." )
114
61
strip (dfg. sessionId) == " " && error (" Session ID is not populated in DFG." )
62
+ ! isValidLabel (dfg. userId) && error (" Node cannot have an ID '$(dfg. userId) '." )
63
+ ! isValidLabel (dfg. robotId) && error (" Node cannot have an ID '$(dfg. robotId) '." )
64
+ ! isValidLabel (dfg. sessionId) && error (" Node cannot have an ID '$(dfg. sessionId) '." )
65
+
115
66
user = User (Symbol (dfg. userId), dfg. userId, " Description for $(dfg. userId) " , Dict {Symbol, String} ())
116
67
robot = Robot (Symbol (dfg. robotId), Symbol (dfg. userId), dfg. robotId, " Description for $(dfg. userId) :$(dfg. robotId) " , Dict {Symbol, String} ())
117
68
session = Session (Symbol (dfg. sessionId), Symbol (dfg. robotId), Symbol (dfg. userId), dfg. sessionId, dfg. description, Dict {Symbol, String} ())
118
69
119
- _getNodeCount (dfg. neo4jInstance, [dfg. userId, " USER" ]) == 0 && createUser (dfg, user)
120
- _getNodeCount (dfg. neo4jInstance, [dfg. userId, dfg. robotId, " ROBOT" ]) == 0 && createRobot (dfg, robot)
121
- if _getNodeCount (dfg. neo4jInstance, [dfg. userId, dfg. robotId, dfg. sessionId, " SESSION" ]) == 0
122
- return createSession (dfg, session)
123
- else
124
- return getSession (dfg)
125
- end
70
+ # NOTE that this doesn't get updated then, you need to use the set* (e.g. setSessionData) functions yourself.
71
+ query = """
72
+ MERGE (u:USER:$(String (user. id)) ) ON CREATE SET $(join ([" u.$k = '$(v) '" for (k,v) in _convertNodeToDict (user)], " , " )) \r\n
73
+ MERGE (r:ROBOT:$(String (user. id)) :$(String (robot. id)) ) ON CREATE SET $(join ([" r.$k = '$(v) '" for (k,v) in _convertNodeToDict (robot)], " , " )) \r\n
74
+ MERGE (s:SESSION:$(String (user. id)) :$(String (robot. id)) :$(String (session. id)) ) ON CREATE SET $(join ([" s.$k = '$(v) '" for (k,v) in _convertNodeToDict (session)], " , " ))
75
+ MERGE (u)-[:ROBOT]->(r)
76
+ MERGE (r)-[:SESSION]->(s)
77
+ """
78
+
79
+ _queryNeo4j (dfg. neo4jInstance, query)
80
+ return nothing
126
81
end
127
82
128
83
"""
129
84
$(SIGNATURES)
130
85
List all sessions for the specified DFG's robot and user.
131
86
Returns nothing if it isn't found.
132
87
"""
133
- function lsSessions (dfg:: CloudGraphsDFG ):: Vector{Session}
88
+ function lsSessions (dfg:: CloudGraphsDFG )
134
89
sessionNodes = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:SESSION:$(dfg. robotId) :$(dfg. userId) )" )
135
90
return map (s -> _convertDictToSession (Neo4j. getnodeproperties (s)), sessionNodes)
136
91
end
@@ -140,7 +95,7 @@ $(SIGNATURES)
140
95
List all robots for the specified DFG's user.
141
96
Returns nothing if it isn't found.
142
97
"""
143
- function lsRobots (dfg:: CloudGraphsDFG ):: Vector{Robot}
98
+ function lsRobots (dfg:: CloudGraphsDFG )
144
99
robotNodes = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:ROBOT:$(dfg. userId) )" )
145
100
return map (s -> _convertDictToRobot (Neo4j. getnodeproperties (s)), robotNodes)
146
101
end
@@ -150,7 +105,7 @@ $(SIGNATURES)
150
105
List all users.
151
106
Returns nothing if it isn't found.
152
107
"""
153
- function lsUsers (dfg:: CloudGraphsDFG ):: Vector{User}
108
+ function lsUsers (dfg:: CloudGraphsDFG )
154
109
userNodes = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:USER)" )
155
110
return map (s -> _convertDictToUser (Neo4j. getnodeproperties (s)), userNodes)
156
111
end
@@ -160,7 +115,7 @@ $(SIGNATURES)
160
115
Get a session specified by userId:robotId:sessionId.
161
116
Returns nothing if it isn't found.
162
117
"""
163
- function getSession (dfg:: CloudGraphsDFG , userId:: Symbol , robotId:: Symbol , sessionId:: Symbol ):: Union{Session, Nothing}
118
+ function getSession (dfg:: CloudGraphsDFG , userId:: Symbol , robotId:: Symbol , sessionId:: Symbol )
164
119
! isValidLabel (userId) && error (" Can't retrieve session with user ID '$(userId) '." )
165
120
! isValidLabel (robotId) && error (" Can't retrieve session with robot ID '$(robotId) '." )
166
121
! isValidLabel (sessionId) && error (" Can't retrieve session with session ID '$(sessionId) '." )
@@ -175,7 +130,7 @@ $(SIGNATURES)
175
130
Get the session specified by the DFG object.
176
131
Returns nothing if it isn't found.
177
132
"""
178
- function getSession (dfg:: CloudGraphsDFG ):: Union{Nothing, Session}
133
+ function getSession (dfg:: CloudGraphsDFG )
179
134
return getSession (dfg, Symbol (dfg. userId), Symbol (dfg. robotId), Symbol (dfg. sessionId))
180
135
end
181
136
@@ -184,7 +139,7 @@ $(SIGNATURES)
184
139
Get a robot specified by userId:robotId.
185
140
Returns nothing if it isn't found.
186
141
"""
187
- function getRobot (dfg:: CloudGraphsDFG , userId:: Symbol , robotId:: Symbol ):: Union{Robot, Nothing}
142
+ function getRobot (dfg:: CloudGraphsDFG , userId:: Symbol , robotId:: Symbol )
188
143
! isValidLabel (userId) && error (" Can't retrieve robot with user ID '$(userId) '." )
189
144
! isValidLabel (robotId) && error (" Can't retrieve robot with robot ID '$(robotId) '." )
190
145
robotNode = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:ROBOT:$(robotId) :$(userId) )" )
@@ -198,7 +153,7 @@ $(SIGNATURES)
198
153
Get the robot specified by the DFG object.
199
154
Returns nothing if it isn't found.
200
155
"""
201
- function getRobot (dfg:: CloudGraphsDFG ):: Union{Nothing, Robot}
156
+ function getRobot (dfg:: CloudGraphsDFG )
202
157
return getRobot (dfg, Symbol (dfg. userId), Symbol (dfg. robotId))
203
158
end
204
159
@@ -207,7 +162,7 @@ $(SIGNATURES)
207
162
Get a user specified by userId.
208
163
Returns nothing if it isn't found.
209
164
"""
210
- function getUser (dfg:: CloudGraphsDFG , userId:: Symbol ):: Union{User, Nothing}
165
+ function getUser (dfg:: CloudGraphsDFG , userId:: Symbol )
211
166
! isValidLabel (userId) && error (" Can't retrieve user with user ID '$(userId) '." )
212
167
userNode = _getNeoNodesFromCyphonQuery (dfg. neo4jInstance, " (node:USER:$(userId) )" )
213
168
length (userNode) == 0 && return nothing
@@ -220,7 +175,7 @@ $(SIGNATURES)
220
175
Get the user specified by the DFG object.
221
176
Returns nothing if it isn't found.
222
177
"""
223
- function getUser (dfg:: CloudGraphsDFG ):: Union{Nothing, User}
178
+ function getUser (dfg:: CloudGraphsDFG )
224
179
return getUser (dfg, Symbol (dfg. userId))
225
180
end
226
181
229
184
$(SIGNATURES)
230
185
DANGER: Clears the whole session from the database.
231
186
"""
232
- function clearSession!! (dfg:: CloudGraphsDFG ):: Nothing
187
+ function clearSession!! (dfg:: CloudGraphsDFG )
233
188
# Perform detach+deletion
234
189
_queryNeo4j (dfg. neo4jInstance, " match (node:$(dfg. userId) :$(dfg. robotId) :$(dfg. sessionId) ) detach delete node " )
235
190
242
197
$(SIGNATURES)
243
198
DANGER: Clears the whole robot + sessions from the database.
244
199
"""
245
- function clearRobot!! (dfg:: CloudGraphsDFG ):: Nothing
200
+ function clearRobot!! (dfg:: CloudGraphsDFG )
246
201
# Perform detach+deletion
247
202
_queryNeo4j (dfg. neo4jInstance, " match (node:$(dfg. userId) :$(dfg. robotId) ) detach delete node " )
248
203
255
210
$(SIGNATURES)
256
211
DANGER: Clears the whole user + robot + sessions from the database.
257
212
"""
258
- function clearUser!! (dfg:: CloudGraphsDFG ):: Nothing
213
+ function clearUser!! (dfg:: CloudGraphsDFG )
259
214
# Perform detach+deletion
260
215
_queryNeo4j (dfg. neo4jInstance, " match (node:$(dfg. userId) ) detach delete node " )
261
216
269
224
DANGER: Copies and overwrites the destination session.
270
225
If no destination specified then it creates a unique one.
271
226
"""
272
- function copySession! (sourceDFG:: CloudGraphsDFG , destDFG:: Union{Nothing, CloudGraphsDFG} ):: CloudGraphsDFG
273
- if destDFG == nothing
227
+ function copySession! (sourceDFG:: CloudGraphsDFG , destDFG:: Union{Nothing, CloudGraphsDFG} )
228
+ if destDFG === nothing
274
229
destDFG = _getDuplicatedEmptyDFG (sourceDFG)
275
230
end
276
231
_copyIntoGraph! (sourceDFG, destDFG, union (listVariables (sourceDFG), listFactors (sourceDFG)), true )
280
235
$(SIGNATURES)
281
236
DANGER: Copies the source to a new unique destination.
282
237
"""
283
- copySession! (sourceDFG:: CloudGraphsDFG ):: CloudGraphsDFG = copySession! (sourceDFG, nothing )
238
+ copySession! (sourceDFG:: CloudGraphsDFG ) = copySession! (sourceDFG, nothing )
0 commit comments