1
- package com .intenthq .hybrid .integrationsV2
1
+ package com .intenthq .action_processor .integrationsV2
2
2
3
- import java .util .concurrent .ConcurrentMap
3
+ import java .nio .charset .StandardCharsets
4
+ import java .util .concurrent .{ConcurrentHashMap , ConcurrentMap }
4
5
5
6
import cats .effect .{IO , Resource }
7
+ import com .intenthq .action_processor .integrations .SourceContext
8
+ import com .intenthq .hybrid .integrationsV2 .ProcessorContext
9
+ import doobie .util .query
6
10
import fs2 .Stream
7
11
8
12
import scala .jdk .CollectionConverters .IteratorHasAsScala
@@ -16,34 +20,66 @@ import scala.jdk.CollectionConverters.IteratorHasAsScala
16
20
* 4. Stream[IO, I] => Stream[IO, O] => Stream[IO, (K,V)] ... Drain ... Stream[IO, (K1,V1)] => Stream[IO, Array[Byte]]
17
21
*/
18
22
23
+ object Aggregate {
24
+ def apply [I , A , AC ](processorContext : ProcessorContext , key : I => Option [A ], counter : I => Long ): fs2.Pipe [IO , I , A ] = sourceStream => {
25
+ Stream
26
+ .resource[IO , ConcurrentMap [String , Long ]](Resource .liftF(IO (repository)))
27
+ .flatMap { _ =>
28
+ sourceStream(processorContext).evalMap { o =>
29
+ if (key(o).nonEmpty) put(o) else IO .unit
30
+ }.drain ++ streamKeyValue.evalMap(i => IO .delay(serializeAggregatedKV(i)))
31
+ }
32
+
33
+ }
34
+ }
35
+ trait Feed [I , A ] {
36
+ def inputStream (processorContext : ProcessorContext ): fs2.Stream [IO , I ]
37
+ def transform (processorContext : ProcessorContext ): fs2.Pipe [IO , I , (A , Long )] = Aggregate ,apply(k = )
38
+ def serialize (a : (A , Long )): Array [Byte ]
39
+
40
+ final def stream (processorContext : ProcessorContext ) =
41
+ inputStream(processorContext)
42
+ .through(transform(processorContext))
43
+ .map(serialize)
44
+ }
19
45
trait Processor {
20
46
def stream (processorContext : ProcessorContext ): fs2.Stream [IO , Array [Byte ]]
21
47
}
22
48
23
- trait Source [I ] { self : Processor =>
24
- protected def sourceStream (processorContext : ProcessorContext ): fs2.Stream [IO , I ]
25
- }
49
+ // trait Source[I] { self: Processor =>
50
+ // protected def sourceStream(processorContext: ProcessorContext): fs2.Stream[IO, I]
51
+ // protected def pipe: fs2.Pipe[IO, I, Array[Byte]] = _.map(serializeSource)
52
+ // def serializeSource(i:I): Array[Byte]
53
+ // }
26
54
27
- trait Sink [O ] { self : Processor =>
28
- def serialize (o2 : O ): Array [ Byte ]
29
- }
55
+ // trait Sink[I, O] { self: Processor =>
56
+ // def serialize(o2: I ): O
57
+ // }
30
58
31
- trait Aggregations [I ] extends Source [ I ] with Sink [( String , Long )] { self : Processor =>
59
+ trait Aggregations [I , NT ] extends Processor {
32
60
33
- val repository : ConcurrentMap [String , Long ]
61
+ lazy val repository : ConcurrentMap [String , Long ] = new ConcurrentHashMap [ String , Long ]()
34
62
35
63
def key (a : I ): String
36
64
def value (a : I ): Long
65
+ def serializeAggregatedKV (a : I ):
66
+
37
67
38
- override def serialize (o2 : (String , Long )): Array [Byte ]
68
+ def whatEveer (processorContext : ProcessorContext ): Stream [IO , (String , Long )] = Stream
69
+ .resource[IO , ConcurrentMap [String , Long ]](Resource .liftF(IO (repository)))
70
+ .flatMap { _ =>
71
+ sourceStream(processorContext).evalMap { o =>
72
+ if (key(o).nonEmpty) put(o) else IO .unit
73
+ }.drain ++ streamKeyValue.evalMap(i => IO .delay(serializeAggregatedKV(i)))
74
+ }
39
75
40
76
override def stream (processorContext : ProcessorContext ): fs2.Stream [IO , Array [Byte ]] =
41
77
Stream
42
78
.resource[IO , ConcurrentMap [String , Long ]](Resource .liftF(IO (repository)))
43
79
.flatMap { _ =>
44
80
sourceStream(processorContext).evalMap { o =>
45
81
if (key(o).nonEmpty) put(o) else IO .unit
46
- }.drain ++ streamKeyValue.evalMap(i => IO .delay(serialize (i)))
82
+ }.drain ++ streamKeyValue.evalMap(i => IO .delay(serializeAggregatedKV (i)))
47
83
}
48
84
49
85
private def put (o : I ): IO [Unit ] =
@@ -63,3 +99,62 @@ trait Aggregations[I] extends Source[I] with Sink[(String, Long)] { self: Proces
63
99
.map(e => (e.getKey, e.getValue))
64
100
65
101
}
102
+
103
+ // object Main {
104
+ // def main(args: Array[String]): Unit = {
105
+ // class A extends Processor with Aggregations[Int] {
106
+ // override protected def sourceStream(processorContext: ProcessorContext): Stream[IO, Int] = fs2.Stream(1, 2, 3, 4)
107
+ // // override val repository: ConcurrentMap[String, Long] = new ConcurrentHashMap[String, Long]()
108
+ // override def key(a: Int): String = a.toString
109
+ // override def value(a: Int): Long = 1L
110
+ // override def serializeAggregatedKV(o2: (String, Long)): Array[Byte] = s"${o2._1},${o2._2}".getBytes(StandardCharsets.UTF_8)
111
+ // }
112
+ // }
113
+ // }
114
+
115
+ object Main2 {
116
+ def main (args : Array [String ]): Unit = {
117
+ class A extends Processor {
118
+ override protected def stream (processorContext : ProcessorContext ): Stream [IO , Array [Byte ]] =
119
+ Stream (1 , 2 , 3 , 4 , 5 ).map(_.toString.getBytes)
120
+ }
121
+ }
122
+ }
123
+
124
+ object Main3 {
125
+ def main (args : Array [String ]): Unit = {
126
+ class A extends Processor with HiveSource [Int ] {
127
+ override protected val driver : String = " Mysql"
128
+ override protected val jdbcUrl : String = " jdbc://blah"
129
+
130
+ override protected def query (processorContext : ProcessorContext ): query.Query0 [Int ] = ???
131
+
132
+ override def serializeRow (o2 : Int ): Array [Byte ] = o2.toString.getBytes
133
+ }
134
+ }
135
+ }
136
+
137
+ S
138
+ I
139
+ O
140
+ Source [S , I , O ] {
141
+ 1 . sourceStream[I ]
142
+ 2 . aggegation : noop MapDB [I , Long ]
143
+ 3 . serialize(I , PartialRow )
144
+ }
145
+
146
+ object Main4 {
147
+ def main (args : Array [String ]): Unit = {
148
+ class A extends Processor with SQLSource [Int ] {
149
+ override protected val driver : String = " Mysql"
150
+ override protected val jdbcUrl : String = " jdbc://blah"
151
+
152
+ override protected def query (processorContext : ProcessorContext ): query.Query0 [Int ] = ???
153
+
154
+ override def serializeRow (o2 : Int ): Array [Byte ] = null
155
+
156
+ override protected def stream (processorContext : ProcessorContext ): Stream [IO , Array [Byte ]] =
157
+
158
+ }
159
+ }
160
+ }
0 commit comments