Skip to content

Commit 5f242b7

Browse files
authored
Bump odb, misc changes (#220)
* add generated schema-aware diffgraphbuilder. Add marker trait StaticType[+T] to AbstractNode, for ad-hoc subtyping. Generate nodestarters. Generate traversal variant of _astOut et al.
1 parent 10d0e22 commit 5f242b7

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

codegen/src/main/scala/overflowdb/codegen/CodeGen.scala

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ class CodeGen(schema: Schema) {
7575
}.mkString("")
7676
}
7777

78+
val starters = mutable.ArrayBuffer[String]()
79+
for(typ <- schema.nodeTypes if typ.starterName.isDefined){
80+
starters.append(
81+
s"""@overflowdb.traversal.help.Doc(info = "All nodes of type ${typ.className}, i.e. with label ${typ.name}")
82+
|def ${typ.starterName.get}: Iterator[nodes.${typ.className}] = overflowdb.traversal.InitialTraversal.from[nodes.${typ.className}](wrapper.graph, "${typ.name}")""".stripMargin)
83+
}
84+
for (typ <- schema.nodeBaseTypes if typ.starterName.isDefined) {
85+
val types = schema.nodeTypes.filter{_.extendzRecursively.contains(typ)}
86+
starters.append(
87+
s"""@overflowdb.traversal.help.Doc(info = "All nodes of type ${typ.className}, i.e. with label in ${types.map{_.name}.sorted.mkString(", ")}")
88+
|def ${typ.starterName.get}: Iterator[nodes.${typ.className}] = wrapper.graph.nodes(${types.map{concrete => "\"" + concrete.name + "\""}.mkString(", ") }).asScala.asInstanceOf[Iterator[nodes.${typ.className}]]""".stripMargin)
89+
}
7890
val domainMain = baseDir.createChild(s"$domainShortName.scala").write(
7991
s"""package $basePackage
8092
|
@@ -143,6 +155,26 @@ class CodeGen(schema: Schema) {
143155
| String.format("$domainShortName (%s)", graph)
144156
|}
145157
|
158+
|class GeneratedNodeStarterExt(val wrapper: ${domainShortName}) extends AnyVal {
159+
|import scala.jdk.CollectionConverters.IteratorHasAsScala
160+
|${starters.mkString("\n\n")}
161+
|}
162+
|
163+
|/**
164+
| * Domain-specific version of diffgraph builder. This is to allow schema checking before diffgraph application
165+
| * in the future, as well as a schema-aware point for providing backwards compatibility in odbv2.
166+
| */
167+
|class DiffGraphBuilder extends overflowdb.BatchedUpdate.DiffGraphBuilder {
168+
| override def absorb(other: overflowdb.BatchedUpdate.DiffGraphBuilder): this.type = {super.absorb(other); this}
169+
| override def addNode(node: overflowdb.DetachedNodeData): this.type = {super.addNode(node); this}
170+
| override def addNode(label: String, keyvalues:Any*): this.type = {super.addNode(label, keyvalues:_*); this}
171+
| override def addEdge(src: overflowdb.NodeOrDetachedNode, dst: overflowdb.NodeOrDetachedNode, label: String): this.type = {super.addEdge(src, dst, label); this}
172+
| override def addEdge(src: overflowdb.NodeOrDetachedNode, dst: overflowdb.NodeOrDetachedNode, label: String, properties: Any*): this.type = {super.addEdge(src, dst, label, properties:_*); this}
173+
| override def setNodeProperty(node: overflowdb.Node, label: String, property: Any): this.type = {super.setNodeProperty(node, label, property); this}
174+
| override def removeNode(node: overflowdb.Node): this.type = {super.removeNode(node); this}
175+
| override def removeEdge(edge: overflowdb.Edge): this.type = {super.removeEdge(edge); this}
176+
|}
177+
|
146178
|""".stripMargin
147179
)
148180
results.append(domainMain)
@@ -387,8 +419,10 @@ class CodeGen(schema: Schema) {
387419
| def isRegex(pattern: String): Boolean = pattern.exists(reChars.contains(_))
388420
|}
389421
|
422+
|trait StaticType[+T]
423+
|
390424
|/** Abstract supertype for overflowdb.Node and NewNode */
391-
|trait AbstractNode extends overflowdb.NodeOrDetachedNode {
425+
|trait AbstractNode extends overflowdb.NodeOrDetachedNode with StaticType[AnyRef]{
392426
| def label: String
393427
|}
394428
|
@@ -1199,29 +1233,44 @@ class CodeGen(schema: Schema) {
11991233

12001234
protected def writeNodeTraversalFiles(outputDir: File): Seq[File] = {
12011235
lazy val nodeTraversalImplicits = {
1202-
def implicitForNodeType(name: String) = {
1236+
def implicitForNodeType(name: String): String = {
12031237
val traversalName = s"${name}TraversalExtGen"
1204-
s"implicit def to$traversalName[NodeType <: $name](trav: IterableOnce[NodeType]): ${traversalName}[NodeType] = new $traversalName(trav)"
1238+
s"implicit def to$traversalName[NodeType <: $name](trav: IterableOnce[NodeType]): ${traversalName}[NodeType] = new $traversalName(trav.iterator)"
12051239
}
12061240

12071241
val implicitsForNodeTraversals =
12081242
schema.nodeTypes.map(_.className).sorted.map(implicitForNodeType).mkString(lineSeparator)
12091243

12101244
val implicitsForNodeBaseTypeTraversals =
12111245
schema.nodeBaseTypes.map(_.className).sorted.map(implicitForNodeType).mkString(lineSeparator)
1246+
val implicitsForAnyNodeTraversals = implicitForNodeType("StoredNode")
1247+
//todo: Relocate to specific file?
1248+
val edgeExt = schema.edgeTypes.map{et =>
1249+
val acc = s"${Helpers.camelCase(et.name)}"
1250+
s"""def _${acc}Out: Iterator[StoredNode] = traversal.flatMap{_._${acc}Out}
1251+
|def _${acc}In: Iterator[StoredNode] = traversal.flatMap{_._${acc}In} """.stripMargin
1252+
}.mkString("\n")
1253+
val anyNodeExtClass =
1254+
s"""class StoredNodeTraversalExtGen[NodeType <: StoredNode](val traversal: Iterator[NodeType]) extends AnyVal {
1255+
| ${edgeExt}
1256+
|}""".stripMargin
12121257

12131258
s"""package $traversalsPackage
12141259
|
12151260
|import $nodesPackage._
12161261
|
12171262
|trait NodeTraversalImplicits extends NodeBaseTypeTraversalImplicits {
1263+
| $implicitsForAnyNodeTraversals
1264+
|
12181265
| $implicitsForNodeTraversals
12191266
|}
12201267
|
12211268
|// lower priority implicits for base types
12221269
|trait NodeBaseTypeTraversalImplicits extends overflowdb.traversal.Implicits {
12231270
| $implicitsForNodeBaseTypeTraversals
12241271
|}
1272+
|
1273+
|$anyNodeExtClass
12251274
|""".stripMargin
12261275
}
12271276

@@ -1598,7 +1647,7 @@ class CodeGen(schema: Schema) {
15981647
|import $nodesPackage._
15991648
|
16001649
|/** Traversal steps for $className */
1601-
|class ${className}TraversalExtGen[NodeType <: $className](val traversal: IterableOnce[NodeType]) extends AnyVal {
1650+
|class ${className}TraversalExtGen[NodeType <: $className](val traversal: Iterator[NodeType]) extends AnyVal {
16021651
|
16031652
|${customStepNameTraversals.mkString(System.lineSeparator)}
16041653
|

codegen/src/main/scala/overflowdb/schema/Schema.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ abstract class AbstractNodeType(val name: String, val comment: Option[String], v
5050
def subtypes(allNodes: Set[AbstractNodeType]): Set[AbstractNodeType]
5151

5252

53+
private var _starterName: Option[String] = Some(camelCase(name))
54+
55+
/** the name for the generated node starter. Custom names can be assigned to prevent compile errors for e.g. `type`.
56+
* Generation of node-starter can be suppressed by passing null, or by calling `withoutStarter()`.*/
57+
def starterName: Option[String] = _starterName
58+
def starterName(name:String): this.type = {this._starterName = Option(name); this}
59+
def withoutStarter(): this.type = starterName(null)
5360
/** properties (including potentially inherited properties) */
5461
override def properties: Seq[Property[_]] = {
5562
val entireClassHierarchy = this +: extendzRecursively

project/Build.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import sbt._
22
import com.lucidchart.sbtcross.BaseProject
33

44
object Versions {
5-
val overflowdb = "1.174"
5+
val overflowdb = "1.177"
66
val scala_2_12 = "2.12.17"
77
val scala_2_13 = "2.13.10"
88
val scala_3 = "3.3.0"

0 commit comments

Comments
 (0)