Skip to content

Commit

Permalink
Update DAS SDK Scala to 0.1.2 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelbranco80 authored Sep 25, 2024
1 parent e8b7831 commit 64007bf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
3 changes: 1 addition & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ lazy val root = (project in file("."))
strictBuildSettings,
publishSettings,
libraryDependencies ++= Seq(
"com.raw-labs" %% "protocol-das" % "0.1.2" % "compile->compile;test->test",
"com.raw-labs" %% "das-sdk-scala" % "0.1.0" % "compile->compile;test->test"
"com.raw-labs" %% "das-sdk-scala" % "0.1.2" % "compile->compile;test->test"
)
)
51 changes: 34 additions & 17 deletions src/main/scala/com/rawlabs/das/server/DASSdkManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ object DASSdkManager {

// TODO (msb): Remove if NOT USED since M hours AND/OR does not exist in creds if it came from creds

private case class DaSDKInMemoryEntry(options: Map[String, String], dasSdk: DASSdk)

/**
* Manages the lifecycle of Data Access Services (DAS) in the server.
*
Expand All @@ -38,7 +40,7 @@ class DASSdkManager(implicit settings: RawSettings) extends StrictLogging {

private val dasSdkLoader = ServiceLoader.load(classOf[DASSdkBuilder]).asScala

private val dasSdksInMemory = mutable.HashMap[DASId, DASSdk]()
private val dasSdksInMemory = mutable.HashMap[DASId, DaSDKInMemoryEntry]()
private val dasSdksInMemoryLock = new Object

// At startup, read any available DAS configurations from the local config file and register them.
Expand All @@ -55,21 +57,30 @@ class DASSdkManager(implicit settings: RawSettings) extends StrictLogging {
def registerDAS(dasType: String, options: Map[String, String], maybeDasId: Option[DASId] = None): DASId = {
dasSdksInMemoryLock.synchronized {
val dasId = maybeDasId.getOrElse(DASId.newBuilder().setId(java.util.UUID.randomUUID().toString).build())
if (dasSdksInMemory.contains(dasId)) {
logger.error(s"DAS with ID $dasId is already registered.")
throw new IllegalArgumentException(s"DAS with id $dasId already registered")
dasSdksInMemory.get(dasId) match {
case Some(DaSDKInMemoryEntry(inMemoryOptions, _)) =>
if (compareOptions(inMemoryOptions, options)) {
logger.warn(s"DAS with ID $dasId is already registered with the same options.")
return dasId
} else {
logger.error(
s"DAS with ID $dasId is already registered. Registered options are: $inMemoryOptions and new options are: $options"
)
throw new IllegalArgumentException(s"DAS with id $dasId already registered")
}
case None =>
logger.debug(s"Registering DAS with ID: $dasId, Type: $dasType")
val dasSdk = dasSdkLoader
.find(_.dasType == dasType)
.getOrElse {
logger.error(s"DAS type '$dasType' not supported.")
throw new IllegalArgumentException(s"DAS type '$dasType' not supported")
}
.build(options)
dasSdksInMemory.put(dasId, DaSDKInMemoryEntry(options, dasSdk))
logger.debug(s"DAS registered successfully with ID: $dasId")
dasId
}
logger.debug(s"Registering DAS with ID: $dasId, Type: $dasType")
val dasSdk = dasSdkLoader
.find(_.dasType == dasType)
.getOrElse {
logger.error(s"DAS type '$dasType' not supported.")
throw new IllegalArgumentException(s"DAS type '$dasType' not supported")
}
.build(options)
dasSdksInMemory.put(dasId, dasSdk)
logger.debug(s"DAS registered successfully with ID: $dasId")
dasId
}
}

Expand Down Expand Up @@ -103,7 +114,7 @@ class DASSdkManager(implicit settings: RawSettings) extends StrictLogging {
dasId,
getDASFromRemote(dasId).getOrElse(throw new IllegalArgumentException(s"DAS not found: $dasId"))
)
}
}.dasSdk
}

/**
Expand Down Expand Up @@ -169,8 +180,14 @@ class DASSdkManager(implicit settings: RawSettings) extends StrictLogging {
* @param dasId The DAS ID to retrieve.
* @return The DAS instance.
*/
private def getDASFromRemote(dasId: DASId): Option[DASSdk] = {
private def getDASFromRemote(dasId: DASId): Option[DaSDKInMemoryEntry] = {
None
}

// Compare options to determine if two DAS instances are the same.
// Ignore options that start with "das_" as they are internal to the DAS SDK.
private def compareOptions(options1: Map[String, String], options2: Map[String, String]): Boolean = {
options1.filterKeys(!_.startsWith("das_")) == options2.filterKeys(!_.startsWith("das_"))
}

}

0 comments on commit 64007bf

Please sign in to comment.