Skip to content

Commit

Permalink
Added SQLIndexedField.IN
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Apr 26, 2024
1 parent c8108ac commit 9ecf8d4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion core/src/main/scala/lightdb/LightDB.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lightdb

import cats.effect.IO
import cats.implicits.{catsSyntaxApplicativeByName, catsSyntaxParallelSequence1}
import cats.implicits.{catsSyntaxApplicativeByName, catsSyntaxParallelSequence1, toTraverseOps}
import fabric.rw._
import lightdb.upgrade.DatabaseUpgrade

Expand All @@ -23,6 +23,8 @@ abstract class LightDB(val directory: Path) {
def collections: List[Collection[_]]
def upgrades: List[DatabaseUpgrade]

def commit(): IO[Unit] = collections.map(_.commit()).sequence.map(_ => ())

protected[lightdb] def verifyInitialized(): Unit = if (!initialized) throw new RuntimeException("Database not initialized!")

def init(truncate: Boolean = false): IO[Unit] = if (_initialized.compareAndSet(false, true)) {
Expand Down
14 changes: 13 additions & 1 deletion core/src/main/scala/lightdb/query/Query.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ case class Query[D <: Document[D]](indexSupport: IndexSupport[D],
sort: List[Sort] = Nil,
scoreDocs: Boolean = false,
pageSize: Int = 1_000,
countTotal: Boolean = false) {
countTotal: Boolean = true) {
def filter(filter: Filter[D]): Query[D] = copy(filter = Some(filter))
def sort(sort: Sort*): Query[D] = copy(sort = this.sort ::: sort.toList)
def clearSort: Query[D] = copy(sort = Nil)
Expand All @@ -34,4 +34,16 @@ case class Query[D <: Document[D]](indexSupport: IndexSupport[D],
}
def idStream(implicit context: SearchContext[D]): fs2.Stream[IO, Id[D]] = pageStream.flatMap(_.idStream)
def stream(implicit context: SearchContext[D]): fs2.Stream[IO, D] = pageStream.flatMap(_.stream)

def toIdList: IO[List[Id[D]]] = indexSupport.withSearchContext { implicit context =>
idStream.compile.toList
}

def toList: IO[List[D]] = indexSupport.withSearchContext { implicit context =>
stream.compile.toList
}

def count: IO[Int] = indexSupport.withSearchContext { implicit context =>
idStream.compile.count.map(_.toInt)
}
}
2 changes: 2 additions & 0 deletions sqlite/src/main/scala/lightdb/sqlite/SQLIndexedField.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ case class SQLIndexedField[F, D <: Document[D]](fieldName: String,
def is(value: F): Filter[D] = SQLFilter[D](s"$fieldName = ?", List(value))

def between(v1: F, v2: F): Filter[D] = SQLFilter[D](s"$fieldName BETWEEN ? AND ?", List(v1, v2))

def IN(values: Seq[F]): Filter[D] = SQLFilter[D](s"$fieldName IN (${values.map(_ => "?").mkString(", ")})", values.toList)
}

0 comments on commit 9ecf8d4

Please sign in to comment.