Skip to content

Commit

Permalink
Minor cleanup and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Apr 16, 2024
1 parent 7d1f8f8 commit f3f42b8
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import cats.effect.IO
import cats.effect.testing.scalatest.AsyncIOSpec
import fabric.rw._
import lightdb._
import lightdb.halo.HaloDBSupport
import lightdb.lucene.LuceneSupport
import lightdb.lucene.index.{IntField, StringField}
import lightdb.query._
import lightdb.sqlite.{SQLIndexedField, SQLiteSupport}
import lightdb.upgrade.DatabaseUpgrade
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AsyncWordSpec
import scribe.{Level, Logger}

import java.nio.file.Paths

class SimpleSpec extends AsyncWordSpec with AsyncIOSpec with Matchers {
class SimpleHaloAndLuceneSpec extends AsyncWordSpec with AsyncIOSpec with Matchers {
private val id1 = Id[Person]("john")
private val id2 = Id[Person]("jane")

Expand Down Expand Up @@ -68,6 +69,7 @@ class SimpleSpec extends AsyncWordSpec with AsyncIOSpec with Matchers {
Person.withSearchContext { implicit context =>
Person
.query
.countTotal(true)
.filter(Person.name.is("Jane Doe"))
.search()
.flatMap { page =>
Expand Down Expand Up @@ -122,7 +124,7 @@ class SimpleSpec extends AsyncWordSpec with AsyncIOSpec with Matchers {
}
"do paginated search" in {
Person.withSearchContext { implicit context =>
Person.query.pageSize(1).search().flatMap { page1 =>
Person.query.pageSize(1).countTotal(true).search().flatMap { page1 =>
page1.page should be(0)
page1.pages should be(2)
page1.hasNext should be(true)
Expand All @@ -144,7 +146,7 @@ class SimpleSpec extends AsyncWordSpec with AsyncIOSpec with Matchers {
}
"do paginated search as a stream" in {
Person.withSearchContext { implicit context =>
Person.query.pageSize(1).stream.compile.toList.map { people =>
Person.query.pageSize(1).countTotal(true).stream.compile.toList.map { people =>
people.length should be(2)
people.map(_.name).toSet should be(Set("John Doe", "Jane Doe"))
}
Expand Down Expand Up @@ -211,7 +213,7 @@ class SimpleSpec extends AsyncWordSpec with AsyncIOSpec with Matchers {
}
}

object DB extends LightDB(directory = Paths.get("testdb")) {
object DB extends LightDB(directory = Paths.get("testdb")) with HaloDBSupport {
// override protected def autoCommit: Boolean = true

val startTime: StoredValue[Long] = stored[Long]("startTime", -1L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cats.effect.IO
import cats.effect.testing.scalatest.AsyncIOSpec
import fabric.rw._
import lightdb._
import lightdb.halo.HaloDBSupport
import lightdb.sqlite.{SQLIndexedField, SQLiteSupport}
import lightdb.upgrade.DatabaseUpgrade
import org.scalatest.matchers.should.Matchers
Expand All @@ -12,7 +13,7 @@ import scribe.{Level, Logger}

import java.nio.file.Paths

class SimpleSpec extends AsyncWordSpec with AsyncIOSpec with Matchers {
class SimpleHaloAndSQLiteSpec extends AsyncWordSpec with AsyncIOSpec with Matchers {
private val id1 = Id[Person]("john")
private val id2 = Id[Person]("jane")

Expand Down Expand Up @@ -102,23 +103,23 @@ class SimpleSpec extends AsyncWordSpec with AsyncIOSpec with Matchers {
person.age should be(21)
}
}
// "search for age range" in {
// Person.withSearchContext { implicit context =>
// Person
// .query
// .filter(Person.age.between(19, 21))
// .search()
// .flatMap { results =>
// results.docs.map { people =>
// people.length should be(2)
// val names = people.map(_.name).toSet
// names should be(Set("John Doe", "Jane Doe"))
// val ages = people.map(_.age).toSet
// ages should be(Set(21, 19))
// }
// }
// }
// }
"search for age range" in {
Person.withSearchContext { implicit context =>
Person
.query
.filter(Person.age.between(19, 21))
.search()
.flatMap { results =>
results.docs.map { people =>
people.length should be(2)
val names = people.map(_.name).toSet
names should be(Set("John Doe", "Jane Doe"))
val ages = people.map(_.age).toSet
ages should be(Set(21, 19))
}
}
}
}
"do paginated search" in {
Person.withSearchContext { implicit context =>
Person.query.pageSize(1).countTotal(true).search().flatMap { page1 =>
Expand Down Expand Up @@ -210,7 +211,7 @@ class SimpleSpec extends AsyncWordSpec with AsyncIOSpec with Matchers {
}
}

object DB extends LightDB(directory = Paths.get("testdb")) {
object DB extends LightDB(directory = Paths.get("testdb")) with HaloDBSupport {
// override protected def autoCommit: Boolean = true

val startTime: StoredValue[Long] = stored[Long]("startTime", -1L)
Expand Down
2 changes: 1 addition & 1 deletion sqlite/src/main/scala/lightdb/sqlite/SQLFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package lightdb.sqlite
import lightdb.Document
import lightdb.query.Filter

case class SQLFilter[F, D <: Document[D]](fieldName: String, condition: String, value: F) extends Filter[D]
case class SQLFilter[D <: Document[D]](sql: String, args: List[Any]) extends Filter[D] with SQLPart
4 changes: 3 additions & 1 deletion sqlite/src/main/scala/lightdb/sqlite/SQLIndexedField.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ case class SQLIndexedField[F, D <: Document[D]](fieldName: String,
get: D => Option[F]) extends IndexedField[F, D] {
def ===(value: F): Filter[D] = is(value)

def is(value: F): Filter[D] = SQLFilter[F, D](fieldName, "=", value)
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))
}
7 changes: 7 additions & 0 deletions sqlite/src/main/scala/lightdb/sqlite/SQLPart.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package lightdb.sqlite

trait SQLPart {
def sql: String

def args: List[Any]
}
14 changes: 6 additions & 8 deletions sqlite/src/main/scala/lightdb/sqlite/SQLiteSupport.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package lightdb.sqlite

import cats.effect.IO
import fabric.io.{JsonFormatter, JsonParser}
import fabric.rw.{Asable, Convertible}
import lightdb.{Document, Id}
import lightdb.index.{IndexSupport, IndexedField}
import lightdb.query.{PagedResults, Query, SearchContext}
Expand Down Expand Up @@ -61,9 +59,9 @@ trait SQLiteSupport[D <: Document[D]] extends IndexSupport[D] {
var params = List.empty[Option[Any]]
val filters = query.filter match {
case Some(f) =>
val filter = f.asInstanceOf[SQLFilter[_, D]]
params = Some(filter.value) :: params
s"WHERE\n ${filter.fieldName} ${filter.condition} ?"
val filter = f.asInstanceOf[SQLPart]
params = params ::: filter.args.map(Option.apply)
s"WHERE\n ${filter.sql}"
case None => ""
}
val total = if (query.countTotal) {
Expand All @@ -73,7 +71,7 @@ trait SQLiteSupport[D <: Document[D]] extends IndexSupport[D] {
| $collectionName
|$filters
|""".stripMargin
val countPs = prepare(sqlCount, params.reverse)
val countPs = prepare(sqlCount, params)
try {
val rs = countPs.executeQuery()
rs.getInt(1)
Expand All @@ -95,7 +93,7 @@ trait SQLiteSupport[D <: Document[D]] extends IndexSupport[D] {
| $offset
|""".stripMargin
// scribe.info(sql)
val ps = prepare(sql, params.reverse)
val ps = prepare(sql, params)
val rs = ps.executeQuery()
try {
val data = this.data(rs)
Expand All @@ -116,7 +114,7 @@ trait SQLiteSupport[D <: Document[D]] extends IndexSupport[D] {
protected def data(rs: ResultSet): SQLData[D] = {
val iterator = new Iterator[Id[D]] {
override def hasNext: Boolean = rs.next()
override def next(): Id[D] = Id[D](rs.getString(1))
override def next(): Id[D] = Id[D](rs.getString("_id"))
}
val ids = iterator.toList
SQLData(ids, None)
Expand Down

0 comments on commit f3f42b8

Please sign in to comment.