Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Sep 7, 2024
1 parent cc96a8a commit a64f084
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 33 deletions.
10 changes: 10 additions & 0 deletions core/src/main/scala/lightdb/Field.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ sealed class Field[Doc <: Document[Doc], V](val name: String,
case _ => false
}

lazy val className: Option[String] = rw.definition match {
case DefType.Opt(DefType.Obj(_, Some(cn))) => Some(cn)
case DefType.Obj(_, Some(cn)) => Some(cn)
case DefType.Opt(DefType.Poly(_, Some(cn))) => Some(cn)
case DefType.Poly(_, Some(cn)) => Some(cn)
case _ => None
}

lazy val isSpatial: Boolean = className.exists(_.startsWith("lightdb.spatial.Geo"))

def getJson(doc: Doc): Json = get(doc).json

override def is(value: V): Filter[Doc] = Filter.Equals(name, value)
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/lightdb/store/Store.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ abstract class Store[Doc <: Document[Doc], Model <: DocumentModel[Doc]] {
protected def toString(doc: Doc): String = JsonFormatter.Compact(doc.json(collection.model.rw))
protected def fromString(string: String): Doc = JsonParser(string).as[Doc](collection.model.rw)

lazy val hasSpatial: Boolean = fields.exists(_.isSpatial)

def init(collection: Collection[Doc, Model]): Unit = {
this.collection = collection
}
Expand Down
32 changes: 7 additions & 25 deletions lucene/src/main/scala/lightdb/lucene/LuceneStore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ class LuceneStore[Doc <: Document[Doc], Model <: DocumentModel[Doc]](directory:
addDoc(id(doc), luceneFields, upsert = true)
}

private def createGeoFields(className: String,
field: Field[Doc, _],
private def createGeoFields(field: Field[Doc, _],
json: Json,
add: LuceneField => Unit): Unit = {
className match {
case "lightdb.spatial.Geo.Point" =>
field.className match {
case Some("lightdb.spatial.Geo.Point") =>
val p = json.as[Geo.Point]
add(new LatLonPoint(field.name, p.latitude, p.longitude))
case _ =>
Expand Down Expand Up @@ -96,15 +95,8 @@ class LuceneStore[Doc <: Document[Doc], Model <: DocumentModel[Doc]](directory:
List(new LuceneField(field.name, t.get(doc), if (fs == LuceneField.Store.YES) TextField.TYPE_STORED else TextField.TYPE_NOT_STORED))
case _ =>
def addJson(json: Json, d: DefType): Unit = {
val className = d match {
case DefType.Opt(DefType.Obj(_, Some(cn))) => cn
case DefType.Obj(_, Some(cn)) => cn
case DefType.Opt(DefType.Poly(_, Some(cn))) => cn
case DefType.Poly(_, Some(cn)) => cn
case _ => ""
}
if (className.startsWith("lightdb.spatial.Geo")) {
if (json != Null) createGeoFields(className, field, json, add)
if (field.isSpatial) {
if (json != Null) createGeoFields(field, json, add)
} else {
d match {
case DefType.Str => json match {
Expand All @@ -117,7 +109,7 @@ class LuceneStore[Doc <: Document[Doc], Model <: DocumentModel[Doc]](directory:
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))
case _ => throw new UnsupportedOperationException(s"Unsupported definition (field: ${field.name}, className: $className): $d for $json")
case _ => throw new UnsupportedOperationException(s"Unsupported definition (field: ${field.name}, className: ${field.className}): $d for $json")
}
}
}
Expand Down Expand Up @@ -294,17 +286,7 @@ class LuceneStore[Doc <: Document[Doc], Model <: DocumentModel[Doc]](directory:
parser.setSplitOnWhitespace(true)
parser.parse(query)
case Filter.Distance(fieldName, from, radius) =>
val field = collection.model.fieldByName[Geo](fieldName)
val className = field.rw.definition match {
case DefType.Opt(DefType.Obj(_, Some(cn))) => cn
case DefType.Obj(_, Some(cn)) => cn
case _ => ""
}
// if (className == "lightdb.spatial.Geo.Point") {
LatLonPoint.newDistanceQuery(fieldName, from.latitude, from.longitude, radius.toMeters)
// } else {
// LatLonShape.newDistanceQuery(fieldName, )
// }
LatLonPoint.newDistanceQuery(fieldName, from.latitude, from.longitude, radius.toMeters)
case Filter.Multi(minShould, clauses) =>
val b = new BooleanQuery.Builder
val hasShould = clauses.exists(c => c.condition == Condition.Should || c.condition == Condition.Filter)
Expand Down
8 changes: 0 additions & 8 deletions sqlite/src/main/scala/lightdb/sql/SQLiteStore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,6 @@ class SQLiteStore[Doc <: Document[Doc], Model <: DocumentModel[Doc]](val connect
}
}

private lazy val hasSpatial: Boolean = fields.exists { field =>
val className = field.rw.definition match {
case DefType.Opt(d) => d.className
case d => d.className
}
className.contains("lightdb.spatial.GeoPoint")
}

override protected def extraFieldsForDistance(d: Conversion.Distance[Doc, _]): List[SQLPart] = {
List(SQLPart(s"ST_Distance(GeomFromText('POINT(${d.from.longitude} ${d.from.latitude})', 4326), ${d.field.name}, true) AS ${d.field.name}Distance"))
}
Expand Down

0 comments on commit a64f084

Please sign in to comment.