-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of #61. Implement the DI using ZIO and add the interfaces for the basic services
- Loading branch information
Showing
6 changed files
with
201 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
framework/arcane-framework/src/main/scala/services/streaming/VersionedDataGraphBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.sneaksanddata.arcane.framework | ||
package services.streaming | ||
|
||
import services.mssql.MsSqlConnection.VersionedBatch | ||
import services.mssql.given_HasVersion_VersionedBatch | ||
import services.mssql.query.LazyQueryResult.OutputType | ||
import services.streaming.base.{StreamGraphBuilder, StreamLifetimeService, VersionedDataProvider} | ||
|
||
import zio.stream.ZStream | ||
import zio.{ZIO, ZLayer} | ||
|
||
import java.time.Duration | ||
|
||
|
||
class VersionedDataGraphBuilder(versionedDataProvider: VersionedDataProvider[Long, VersionedBatch], | ||
streamLifetimeService: StreamLifetimeService) extends StreamGraphBuilder[OutputType] { | ||
/** | ||
* Builds a stream that reads the changes from the database. | ||
* | ||
* @return The stream that reads the changes from the database. | ||
*/ | ||
def create: ZStream[Any, Throwable, OutputType] = | ||
ZStream.unfoldZIO(versionedDataProvider.firstVersion) { previousVersion => | ||
if streamLifetimeService.cancelled then ZIO.succeed(None) else continueStream(previousVersion) | ||
} | ||
|
||
private def continueStream(previousVersion: Option[Long]): ZIO[Any, Throwable, Some[(OutputType, Option[Long])]] = | ||
versionedDataProvider.requestChanges(previousVersion, Duration.ofDays(1)) map { versionedBatch => | ||
val latestVersion = versionedBatch.getLatestVersion | ||
val (queryResult, _) = versionedBatch | ||
Some(queryResult.read, latestVersion) | ||
} | ||
} | ||
|
||
object VersionedDataGraphBuilder: | ||
val layer: ZLayer[VersionedDataProvider[Long, VersionedBatch] & StreamLifetimeService, Nothing, StreamGraphBuilder[OutputType]] = | ||
ZLayer { | ||
for { | ||
dp <- ZIO.service[VersionedDataProvider[Long, VersionedBatch]] | ||
ls <- ZIO.service[StreamLifetimeService] | ||
} yield new VersionedDataGraphBuilder(dp, ls) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
framework/arcane-framework/src/main/scala/services/streaming/base/StreamGraphBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.sneaksanddata.arcane.framework | ||
package services.streaming.base | ||
|
||
import zio.stream.ZStream | ||
|
||
/** | ||
* A trait that represents a stream graph builder. | ||
* @tparam IncomingType The type of the incoming data. | ||
*/ | ||
trait StreamGraphBuilder[IncomingType] { | ||
|
||
/** | ||
* Creates a ZStream for the stream graph. | ||
* | ||
* @return ZStream (stream source for the stream graph). | ||
*/ | ||
def create: ZStream[Any, Throwable, IncomingType] | ||
} |
29 changes: 29 additions & 0 deletions
29
...ework/arcane-framework/src/main/scala/services/streaming/base/VersionedDataProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.sneaksanddata.arcane.framework | ||
package services.streaming.base | ||
|
||
import zio.Task | ||
|
||
import java.time.Duration | ||
|
||
/** | ||
* Provides a way to get the changes marked with version from a data source. | ||
* @tparam DataVersionType The type of the data version. | ||
* @tparam DataBatchType The type of the data batch. | ||
*/ | ||
trait VersionedDataProvider[DataVersionType, DataBatchType: HasVersion] { | ||
|
||
/** | ||
* Requests the changes from the data source. | ||
* | ||
* @param previousVersion The previous version of the data. | ||
* @param lookBackInterval The interval to look back for changes if the version is empty. | ||
* @return The changes from the data source. | ||
*/ | ||
def requestChanges(previousVersion: Option[DataVersionType], lookBackInterval: Duration): Task[DataBatchType] | ||
|
||
/** | ||
* The first version of the data. | ||
*/ | ||
val firstVersion: Option[DataVersionType] = None | ||
|
||
} |
Oops, something went wrong.