neo4j/graph: Implement optimization services of `SequentialNodeCollection`.
[nit.git] / lib / neo4j / graph / sequential_id.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
9 # another product.
10
11 # Provides a sequential identification scheme for Neo4j nodes.
12 module neo4j::graph::sequential_id
13
14 import graph
15 private import pipeline
16
17
18 # A Neo4j node collection using a sequential identification scheme.
19 #
20 # The local IDs are sequential numbers (integers) starting at `1`.
21 #
22 # Note: When loading nodes, the local IDs should forms a mostly contiguous
23 # range starting at `1`. Else, this collection will consume a lot of memory.
24 # Futhermore, the local IDs **must** be positive.
25 #
26 # ~~~nit
27 # var nodes = new SequentialNodeCollection("id")
28 # var a = nodes.create_node
29 # var b = new NeoNode
30 # var c = new NeoNode
31 #
32 # nodes.register b
33 # c["id"] = 4
34 # nodes.add c
35 # assert a["id"] == 1
36 # assert b["id"] == 2
37 # assert c["id"] == 4
38 # assert nodes.to_a == [a, b, c]
39 # assert nodes.length == 3
40 #
41 # nodes.compact
42 # assert a["id"] == 1
43 # assert b["id"] == 2
44 # assert c["id"] == 3
45 # assert nodes.to_a == [a, b, c]
46 # assert nodes.length == 3
47 # ~~~
48 class SequentialNodeCollection
49 super NeoNodeCollection
50
51 redef type ID_TYPE: Int
52
53 private var nodes = new Array[nullable NeoNode]
54
55 redef var length = 0
56
57 redef fun iterator do return new NullSkipper[NeoNode](self.nodes.iterator)
58
59 redef fun [](id) do return nodes[id].as(NeoNode)
60
61 redef fun get_or_null(id) do
62 if id < 0 or id > nodes.length then return null
63 return nodes[id]
64 end
65
66 redef fun has_id(id: Int): Bool do
67 return id >= 0 and id < nodes.length and nodes[id] isa NeoNode
68 end
69
70 redef fun enlarge(cap) do nodes.enlarge(cap)
71
72 redef fun register(node) do
73 nodes.add node
74 id_of(node) = nodes.length
75 length += 1
76 end
77
78 redef fun add(node) do
79 var id = node[id_property]
80 assert id isa Int else
81 sys.stderr.write "The local ID must be an `Int`.\n"
82 end
83 assert id >= 0 else
84 sys.stderr.write "The local ID must be greater or equal to 0. Got {id}.\n"
85 end
86 # Pad with nulls.
87 nodes.enlarge(id)
88 var delta = id - nodes.length
89 while delta > 0 do
90 nodes.add null
91 delta -= 1
92 end
93 nodes[id] = node
94 length += 1
95 end
96
97 redef fun remove_at(id) do
98 nodes[id] = null
99 length -= 1
100 end
101
102 redef fun clear do
103 nodes.clear
104 length = 0
105 end
106
107 redef fun compact do
108 var i = iterator
109
110 nodes = new Array[nullable NeoNode]
111 for n in i do
112 nodes.add n
113 id_of(n) = nodes.length
114 end
115 end
116 end