Skip to content

Commit 188c7f2

Browse files
committed
WIP on creating underlying structure
1 parent b8f4f2e commit 188c7f2

File tree

3 files changed

+106
-110
lines changed

3 files changed

+106
-110
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Additional exports
2+
export copySession!
3+
# Please be careful with these
4+
# With great power comes great "Oh crap, I deleted everything..."
5+
export clearSession!!, clearRobot!!, clearUser!!
6+
7+
"""
8+
$(SIGNATURES)
9+
DANGER: Clears the whole session from the database.
10+
"""
11+
function clearSession!!(dfg::CloudGraphsDFG)::Nothing
12+
# Perform detach+deletion
13+
_getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId)) detach delete node ")
14+
15+
# Clearing history
16+
dfg.addHistory = Symbol[]
17+
empty!(dfg.variableCache)
18+
empty!(dfg.factorCache)
19+
empty!(dfg.labelDict)
20+
return nothing
21+
end
22+
23+
"""
24+
$(SIGNATURES)
25+
DANGER: Clears the whole robot + sessions from the database.
26+
"""
27+
function clearRobot!!(dfg::CloudGraphsDFG)::Nothing
28+
# Perform detach+deletion
29+
_getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:$(dfg.userId):$(dfg.robotId)) detach delete node ")
30+
31+
# Clearing history
32+
dfg.addHistory = Symbol[]
33+
empty!(dfg.variableCache)
34+
empty!(dfg.factorCache)
35+
empty!(dfg.labelDict)
36+
return nothing
37+
end
38+
39+
"""
40+
$(SIGNATURES)
41+
DANGER: Clears the whole user + robot + sessions from the database.
42+
"""
43+
function clearUser!!(dfg::CloudGraphsDFG)::Nothing
44+
# Perform detach+deletion
45+
_getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:$(dfg.userId)) detach delete node ")
46+
47+
# Clearing history
48+
dfg.addHistory = Symbol[]
49+
empty!(dfg.variableCache)
50+
empty!(dfg.factorCache)
51+
empty!(dfg.labelDict)
52+
return nothing
53+
end
54+
55+
"""
56+
$(SIGNATURES)
57+
DANGER: Copies and overwrites the destination session.
58+
If no destination specified then it creates a unique one.
59+
"""
60+
function copySession!(sourceDFG::CloudGraphsDFG, destDFG::Union{Nothing, CloudGraphsDFG})::CloudGraphsDFG
61+
if destDFG == nothing
62+
destDFG = _getDuplicatedEmptyDFG(sourceDFG)
63+
end
64+
_copyIntoGraph!(sourceDFG, destDFG, union(getVariableIds(sourceDFG), getFactorIds(sourceDFG)), true)
65+
return destDFG
66+
end
67+
"""
68+
$(SIGNATURES)
69+
DANGER: Copies the source to a new unique destination.
70+
"""
71+
copySession!(sourceDFG::CloudGraphsDFG)::CloudGraphsDFG = copySession!(sourceDFG, nothing)
72+
73+
74+
getUserData(dfg::CloudGraphsDFG)::Dict{Symbol, String} = _getNodeProperty(dfg.neo4jInstance, [dfg.userId, "USER"])
75+
function setUserData(dfg::CloudGraphsDFG, data::Dict{Symbol, String})::Bool
76+
error("Not implemented yet")
77+
return true
78+
end
79+
getRobotData(dfg::CloudGraphsDFG)::Dict{Symbol, String} = _getNodeProperty(dfg.neo4jInstance, [dfg.userId, dfg.robotId, "ROBOT"])
80+
function setRobotData(dfg::CloudGraphsDFG, data::Dict{Symbol, String})::Bool
81+
error("Not implemented yet")
82+
return true
83+
end
84+
getSessionData(dfg::CloudGraphsDFG)::Dict{Symbol, String} = _getNodeProperty(dfg.neo4jInstance, [dfg.userId, dfg.robotId, dfg.sessionId, "SESSION"])
85+
function setSessionData(dfg::CloudGraphsDFG, data::Dict{Symbol, String})::Bool
86+
error("Not implemented yet")
87+
return true
88+
end

src/CloudGraphsDFG/services/CloudGraphsDFG.jl

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
# Additional exports
2-
export copySession!
3-
# Please be careful with these
4-
# With great power comes great "Oh crap, I deleted everything..."
5-
export clearSession!!, clearRobot!!, clearUser!!
6-
7-
## End
81

92
"""
103
$(SIGNATURES)
@@ -74,72 +67,6 @@ function exists(dfg::CloudGraphsDFG, node::N) where N <: DFGNode
7467
return exists(dfg, node.label)
7568
end
7669

77-
"""
78-
$(SIGNATURES)
79-
DANGER: Clears the whole session from the database.
80-
"""
81-
function clearSession!!(dfg::CloudGraphsDFG)::Nothing
82-
# Perform detach+deletion
83-
_getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:$(dfg.userId):$(dfg.robotId):$(dfg.sessionId)) detach delete node ")
84-
85-
# Clearing history
86-
dfg.addHistory = Symbol[]
87-
empty!(dfg.variableCache)
88-
empty!(dfg.factorCache)
89-
empty!(dfg.labelDict)
90-
return nothing
91-
end
92-
93-
"""
94-
$(SIGNATURES)
95-
DANGER: Clears the whole robot + sessions from the database.
96-
"""
97-
function clearRobot!!(dfg::CloudGraphsDFG)::Nothing
98-
# Perform detach+deletion
99-
_getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:$(dfg.userId):$(dfg.robotId)) detach delete node ")
100-
101-
# Clearing history
102-
dfg.addHistory = Symbol[]
103-
empty!(dfg.variableCache)
104-
empty!(dfg.factorCache)
105-
empty!(dfg.labelDict)
106-
return nothing
107-
end
108-
109-
"""
110-
$(SIGNATURES)
111-
DANGER: Clears the whole user + robot + sessions from the database.
112-
"""
113-
function clearUser!!(dfg::CloudGraphsDFG)::Nothing
114-
# Perform detach+deletion
115-
_getNeoNodesFromCyphonQuery(dfg.neo4jInstance, "(node:$(dfg.userId)) detach delete node ")
116-
117-
# Clearing history
118-
dfg.addHistory = Symbol[]
119-
empty!(dfg.variableCache)
120-
empty!(dfg.factorCache)
121-
empty!(dfg.labelDict)
122-
return nothing
123-
end
124-
125-
"""
126-
$(SIGNATURES)
127-
DANGER: Copies and overwrites the destination session.
128-
If no destination specified then it creates a unique one.
129-
"""
130-
function copySession!(sourceDFG::CloudGraphsDFG, destDFG::Union{Nothing, CloudGraphsDFG})::CloudGraphsDFG
131-
if destDFG == nothing
132-
destDFG = _getDuplicatedEmptyDFG(sourceDFG)
133-
end
134-
_copyIntoGraph!(sourceDFG, destDFG, union(getVariableIds(sourceDFG), getFactorIds(sourceDFG)), true)
135-
return destDFG
136-
end
137-
"""
138-
$(SIGNATURES)
139-
DANGER: Copies the source to a new unique destination.
140-
"""
141-
copySession!(sourceDFG::CloudGraphsDFG)::CloudGraphsDFG = copySession!(sourceDFG, nothing)
142-
14370
"""
14471
$SIGNATURES
14572
@@ -156,22 +83,6 @@ Return whether `sym::Symbol` represents a factor vertex in the graph.
15683
isFactor(dfg::CloudGraphsDFG, sym::Symbol)::Bool =
15784
"FACTOR" in _getNodeTags(dfg.neo4jInstance, [dfg.userId, dfg.robotId, dfg.sessionId, String(sym)])
15885

159-
getUserData(dfg::CloudGraphsDFG)::Dict{Symbol, String} = _getNodeProperty(dfg.neo4jInstance, [dfg.userId, "USER"])
160-
function setUserData(dfg::CloudGraphsDFG, data::Dict{Symbol, String})::Bool
161-
error("Not implemented yet")
162-
return true
163-
end
164-
getRobotData(dfg::CloudGraphsDFG)::Dict{Symbol, String} = _getNodeProperty(dfg.neo4jInstance, [dfg.userId, dfg.robotId, "ROBOT"])
165-
function setRobotData(dfg::CloudGraphsDFG, data::Dict{Symbol, String})::Bool
166-
error("Not implemented yet")
167-
return true
168-
end
169-
getSessionData(dfg::CloudGraphsDFG)::Dict{Symbol, String} = _getNodeProperty(dfg.neo4jInstance, [dfg.userId, dfg.robotId, dfg.sessionId, "SESSION"])
170-
function setSessionData(dfg::CloudGraphsDFG, data::Dict{Symbol, String})::Bool
171-
error("Not implemented yet")
172-
return true
173-
end
174-
17586
"""
17687
$(SIGNATURES)
17788
Add a DFGVariable to a DFG.

src/CloudGraphsDFG/services/CommonFunctions.jl

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ function _getNodeTags(neo4jInstance::Neo4jInstance, nodeLabels::Vector{String}):
5555
return result.results[1]["data"][1]["row"][1]
5656
end
5757

58+
function _getNodeCount(neo4jInstance::Neo4jInstance, nodeLabels::Vector{String})::Int
59+
query = "match (n:$(join(nodeLabels, ":"))) return count(n)"
60+
result = DistributedFactorGraphs._queryNeo4j(neo4jInstance, query)
61+
length(result.results[1]["data"]) != 1 && return 0
62+
return parse(Int, result.results[1]["data"][1]["row"][1])
63+
end
5864
"""
5965
$(SIGNATURES)
6066
Returns the list of CloudGraph nodes that matches the Cyphon query.
@@ -93,25 +99,13 @@ function _getRobotNeoNodeForUser(neo4jInstance::Neo4jInstance, userId::String, r
9399
return nodes[1]
94100
end
95101

96-
"""
97-
$(SIGNATURES)
98-
Utility function to get all robots for a user.
99-
"""
100-
function _getRobotNeoNodesForUser(userId::String)::Vector{Neo4j.Node}
101-
# 2. Perform the transaction
102-
cloudGraph = Main.App.NaviConfig.cgConnection
103-
nodes = _getNeoNodesFromCyphonQuery("(u:USER:$userId)-[:ROBOT]->(node:ROBOT)", "id")
104-
return nodes
105-
end
106-
107102
"""
108103
$(SIGNATURES)
109104
Utility function to get a user root node given an ID.
110105
"""
111-
function _getUserNeoNode(userId::String)::Neo4j.Node
106+
function _getUserNeoNode(neo4jInstance::Neo4jInstance, userId::String)::Neo4j.Node
112107
# 2. Perform the transaction
113-
cloudGraph = Main.App.NaviConfig.cgConnection
114-
nodes = _getNeoNodesFromCyphonQuery("(node:USER:$userId)")
108+
nodes = _getNeoNodesFromCyphonQuery(neo4jInstance, "(node:USER:$userId)")
115109
if(length(nodes) != 1)
116110
error("Expected one user node with labels $userId and USER, received $(length(nodes)).")
117111
end
@@ -123,10 +117,9 @@ end
123117
$(SIGNATURES)
124118
Utility function to get a Neo4j node sessions for a robot.
125119
"""
126-
function _getSessionNeoNodesForRobot(userId::String, robotId::String)::Vector{Neo4j.Node}
120+
function _getSessionNeoNodesForRobot(neo4jInstance::Neo4jInstance, userId::String, robotId::String)::Vector{Neo4j.Node}
127121
# 2. Perform the transaction
128-
cloudGraph = Main.App.NaviConfig.cgConnection
129-
nodes = _getNeoNodesFromCyphonQuery("(u:USER:$userId)-[:ROBOT]->(r:ROBOT:$robotId)-[:SESSION]->(node:SESSION)", "id")
122+
nodes = _getNeoNodesFromCyphonQuery(neo4jInstance, "(u:USER:$userId)-[:ROBOT]->(r:ROBOT:$robotId)-[:SESSION]->(node:SESSION)", "id")
130123
return nodes
131124
end
132125

@@ -135,11 +128,15 @@ $(SIGNATURES)
135128
Bind the SESSION node to the inital variable.
136129
Doesn't check existence so please don't call twice.
137130
"""
138-
function _bindSessionNodeToInitialVariable(userId::String, robotId::String, sessionId::String, initialVariableLabel::String)::Nothing
131+
function _bindSessionNodeToInitialVariable(neo4jInstance::Neo4jInstance, userId::String, robotId::String, sessionId::String, initialVariableLabel::String)::Nothing
139132
# 2. Perform the transaction
140-
cloudGraph = Main.App.NaviConfig.cgConnection
141-
loadtx = transaction(cloudGraph.neo4j.connection)
142-
query = "match (session:SESSION:$userId:$robotId:$sessionId),(var:VARIABLE:$userId:$robotId:$sessionId {label: '$initialVariableLabel'}) CREATE (session)-[:VARIABLE]->(var) return id(var)";
133+
loadtx = transaction(neo4jInstance.connection)
134+
query = """
135+
match (session:SESSION:$userId:$robotId:$sessionId),
136+
(var:VARIABLE:$userId:$robotId:$sessionId
137+
{label: '$initialVariableLabel'})
138+
CREATE (session)-[:VARIABLE]->(var) return id(var)
139+
""";
143140
loadtx(query; submit=true)
144141
commit(loadtx)
145142
return nothing

0 commit comments

Comments
 (0)