-
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.
Better separation of Model and Collection and listener model
- Loading branch information
1 parent
67f89ed
commit 82a5efc
Showing
27 changed files
with
233 additions
and
116 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
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
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
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
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
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
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,13 @@ | ||
package lightdb.model | ||
|
||
sealed trait DocumentAction | ||
|
||
object DocumentAction { | ||
case object PreSet extends DocumentAction | ||
|
||
case object PostSet extends DocumentAction | ||
|
||
case object PreDelete extends DocumentAction | ||
|
||
case object PostDelete extends DocumentAction | ||
} |
45 changes: 45 additions & 0 deletions
45
core/src/main/scala/lightdb/model/DocumentActionSupport.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,45 @@ | ||
package lightdb.model | ||
|
||
import cats.effect.IO | ||
import fabric.Json | ||
import lightdb.{DocLock, Document, Id} | ||
|
||
trait DocumentActionSupport[D <: Document[D]] { | ||
val preSet: DocumentActions[D, D] = DocumentActions[D, D](DocumentAction.PreSet) | ||
val preSetJson: DocumentActions[D, Json] = DocumentActions[D, Json](DocumentAction.PreSet) | ||
val postSet: DocumentActions[D, D] = DocumentActions[D, D](DocumentAction.PostSet) | ||
|
||
val preDeleteId: DocumentActions[D, Id[D]] = DocumentActions[D, Id[D]](DocumentAction.PreDelete) | ||
val preDelete: DocumentActions[D, D] = DocumentActions[D, D](DocumentAction.PreDelete) | ||
val postDelete: DocumentActions[D, D] = DocumentActions[D, D](DocumentAction.PostDelete) | ||
|
||
val commitActions: UnitActions = new UnitActions | ||
val disposeActions: UnitActions = new UnitActions | ||
|
||
protected def doSet(doc: D, | ||
collection: AbstractCollection[D], | ||
set: (Id[D], Json) => IO[Unit]) | ||
(implicit lock: DocLock[D]): IO[D] = preSet.invoke(doc, collection).flatMap { | ||
case Some(doc) => preSetJson.invoke(collection.rw.read(doc), collection).flatMap { | ||
case Some(json) => set(doc._id, json).flatMap { _ => | ||
postSet.invoke(doc, collection).map(_ => doc) | ||
} | ||
case None => IO.pure(doc) | ||
} | ||
case None => IO.pure(doc) | ||
} | ||
|
||
protected def doDelete(id: Id[D], | ||
collection: AbstractCollection[D], | ||
get: Id[D] => IO[D], | ||
delete: Id[D] => IO[Unit]) | ||
(implicit lock: DocLock[D]): IO[Id[D]] = preDeleteId.invoke(id, collection).flatMap { | ||
case Some(id) => get(id).flatMap(doc => preDelete.invoke(doc, collection).flatMap { | ||
case Some(doc) => delete(doc._id).flatMap { _ => | ||
postDelete.invoke(doc, collection).map(_ => doc._id) | ||
} | ||
case None => IO.pure(id) | ||
}) | ||
case None => IO.pure(id) | ||
} | ||
} |
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,27 @@ | ||
package lightdb.model | ||
|
||
import cats.effect.IO | ||
import lightdb.Document | ||
|
||
case class DocumentActions[D <: Document[D], Value](action: DocumentAction) { | ||
private var list = List.empty[DocumentListener[D, Value]] | ||
|
||
def +=(listener: DocumentListener[D, Value]): Unit = add(listener) | ||
|
||
def add(listener: DocumentListener[D, Value]): Unit = synchronized { | ||
list = list ::: List(listener) | ||
} | ||
|
||
private[model] def invoke(value: Value, collection: AbstractCollection[D]): IO[Option[Value]] = { | ||
def recurse(value: Value, list: List[DocumentListener[D, Value]]): IO[Option[Value]] = if (list.isEmpty) { | ||
IO.pure(Some(value)) | ||
} else { | ||
list.head(action, value, collection).flatMap { | ||
case Some(v) => recurse(v, list.tail) | ||
case None => IO.pure(None) | ||
} | ||
} | ||
|
||
recurse(value, list) | ||
} | ||
} |
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,8 @@ | ||
package lightdb.model | ||
|
||
import cats.effect.IO | ||
import lightdb.Document | ||
|
||
trait DocumentListener[D <: Document[D], Value] { | ||
def apply(action: DocumentAction, value: Value, collection: AbstractCollection[D]): IO[Option[Value]] | ||
} |
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
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
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,15 @@ | ||
package lightdb.model | ||
|
||
import cats.effect.IO | ||
|
||
class UnitActions { | ||
private var list = List.empty[() => IO[Unit]] | ||
|
||
def +=(listener: => IO[Unit]): Unit = add(listener) | ||
|
||
def add(listener: => IO[Unit]): Unit = synchronized { | ||
list = list ::: List(() => listener) | ||
} | ||
|
||
private[model] def invoke(): IO[Unit] = list.map(_()).sequence.map(_ => ()) | ||
} |
Oops, something went wrong.