From e4eb289c9c41aa6b69d1837241fe58339edbbb3f Mon Sep 17 00:00:00 2001 From: Matt Hicks Date: Sat, 21 Dec 2024 15:17:29 -0600 Subject: [PATCH] Fixed bug in LuceneStore not indexing empty arrays properly Updated SplitStore to not re-index collections with no indexes --- build.sbt | 4 ++-- core/src/main/scala/lightdb/store/split/SplitStore.scala | 2 +- core/src/test/scala/spec/AbstractBasicSpec.scala | 6 ++++++ lucene/src/main/scala/lightdb/lucene/LuceneStore.scala | 8 +++++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/build.sbt b/build.sbt index e68a2f3..50ea226 100644 --- a/build.sbt +++ b/build.sbt @@ -15,8 +15,8 @@ val developerURL: String = "https://matthicks.com" name := projectName ThisBuild / organization := org -ThisBuild / version := "1.2.2" -ThisBuild / scalaVersion := scala213 +ThisBuild / version := "1.2.3-SNAPSHOT" +ThisBuild / scalaVersion := scala3 ThisBuild / crossScalaVersions := allScalaVersions ThisBuild / scalacOptions ++= Seq("-unchecked", "-deprecation") diff --git a/core/src/main/scala/lightdb/store/split/SplitStore.scala b/core/src/main/scala/lightdb/store/split/SplitStore.scala index af9b9d3..56c4a3e 100644 --- a/core/src/main/scala/lightdb/store/split/SplitStore.scala +++ b/core/src/main/scala/lightdb/store/split/SplitStore.scala @@ -81,7 +81,7 @@ case class SplitStore[Doc <: Document[Doc], Model <: DocumentModel[Doc]](overrid override def verify(): Boolean = transaction { implicit transaction => val storageCount = storage.count val searchCount = searching.count - if (storageCount != searchCount) { + if (storageCount != searchCount && model.fields.count(_.indexed) > 1) { scribe.warn(s"$name out of sync! Storage Count: $storageCount, Search Count: $searchCount. Re-Indexing...") reIndexInternal() scribe.info(s"$name re-indexed successfully!") diff --git a/core/src/test/scala/spec/AbstractBasicSpec.scala b/core/src/test/scala/spec/AbstractBasicSpec.scala index 8136e68..76e22bd 100644 --- a/core/src/test/scala/spec/AbstractBasicSpec.scala +++ b/core/src/test/scala/spec/AbstractBasicSpec.scala @@ -327,6 +327,12 @@ abstract class AbstractBasicSpec extends AnyWordSpec with Matchers { spec => people.map(_.name) should be(List("Oscar")) } } + "materialize empty nicknames" in { + db.people.transaction { implicit transaction => + val people = db.people.query.filter(_.name === "Ian").search.materialized(p => List(p.nicknames)).list + people.map(m => m(_.nicknames)) should be(List(Set.empty)) + } + } "query with single-value, multiple nicknames" in { db.people.transaction { implicit transaction => val people = db.people.query diff --git a/lucene/src/main/scala/lightdb/lucene/LuceneStore.scala b/lucene/src/main/scala/lightdb/lucene/LuceneStore.scala index 5652ab5..5df3bca 100644 --- a/lucene/src/main/scala/lightdb/lucene/LuceneStore.scala +++ b/lucene/src/main/scala/lightdb/lucene/LuceneStore.scala @@ -164,7 +164,13 @@ class LuceneStore[Doc <: Document[Doc], Model <: DocumentModel[Doc]](name: Strin case DefType.Opt(d) => addJson(json, d) case DefType.Json | DefType.Obj(_, _) => add(new StringField(field.name, JsonFormatter.Compact(json), fs)) case _ if json == Null => // Ignore null values - case DefType.Arr(d) => json.asVector.foreach(json => addJson(json, d)) + case DefType.Arr(d) => + val v = json.asVector + if (v.isEmpty) { + add(new StringField(field.name, "[]", fs)) + } else { + v.foreach(json => addJson(json, d)) + } case DefType.Bool => add(new IntField(field.name, if (json.asBoolean) 1 else 0, fs)) case DefType.Int => add(new LongField(field.name, json.asLong, fs)) case DefType.Dec => add(new DoubleField(field.name, json.asDouble, fs))