Skip to content

Commit d1f72b0

Browse files
authored
GEOMESA-3530 Partitioned Postgis - make getIndexColumns() public (#3436)
1 parent 1a01a47 commit d1f72b0

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

geomesa-gt/geomesa-gt-partitioning/src/main/scala/org/locationtech/geomesa/gt/partition/postgis/dialect/PartitionedPostgisDialect.scala

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import org.geotools.geometry.jts._
1818
import org.geotools.jdbc.JDBCDataStore
1919
import org.geotools.referencing.CRS
2020
import org.geotools.util.factory.Hints
21-
import org.locationtech.geomesa.gt.partition.postgis.dialect.PartitionedPostgisDialect.SftUserData
21+
import org.locationtech.geomesa.gt.partition.postgis.dialect.PartitionedPostgisDialect.{SftUserData, getIndexedColumns}
2222
import org.locationtech.geomesa.gt.partition.postgis.dialect.filter.SplitFilterVisitor
2323
import org.locationtech.geomesa.gt.partition.postgis.dialect.functions.{LogCleaner, TruncateToPartition, TruncateToTenMinutes}
2424
import org.locationtech.geomesa.gt.partition.postgis.dialect.procedures._
@@ -217,9 +217,19 @@ class PartitionedPostgisDialect(store: JDBCDataStore, grants: Seq[RoleName] = Se
217217
UserDataTable.read(cx, schemaName, sft.getTypeName).foreach { case (k, v) => sft.getUserData.put(k, v) }
218218

219219
// populate flags on indexed attributes
220-
getIndexedColumns(cx, sft.getTypeName).foreach { attribute =>
221-
Option(sft.getDescriptor(attribute)).foreach(_.getUserData.put(AttributeOptions.OptIndex, "true"))
222-
}
220+
getIndexedColumns(cx, sft.getTypeName)
221+
.recover {
222+
case NonFatal(throwable) =>
223+
logger.warn(s"SimpleFeatureType: ${sft.getTypeName} could not load attributes with indices", throwable)
224+
List.empty
225+
}
226+
.get
227+
.foreach { attribute =>
228+
Try(sft.getDescriptor(attribute)).fold(
229+
throwable => logger.warn(s"SimpleFeatureType: ${sft.getTypeName} could not load attribute descriptor by name: $attribute", throwable),
230+
_.getUserData.put(AttributeOptions.OptIndex, "true")
231+
)
232+
}
223233
}
224234

225235
override def preDropTable(schemaName: String, sft: SimpleFeatureType, cx: Connection): Unit = {
@@ -355,40 +365,9 @@ class PartitionedPostgisDialect(store: JDBCDataStore, grants: Seq[RoleName] = Se
355365
"text"
356366
}
357367
}
358-
359-
/**
360-
* Gets a list of indexed columns for the given type
361-
*
362-
* @param cx connection
363-
* @param typeName feature type name
364-
* @return
365-
*/
366-
private def getIndexedColumns(cx: Connection, typeName: String): Seq[String] = {
367-
val attributesWithIndicesSql =
368-
s"""select distinct(att.attname) as indexed_attribute_name
369-
|from pg_class obj
370-
|join pg_index idx on idx.indrelid = obj.oid
371-
|join pg_attribute att on att.attrelid = obj.oid and att.attnum = any(idx.indkey)
372-
|join pg_views v on v.viewname = ?
373-
|where obj.relname = concat(?, ${PartitionedTableSuffix.quoted})
374-
|order by att.attname;""".stripMargin
375-
try {
376-
WithClose(cx.prepareStatement(attributesWithIndicesSql)) { statement =>
377-
statement.setString(1, typeName)
378-
statement.setString(2, typeName)
379-
WithClose(statement.executeQuery()) { rs =>
380-
Iterator.continually(rs).takeWhile(_.next()).map(_.getString(1)).toList
381-
}
382-
}
383-
} catch {
384-
case NonFatal(e) =>
385-
logger.warn(s"Error loading attributes with indices for schema $typeName:", e)
386-
Seq.empty
387-
}
388-
}
389368
}
390369

391-
object PartitionedPostgisDialect extends Conversions {
370+
object PartitionedPostgisDialect extends Conversions with StrictLogging {
392371

393372
private val IgnoredTables = Seq("pg_stat_statements", "pg_stat_statements_info")
394373

@@ -490,4 +469,29 @@ object PartitionedPostgisDialect extends Conversions {
490469
def getCoordinateDimensions: Option[Int] =
491470
Option(d.getUserData.get(Hints.COORDINATE_DIMENSION)).map(int)
492471
}
472+
473+
/**
474+
* Get a list of indexed columns for the given SimpleFeatureType
475+
*
476+
* @param cx connection
477+
* @param typeName feature type name
478+
* @return a sequence of SimpleFeatureType attribute names which have an index
479+
*/
480+
def getIndexedColumns(cx: Connection, typeName: String): Try[List[String]] = {
481+
val attributesWithIndicesSql =
482+
s"""select distinct(att.attname) as indexed_attribute_name
483+
|from pg_class obj
484+
|join pg_index idx on idx.indrelid = obj.oid
485+
|join pg_attribute att on att.attrelid = obj.oid and att.attnum = any(idx.indkey)
486+
|where obj.relname = concat(?, ${PartitionedTableSuffix.quoted})
487+
|order by att.attname;""".stripMargin
488+
Try {
489+
WithClose(cx.prepareStatement(attributesWithIndicesSql)) { statement =>
490+
statement.setString(1, typeName)
491+
WithClose(statement.executeQuery()) { rs =>
492+
Iterator.continually(rs).takeWhile(_.next()).map(_.getString(1)).toList
493+
}
494+
}
495+
}
496+
}
493497
}

0 commit comments

Comments
 (0)