Property definitions

neo4j $ JsonGraphStore :: defaultinit
# 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