Skip to content

Commit 04d7320

Browse files
committed
Optimizing the creation of a CGDFG.
1 parent 229913c commit 04d7320

File tree

4 files changed

+39
-100
lines changed

4 files changed

+39
-100
lines changed

src/CloudGraphsDFG/entities/CloudGraphsDFG.jl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,10 @@ mutable struct CloudGraphsDFG{T <: AbstractParams} <: AbstractDFG{T}
3333
!isValidLabel(robotId) && error("'$robotId' is not a valid Robot ID")
3434
!isValidLabel(sessionId) && error("'$sessionId' is not a valid Session ID")
3535

36-
# neo4jConnection = Neo4j.Connection(host, port=port, user=dbUser, password=dbPassword);
37-
# graph = Neo4j.getgraph(neo4jConnection)
38-
# neo4jInstance = Neo4jInstance(neo4jConnection, graph)
39-
4036
dfg = new{T}(neo4jInstance, userId, robotId, sessionId, description, addHistory, solverParams, blobStores)
4137
# Create the session if it doesn't already exist
4238
if createSessionNodes
4339
createDfgSessionIfNotExist(dfg)
44-
setUserData!(dfg, userData)
45-
setRobotData!(dfg, robotData)
46-
setSessionData!(dfg, sessionData)
47-
setDescription!(dfg, description)
4840
end
4941

5042
return dfg

src/CloudGraphsDFG/services/CGStructure.jl

Lines changed: 36 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ end
55

66
# Fastest way I can think to convert the data into a dict
77
#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
99
cp = deepcopy(abstractNode)
1010
data = length(cp.data) != 0 ? JSON2.write(cp.data) : "{}"
1111
ser = JSON2.read(JSON2.write(abstractNode), Dict{String, Any})
@@ -14,8 +14,7 @@ function _convertNodeToDict(abstractNode::N)::Dict{String, Any} where N <: Abstr
1414
return ser
1515
end
1616

17-
#TODO: Refactor, #HACK :D (but it works!)
18-
function _convertDictToSession(dict::Dict{String, Any})::Session
17+
function _convertDictToSession(dict::Dict{String, Any})
1918
data = JSON2.read(String(base64decode(dict["data"])), Dict{Symbol, String})
2019
session = Session(
2120
Symbol(dict["id"]),
@@ -28,8 +27,7 @@ function _convertDictToSession(dict::Dict{String, Any})::Session
2827
dict["lastUpdatedTimestamp"])
2928
return session
3029
end
31-
#TODO: Refactor, #HACK :D (but it works!)
32-
function _convertDictToRobot(dict::Dict{String, Any})::Robot
30+
function _convertDictToRobot(dict::Dict{String, Any})
3331
data = JSON2.read(String(base64decode(dict["data"])), Dict{Symbol, String})
3432
robot = Robot(
3533
Symbol(dict["id"]),
@@ -41,8 +39,7 @@ function _convertDictToRobot(dict::Dict{String, Any})::Robot
4139
dict["lastUpdatedTimestamp"])
4240
return robot
4341
end
44-
#TODO: Refactor, #HACK :D (but it works!)
45-
function _convertDictToUser(dict::Dict{String, Any})::User
42+
function _convertDictToUser(dict::Dict{String, Any})
4643
data = JSON2.read(String(base64decode(dict["data"])), Dict{Symbol, String})
4744
user = User(
4845
Symbol(dict["id"]),
@@ -54,83 +51,41 @@ function _convertDictToUser(dict::Dict{String, Any})::User
5451
return user
5552
end
5653

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-
10754
"""
10855
$(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.
11057
"""
111-
function createDfgSessionIfNotExist(dfg::CloudGraphsDFG)::Session
58+
function createDfgSessionIfNotExist(dfg::CloudGraphsDFG)
11259
strip(dfg.userId) == "" && error("User ID is not populated in DFG.")
11360
strip(dfg.robotId) == "" && error("Robot ID is not populated in DFG.")
11461
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+
11566
user = User(Symbol(dfg.userId), dfg.userId, "Description for $(dfg.userId)", Dict{Symbol, String}())
11667
robot = Robot(Symbol(dfg.robotId), Symbol(dfg.userId), dfg.robotId, "Description for $(dfg.userId):$(dfg.robotId)", Dict{Symbol, String}())
11768
session = Session(Symbol(dfg.sessionId), Symbol(dfg.robotId), Symbol(dfg.userId), dfg.sessionId, dfg.description, Dict{Symbol, String}())
11869

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
12681
end
12782

12883
"""
12984
$(SIGNATURES)
13085
List all sessions for the specified DFG's robot and user.
13186
Returns nothing if it isn't found.
13287
"""
133-
function lsSessions(dfg::CloudGraphsDFG)::Vector{Session}
88+
function lsSessions(dfg::CloudGraphsDFG)
13489
sessionNodes = _getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:SESSION:$(dfg.robotId):$(dfg.userId))")
13590
return map(s -> _convertDictToSession(Neo4j.getnodeproperties(s)), sessionNodes)
13691
end
@@ -140,7 +95,7 @@ $(SIGNATURES)
14095
List all robots for the specified DFG's user.
14196
Returns nothing if it isn't found.
14297
"""
143-
function lsRobots(dfg::CloudGraphsDFG)::Vector{Robot}
98+
function lsRobots(dfg::CloudGraphsDFG)
14499
robotNodes = _getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:ROBOT:$(dfg.userId))")
145100
return map(s -> _convertDictToRobot(Neo4j.getnodeproperties(s)), robotNodes)
146101
end
@@ -150,7 +105,7 @@ $(SIGNATURES)
150105
List all users.
151106
Returns nothing if it isn't found.
152107
"""
153-
function lsUsers(dfg::CloudGraphsDFG)::Vector{User}
108+
function lsUsers(dfg::CloudGraphsDFG)
154109
userNodes = _getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:USER)")
155110
return map(s -> _convertDictToUser(Neo4j.getnodeproperties(s)), userNodes)
156111
end
@@ -160,7 +115,7 @@ $(SIGNATURES)
160115
Get a session specified by userId:robotId:sessionId.
161116
Returns nothing if it isn't found.
162117
"""
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)
164119
!isValidLabel(userId) && error("Can't retrieve session with user ID '$(userId)'.")
165120
!isValidLabel(robotId) && error("Can't retrieve session with robot ID '$(robotId)'.")
166121
!isValidLabel(sessionId) && error("Can't retrieve session with session ID '$(sessionId)'.")
@@ -175,7 +130,7 @@ $(SIGNATURES)
175130
Get the session specified by the DFG object.
176131
Returns nothing if it isn't found.
177132
"""
178-
function getSession(dfg::CloudGraphsDFG)::Union{Nothing, Session}
133+
function getSession(dfg::CloudGraphsDFG)
179134
return getSession(dfg, Symbol(dfg.userId), Symbol(dfg.robotId), Symbol(dfg.sessionId))
180135
end
181136

@@ -184,7 +139,7 @@ $(SIGNATURES)
184139
Get a robot specified by userId:robotId.
185140
Returns nothing if it isn't found.
186141
"""
187-
function getRobot(dfg::CloudGraphsDFG, userId::Symbol, robotId::Symbol)::Union{Robot, Nothing}
142+
function getRobot(dfg::CloudGraphsDFG, userId::Symbol, robotId::Symbol)
188143
!isValidLabel(userId) && error("Can't retrieve robot with user ID '$(userId)'.")
189144
!isValidLabel(robotId) && error("Can't retrieve robot with robot ID '$(robotId)'.")
190145
robotNode = _getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:ROBOT:$(robotId):$(userId))")
@@ -198,7 +153,7 @@ $(SIGNATURES)
198153
Get the robot specified by the DFG object.
199154
Returns nothing if it isn't found.
200155
"""
201-
function getRobot(dfg::CloudGraphsDFG)::Union{Nothing, Robot}
156+
function getRobot(dfg::CloudGraphsDFG)
202157
return getRobot(dfg, Symbol(dfg.userId), Symbol(dfg.robotId))
203158
end
204159

@@ -207,7 +162,7 @@ $(SIGNATURES)
207162
Get a user specified by userId.
208163
Returns nothing if it isn't found.
209164
"""
210-
function getUser(dfg::CloudGraphsDFG, userId::Symbol)::Union{User, Nothing}
165+
function getUser(dfg::CloudGraphsDFG, userId::Symbol)
211166
!isValidLabel(userId) && error("Can't retrieve user with user ID '$(userId)'.")
212167
userNode = _getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:USER:$(userId))")
213168
length(userNode) == 0 && return nothing
@@ -220,7 +175,7 @@ $(SIGNATURES)
220175
Get the user specified by the DFG object.
221176
Returns nothing if it isn't found.
222177
"""
223-
function getUser(dfg::CloudGraphsDFG)::Union{Nothing, User}
178+
function getUser(dfg::CloudGraphsDFG)
224179
return getUser(dfg, Symbol(dfg.userId))
225180
end
226181

@@ -229,7 +184,7 @@ end
229184
$(SIGNATURES)
230185
DANGER: Clears the whole session from the database.
231186
"""
232-
function clearSession!!(dfg::CloudGraphsDFG)::Nothing
187+
function clearSession!!(dfg::CloudGraphsDFG)
233188
# Perform detach+deletion
234189
_queryNeo4j(dfg.neo4jInstance, "match (node:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId)) detach delete node ")
235190

@@ -242,7 +197,7 @@ end
242197
$(SIGNATURES)
243198
DANGER: Clears the whole robot + sessions from the database.
244199
"""
245-
function clearRobot!!(dfg::CloudGraphsDFG)::Nothing
200+
function clearRobot!!(dfg::CloudGraphsDFG)
246201
# Perform detach+deletion
247202
_queryNeo4j(dfg.neo4jInstance, "match (node:$(dfg.userId):$(dfg.robotId)) detach delete node ")
248203

@@ -255,7 +210,7 @@ end
255210
$(SIGNATURES)
256211
DANGER: Clears the whole user + robot + sessions from the database.
257212
"""
258-
function clearUser!!(dfg::CloudGraphsDFG)::Nothing
213+
function clearUser!!(dfg::CloudGraphsDFG)
259214
# Perform detach+deletion
260215
_queryNeo4j(dfg.neo4jInstance, "match (node:$(dfg.userId)) detach delete node ")
261216

@@ -269,8 +224,8 @@ end
269224
DANGER: Copies and overwrites the destination session.
270225
If no destination specified then it creates a unique one.
271226
"""
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
274229
destDFG = _getDuplicatedEmptyDFG(sourceDFG)
275230
end
276231
_copyIntoGraph!(sourceDFG, destDFG, union(listVariables(sourceDFG), listFactors(sourceDFG)), true)
@@ -280,4 +235,4 @@ end
280235
$(SIGNATURES)
281236
DANGER: Copies the source to a new unique destination.
282237
"""
283-
copySession!(sourceDFG::CloudGraphsDFG)::CloudGraphsDFG = copySession!(sourceDFG, nothing)
238+
copySession!(sourceDFG::CloudGraphsDFG) = copySession!(sourceDFG, nothing)

src/CloudGraphsDFG/services/CommonFunctions.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Returns the transaction for a given query.
66
NOTE: Must commit(transaction) after you're done.
77
"""
88
function _queryNeo4j(neo4jInstance::Neo4jInstance, query::String; currentTransaction::Union{Nothing, Neo4j.Transaction}=nothing)
9-
@debug "[Query] $(currentTransaction != nothing ? "[TRANSACTION]" : "") $query"
10-
if currentTransaction == nothing
9+
@debug "[Query] $(currentTransaction !== nothing ? "[TRANSACTION]" : "") $query"
10+
if currentTransaction === nothing
1111
loadtx = transaction(neo4jInstance.connection)
1212
loadtx(query)
1313
# Have to finish the transaction

test/CGStructureTests.jl

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,11 @@ user = User(Symbol(dfg.userId), "Bob Zack", "Description", Dict{Symbol, String}(
2424
robot = Robot(Symbol(dfg.robotId), user.id, "Test robot", "Description", Dict{Symbol, String}())
2525
session = Session(Symbol(dfg.sessionId), robot.id, user.id, "Test Session", "Description", Dict{Symbol, String}())
2626

27-
@test createUser(dfg, user) == user
28-
@test createRobot(dfg, robot) == robot
29-
@test createSession(dfg, session) == session
30-
@test map(s -> s.id, lsSessions(dfg)) == [Symbol(dfg.sessionId)]
31-
@test map(s -> s.id, lsRobots(dfg)) == [Symbol(dfg.robotId)]
32-
@test Symbol(dfg.userId) in map(u -> u.id, lsUsers(dfg))
33-
3427
# Test errors
3528
dfgError = deepcopy(dfg)
3629
# User/robot/session ID's can't start with numbers and can't have spaces.
3730
dfgError.userId = "1testNope"
38-
user = User(Symbol(dfgError.userId), "Bob Zack", "Description", Dict{Symbol, String}())
39-
@test_throws Exception createUser(dfgError, user)
31+
@test_throws Exception createDfgSessionIfNotExist(dfg)
4032

4133
@test getUserData(dfg) == Dict{Symbol, String}()
4234
@test getRobotData(dfg) == Dict{Symbol, String}()

0 commit comments

Comments
 (0)