Skip to content

Commit 499f54f

Browse files
authored
Merge pull request #6891 from s1ck/backport-nodesbuilder-changes
Backport NodesBuilder changes
2 parents 10bd6bc + 24bf084 commit 499f54f

File tree

31 files changed

+893
-291
lines changed

31 files changed

+893
-291
lines changed

core/src/main/java/org/neo4j/gds/api/CSRGraphStoreFactory.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,12 @@ public CSRGraphStoreFactory(
4242
super(graphProjectConfig, capabilities, loadingContext, dimensions);
4343
}
4444

45-
protected CSRGraphStore createGraphStore(
46-
Nodes nodes,
47-
RelationshipImportResult relationshipImportResult
48-
) {
45+
protected CSRGraphStore createGraphStore(Nodes nodes, RelationshipImportResult relationshipImportResult) {
4946
return new GraphStoreBuilder()
5047
.databaseId(DatabaseId.of(loadingContext.graphDatabaseService()))
5148
.capabilities(capabilities)
5249
.schema(computeGraphSchema(nodes, relationshipImportResult))
53-
.nodes(Nodes.of(nodes.idMap(), nodes.properties()))
50+
.nodes(nodes)
5451
.relationshipImportResult(relationshipImportResult)
5552
.concurrency(graphProjectConfig.readConcurrency())
5653
.build();

core/src/main/java/org/neo4j/gds/core/loading/ArrayIdMap.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public class ArrayIdMap extends LabeledIdMap {
6161

6262
private final long highestNeoId;
6363

64-
private final HugeLongArray graphIds;
65-
private final HugeSparseLongArray nodeToGraphIds;
64+
private final HugeLongArray internalToOriginalIds;
65+
private final HugeSparseLongArray originalToInternalIds;
6666

6767
public static MemoryEstimation memoryEstimation() {
6868
return ESTIMATION;
@@ -72,26 +72,26 @@ public static MemoryEstimation memoryEstimation() {
7272
* initialize the map with pre-built sub arrays
7373
*/
7474
public ArrayIdMap(
75-
HugeLongArray graphIds,
76-
HugeSparseLongArray nodeToGraphIds,
75+
HugeLongArray internalToOriginalIds,
76+
HugeSparseLongArray originalToInternalIds,
7777
LabelInformation labelInformation,
7878
long nodeCount,
7979
long highestNeoId
8080
) {
8181
super(labelInformation, nodeCount);
82-
this.graphIds = graphIds;
83-
this.nodeToGraphIds = nodeToGraphIds;
82+
this.internalToOriginalIds = internalToOriginalIds;
83+
this.originalToInternalIds = originalToInternalIds;
8484
this.highestNeoId = highestNeoId;
8585
}
8686

8787
@Override
8888
public long toMappedNodeId(long originalNodeId) {
89-
return nodeToGraphIds.get(originalNodeId);
89+
return originalToInternalIds.get(originalNodeId);
9090
}
9191

9292
@Override
9393
public long toOriginalNodeId(long mappedNodeId) {
94-
return graphIds.get(mappedNodeId);
94+
return internalToOriginalIds.get(mappedNodeId);
9595
}
9696

9797
@Override
@@ -106,7 +106,7 @@ public IdMap rootIdMap() {
106106

107107
@Override
108108
public boolean contains(final long originalNodeId) {
109-
return nodeToGraphIds.contains(originalNodeId);
109+
return originalToInternalIds.contains(originalNodeId);
110110
}
111111

112112
@Override
@@ -141,7 +141,7 @@ public Optional<FilteredIdMap> withFilteredLabels(Collection<NodeLabel> nodeLabe
141141

142142
HugeSparseLongArray newNodeToGraphIds = ArrayIdMapBuilderOps.buildSparseIdMap(
143143
newNodeCount,
144-
nodeToGraphIds.capacity(),
144+
originalToInternalIds.capacity(),
145145
concurrency,
146146
newGraphIds
147147
);

core/src/main/java/org/neo4j/gds/core/loading/ArrayIdMapBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public IdMap build(
6969
) {
7070
adders.close();
7171
long nodeCount = this.size();
72-
var graphIds = this.array();
73-
return ArrayIdMapBuilderOps.build(graphIds, nodeCount, labelInformationBuilder, highestNodeId, concurrency);
72+
HugeLongArray internalToOriginalIds = this.array();
73+
return ArrayIdMapBuilderOps.build(internalToOriginalIds, nodeCount, labelInformationBuilder, highestNodeId, concurrency);
7474
}
7575

7676
public HugeLongArray array() {

core/src/main/java/org/neo4j/gds/core/loading/ArrayIdMapBuilderOps.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,28 @@
3131
public final class ArrayIdMapBuilderOps {
3232

3333
static ArrayIdMap build(
34-
HugeLongArray graphIds,
34+
HugeLongArray internalToOriginalIds,
3535
long nodeCount,
3636
LabelInformation.Builder labelInformationBuilder,
3737
long highestNodeId,
3838
int concurrency
3939
) {
4040
if (highestNodeId == NodesBuilder.UNKNOWN_MAX_ID) {
41-
highestNodeId = graphIds.asNodeProperties().getMaxLongPropertyValue().orElse(NodesBuilder.UNKNOWN_MAX_ID);
41+
highestNodeId = internalToOriginalIds.asNodeProperties().getMaxLongPropertyValue().orElse(NodesBuilder.UNKNOWN_MAX_ID);
4242
}
4343

44-
HugeSparseLongArray nodeToGraphIds = buildSparseIdMap(
44+
HugeSparseLongArray originalToInternalIds = buildSparseIdMap(
4545
nodeCount,
4646
highestNodeId,
4747
concurrency,
48-
graphIds
48+
internalToOriginalIds
4949
);
5050

51-
var labelInformation = labelInformationBuilder.build(nodeCount, nodeToGraphIds::get);
51+
var labelInformation = labelInformationBuilder.build(nodeCount, originalToInternalIds::get);
5252

5353
return new ArrayIdMap(
54-
graphIds,
55-
nodeToGraphIds,
54+
internalToOriginalIds,
55+
originalToInternalIds,
5656
labelInformation,
5757
nodeCount,
5858
highestNodeId

core/src/main/java/org/neo4j/gds/core/loading/CSRGraphStoreUtil.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package org.neo4j.gds.core.loading;
2121

2222
import org.jetbrains.annotations.NotNull;
23-
import org.neo4j.gds.NodeLabel;
2423
import org.neo4j.gds.RelationshipType;
2524
import org.neo4j.gds.api.DatabaseId;
2625
import org.neo4j.gds.api.Graph;
@@ -43,7 +42,6 @@
4342
import org.neo4j.gds.core.huge.HugeGraph;
4443
import org.neo4j.values.storable.NumberType;
4544

46-
import java.util.Collection;
4745
import java.util.Map;
4846
import java.util.Optional;
4947
import java.util.function.Function;
@@ -106,7 +104,7 @@ public static CSRGraphStore createFromGraph(
106104
// TODO: is it correct that we only use this for generated graphs?
107105
.capabilities(ImmutableStaticCapabilities.of(false))
108106
.schema(schema)
109-
.nodes(Nodes.of(graph.idMap(), nodeProperties))
107+
.nodes(ImmutableNodes.of(schema.nodeSchema(), graph.idMap(), nodeProperties))
110108
.relationshipImportResult(relationshipImportResult)
111109
.graphProperties(GraphPropertyStore.empty())
112110
.concurrency(concurrency)
@@ -203,25 +201,12 @@ public static void extractNodeProperties(
203201
nodeImportResultBuilder.properties(propertyStoreBuilder.build());
204202
}
205203

204+
// TODO: remove this method
206205
public static GraphSchema computeGraphSchema(
207206
Nodes nodes,
208-
Function<NodeLabel, Collection<String>> propertiesByLabel,
209207
RelationshipImportResult relationshipImportResult
210208
) {
211-
var nodeProperties = nodes.properties().properties();
212-
213-
var nodeSchema = NodeSchema.empty();
214-
for (var label : nodes.idMap().availableNodeLabels()) {
215-
var entry = nodeSchema.getOrCreateLabel(label);
216-
for (var propertyKey : propertiesByLabel.apply(label)) {
217-
entry.addProperty(
218-
propertyKey,
219-
nodeProperties.get(propertyKey).propertySchema()
220-
);
221-
}
222-
}
223-
nodes.idMap().availableNodeLabels().forEach(nodeSchema::getOrCreateLabel);
224-
209+
var nodeSchema = nodes.schema();
225210
var relationshipSchema = RelationshipSchema.empty();
226211

227212
relationshipImportResult.importResults().forEach(((relationshipType, singleTypeRelationshipImportResult) -> {

core/src/main/java/org/neo4j/gds/core/loading/CypherFactory.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ protected GraphSchema computeGraphSchema(
124124
) {
125125
return CSRGraphStoreUtil.computeGraphSchema(
126126
nodes,
127-
(__) -> nodes.properties().keySet(),
128127
relationshipImportResult
129128
);
130129
}
@@ -141,25 +140,25 @@ public CSRGraphStore build() {
141140
).load(ktx.internalTransaction());
142141

143142
progressTracker.beginSubTask("Loading");
144-
var idMapAndProperties = new CypherNodeLoader(
143+
var nodes = new CypherNodeLoader(
145144
nodeQuery(),
146145
nodeCount.rows(),
147146
cypherConfig,
148147
loadingContext,
149148
progressTracker
150149
).load(ktx.internalTransaction());
151150

152-
var relationshipsAndProperties = new CypherRelationshipLoader(
151+
var relationshipImportResult = new CypherRelationshipLoader(
153152
relationshipQuery(),
154-
idMapAndProperties.idMap(),
153+
nodes.idMap(),
155154
cypherConfig,
156155
loadingContext,
157156
progressTracker
158157
).load(ktx.internalTransaction());
159158

160159
var graphStore = createGraphStore(
161-
idMapAndProperties,
162-
relationshipsAndProperties
160+
nodes,
161+
relationshipImportResult
163162
);
164163

165164
progressTracker.endSubTask("Loading");

core/src/main/java/org/neo4j/gds/core/loading/CypherNodeLoader.java

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,15 @@
2020
package org.neo4j.gds.core.loading;
2121

2222
import org.immutables.value.Value;
23-
import org.neo4j.gds.PropertyMapping;
2423
import org.neo4j.gds.api.GraphLoaderContext;
2524
import org.neo4j.gds.api.PropertyState;
26-
import org.neo4j.gds.api.properties.nodes.NodePropertyValues;
2725
import org.neo4j.gds.config.GraphProjectFromCypherConfig;
2826
import org.neo4j.gds.core.loading.construction.GraphFactory;
2927
import org.neo4j.gds.core.loading.construction.NodesBuilder;
3028
import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker;
3129
import org.neo4j.kernel.impl.coreapi.InternalTransaction;
3230

33-
import java.util.Map;
3431
import java.util.Set;
35-
import java.util.stream.Collectors;
3632

3733
@Value.Enclosing
3834
class CypherNodeLoader extends CypherRecordLoader<Nodes> {
@@ -71,6 +67,7 @@ BatchLoadResult loadSingleBatch(InternalTransaction tx, int bufferSize) {
7167
.maxOriginalId(NodesBuilder.UNKNOWN_MAX_ID)
7268
.hasLabelInformation(hasLabelInformation)
7369
.hasProperties(!propertyColumns.isEmpty())
70+
.propertyState(PropertyState.TRANSIENT)
7471
.build();
7572

7673
nodeSubscriber.initialize(subscription.fieldNames(), this.nodesBuilder);
@@ -99,12 +96,7 @@ void updateCounts(BatchLoadResult result) {
9996

10097
@Override
10198
Nodes result() {
102-
var nodes = nodesBuilder.build(highestNodeId);
103-
var idMap = nodes.idMap();
104-
var nodeProperties = nodes.properties().propertyValues();
105-
var nodePropertiesWithPropertyMappings = propertiesWithPropertyMappings(nodeProperties);
106-
107-
return Nodes.of(idMap, nodePropertiesWithPropertyMappings, PropertyState.TRANSIENT);
99+
return nodesBuilder.build(highestNodeId);
108100
}
109101

110102
@Override
@@ -121,16 +113,4 @@ Set<String> getReservedColumns() {
121113
QueryType queryType() {
122114
return QueryType.NODE;
123115
}
124-
125-
private static Map<PropertyMapping, NodePropertyValues> propertiesWithPropertyMappings(Map<String, NodePropertyValues> properties) {
126-
return properties.entrySet()
127-
.stream()
128-
.collect(Collectors.toMap(
129-
propertiesByKey -> PropertyMapping.of(
130-
propertiesByKey.getKey(),
131-
propertiesByKey.getValue().valueType().fallbackValue()
132-
),
133-
Map.Entry::getValue
134-
));
135-
}
136116
}

core/src/main/java/org/neo4j/gds/core/loading/IndexPropertyMappings.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,29 @@
4646
import static java.util.stream.Collectors.toMap;
4747
import static org.neo4j.gds.core.GraphDimensions.ANY_LABEL;
4848

49+
// TODO: should be named LoadablePropertyMappings
4950
final class IndexPropertyMappings {
5051

51-
static LoadablePropertyMappings prepareProperties(
52-
GraphProjectFromStoreConfig graphProjectConfig,
53-
GraphDimensions graphDimensions,
54-
TransactionContext transaction
55-
) {
56-
Map<NodeLabel, PropertyMappings> storeLoadedProperties = graphProjectConfig
52+
static Map<NodeLabel, PropertyMappings> propertyMappings(GraphProjectFromStoreConfig graphProjectConfig) {
53+
return graphProjectConfig
5754
.nodeProjections()
5855
.projections()
5956
.entrySet()
6057
.stream()
6158
.collect(toMap(
6259
Map.Entry::getKey,
63-
entry -> entry.getValue().properties()
60+
entry -> entry
61+
.getValue()
62+
.properties()
6463
));
64+
}
6565

66-
return prepareLoadableProperties(graphDimensions, transaction, storeLoadedProperties);
66+
static LoadablePropertyMappings prepareProperties(
67+
GraphProjectFromStoreConfig graphProjectConfig,
68+
GraphDimensions graphDimensions,
69+
TransactionContext transaction
70+
) {
71+
return prepareLoadableProperties(graphDimensions, transaction, propertyMappings(graphProjectConfig));
6772
}
6873

6974
private static LoadablePropertyMappings prepareLoadableProperties(

core/src/main/java/org/neo4j/gds/core/loading/LazyIdMapBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.neo4j.gds.api.IdMap;
2424
import org.neo4j.gds.api.PartialIdMap;
2525
import org.neo4j.gds.api.properties.nodes.NodePropertyValues;
26+
import org.neo4j.gds.api.schema.NodeSchema;
2627
import org.neo4j.gds.core.loading.construction.GraphFactory;
2728
import org.neo4j.gds.core.loading.construction.NodeLabelToken;
2829
import org.neo4j.gds.core.loading.construction.NodesBuilder;
@@ -110,6 +111,8 @@ public interface HighLimitIdMapAndProperties {
110111

111112
PartialIdMap intermediateIdMap();
112113

114+
NodeSchema schema();
115+
113116
Optional<Map<String, NodePropertyValues>> nodeProperties();
114117
}
115118

@@ -141,6 +144,7 @@ public OptionalLong rootNodeCount() {
141144
.builder()
142145
.idMap(idMap)
143146
.intermediateIdMap(partialIdMap)
147+
.schema(nodes.schema())
144148
.nodeProperties(nodes.properties().propertyValues())
145149
.build();
146150
}

core/src/main/java/org/neo4j/gds/core/loading/NativeFactory.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,6 @@ protected GraphSchema computeGraphSchema(
301301
) {
302302
return CSRGraphStoreUtil.computeGraphSchema(
303303
nodes,
304-
(label) -> storeConfig.nodeProjections().projections().get(label).properties().propertyKeys(),
305304
relationshipImportResult
306305
);
307306
}

0 commit comments

Comments
 (0)