diff --git a/core/src/main/scala/lightdb/collection/Collection.scala b/core/src/main/scala/lightdb/collection/Collection.scala index 0ef4cad5..d5d3025c 100644 --- a/core/src/main/scala/lightdb/collection/Collection.scala +++ b/core/src/main/scala/lightdb/collection/Collection.scala @@ -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) } } diff --git a/core/src/main/scala/lightdb/store/Store.scala b/core/src/main/scala/lightdb/store/Store.scala index 7394da2c..d884c3b8 100644 --- a/core/src/main/scala/lightdb/store/Store.scala +++ b/core/src/main/scala/lightdb/store/Store.scala @@ -1,5 +1,6 @@ package lightdb.store +import fabric.Json import fabric.io.{JsonFormatter, JsonParser} import fabric.rw.{Asable, Convertible} import lightdb.aggregate.AggregateQuery @@ -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] diff --git a/core/src/test/scala/spec/AbstractKeyValueSpec.scala b/core/src/test/scala/spec/AbstractKeyValueSpec.scala index de909500..07e6eb34 100644 --- a/core/src/test/scala/spec/AbstractKeyValueSpec.scala +++ b/core/src/test/scala/spec/AbstractKeyValueSpec.scala @@ -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} diff --git a/halodb/src/main/scala/lightdb/halodb/HaloDBStore.scala b/halodb/src/main/scala/lightdb/halodb/HaloDBStore.scala index 2625ab51..da8d8b9e 100644 --- a/halodb/src/main/scala/lightdb/halodb/HaloDBStore.scala +++ b/halodb/src/main/scala/lightdb/halodb/HaloDBStore.scala @@ -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 @@ -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 @@ -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")