Apart from properties and relationships (edges), nodes can also be labeled with zero or more labels.
A label is a String
that is used to group nodes into sets.
All nodes labeled with the same label belongs to the same set.
A node may be labeled with any number of labels, including none,
making labels an optional addition to the graph.
Creating new nodes:
var client = new Neo4jClient("http://neo4j:7474")
var andres = new NeoNode
andres.labels.add "Person"
andres["name"] = "Andres"
andres["age"] = 22
client.save_node(andres)
assert andres.is_linked
Get nodes from Neo4j:
var url = andres.url.to_s
var node = client.load_node(url)
assert node["name"] == "Andres"
assert node["age"].to_s.to_i == 22
neo4j :: NeoNode :: defaultinit
neo4j :: NeoNode :: from_json_object
Retrieve the node from the specified JSON value.neo4j :: json_graph_store $ NeoNode :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONneo4j :: json_graph_store $ NeoNode :: append_json_for
Append the JSON representation of the node to the specified buffer.serialization :: Serializable :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONserialization :: Serializable :: accept_msgpack_attribute_counter
Hook to customize the behavior of theAttributeCounter
serialization :: Serializable :: accept_msgpack_serializer
Hook to customize the serialization of this class to MessagePackserialization :: Serializable :: add_to_bundle
Called by[]=
to dynamically choose the appropriate method according
neo4j :: NeoEntity :: append_json_for
Append the JSON representation of the entity to the specified buffer.core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
serialization :: Serializable :: core_serialize_to
Actual serialization ofself
to serializer
neo4j :: NeoNode :: defaultinit
core :: Object :: defaultinit
neo4j :: NeoEntity :: defaultinit
serialization :: Serializable :: from_deserializer
Create an instance of this class from thedeserializer
neo4j :: NeoNode :: from_json_object
Retrieve the node from the specified JSON value.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.
serialization :: Serializable :: msgpack_extra_array_items
Hook to request a larger than usual metadata arraycore :: Object :: output_class_name
Display class name on stdout (debug only).neo4j :: NeoEntity :: properties
In Neo4j, both nodes and relationships can contain properties.serialization :: Serializable :: serialize_msgpack
Serializeself
to MessagePack bytes
serialization :: Serializable :: serialize_to
Serializeself
to serializer
serialization :: Serializable :: serialize_to_json
Serializeself
to JSON
serialization :: Serializable :: to_pretty_json
Serializeself
to plain pretty JSON
Serializer::serialize
# Nodes are used to represent entities stored in base.
# Apart from properties and relationships (edges),
# nodes can also be labeled with zero or more labels.
#
# A label is a `String` that is used to group nodes into sets.
# All nodes labeled with the same label belongs to the same set.
# A node may be labeled with any number of labels, including none,
# making labels an optional addition to the graph.
#
# Creating new nodes:
#
# var client = new Neo4jClient("http://neo4j:7474")
#
# var andres = new NeoNode
# andres.labels.add "Person"
# andres["name"] = "Andres"
# andres["age"] = 22
# client.save_node(andres)
# assert andres.is_linked
#
# Get nodes from Neo4j:
#
# var url = andres.url.to_s
# var node = client.load_node(url)
# assert node["name"] == "Andres"
# assert node["age"].to_s.to_i == 22
class NeoNode
super NeoEntity
private var internal_labels: nullable Array[String] = null
private var internal_in_edges: nullable List[NeoEdge] = null
private var internal_out_edges: nullable List[NeoEdge] = null
init do
super
self.internal_labels = new Array[String]
self.internal_in_edges = new List[NeoEdge]
self.internal_out_edges = new List[NeoEdge]
end
redef fun to_s do
var tpl = new FlatBuffer
tpl.append "\{"
tpl.append "labels: [{labels.join(", ")}],"
tpl.append "data: {properties.to_json}"
tpl.append "\}"
return tpl.write_to_string
end
# A label is a `String` that is used to group nodes into sets.
# A node may be labeled with any number of labels, including none.
# All nodes labeled with the same label belongs to the same set.
#
# Many database queries can work with these sets instead of the whole graph,
# making queries easier to write and more efficient.
#
# Labels are loaded lazily
fun labels: Array[String] do return internal_labels or else load_labels
private fun load_labels: Array[String] do
var labels = new Array[String]
var res = neo.get(url.to_s / "labels")
if res isa JsonArray then
for val in res do labels.add val.to_s
end
internal_labels = labels
return labels
end
# Get the list of `NeoEdge` pointing to `self`
#
# Edges are loaded lazily
fun in_edges: List[NeoEdge] do return internal_in_edges or else load_in_edges
private fun load_in_edges: List[NeoEdge] do
var edges = new List[NeoEdge]
var res = neo.get(url.to_s / "relationships/in").as(JsonArray)
for obj in res do
edges.add(new NeoEdge.from_json(neo, obj.as(JsonObject)))
end
internal_in_edges = edges
return edges
end
# Get the list of `NeoEdge` pointing from `self`
#
# Edges are loaded lazily
fun out_edges: List[NeoEdge] do return internal_out_edges or else load_out_edges
private fun load_out_edges: List[NeoEdge] do
var edges = new List[NeoEdge]
var res = neo.get(url.to_s / "relationships/out")
for obj in res.as(JsonArray) do
edges.add(new NeoEdge.from_json(neo, obj.as(JsonObject)))
end
internal_out_edges = edges
return edges
end
# Get nodes pointed by `self` following a `rel_type` edge
fun out_nodes(rel_type: String): Array[NeoNode] do
var res = new Array[NeoNode]
for edge in out_edges do
if edge.rel_type == rel_type then res.add edge.to
end
return res
end
# Get nodes pointing to `self` following a `rel_type` edge
fun in_nodes(rel_type: String): Array[NeoNode] do
var res = new Array[NeoNode]
for edge in in_edges do
if edge.rel_type == rel_type then res.add edge.from
end
return res
end
end
lib/neo4j/neo4j.nit:554,1--670,3
# Make `NeoNode` `Serializable`.
redef class NeoNode
super Serializable
# Retrieve the node from the specified JSON value.
#
# Note: Here, the `"id"` is optional and ignored.
#
# SEE: `JsonGraph`
#
# var node = new NeoNode.from_json("""
# {
# "labels": ["foo", "Bar"],
# "properties": {
# "baz": 42
# }
# }
# """)
# assert ["foo", "Bar"] == node.labels
# assert 42 == node["baz"]
init from_json(t: Text) do
from_json_object(t.parse_json.as(JsonObject))
end
# Retrieve the node from the specified JSON value.
#
# Note: Here, the `"id"` is optional and ignored.
#
# SEE: `JsonGraph`
init from_json_object(o: JsonObject) do
init
var labels = o["labels"].as(JsonArray)
for lab in labels do self.labels.add(lab.as(String))
var json_properties = o["properties"].as(JsonObject)
properties.add_all(json_properties)
end
redef fun accept_json_serializer(v) do
v.stream.write "\{\"labels\":["
var i = labels.iterator
if i.is_ok then
i.item.serialize_to v
i.next
for lab in i do
v.stream.write ","
lab.serialize_to v
end
end
v.stream.write "],\"properties\":"
properties.serialize_to v
v.stream.write "}"
end
redef fun to_s do return to_json
# Append the JSON representation of the node to the specified buffer.
redef fun append_json_for(graph, v) do
accept_json_serializer v
end
end
lib/neo4j/graph/json_graph_store.nit:229,1--288,3