Batches are used to perform multiple operations on the REST API in one cURL request.

This can significantly improve performance for large insert and update operations.

see: http://docs.neo4j.org/chunked/milestone/rest-api-batch-ops.html

This service is transactional. If any of the operations performed fails (returns a non-2xx HTTP status code), the transaction will be rolled back and all changes will be undone.

Example:

var client = new Neo4jClient("http://neo4j:7474")

var node1 = new NeoNode
var node2 = new NeoNode
var edge = new NeoEdge(node1, "TO", node2)

var batch = new NeoBatch(client)
batch.save_node(node1)
batch.save_node(node2)
batch.save_edge(edge)
batch.execute

assert node1.is_linked
assert node2.is_linked
assert edge.is_linked

Introduced properties

fun client: Neo4jClient

neo4j :: NeoBatch :: client

Neo4j client connector
protected fun client=(client: Neo4jClient)

neo4j :: NeoBatch :: client=

Neo4j client connector
fun execute: List[NeoError]

neo4j :: NeoBatch :: execute

Execute the batch and update local nodes
fun jobs: HashMap[Int, NeoJob]

neo4j :: NeoBatch :: jobs

Jobs to perform in this batch
protected fun jobs=(jobs: HashMap[Int, NeoJob])

neo4j :: NeoBatch :: jobs=

Jobs to perform in this batch
fun load_node(node: NeoNode)

neo4j :: NeoBatch :: load_node

Load a node in batch mode also load labels, data and edges
fun load_node_edges(node: NeoNode)

neo4j :: NeoBatch :: load_node_edges

Load in and out edges into node
fun new_job(nentity: NeoEntity): NeoJob

neo4j :: NeoBatch :: new_job

Append a new job to the batch in JSON Format
fun save_edge(edge: NeoEdge)

neo4j :: NeoBatch :: save_edge

Create an edge
fun save_edges(edges: Collection[NeoEdge])

neo4j :: NeoBatch :: save_edges

Create multiple edges
fun save_entity(nentity: NeoEntity)

neo4j :: NeoBatch :: save_entity

Create a NeoNode or a NeoEdge in batch mode.
fun save_node(node: NeoNode)

neo4j :: NeoBatch :: save_node

Create a node in batch mode also create labels and edges
fun save_nodes(nodes: Collection[NeoNode])

neo4j :: NeoBatch :: save_nodes

Create multiple nodes

Redefined properties

redef type SELF: NeoBatch

neo4j $ NeoBatch :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun client: Neo4jClient

neo4j :: NeoBatch :: client

Neo4j client connector
protected fun client=(client: Neo4jClient)

neo4j :: NeoBatch :: client=

Neo4j client connector
fun execute: List[NeoError]

neo4j :: NeoBatch :: execute

Execute the batch and update local nodes
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun jobs: HashMap[Int, NeoJob]

neo4j :: NeoBatch :: jobs

Jobs to perform in this batch
protected fun jobs=(jobs: HashMap[Int, NeoJob])

neo4j :: NeoBatch :: jobs=

Jobs to perform in this batch
fun load_node(node: NeoNode)

neo4j :: NeoBatch :: load_node

Load a node in batch mode also load labels, data and edges
fun load_node_edges(node: NeoNode)

neo4j :: NeoBatch :: load_node_edges

Load in and out edges into node
fun new_job(nentity: NeoEntity): NeoJob

neo4j :: NeoBatch :: new_job

Append a new job to the batch in JSON Format
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun save_edge(edge: NeoEdge)

neo4j :: NeoBatch :: save_edge

Create an edge
fun save_edges(edges: Collection[NeoEdge])

neo4j :: NeoBatch :: save_edges

Create multiple edges
fun save_entity(nentity: NeoEntity)

neo4j :: NeoBatch :: save_entity

Create a NeoNode or a NeoEdge in batch mode.
fun save_node(node: NeoNode)

neo4j :: NeoBatch :: save_node

Create a node in batch mode also create labels and edges
fun save_nodes(nodes: Collection[NeoNode])

neo4j :: NeoBatch :: save_nodes

Create multiple nodes
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram neo4j::NeoBatch NeoBatch core::Object Object neo4j::NeoBatch->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

neo4j $ NeoBatch
# Batches are used to perform multiple operations on the REST API in one cURL request.
# This can significantly improve performance for large insert and update operations.
#
# see: http://docs.neo4j.org/chunked/milestone/rest-api-batch-ops.html
#
# This service is transactional.
# If any of the operations performed fails (returns a non-2xx HTTP status code),
# the transaction will be rolled back and all changes will be undone.
#
# Example:
#
#     var client = new Neo4jClient("http://neo4j:7474")
#
#     var node1 = new NeoNode
#     var node2 = new NeoNode
#     var edge = new NeoEdge(node1, "TO", node2)
#
#     var batch = new NeoBatch(client)
#     batch.save_node(node1)
#     batch.save_node(node2)
#     batch.save_edge(edge)
#     batch.execute
#
#     assert node1.is_linked
#     assert node2.is_linked
#     assert edge.is_linked
class NeoBatch

	# Neo4j client connector
	var client: Neo4jClient

	# Jobs to perform in this batch
	#
	# The batch service expects an array of job descriptions as input,
	# each job description describing an action to be performed via the normal server API.
	var jobs = new HashMap[Int, NeoJob]

	# Append a new job to the batch in JSON Format
	# see `NeoJob`
	fun new_job(nentity: NeoEntity): NeoJob do
		var id = jobs.length
		var job = new NeoJob(id, nentity)
		jobs[id] = job
		return job
	end

	# Load a node in batch mode also load labels, data and edges
	fun load_node(node: NeoNode) do
		var job = new_job(node)
		job.action = load_node_data_action
		job.method = "GET"
		if node.id != null then
			job.to = "/node/{node.id.to_s}"
		else
			job.to = "\{{node.batch_id.to_s}\}"
		end
		job = new_job(node)
		job.action = load_node_labels_action
		job.method = "GET"
		if node.id != null then
			job.to = "/node/{node.id.to_s}/labels"
		else
			job.to = "\{{node.batch_id.to_s}\}/labels"
		end
	end

	# Load in and out edges into node
	fun load_node_edges(node: NeoNode) do
		var job = new_job(node)
		job.action = load_node_in_edges_action
		job.method = "GET"
		if node.id != null then
			job.to = "/node/{node.id.to_s}/relationships/in"
		else
			job.to = "\{{node.batch_id.to_s}\}/relationships/in"
		end
		job = new_job(node)
		job.action = load_node_out_edges_action
		job.method = "GET"
		if node.id != null then
			job.to = "/node/{node.id.to_s}/relationships/out"
		else
			job.to = "\{{node.batch_id.to_s}\}/relationships/out"
		end
	end

	# Create a `NeoNode` or a `NeoEdge` in batch mode.
	fun save_entity(nentity: NeoEntity) do
		if nentity isa NeoNode then
			save_node(nentity)
		else if nentity isa NeoEdge then
			save_edge(nentity)
		else abort
	end

	# Create a node in batch mode also create labels and edges
	fun save_node(node: NeoNode) do
		if node.id != null or node.batch_id != null then return
		# create node
		var job = new_job(node)
		node.batch_id = job.id
		job.action = create_node_action
		job.method = "POST"
		job.to = "/node"
		job.body = node.properties
		# add labels
		job = new_job(node)
		job.method = "POST"
		job.to = "\{{node.batch_id.to_s}\}/labels"
		job.body = new JsonArray.from(node.labels)
		# add edges
		#save_edges(node.out_edges)
	end

	# Create multiple nodes
	# also create labels and edges
	fun save_nodes(nodes: Collection[NeoNode]) do for node in nodes do save_node(node)

	# Create an edge
	# nodes `edge.from` and `edge.to` will be created if not in base
	fun save_edge(edge: NeoEdge) do
		if edge.id != null or edge.batch_id != null then return
		# create nodes
		save_node(edge.from)
		save_node(edge.to)
		# create edge
		var job = new_job(edge)
		edge.batch_id = job.id
		job.action = create_edge_action
		job.method = "POST"
		if edge.from.id != null then
			job.to = "/node/{edge.from.id.to_s}/relationships"
		else
			job.to = "\{{edge.from.batch_id.to_s}\}/relationships"
		end
		job.body = edge.to_rest
	end

	# Create multiple edges
	fun save_edges(edges: Collection[NeoEdge]) do for edge in edges do save_edge(edge)

	# Execute the batch and update local nodes
	fun execute: List[NeoError] do
		var request = new JsonPOST(client.batch_url)
		# request.headers["X-Stream"] = "true"
		var json_jobs = new JsonArray
		for job in jobs.values do json_jobs.add job.to_rest
		request.json_data = json_jobs
		var response = request.execute
		var res = client.parse_response(response)
		return finalize_batch(res)
	end

	# Associate data from response in original nodes and edges
	private fun finalize_batch(response: Serializable): List[NeoError] do
		var errors = new List[NeoError]
		if not response isa JsonArray then
			errors.add(new NeoError("Unexpected batch response format.", "Neo4jError"))
			return errors
		end
		# print " {res.length} jobs executed"
		for res in response do
			if not res isa JsonObject then
				errors.add(new NeoError("Unexpected job format in batch response.", "Neo4jError"))
				continue
			end
			var id = res["id"].as(Int)
			var job = jobs[id]
			if job.action == create_node_action then
				var node = job.entity.as(NeoNode)
				node.batch_id = null
				node.url = res["location"].to_s
			else if job.action == create_edge_action then
				var edge = job.entity.as(NeoEdge)
				edge.batch_id = null
				edge.url = res["location"].to_s
			else if job.action == load_node_data_action then
				var node = job.entity.as(NeoNode)
				node.internal_properties = res["body"].as(JsonObject)["data"].as(JsonObject)
			else if job.action == load_node_labels_action then
				var node = job.entity.as(NeoNode)
				var labels = new Array[String]
				for l in res["body"].as(JsonArray) do labels.add l.to_s
				node.internal_labels = labels
			else if job.action == load_node_in_edges_action then
				var node = job.entity.as(NeoNode)
				var edges = res["body"].as(JsonArray)
				node.internal_in_edges = new List[NeoEdge]
				for edge in edges do
					node.internal_in_edges.add client.load_edge(edge.as(JsonObject)["self"].to_s)
				end
			else if job.action == load_node_out_edges_action then
				var node = job.entity.as(NeoNode)
				var edges = res["body"].as(JsonArray)
				node.internal_out_edges = new List[NeoEdge]
				for edge in edges do
					node.internal_out_edges.add client.load_edge(edge.as(JsonObject)["self"].to_s)
				end
			end
		end
		return errors
	end

	# JobActions
	# TODO replace with enum

	private fun create_node_action: Int do return 1
	private fun create_edge_action: Int do return 2
	private fun load_node_data_action: Int do return 3
	private fun load_node_labels_action: Int do return 4
	private fun load_node_in_edges_action: Int do return 5
	private fun load_node_out_edges_action: Int do return 6
end
lib/neo4j/neo4j.nit:760,1--972,3