]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/indexer/runners.py
introduce external processing in indexer
[nominatim.git] / nominatim / indexer / runners.py
1 """
2 Mix-ins that provide the actual commands for the indexer for various indexing
3 tasks.
4 """
5 # pylint: disable=C0111
6
7 class AbstractPlacexRunner:
8     """ Returns SQL commands for indexing of the placex table.
9     """
10     SELECT_SQL = 'SELECT place_id, (placex_prepare_update(placex)).* FROM placex'
11
12     def __init__(self, rank):
13         self.rank = rank
14         self._sql_terms = 0
15         self._cached_index_sql = None
16
17     def _index_sql(self, num_places):
18         if num_places != self._sql_terms:
19             self._cached_index_sql = \
20                 """ UPDATE placex
21                     SET indexed_status = 0, address = v.addr
22                     FROM (VALUES {}) as v(id, addr)
23                     WHERE place_id = v.id
24                 """.format(','.join(["(%s, %s::hstore)"]  * num_places))
25             self._sql_terms = num_places
26
27         return self._cached_index_sql
28
29
30     def index_places(self, worker, places):
31         values = []
32         for place in places:
33             values.extend((place[x] for x in ('place_id', 'address')))
34
35         worker.perform(self._index_sql(len(places)), values)
36
37
38 class RankRunner(AbstractPlacexRunner):
39     """ Returns SQL commands for indexing one rank within the placex table.
40     """
41
42     def name(self):
43         return "rank {}".format(self.rank)
44
45     def sql_count_objects(self):
46         return """SELECT count(*) FROM placex
47                   WHERE rank_address = {} and indexed_status > 0
48                """.format(self.rank)
49
50     def sql_get_objects(self):
51         return """{} WHERE indexed_status > 0 and rank_address = {}
52                      ORDER BY geometry_sector
53                """.format(self.SELECT_SQL, self.rank)
54
55
56 class BoundaryRunner(AbstractPlacexRunner):
57     """ Returns SQL commands for indexing the administrative boundaries
58         of a certain rank.
59     """
60
61     def name(self):
62         return "boundaries rank {}".format(self.rank)
63
64     def sql_count_objects(self):
65         return """SELECT count(*) FROM placex
66                   WHERE indexed_status > 0
67                     AND rank_search = {}
68                     AND class = 'boundary' and type = 'administrative'
69                """.format(self.rank)
70
71     def sql_get_objects(self):
72         return """{} WHERE indexed_status > 0 and rank_search = {}
73                            and class = 'boundary' and type = 'administrative'
74                      ORDER BY partition, admin_level
75                """.format(self.SELECT_SQL, self.rank)
76
77
78 class InterpolationRunner:
79     """ Returns SQL commands for indexing the address interpolation table
80         location_property_osmline.
81     """
82
83     @staticmethod
84     def name():
85         return "interpolation lines (location_property_osmline)"
86
87     @staticmethod
88     def sql_count_objects():
89         return """SELECT count(*) FROM location_property_osmline
90                   WHERE indexed_status > 0"""
91
92     @staticmethod
93     def sql_get_objects():
94         return """SELECT place_id FROM location_property_osmline
95                   WHERE indexed_status > 0
96                   ORDER BY geometry_sector"""
97
98     @staticmethod
99     def index_places(worker, ids):
100         worker.perform(""" UPDATE location_property_osmline
101                            SET indexed_status = 0 WHERE place_id IN ({})
102                        """.format(','.join((str(i[0]) for i in ids))))
103
104
105 class PostcodeRunner:
106     """ Provides the SQL commands for indexing the location_postcode table.
107     """
108
109     @staticmethod
110     def name():
111         return "postcodes (location_postcode)"
112
113     @staticmethod
114     def sql_count_objects():
115         return 'SELECT count(*) FROM location_postcode WHERE indexed_status > 0'
116
117     @staticmethod
118     def sql_get_objects():
119         return """SELECT place_id FROM location_postcode
120                   WHERE indexed_status > 0
121                   ORDER BY country_code, postcode"""
122
123     @staticmethod
124     def index_places(worker, ids):
125         worker.perform(""" UPDATE location_postcode SET indexed_status = 0
126                            WHERE place_id IN ({})
127                        """.format(','.join((str(i[0]) for i in ids))))