11package streams
22
3- import org.neo4j.kernel.impl.core.EmbeddedProxySPI
4- import org.neo4j.kernel.impl.core.GraphProperties
53import org.neo4j.kernel.internal.GraphDatabaseAPI
6- import streams.serialization.JSONUtils
7- import streams.service.STREAMS_TOPIC_KEY
84import streams.service.TopicType
95import streams.service.Topics
106import streams.utils.Neo4jUtils
7+ import java.util.Collections
8+ import java.util.concurrent.ConcurrentHashMap
119
12- class StreamsTopicService (private val db : GraphDatabaseAPI ) {
13- private val properties : GraphProperties = db.dependencyResolver.resolveDependency( EmbeddedProxySPI ::class .java).newGraphPropertiesProxy( )
10+ class StreamsTopicService (db : GraphDatabaseAPI ) {
11+ private val log = Neo4jUtils .getLogService(db).getUserLog( StreamsTopicService ::class .java)
1412
15- fun clearAll () { // TODO move to Neo4jUtils#executeInWriteableInstance
16- if (! Neo4jUtils .isWriteableInstance(db)) {
17- return
18- }
19- return db.beginTx().use {
20- val keys = properties.allProperties
21- .filterKeys { it.startsWith(STREAMS_TOPIC_KEY ) }
22- .keys
23- keys.forEach {
24- properties.removeProperty(it)
25- }
26- it.success()
27- }
13+ private val storage = ConcurrentHashMap <TopicType , Any >()
14+
15+ fun clearAll () {
16+ storage.clear()
2817 }
2918
30- fun set (topicType : TopicType , data : Any ) = Neo4jUtils .executeInWriteableInstance(db) {
31- db.beginTx().use {
32- if (properties.hasProperty(topicType.key)) {
33- val topicData = JSONUtils .readValue<Any >(properties.getProperty(topicType.key))
34- val newData = when (topicData) {
35- is Map <* , * > -> topicData + (data as Map <String , Any ?>)
36- is Collection <* > -> topicData + (data as Collection <String >)
37- else -> throw RuntimeException (" Unsupported data $data for topic type $topicType " )
38- }
39- properties.setProperty(topicType.key, JSONUtils .writeValueAsString(newData))
40- } else {
41- properties.setProperty(topicType.key, JSONUtils .writeValueAsString(data))
42- }
43- it.success()
19+ fun set (topicType : TopicType , data : Any ) {
20+ val runtimeException = RuntimeException (" Unsupported data $data for topic type $topicType " )
21+ var oldData = storage[topicType]
22+ oldData = oldData ? : when (data) {
23+ is Map <* , * > -> emptyMap<String , Any ?>()
24+ is Collection <* > -> emptyList<String >()
25+ else -> throw runtimeException
26+ }
27+ val newData = when (oldData) {
28+ is Map <* , * > -> oldData + (data as Map <String , Any ?>)
29+ is Collection <* > -> oldData + (data as Collection <String >)
30+ else -> throw runtimeException
4431 }
32+ storage[topicType] = newData
4533 }
4634
47- fun remove (topicType : TopicType , topic : String ) = Neo4jUtils .executeInWriteableInstance(db) {
48- db.beginTx().use {
49- if (properties.hasProperty(topicType.key)) {
50- val topicData = JSONUtils .readValue<Any >(properties.getProperty(topicType.key))
51- val newData = when (topicData) {
52- is Map <* , * > -> topicData.filterKeys { it.toString() != topic }
53- is Collection <* > -> topicData.filter { it.toString() != topic }
54- else -> throw RuntimeException (" Unsupported data $topicData for topic type $topicType " )
55- }
56- val isEmpty = when (newData) {
57- is Map <* , * > -> newData.isEmpty()
58- is Collection <* > -> newData.isEmpty()
59- else -> throw RuntimeException (" Unsupported data $topicData for topic type $topicType " )
60- }
61- if (isEmpty) {
62- properties.removeProperty(topicType.key)
63- } else {
64- properties.setProperty(topicType.key, JSONUtils .writeValueAsString(newData))
65- }
66- }
67- it.success()
35+ fun remove (topicType : TopicType , topic : String ) {
36+ val topicData = storage[topicType] ? : return
37+
38+ val runtimeException = RuntimeException (" Unsupported data $topicData for topic type $topicType " )
39+ val filteredData = when (topicData) {
40+ is Map <* , * > -> topicData.filterKeys { it.toString() != topic }
41+ is Collection <* > -> topicData.filter { it.toString() != topic }
42+ else -> throw runtimeException
6843 }
44+
45+ storage[topicType] = filteredData
6946 }
7047
71- fun getTopicType (topic : String ) = Neo4jUtils .executeInWriteableInstance(db) {
72- db.beginTx().use {
73- TopicType .values().find {
74- if (! properties.hasProperty(it.key)) {
75- false
76- } else {
77- val data = JSONUtils .readValue<Any >(properties.getProperty(it.key))
78- when (data) {
79- is Map <* , * > -> data.containsKey(topic)
80- is Collection <* > -> data.contains(topic)
81- else -> false
82- }
48+ fun getTopicType (topic : String ) = TopicType .values()
49+ .find {
50+ val topicData = storage[it]
51+ when (topicData) {
52+ is Map <* , * > -> topicData.containsKey(topic)
53+ is Collection <* > -> topicData.contains(topic)
54+ else -> false
8355 }
8456 }
85- }
86- }
8757
88- fun getTopics () = db.beginTx().use {
89- TopicType .values()
90- .filter { properties.hasProperty(it.key) }
91- .flatMap {
92- val data = JSONUtils .readValue<Any >(properties.getProperty(it.key))
93- when (data) {
94- is Map <* , * > -> data.keys
95- is Collection <* > -> data.toSet()
96- else -> emptySet()
97- }
98- }.toSet() as Set <String >
99- }
58+ fun getTopics () = TopicType .values()
59+ .flatMap {
60+ val data = storage[it]
61+ when (data) {
62+ is Map <* , * > -> data.keys
63+ is Collection <* > -> data.toSet()
64+ else -> emptySet<String >()
65+ }
66+ }.toSet() as Set <String >
10067
10168 fun setAll (topics : Topics ) {
10269 topics.asMap().forEach { topicType, data ->
10370 set(topicType, data)
10471 }
10572 }
10673
107- fun getCypherTemplate (topic : String ) = db.beginTx().use {
108- if (properties.hasProperty(TopicType .CYPHER .key)) {
109- val data = JSONUtils .readValue<Map <String , String >>(properties.getProperty(TopicType .CYPHER .key))
110- data[topic]
111- } else {
112- null
113- }
114- }
74+ fun getCypherTemplate (topic : String ) = (storage.getOrDefault(TopicType .CYPHER , emptyMap<String , String >()) as Map <String , String >)
75+ .let { it[topic] }
11576
116- fun getAll () = db.beginTx().use {
117- TopicType .values()
118- .filter { properties.hasProperty(it.key) }
119- .map { it to JSONUtils .readValue<Any >(properties.getProperty(it.key)) }
120- .toMap()
121- }
77+ fun getAll (): Map <TopicType , Any > = Collections .unmodifiableMap(storage)
12278
12379}
0 commit comments