sidnt / lmdz

0 stars 0 forks source link

backing the entire zio stm and tref apis, on lmdb #49

Open sidnt opened 4 years ago

sidnt commented 4 years ago

should be possible. such that whenever the app launches, it has an lmdb store to command in, and whenever it exits, it saves its state into the store. even if it crashes, the state is stored in lmdb, given lmdb's crash proof design.

sidnt commented 4 years ago

how to make this ↓ run on lmdb?

def transfer(receiver: TRef[Int], sender: TRef[Int], much: Int): UIO[Int] =

  STM.atomically {
    for {
      balance <- sender.get
      _       <- STM.check(balance >= much) /*ie dereferencing a non TRef, isn't a problem*/
      _       <- receiver.update(_ + much) /*but each of these for generators, ie, <- */
      _       <- sender.update(_ - much) /*have RT as STM*/
      newAmnt <- receiver.get
    } yield newAmnt
  }

  val action: UIO[Int] =
    for {
      t <- STM.atomically(TRef.make(0).zip(TRef.make(20000)))
      (receiver, sender) = t
      balance <- transfer(receiver, sender, 1000)
    } yield balance