2 Mix-ins that provide the actual commands for the indexer for various indexing
8 from psycopg2 import sql as pysql
10 # pylint: disable=C0111
12 def _mk_valuelist(template, num):
13 return pysql.SQL(',').join([pysql.SQL(template)] * num)
15 class AbstractPlacexRunner:
16 """ Returns SQL commands for indexing of the placex table.
18 SELECT_SQL = pysql.SQL('SELECT place_id FROM placex ')
19 UPDATE_LINE = "(%s, %s::hstore, %s::hstore, %s::int, %s::jsonb)"
21 def __init__(self, rank, analyzer):
23 self.analyzer = analyzer
27 @functools.lru_cache(maxsize=1)
28 def _index_sql(num_places):
31 SET indexed_status = 0, address = v.addr, token_info = v.ti,
32 name = v.name, linked_place_id = v.linked_place_id
33 FROM (VALUES {}) as v(id, name, addr, linked_place_id, ti)
35 """).format(_mk_valuelist(AbstractPlacexRunner.UPDATE_LINE, num_places))
39 def get_place_details(worker, ids):
40 worker.perform("""SELECT place_id, (placex_prepare_update(placex)).*
41 FROM placex WHERE place_id IN %s""",
42 (tuple((p[0] for p in ids)), ))
45 def index_places(self, worker, places):
48 for field in ('place_id', 'name', 'address', 'linked_place_id'):
49 values.append(place[field])
50 values.append(psycopg2.extras.Json(self.analyzer.process_place(place)))
52 worker.perform(self._index_sql(len(places)), values)
55 class RankRunner(AbstractPlacexRunner):
56 """ Returns SQL commands for indexing one rank within the placex table.
60 return "rank {}".format(self.rank)
62 def sql_count_objects(self):
63 return pysql.SQL("""SELECT count(*) FROM placex
64 WHERE rank_address = {} and indexed_status > 0
65 """).format(pysql.Literal(self.rank))
67 def sql_get_objects(self):
68 return self.SELECT_SQL + pysql.SQL(
69 """WHERE indexed_status > 0 and rank_address = {}
70 ORDER BY geometry_sector
71 """).format(pysql.Literal(self.rank))
74 class BoundaryRunner(AbstractPlacexRunner):
75 """ Returns SQL commands for indexing the administrative boundaries
80 return "boundaries rank {}".format(self.rank)
82 def sql_count_objects(self):
83 return pysql.SQL("""SELECT count(*) FROM placex
84 WHERE indexed_status > 0
86 AND class = 'boundary' and type = 'administrative'
87 """).format(pysql.Literal(self.rank))
89 def sql_get_objects(self):
90 return self.SELECT_SQL + pysql.SQL(
91 """WHERE indexed_status > 0 and rank_search = {}
92 and class = 'boundary' and type = 'administrative'
93 ORDER BY partition, admin_level
94 """).format(pysql.Literal(self.rank))
97 class InterpolationRunner:
98 """ Returns SQL commands for indexing the address interpolation table
99 location_property_osmline.
102 def __init__(self, analyzer):
103 self.analyzer = analyzer
108 return "interpolation lines (location_property_osmline)"
111 def sql_count_objects():
112 return """SELECT count(*) FROM location_property_osmline
113 WHERE indexed_status > 0"""
116 def sql_get_objects():
117 return """SELECT place_id
118 FROM location_property_osmline
119 WHERE indexed_status > 0
120 ORDER BY geometry_sector"""
124 def get_place_details(worker, ids):
125 worker.perform("""SELECT place_id, get_interpolation_address(address, osm_id) as address
126 FROM location_property_osmline WHERE place_id IN %s""",
127 (tuple((p[0] for p in ids)), ))
131 @functools.lru_cache(maxsize=1)
132 def _index_sql(num_places):
133 return pysql.SQL("""UPDATE location_property_osmline
134 SET indexed_status = 0, address = v.addr, token_info = v.ti
135 FROM (VALUES {}) as v(id, addr, ti)
136 WHERE place_id = v.id
137 """).format(_mk_valuelist("(%s, %s::hstore, %s::jsonb)", num_places))
140 def index_places(self, worker, places):
143 values.extend((place[x] for x in ('place_id', 'address')))
144 values.append(psycopg2.extras.Json(self.analyzer.process_place(place)))
146 worker.perform(self._index_sql(len(places)), values)
150 class PostcodeRunner:
151 """ Provides the SQL commands for indexing the location_postcode table.
156 return "postcodes (location_postcode)"
159 def sql_count_objects():
160 return 'SELECT count(*) FROM location_postcode WHERE indexed_status > 0'
163 def sql_get_objects():
164 return """SELECT place_id FROM location_postcode
165 WHERE indexed_status > 0
166 ORDER BY country_code, postcode"""
169 def index_places(worker, ids):
170 worker.perform(pysql.SQL("""UPDATE location_postcode SET indexed_status = 0
171 WHERE place_id IN ({})""")
172 .format(pysql.SQL(',').join((pysql.Literal(i[0]) for i in ids))))