-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
NodeLabelRange.java
158 lines (146 loc) · 5.25 KB
/
NodeLabelRange.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.api.labelscan;
import org.neo4j.collection.primitive.Primitive;
import org.neo4j.collection.primitive.PrimitiveLongList;
import static java.lang.Math.toIntExact;
import static org.neo4j.collection.primitive.PrimitiveLongCollections.EMPTY_LONG_ARRAY;
/**
* Represents a range of nodes and label ids attached to those nodes. All nodes in the range are present in
* {@link #nodes() nodes array}, but not all node ids will have corresponding {@link #labels(long) labels},
* where an empty long[] will be returned instead.
*/
public class NodeLabelRange
{
private final long idRange;
private final long[] nodes;
private final long[][] labels;
/**
* @param idRange node id range, e.g. in which id span the nodes are.
* @param labels long[][] where first dimension is relative node id in this range, i.e. 0-rangeSize
* and second the label ids for that node, potentially empty if there are none for that node.
* The first dimension must be the size of the range.
*/
public NodeLabelRange( long idRange, long[][] labels )
{
this.idRange = idRange;
this.labels = labels;
int rangeSize = labels.length;
long baseNodeId = idRange * rangeSize;
this.nodes = new long[rangeSize];
for ( int i = 0; i < rangeSize; i++ )
{
nodes[i] = baseNodeId + i;
}
}
/**
* @return the range id of this range. This is the base node id divided by range size.
* Example: A store with nodes 1,3,20,22 and a range size of 16 would return ranges:
* - rangeId=0, nodes=1,3
* - rangeId=1, nodes=20,22
*/
public long id()
{
return idRange;
}
/**
* @return node ids in this range, the nodes in this array may or may not have {@link #labels(long) labels}
* attached to it.
*/
public long[] nodes()
{
return nodes;
}
/**
* Returns the label ids (as longs) for the given node id. The {@code nodeId} must be one of the ids
* from {@link #nodes()}.
*
* @param nodeId the node id to return labels for.
* @return label ids for the given {@code nodeId}.
*/
public long[] labels( long nodeId )
{
long firstNodeId = idRange * labels.length;
int index = toIntExact( nodeId - firstNodeId );
assert index >= 0 && index < labels.length : "nodeId:" + nodeId + ", idRange:" + idRange;
return labels[index] != null ? labels[index] : EMPTY_LONG_ARRAY;
}
private static String toString( String prefix, long[] nodes, long[][] labels )
{
StringBuilder result = new StringBuilder( prefix );
result.append( "; {" );
for ( int i = 0; i < nodes.length; i++ )
{
if ( i != 0 )
{
result.append( ", " );
}
result.append( "Node[" ).append( nodes[i] ).append( "]: Labels[" );
String sep = "";
if ( labels[i] != null )
{
for ( long labelId : labels[i] )
{
result.append( sep ).append( labelId );
sep = ", ";
}
}
else
{
result.append( "null" );
}
result.append( "]" );
}
return result.append( "}]" ).toString();
}
@Override
public String toString()
{
String rangeString = idRange * labels.length + "-" + (idRange + 1) * labels.length;
String prefix = "NodeLabelRange[idRange=" + rangeString;
return toString( prefix, nodes, labels );
}
public static void readBitmap( long bitmap, long labelId, PrimitiveLongList[] labelsPerNode )
{
while ( bitmap != 0 )
{
int relativeNodeId = Long.numberOfTrailingZeros( bitmap );
if ( labelsPerNode[relativeNodeId] == null )
{
labelsPerNode[relativeNodeId] = Primitive.longList();
}
labelsPerNode[relativeNodeId].add( labelId );
bitmap &= bitmap - 1;
}
}
public static long[][] convertState( PrimitiveLongList[] state )
{
long[][] labelIdsByNodeIndex = new long[state.length][];
for ( int i = 0; i < state.length; i++ )
{
PrimitiveLongList labelIdList = state[i];
if ( labelIdList != null )
{
labelIdsByNodeIndex[i] = labelIdList.toArray();
}
}
return labelIdsByNodeIndex;
}
}