Skip to content

Commit c630f7a

Browse files
committed
count graph elements during proto conversion
flatgraph can log more cheaply log this info internally while loading a graph. ODB graphs are converted on the fly to flatgraph, so that'll happen for those graphs as well. Add this for protos, and all formats are covered.
1 parent 9584dc0 commit c630f7a

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

codepropertygraph/src/main/scala/io/shiftleft/codepropertygraph/cpgloading/CpgLoader.scala

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ object CpgLoader {
4040
load(absolutePath, persistTo = path.resolveSibling(s"${path.getFileName}.fg"))
4141
} else {
4242
// assuming it's flatgraph format
43-
val cpg = Cpg.withStorage(absolutePath)
44-
logger.debug(s"Loaded Cpg with ${cpg.graph.nodeCount} nodes and ${cpg.graph.edgeCount} edges")
45-
cpg
43+
Cpg.withStorage(absolutePath)
4644
}
4745
}
4846

@@ -60,7 +58,7 @@ object CpgLoader {
6058
if (persistTo != from)
6159
Files.deleteIfExists(persistTo)
6260

63-
val cpg = if (!Files.exists(absolutePath)) {
61+
if (!Files.exists(absolutePath)) {
6462
throw new FileNotFoundException(s"given input file $absolutePath does not exist")
6563
} else if (isProtoFormat(absolutePath)) {
6664
logger.debug(s"Converting $from from proto cpg into new flatgraph storage: $persistTo")
@@ -75,9 +73,6 @@ object CpgLoader {
7573
s"unknown file format - we probed the first bytes but it didn't look like one of our known formats (proto.zip, flatgraph, overflowdb)"
7674
)
7775
}
78-
logger.debug(s"Loaded Cpg with ${cpg.graph.nodeCount} nodes and ${cpg.graph.edgeCount} edges")
79-
80-
cpg
8176
}
8277

8378
/** Determine whether the CPG is a legacy (proto) CPG */

codepropertygraph/src/main/scala/io/shiftleft/codepropertygraph/cpgloading/ProtoCpgLoader.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ object ProtoCpgLoader {
4141
implicit val interner: StringInterner = StringInterner.makeStrongInterner()
4242
val protoToGraphNodeMappings = new ProtoToGraphNodeMappings
4343
val cpg = openOrCreateCpg(storagePath)
44+
var edgeCount = 0
45+
var propCount = 0
4446

4547
// first pass: add the raw nodes without any properties or edges
4648
addNodesRaw(protoCpgs().flatMap(cpgProto => nodesIter(cpgProto)), cpg.graph, protoToGraphNodeMappings)
@@ -54,20 +56,23 @@ object ProtoCpgLoader {
5456
.findGNode(protoNode)
5557
.getOrElse(throw new ConversionException(s"node with proto node id=$protoNodeId not found in graph"))
5658
protoNode.getPropertyList.iterator().asScala.foreach { protoProperty =>
59+
propCount += 1
5760
diffGraph.setNodeProperty(gNode, protoProperty.getName.name(), extractPropertyValue(protoProperty.getValue()))
5861
}
5962
}
6063

6164
protoCpg.getEdgeList.iterator().asScala.foreach { protoEdge =>
6265
List(protoEdge.getSrc, protoEdge.getDst).map(protoToGraphNodeMappings.findGNode) match {
6366
case List(Some(srcNode), Some(dstNode)) =>
67+
edgeCount += 1
6468
diffGraph.addEdge(srcNode, dstNode, protoEdge.getType.name(), extractEdgePropertyValue(protoEdge))
6569
case _ => // at least one of the nodes doesn't exist in the cpg, most likely because it was filtered out - ignore
6670
}
6771
}
6872
}
6973

7074
DiffGraphApplier.applyDiff(cpg.graph, diffGraph)
75+
logger.debug(s"Loaded proto graph with ${cpg.graph.nodeCount} nodes, $edgeCount edges, $propCount properties")
7176
cpg
7277
}
7378

0 commit comments

Comments
 (0)