3b3559a199ec824d865c2c4fa267787360e51541
1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
11 # Provides a sequential identification scheme for Neo4j nodes.
12 module neo4j
::graph
::sequential_id
15 private import pipeline
18 # A Neo4j node collection using a sequential identification scheme.
20 # The local IDs are sequential numbers (integers) starting at `1`.
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.
27 # var nodes = new SequentialNodeCollection("id")
28 # var a = nodes.create_node
38 # assert nodes.to_a == [a, b, c]
40 class SequentialNodeCollection
41 super NeoNodeCollection
43 redef type ID_TYPE: Int
45 private var nodes
= new Array[nullable NeoNode]
47 redef fun iterator
do return new NullSkipper[NeoNode](self.nodes
.iterator
)
49 redef fun get_or_null
(id
) do
50 if id
< 0 or id
> nodes
.length
then return null
54 redef fun register
(node
) do
56 id_of
(node
) = nodes
.length
59 redef fun add
(node
) do
60 var id
= node
[id_property
]
61 assert id
isa Int else
62 sys
.stderr
.write
"The local ID must be an `Int`.\n"
65 sys
.stderr
.write
"The local ID must be greater or equal to 0. Got {id}.\n"
68 var delta
= id
- nodes
.length
76 redef fun remove_at
(id
) do