Skip to content

Commit

Permalink
Graph.nodes() without given labels should return all nodes, not zero (
Browse files Browse the repository at this point in the history
  • Loading branch information
mpollmeier authored Mar 14, 2024
1 parent 7cbc49b commit 4c62caf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
11 changes: 8 additions & 3 deletions core/src/main/scala/flatgraph/Graph.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,28 @@ class Graph(val schema: Schema, val storagePathMaybe: Option[Path] = None) exten
private[flatgraph] val livingNodeCountByKind: Array[Int] = new Array[Int](nodeKindCount)

/** Note: this included `deleted` nodes! You might want to use `livingNodeCountByKind` instead. */
private[flatgraph] def nodeCountByKind(kind: Int): Int = nodesArray(kind).length
private[flatgraph] def nodeCountByKind(kind: Int): Int =
if (nodesArray.length <= kind) 0
else nodesArray(kind).length

private[flatgraph] val properties = new Array[AnyRef](nodeKindCount * propertiesCount * PropertySlotSize)
private[flatgraph] val inverseIndices = new AtomicReferenceArray[Object](nodeKindCount * propertiesCount * PropertySlotSize)
private[flatgraph] val nodesArray: Array[Array[GNode]] = makeNodesArray()
private[flatgraph] val neighbors: Array[AnyRef] = makeNeighbors()

def _nodes(nodeKind: Int): InitNodeIterator[GNode] = {
if (nodeCountByKind(nodeKind) == livingNodeCountByKind(nodeKind)) new InitNodeIteratorArray[GNode](nodesArray(nodeKind))
if (nodeKind < 0 || schema.getNumberOfNodeKinds <= nodeKind) InitNodeIteratorArray(Array.empty[GNode])
else if (nodeCountByKind(nodeKind) == livingNodeCountByKind(nodeKind)) new InitNodeIteratorArray[GNode](nodesArray(nodeKind))
else new InitNodeIteratorArrayFiltered[GNode](nodesArray(nodeKind))
}

def nodes(label: String): InitNodeIterator[GNode] =
_nodes(schema.getNodeKindByLabel(label))

/** Lookup nodes for the given labels, or all nodes if no label is given (`.nodes()`) */
def nodes(labels: String*): Iterator[GNode] =
labels.iterator.flatMap(nodes)
if (labels.isEmpty) allNodes
else labels.iterator.flatMap(nodes)

/** Lookup node by id - note: this may return null or throw an exception if the referenced node doesn't exist */
def node(id: Long): GNode =
Expand Down
23 changes: 23 additions & 0 deletions core/src/test/scala/flatgraph/GraphTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,29 @@ class GraphTests extends AnyWordSpec with Matchers {
testSerialization(g)
}

"some basic lookups" in {
val schema = TestSchema.make(2, 1)
val g = new Graph(schema)
val V0_0 = new GenericDNode(0)
val V0_1 = new GenericDNode(0)
val V1_0 = new GenericDNode(1)
DiffGraphApplier.applyDiff(
g,
new DiffGraphBuilder(schema)
._addEdge(V0_0, V0_0, 0)
._addEdge(V0_1, V1_0, 0)
)

g.nodes().label.toSet shouldBe Set("V0", "V1")
g.allNodes.size shouldBe 3
g.nodes().size shouldBe 3
g.nodes("V0").size shouldBe 2
g.nodes("V1").size shouldBe 1
g.nodes("V0", "V1").size shouldBe 3
g.nodes("V0", "V1", "V2").size shouldBe 3
g.nodes("V2").size shouldBe 0
}

"basically work with multiple edge and node types" in {
val schema = TestSchema.make(3, 2)
val g = new Graph(schema)
Expand Down

0 comments on commit 4c62caf

Please sign in to comment.