Created
December 4, 2019 09:53
-
-
Save thobson/907064fde2b0d013d2f30c4ad97a03b0 to your computer and use it in GitHub Desktop.
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
class TableManager[F[_]](guard: Semaphore[F],)(implicit F: Sync[F], TableOps: TableOps[F]) { | |
// this feels hacky | |
private val state = mutable.Map.empty[String, Table] | |
def tableFor(name: String, createIfMissing: Boolean): F[Table] = guard.withPermit { | |
state.get(name) match { | |
case Some(table) => table.pure[F] | |
case None => for { | |
newTable <- TableOps.open(buildPath(name), createIfMissing) | |
_ <- F.delay(state += (name -> newTable)) | |
} yield newTable | |
} | |
} | |
def dropTable(name: String): F[Unit] = guard.withPermit { ... } | |
} | |
object TableManager { | |
import cats.syntax.functor._ | |
def apply[F[_] : Sync : TableOps]: F[TableManager[F]] = { | |
Semaphore[F](1).flatMap(s => F.delay(new TableManager[F](s))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment