@@ -2,17 +2,18 @@ package streams
22
33import org.neo4j.graphdb.Label
44import org.neo4j.kernel.internal.GraphDatabaseAPI
5+ import streams.extensions.isSystemDb
56import streams.serialization.JSONUtils
67import streams.service.STREAMS_TOPIC_KEY
78import streams.service.TopicType
89import streams.service.Topics
910import streams.utils.Neo4jUtils
1011import java.lang.IllegalArgumentException
1112
12- class StreamsTopicService (private val db : GraphDatabaseAPI ) {
13+ class StreamsTopicService (private val systemDb : GraphDatabaseAPI ) {
1314
1415 init {
15- if (db.databaseName() != Neo4jUtils . SYSTEM_DATABASE_NAME ) {
16+ if (! systemDb.isSystemDb() ) {
1617 throw IllegalArgumentException (" GraphDatabaseAPI must be an instance of ${Neo4jUtils .SYSTEM_DATABASE_NAME } database" )
1718 }
1819 }
@@ -24,18 +25,18 @@ class StreamsTopicService(private val db: GraphDatabaseAPI) {
2425 }
2526
2627 fun clearAll () { // TODO move to Neo4jUtils#executeInWriteableInstance
27- if (! Neo4jUtils .isWriteableInstance(db )) {
28+ if (! Neo4jUtils .isWriteableInstance(systemDb )) {
2829 return
2930 }
30- return db .beginTx().use {
31+ return systemDb .beginTx().use {
3132 it.findNodes(Label .label(STREAMS_TOPIC_KEY ))
3233 .forEach { it.delete() }
3334 it.commit()
3435 }
3536 }
3637
37- fun set (topicType : TopicType , data : Any ) = Neo4jUtils .executeInWriteableInstance(db ) {
38- db .beginTx().use {
38+ fun set (topicType : TopicType , data : Any ) = Neo4jUtils .executeInWriteableInstance(systemDb ) {
39+ systemDb .beginTx().use {
3940 val topicTypeLabel = Label .label(topicType.key)
4041 val findNodes = it.findNodes(topicTypeLabel)
4142 val node = if (findNodes.hasNext()) {
@@ -65,8 +66,8 @@ class StreamsTopicService(private val db: GraphDatabaseAPI) {
6566 }
6667 }
6768
68- fun remove (topicType : TopicType , topic : String ) = Neo4jUtils .executeInWriteableInstance(db ) {
69- db .beginTx().use {
69+ fun remove (topicType : TopicType , topic : String ) = Neo4jUtils .executeInWriteableInstance(systemDb ) {
70+ systemDb .beginTx().use {
7071 val topicTypeLabel = Label .label(topicType.key)
7172 val findNodes = it.findNodes(topicTypeLabel)
7273 val node = if (findNodes.hasNext()) {
@@ -93,8 +94,8 @@ class StreamsTopicService(private val db: GraphDatabaseAPI) {
9394 }
9495 }
9596
96- fun getTopicType (topic : String ) = Neo4jUtils .executeInWriteableInstance(db ) {
97- db .beginTx().use { tx ->
97+ fun getTopicType (topic : String ) = Neo4jUtils .executeInWriteableInstance(systemDb ) {
98+ systemDb .beginTx().use { tx ->
9899 TopicType .values()
99100 .find {
100101 val topicTypeLabel = Label .label(it.key)
@@ -114,7 +115,7 @@ class StreamsTopicService(private val db: GraphDatabaseAPI) {
114115 }
115116 }
116117
117- fun getTopics () = db .beginTx().use { tx ->
118+ fun getTopics () = systemDb .beginTx().use { tx ->
118119 TopicType .values()
119120 .flatMap {
120121 val topicTypeLabel = Label .label(it.key)
@@ -138,8 +139,8 @@ class StreamsTopicService(private val db: GraphDatabaseAPI) {
138139 }
139140 }
140141
141- fun getCypherTemplate (topic : String ) = db .beginTx().use {
142- db .beginTx().use {
142+ fun getCypherTemplate (topic : String ) = systemDb .beginTx().use {
143+ systemDb .beginTx().use {
143144 val topicTypeLabel = Label .label(TopicType .CYPHER .key)
144145 val findNodes = it.findNodes(topicTypeLabel)
145146 if (! findNodes.hasNext()) {
@@ -156,7 +157,7 @@ class StreamsTopicService(private val db: GraphDatabaseAPI) {
156157 }
157158 }
158159
159- fun getAll () = db .beginTx().use { tx ->
160+ fun getAll () = systemDb .beginTx().use { tx ->
160161 TopicType .values()
161162 .mapNotNull {
162163 val topicTypeLabel = Label .label(it.key)
0 commit comments