-
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.
Cleanup and added RecordDocument support
- Loading branch information
1 parent
64c04fd
commit caa86b4
Showing
5 changed files
with
85 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package lightdb | ||
|
||
import fabric.rw.RW | ||
|
||
case class IndexedLink[D <: Document[D]](_id: Id[IndexedLink[D]], | ||
links: List[Id[D]]) extends Document[IndexedLink[D]] | ||
|
||
object IndexedLink { | ||
implicit def rw[D <: Document[D]]: RW[IndexedLink[D]] = RW.gen | ||
} |
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,60 @@ | ||
package lightdb | ||
|
||
import cats.effect.IO | ||
|
||
case class IndexedLinks[V, D <: Document[D]](createKey: V => String, | ||
createV: D => V, | ||
store: Store, | ||
collection: Collection[D]) { | ||
protected[lightdb] def add(doc: D): IO[Unit] = { | ||
val v = createV(doc) | ||
for { | ||
existing <- link(v) | ||
updated = existing match { | ||
case Some(l) => l.copy(links = l.links ::: List(doc._id)) | ||
case None => | ||
val key = createKey(v) | ||
val id = Id[IndexedLink[D]](key) | ||
IndexedLink(_id = id, links = List(doc._id)) | ||
} | ||
_ <- store.putJson(updated) | ||
} yield () | ||
} | ||
|
||
protected[lightdb] def remove(doc: D): IO[Unit] = { | ||
val v = createV(doc) | ||
for { | ||
existing <- link(v) | ||
updated = existing match { | ||
case Some(l) => | ||
val updatedLinks = l.links.filterNot(_ == doc._id) | ||
if (updatedLinks.isEmpty) { | ||
None | ||
} else { | ||
Some(l.copy(links = updatedLinks)) | ||
} | ||
case None => None | ||
} | ||
_ <- updated match { | ||
case Some(l) => store.putJson(l) | ||
case None => IO.unit | ||
} | ||
} yield () | ||
} | ||
|
||
protected[lightdb] def link(value: V): IO[Option[IndexedLink[D]]] = { | ||
val key = createKey(value) | ||
val id = Id[IndexedLink[D]](key) | ||
store.getJson(id) | ||
} | ||
|
||
def queryIds(value: V): fs2.Stream[IO, Id[D]] = { | ||
val io = link(value).map { | ||
case Some(link) => fs2.Stream[IO, Id[D]](link.links: _*) | ||
case None => fs2.Stream.empty | ||
} | ||
fs2.Stream.force[IO, Id[D]](io) | ||
} | ||
|
||
def query(value: V): fs2.Stream[IO, D] = queryIds(value).evalMap(collection.apply) | ||
} |
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 | ||
|
||
trait RecordDocument[D <: RecordDocument[D]] extends Document[D] { | ||
def created: Long | ||
def modified: Long | ||
|
||
def modify(): D | ||
} |
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,7 @@ | ||
package lightdb | ||
|
||
import cats.effect.IO | ||
|
||
abstract class RecordDocumentCollection[D <: RecordDocument[D]](collectionName: String, db: LightDB) extends Collection[D](collectionName, db) { | ||
override protected def preSet(doc: D): IO[D] = super.preSet(doc.modify()) | ||
} |