Skip to content

Commit

Permalink
Better support for Materialized to use JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Jun 9, 2024
1 parent 903f896 commit 1a83cb5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
9 changes: 6 additions & 3 deletions core/src/main/scala/lightdb/index/Materialized.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package lightdb.index

import fabric.Json
import fabric.rw.{Asable, RW}
import lightdb.{Document, Id}

class Materialized[D <: Document[D]](map: Map[Index[_, D], Any]) {
def get[F](index: Index[F, D]): Option[F] = map.get(index).map(_.asInstanceOf[F])
def apply[F](index: Index[F, D]): F = get(index).getOrElse(throw new NullPointerException(s"${index.fieldName} not found in [${map.keySet.map(_.fieldName).mkString(", ")}]"))
class Materialized[D <: Document[D]](json: Json) {
def get[F](index: Index[F, D]): Option[F] = json.get(index.fieldName).map(_.as[F](index.rw))
def apply[F](index: Index[F, D]): F = get(index).getOrElse(throw new NullPointerException(s"${index.fieldName} not found in $json"))
def as[T](implicit rw: RW[T]): T = json.as[T]
}
12 changes: 6 additions & 6 deletions sql/src/main/scala/lightdb/sql/SQLSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ trait SQLSupport[D <: Document[D]] extends IndexSupport[D] {

override def next(): Materialized[D] = {
val map = index.fields.filter(_.materialize).map { index =>
index -> getValue(rs, index)
index.fieldName -> getJson(rs, index.fieldName)
}.toMap
new Materialized[D](map)
new Materialized[D](Obj(map))
}
}
iterator.toList
Expand Down Expand Up @@ -213,10 +213,10 @@ trait SQLSupport[D <: Document[D]] extends IndexSupport[D] {
case _ => ps.setString(index, JsonFormatter.Compact(value))
}

private def getValue[F](rs: ResultSet, index: Index[F, D]): Any = rs.getObject(index.fieldName) match {
case s: String => index.rw.write(str(s))
case i: java.lang.Integer => index.rw.write(num(i.intValue()))
case v => throw new UnsupportedOperationException(s"${index.fieldName} returned $v (${v.getClass.getName})")
private def getJson(rs: ResultSet, fieldName: String): Json = rs.getObject(fieldName) match {
case s: String => str(s)
case i: java.lang.Integer => num(i.intValue())
case v => throw new UnsupportedOperationException(s"$fieldName returned $v (${v.getClass.getName})")
}

private def commit(): IO[Unit] = IO.blocking {
Expand Down

0 comments on commit 1a83cb5

Please sign in to comment.