Skip to content

Commit aca5e45

Browse files
committed
point_rdd: cleanup
1 parent fbca75a commit aca5e45

4 files changed

Lines changed: 69 additions & 7 deletions

File tree

src/main/scala/org/zouzias/spark/lucenerdd/spatial/commons/MaxPointMonoid.scala renamed to src/main/scala/org/zouzias/spark/lucenerdd/aggregate/MaxPointMonoid.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.zouzias.spark.lucenerdd.spatial.commons
17+
package org.zouzias.spark.lucenerdd.aggregate
1818

1919
import com.twitter.algebird.Monoid
2020
import org.zouzias.spark.lucenerdd.spatial.point.PointLuceneRDD.PointType
2121

22+
/**
23+
* Maximum point [[Monoid]] used for spatial linkage
24+
*
25+
* Keeps the maximum value per coordinate
26+
*/
2227
object MaxPointMonoid extends Monoid[PointType] {
28+
2329
override def zero: PointType = (Double.MaxValue, Double.MaxValue)
2430

2531
override def plus(x: PointType, y: PointType): PointType = {

src/main/scala/org/zouzias/spark/lucenerdd/spatial/commons/MinPointMonoid.scala renamed to src/main/scala/org/zouzias/spark/lucenerdd/aggregate/MinPointMonoid.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.zouzias.spark.lucenerdd.spatial.commons
17+
package org.zouzias.spark.lucenerdd.aggregate
1818

1919
import com.twitter.algebird.Monoid
2020
import org.zouzias.spark.lucenerdd.spatial.point.PointLuceneRDD.PointType
2121

22+
/**
23+
* Minimum point Monoid used for spatial linkage
24+
*
25+
* Keeps the minimum value per coordinate
26+
*/
2227
object MinPointMonoid extends Monoid[PointType] {
28+
2329
override def zero: PointType = (Double.MinValue, Double.MinValue)
2430

2531
override def plus(x: PointType, y: PointType): PointType = {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.zouzias.spark.lucenerdd.spatial.commons
18+
19+
import org.apache.spark.Partitioner
20+
import org.zouzias.spark.lucenerdd.spatial.point.PointLuceneRDD.PointType
21+
22+
import scala.util.Random
23+
24+
/**
25+
* Spark RDD [[Partitioner]] based on the x-axis and bounds per partition
26+
* @param boundsPerPart Bounds of x-axis (minX, maxX) per RDD's partition
27+
*/
28+
case class SpatialByXPartitioner(boundsPerPart: Array[(Double, Double)]) extends Partitioner {
29+
override def numPartitions: Int = boundsPerPart.length
30+
31+
override def getPartition(key: Any): Int = {
32+
val keyPoint = key.asInstanceOf[PointType]
33+
val indexOpt = boundsPerPart.indexWhere{ case (minX, maxX) =>
34+
minX <= keyPoint._1 && keyPoint._1 <= maxX
35+
}
36+
37+
// If key is not assigned to a partition, randomly assign the key
38+
if (indexOpt == -1) Random.nextInt(numPartitions) else indexOpt
39+
}
40+
}

src/main/scala/org/zouzias/spark/lucenerdd/spatial/point/PointLuceneRDD.scala

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ package org.zouzias.spark.lucenerdd.spatial.point
1919
import com.twitter.algebird._
2020
import org.apache.lucene.document.Document
2121
import org.apache.lucene.spatial.query.SpatialOperation
22-
import org.apache.spark.{OneToOneDependency, Partition, TaskContext}
22+
import org.apache.spark.{OneToOneDependency, Partition, Partitioner, TaskContext}
2323
import org.apache.spark.rdd.RDD
2424
import org.apache.spark.sql.{DataFrame, Dataset, Row}
2525
import org.apache.spark.storage.StorageLevel
26+
import org.zouzias.spark.lucenerdd.aggregate.{MaxPointMonoid, MinPointMonoid}
2627
import org.zouzias.spark.lucenerdd.analyzers.AnalyzerConfigurable
2728
import org.zouzias.spark.lucenerdd.config.ShapeLuceneRDDConfigurable
2829
import org.zouzias.spark.lucenerdd.models.SparkScoreDoc
2930
import org.zouzias.spark.lucenerdd.query.{LuceneQueryHelpers, SimilarityConfigurable}
3031
import org.zouzias.spark.lucenerdd.response.{LuceneRDDResponse, LuceneRDDResponsePartition}
31-
import org.zouzias.spark.lucenerdd.spatial.commons.{MaxPointMonoid, MinPointMonoid}
32+
import org.zouzias.spark.lucenerdd.spatial.commons.SpatialByXPartitioner
3233
import org.zouzias.spark.lucenerdd.spatial.point.PointLuceneRDD.PointType
3334
import org.zouzias.spark.lucenerdd.spatial.point.partition.{AbstractPointLuceneRDDPartition, PointLuceneRDDPartition}
3435
import org.zouzias.spark.lucenerdd.versioning.Versionable
@@ -198,7 +199,7 @@ class PointLuceneRDD[V: ClassTag]
198199
*
199200
* @param shapeWKT Shape in WKT format
200201
* @param k Number of element to return
201-
* @param operationName
202+
* @param operationName Spatial operation name, i.e., intersection, contained, etc.
202203
* @return
203204
*/
204205
def spatialSearch(shapeWKT: String, k: Int,
@@ -213,7 +214,7 @@ class PointLuceneRDD[V: ClassTag]
213214
* @param center given as (x, y)
214215
* @param radius in kilometers (KM)
215216
* @param k
216-
* @param operationName
217+
* @param operationName Spatial operation name, i.e., intersection, contained, etc.
217218
* @return
218219
*/
219220
def bboxSearch(center: PointType, radius: Double, k: Int,
@@ -228,7 +229,7 @@ class PointLuceneRDD[V: ClassTag]
228229
* @param lowerLeft Lower left corner
229230
* @param upperRight Upper right corner
230231
* @param k Number of results
231-
* @param operationName Intersect, contained, etc.
232+
* @param operationName Spatial operation name, i.e., intersection, contained, etc.
232233
* @return
233234
*/
234235
def bboxSearch(lowerLeft: PointType,
@@ -257,6 +258,15 @@ class PointLuceneRDD[V: ClassTag]
257258
}
258259
}
259260

261+
/**
262+
* Returns an x-axis spatial [[Partitioner]] that is used in link related methods
263+
* @return Spark [[Partitioner]]
264+
*/
265+
def spatialPartitioner(): Partitioner = {
266+
val bpp = boundsPerPartition().map(x => (x._1._1, x._2._1)).collect()
267+
SpatialByXPartitioner(bpp)
268+
}
269+
260270
override def count(): Long = {
261271
logInfo("Count requested")
262272
partitionsRDD.map(_.size).reduce(_ + _)

0 commit comments

Comments
 (0)