The graph (or the specified part of it) is stored as a JSON object with the following properties:
"nodes"
: An array with all nodes. Each node is an object with the
following properties:"labels"
: An array of all applied labels."properties"
: An object mapping each defined property to its value."edges"
: An array with all relationships. Each relationship is an object
with the following properties:"type"
: The type (String
) of the relationship."properties"
: An object mapping each defined property to its value."from"
: The local ID of the source node."to"
: The local ID of the destination node.import neo4j::graph::sequential_id
var graph = new NeoGraph(new SequentialNodeCollection("nid"))
var a = new NeoNode
a.labels.add "Foo"
a["answer"] = 42
a["Ultimate question of"] = new JsonArray.from(["life",
"the Universe", "and Everything."])
graph.nodes.register a
var b = graph.create_node
b.labels.add "Foo"
b.labels.add "Bar"
graph.edges.add new NeoEdge(a, "BAZ", b)
var ostream = new StringWriter
var store = new JsonGraphStore(graph)
store.ostream = ostream
store.save
assert ostream.to_s == """{"nodes":[""" + """
{"labels":["Foo"],"properties":{"answer":42,""" + """
"Ultimate question of":["life","the Universe","and Everything."],""" + """
"nid":1}},""" + """
{"labels":["Foo","Bar"],"properties":{"nid":2}}],""" + """
"edges":[{"type":"BAZ","properties":{},"from":1,"to":2}]}"""
graph.nodes.clear
graph.edges.clear
store.istream = new StringReader(ostream.to_s)
store.load
assert 1 == graph.edges.length
for edge in graph.edges do
assert "BAZ" == edge.rel_type
assert a.labels == edge.from.labels
for k, v in a.properties do assert v == edge.from.properties[k]
assert b.labels == edge.to.labels
for k, v in b.properties do assert v == edge.to.properties[k]
end
assert 2 == graph.nodes.length
neo4j :: JsonGraphStore :: defaultinit
neo4j :: JsonGraphStore :: from_string
Use the specified string to load the graph.neo4j :: JsonGraphStore :: istream=
The stream to use forload
.
neo4j :: JsonGraphStore :: ostream
The stream to use forsave
and save_part
.
neo4j :: JsonGraphStore :: ostream=
The stream to use forsave
and save_part
.
neo4j $ JsonGraphStore :: SELF
Type of this instance, automatically specialized in every classneo4j $ JsonGraphStore :: isolated_save
Can we save the graph without conflict?neo4j $ JsonGraphStore :: save_part
Save the specified part of the graph.core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
neo4j :: JsonGraphStore :: defaultinit
core :: Object :: defaultinit
neo4j :: GraphStore :: defaultinit
progression :: Trackable :: defaultinit
progression :: Trackable :: fire_done
Notice the registeredProgessionListener
that the operation is done.
progression :: Trackable :: fire_progressed
Notice the registeredProgessionListener
that the operation progressed.
progression :: Trackable :: fire_started
Notice the registeredProgessionListener
that the operation started.
neo4j :: JsonGraphStore :: from_string
Use the specified string to load the graph.core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
neo4j :: GraphStore :: isolated_save
Can we save the graph without conflict?neo4j :: JsonGraphStore :: istream=
The stream to use forload
.
neo4j :: JsonGraphStore :: ostream
The stream to use forsave
and save_part
.
neo4j :: JsonGraphStore :: ostream=
The stream to use forsave
and save_part
.
core :: Object :: output_class_name
Display class name on stdout (debug only).progression :: Trackable :: progression_listeners
Listen to the progression of the operation.progression :: Trackable :: progression_listeners=
Listen to the progression of the operation.neo4j :: GraphStore :: save_part
Save the specified part of the graph.ProgressionListener
.
# Save or load a graph using a JSON document.
#
# The graph (or the specified part of it) is stored as a JSON object with the
# following properties:
#
# * `"nodes"`: An array with all nodes. Each node is an object with the
# following properties:
# * `"labels"`: An array of all applied labels.
# * `"properties"`: An object mapping each defined property to its value.
# * `"edges"`: An array with all relationships. Each relationship is an object
# with the following properties:
# * `"type"`: The type (`String`) of the relationship.
# * `"properties"`: An object mapping each defined property to its value.
# * `"from"`: The local ID of the source node.
# * `"to"`: The local ID of the destination node.
#
# ~~~nit
# import neo4j::graph::sequential_id
#
# var graph = new NeoGraph(new SequentialNodeCollection("nid"))
# var a = new NeoNode
# a.labels.add "Foo"
# a["answer"] = 42
# a["Ultimate question of"] = new JsonArray.from(["life",
# "the Universe", "and Everything."])
# graph.nodes.register a
# var b = graph.create_node
# b.labels.add "Foo"
# b.labels.add "Bar"
# graph.edges.add new NeoEdge(a, "BAZ", b)
#
# var ostream = new StringWriter
# var store = new JsonGraphStore(graph)
# store.ostream = ostream
# store.save
# assert ostream.to_s == """{"nodes":[""" + """
# {"labels":["Foo"],"properties":{"answer":42,""" + """
# "Ultimate question of":["life","the Universe","and Everything."],""" + """
# "nid":1}},""" + """
# {"labels":["Foo","Bar"],"properties":{"nid":2}}],""" + """
# "edges":[{"type":"BAZ","properties":{},"from":1,"to":2}]}"""
#
# graph.nodes.clear
# graph.edges.clear
# store.istream = new StringReader(ostream.to_s)
# store.load
# assert 1 == graph.edges.length
# for edge in graph.edges do
# assert "BAZ" == edge.rel_type
# assert a.labels == edge.from.labels
# for k, v in a.properties do assert v == edge.from.properties[k]
# assert b.labels == edge.to.labels
# for k, v in b.properties do assert v == edge.to.properties[k]
# end
# assert 2 == graph.nodes.length
# ~~~
class JsonGraphStore
super GraphStore
# The stream to use for `load`.
var istream: nullable Reader = null is writable
# The stream to use for `save` and `save_part`.
var ostream: nullable Writer = null is writable
# Use the specified `Duplex`.
init from_io(graph: NeoGraph, iostream: Duplex) do
init(graph)
istream = iostream
ostream = iostream
end
# Use the specified string to load the graph.
init from_string(graph: NeoGraph, string: String) do
init(graph)
istream = new StringReader(string)
end
redef fun isolated_save do return true
redef fun load do
var istream = self.istream
assert istream isa Reader
fire_started
graph.load_json(istream.read_all)
fire_done
end
redef fun save_part(nodes, edges) do
var ostream = self.ostream
assert ostream isa Writer
fire_started
ostream.write(graph.to_json)
fire_done
end
end
lib/neo4j/graph/json_graph_store.nit:16,1--111,3