Skip to content

Commit

Permalink
Cleanup and added RecordDocument support
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Apr 7, 2024
1 parent 64c04fd commit caa86b4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 63 deletions.
63 changes: 0 additions & 63 deletions core/src/main/scala/lightdb/Collection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,67 +92,4 @@ object Collection {
def apply[D <: Document[D]](collectionName: String, db: LightDB)(implicit docRW: RW[D]): Collection[D] = new Collection[D](collectionName, db) {
override implicit val rw: RW[D] = docRW
}
}

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 query(value: V): fs2.Stream[IO, D] = {
val io = link(value).map {
case Some(link) => fs2.Stream[IO, Id[D]](link.links: _*)
.evalMap(collection.apply)
case None => fs2.Stream.empty
}
fs2.Stream.force[IO, D](io)
}
}

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
}
10 changes: 10 additions & 0 deletions core/src/main/scala/lightdb/IndexedLink.scala
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
}
60 changes: 60 additions & 0 deletions core/src/main/scala/lightdb/IndexedLinks.scala
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)
}
8 changes: 8 additions & 0 deletions core/src/main/scala/lightdb/RecordDocument.scala
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
}
7 changes: 7 additions & 0 deletions core/src/main/scala/lightdb/RecordDocumentCollection.scala
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())
}

0 comments on commit caa86b4

Please sign in to comment.