Skip to content

Commit

Permalink
Better support for JSON iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Oct 31, 2024
1 parent 591f0ce commit 96c2654
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion core/src/main/scala/lightdb/collection/Collection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ case class Collection[Doc <: Document[Doc], Model <: DocumentModel[Doc]](name: S
}

def iterator[Return](f: Iterator[Json] => Return): Return = transaction { implicit transaction =>
val iterator = collection.iterator.map(doc => doc.json(model.rw))
val iterator = collection.store.jsonIterator
f(iterator)
}
}
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala/lightdb/store/Store.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lightdb.store

import fabric.Json
import fabric.io.{JsonFormatter, JsonParser}
import fabric.rw.{Asable, Convertible}
import lightdb.aggregate.AggregateQuery
Expand Down Expand Up @@ -61,6 +62,8 @@ abstract class Store[Doc <: Document[Doc], Model <: DocumentModel[Doc]] {

def iterator(implicit transaction: Transaction[Doc]): Iterator[Doc]

def jsonIterator(implicit transaction: Transaction[Doc]): Iterator[Json] = iterator.map(_.json(collection.model.rw))

def doSearch[V](query: Query[Doc, Model], conversion: Conversion[Doc, V])
(implicit transaction: Transaction[Doc]): SearchResults[Doc, Model, V]

Expand Down
2 changes: 1 addition & 1 deletion core/src/test/scala/spec/AbstractKeyValueSpec.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package spec

import fabric.rw.RW
import fabric.rw._
import lightdb.collection.Collection
import lightdb.{Id, LightDB}
import lightdb.doc.{Document, DocumentModel, JsonConversion}
Expand Down
12 changes: 10 additions & 2 deletions halodb/src/main/scala/lightdb/halodb/HaloDBStore.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lightdb.halodb

import com.oath.halodb.{HaloDB, HaloDBOptions}
import fabric.Json
import fabric.io.{JsonFormatter, JsonParser}
import fabric.rw.{Asable, Convertible}
import lightdb.aggregate.AggregateQuery
Expand Down Expand Up @@ -60,11 +61,15 @@ class HaloDBStore[Doc <: Document[Doc], Model <: DocumentModel[Doc]](directory:
}

private def bytes2Doc(bytes: Array[Byte]): Doc = {
val jsonString = new String(bytes, "UTF-8")
val json = JsonParser(jsonString)
val json = bytes2Json(bytes)
json.as[Doc](collection.model.rw)
}

private def bytes2Json(bytes: Array[Byte]): Json = {
val jsonString = new String(bytes, "UTF-8")
JsonParser(jsonString)
}

override def delete[V](field: UniqueIndex[Doc, V], value: V)(implicit transaction: Transaction[Doc]): Boolean = {
instance.delete(value.asInstanceOf[Id[Doc]].bytes)
true
Expand All @@ -75,6 +80,9 @@ class HaloDBStore[Doc <: Document[Doc], Model <: DocumentModel[Doc]](directory:
override def iterator(implicit transaction: Transaction[Doc]): Iterator[Doc] = instance.newIterator().asScala
.map(_.getValue).map(bytes2Doc)

override def jsonIterator(implicit transaction: Transaction[Doc]): Iterator[Json] = instance.newIterator().asScala
.map(_.getValue).map(bytes2Json)

override def doSearch[V](query: Query[Doc, Model], conversion: Conversion[Doc, V])
(implicit transaction: Transaction[Doc]): SearchResults[Doc, Model, V] =
throw new UnsupportedOperationException("HaloDBStore does not support searching")
Expand Down

0 comments on commit 96c2654

Please sign in to comment.