@@ -83,84 +83,34 @@ class PointLuceneRDD[V: ClassTag]
8383 new LuceneRDDResponse (partitionsRDD.map(f), SparkScoreDoc .ascending)
8484 }
8585
86- private def linker [ T : ClassTag , S : ClassTag ](that : RDD [T ],
87- pointFunctor : T => S ,
88- mapper : ( S , AbstractPointLuceneRDDPartition [ V ]) =>
89- Iterable [ SparkScoreDoc ] ,
90- linkerMethod : String )
86+ def linkByRadius [ T : ClassTag ](that : RDD [T ],
87+ pointFunctor : T => PointType ,
88+ topK : Int = DefaultTopK ,
89+ radius : Double ,
90+ linkerMethod : String = getShapeLinkerMethod )
9191 : RDD [(T , Array [SparkScoreDoc ])] = {
92- logInfo(" Point Linkage requested" )
92+ logInfo(" linkByRadius requested" )
93+
94+ val partitioner = SpatialByXPartitioner (boundsPerPartition()
95+ .map(x => (x._1._1, x._2._1)).collect()
96+ )
9397
94- val topKMonoid = new TopKMonoid [SparkScoreDoc ](MaxDefaultTopKValue )(SparkScoreDoc .ascending)
9598 val queries = that.zipWithIndex().map(_.swap)
99+ val queriesPart = queries.mapValues(pointFunctor).partitionBy(partitioner)
96100
97- val resultsByPart = linkerMethod match {
98- case " cartesian " =>
99- val concatenated = queries.mapValues(pointFunctor).glom()
101+ val coGrouped = partitionsRDD.zipPartitions(queriesPart, preservesPartitioning = true )
102+ { case tp =>
103+ val queries = tp._2.toArray
100104
101- concatenated.cartesian(partitionsRDD)
102- .flatMap { case (qs, lucene) =>
103- qs.map { case (ind, query) =>
104- (ind, topKMonoid.build(mapper(query, lucene)))
105- }
105+ tp._1.flatMap{lucene =>
106+ queries.map{q =>
107+ (q._1,
108+ lucene.circleSearch(q._2, radius, topK, linkerMethod).toArray)
106109 }
107- case _ =>
108- logInfo(" Collecting query points to driver" )
109- val collectedQueries = queries.mapValues(pointFunctor).collect()
110- val queriesB = partitionsRDD.context.broadcast(collectedQueries)
111-
112- partitionsRDD.mapPartitions { partitions =>
113- partitions.flatMap { partition =>
114- queriesB.value.map { case (index, query) =>
115- (index, topKMonoid.build(mapper(query, partition)))
116- }
117- }
118110 }
119- }
120-
121- logInfo(" Computing top-k linkage per partition" )
122- val results = resultsByPart.reduceByKey(topKMonoid.plus)
123-
124- queries.join(results).values
125- .map(joined => (joined._1, joined._2.items.toArray))
126- }
127-
128- /**
129- * Link entities if their shapes are within a distance in kilometers (km)
130- *
131- * Links this and that based on distance threshold
132- *
133- * @param that An RDD of entities to be linked
134- * @param shapeFunctor Function that generates a point from each element of other
135- * @param linkerMethod Method to perform linkage
136- * @tparam T A type
137- * @return an RDD of Tuple2 that contains the linked results
138- *
139- * Note: Currently the query coordinates of the other RDD are collected to the driver and
140- * broadcast to the workers.
141- */
142- def linkByInstersection [T : ClassTag ](that : RDD [T ],
143- shapeFunctor : T => String ,
144- topK : Int = DefaultTopK ,
145- linkerMethod : String = getShapeLinkerMethod)
146- : RDD [(T , Array [SparkScoreDoc ])] = {
147- logInfo(" linkByInstersection requested" )
148- linker[T , String ](that, shapeFunctor, (queryShape : String , part) =>
149- part.spatialSearch(queryShape, topK, SpatialOperation .Intersects .getName),
150- linkerMethod)
151- }
152-
111+ }
153112
154- def linkByRadius [T : ClassTag ](that : RDD [T ],
155- pointFunctor : T => PointType ,
156- topK : Int = DefaultTopK ,
157- radius : Double ,
158- linkerMethod : String = getShapeLinkerMethod)
159- : RDD [(T , Array [SparkScoreDoc ])] = {
160- logInfo(" linkByRadius requested" )
161- linker[T , PointType ](that, pointFunctor, (queryPoint, part) =>
162- part.circleSearch(queryPoint, radius, topK, SpatialOperation .Intersects .getName),
163- linkerMethod)
113+ queries.join(coGrouped).values
164114 }
165115
166116 /**
@@ -403,5 +353,4 @@ object PointLuceneRDD extends Versionable
403353 /** Algebird bounding box aggregator */
404354
405355 val boundingBoxMonoid = new Tuple2Monoid ()(MinPointMonoid , MaxPointMonoid )
406-
407356}
0 commit comments