Skip to content

Instantly share code, notes, and snippets.

@thobson
Created December 4, 2019 09:53
Show Gist options
  • Save thobson/907064fde2b0d013d2f30c4ad97a03b0 to your computer and use it in GitHub Desktop.
Save thobson/907064fde2b0d013d2f30c4ad97a03b0 to your computer and use it in GitHub Desktop.
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