]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/index.c
gb postcode data (os open license)
[nominatim.git] / nominatim / index.c
1 /*
2 */
3
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <assert.h>
9 #include <pthread.h>
10 #include <time.h>
11 #include <stdint.h>
12
13 #include <libpq-fe.h>
14
15 #include "nominatim.h"
16 #include "index.h"
17 #include "export.h"
18 #include "postgresql.h"
19
20 extern int verbose;
21
22 void nominatim_index(int rank_min, int rank_max, int num_threads, const char *conninfo, const char *structuredoutputfile)
23 {
24         struct index_thread_data * thread_data;
25         pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
26         int tuples, count, sleepcount;
27
28         time_t rankStartTime;
29         int rankTotalTuples;
30         int rankCountTuples;
31         float rankPerSecond;
32
33         PGconn *conn;
34         PGresult * res;
35         PGresult * resSectors;
36         PGresult * resPlaces;
37
38         int rank;
39         int i;
40         int iSector;
41
42     const char *paramValues[2];
43     int         paramLengths[2];
44     int         paramFormats[2];
45     uint32_t    paramRank;
46     uint32_t    paramSector;
47     uint32_t    sector;
48
49     xmlTextWriterPtr writer;
50         pthread_mutex_t writer_mutex = PTHREAD_MUTEX_INITIALIZER;
51
52     Oid pg_prepare_params[2];
53
54     conn = PQconnectdb(conninfo);
55     if (PQstatus(conn) != CONNECTION_OK) {
56         fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn));
57         exit(EXIT_FAILURE);
58     }
59
60     pg_prepare_params[0] = PG_OID_INT4;
61     res = PQprepare(conn, "index_sectors",
62         "select geometry_sector,count(*) from placex where rank_search = $1 and indexed_status > 0 group by geometry_sector order by geometry_sector",
63         1, pg_prepare_params);
64     if (PQresultStatus(res) != PGRES_COMMAND_OK)
65     {
66         fprintf(stderr, "Failed preparing index_sectors: %s\n", PQerrorMessage(conn));
67         exit(EXIT_FAILURE);
68     }
69     PQclear(res);
70
71     pg_prepare_params[0] = PG_OID_INT4;
72     pg_prepare_params[1] = PG_OID_INT4;
73     res = PQprepare(conn, "index_sector_places",
74         "select place_id from placex where rank_search = $1 and geometry_sector = $2 and indexed_status > 0",
75         2, pg_prepare_params);
76     if (PQresultStatus(res) != PGRES_COMMAND_OK)
77     {
78         fprintf(stderr, "Failed preparing index_sector_places: %s\n", PQerrorMessage(conn));
79         exit(EXIT_FAILURE);
80     }
81     PQclear(res);
82
83     // Build the data for each thread
84     thread_data = (struct index_thread_data *)malloc(sizeof(struct index_thread_data)*num_threads);
85         for (i = 0; i < num_threads; i++)
86         {
87                 thread_data[i].conn = PQconnectdb(conninfo);
88             if (PQstatus(thread_data[i].conn) != CONNECTION_OK) {
89                 fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(thread_data[i].conn));
90                 exit(EXIT_FAILURE);
91             }
92
93             pg_prepare_params[0] = PG_OID_INT4;
94             res = PQprepare(thread_data[i].conn, "index_placex",
95                 "update placex set indexed_status = 0 where place_id = $1",
96                 1, pg_prepare_params);
97             if (PQresultStatus(res) != PGRES_COMMAND_OK)
98             {
99                 fprintf(stderr, "Failed preparing index_placex: %s\n", PQerrorMessage(conn));
100                 exit(EXIT_FAILURE);
101             }
102             PQclear(res);
103
104             nominatim_exportCreatePreparedQueries(thread_data[i].conn);
105         }
106
107         // Create the output file
108         writer = NULL;
109         if (structuredoutputfile)
110         {
111                 writer = nominatim_exportXMLStart(structuredoutputfile);
112         }
113
114     fprintf(stderr, "Starting indexing rank (%i > %i ) using %i treads\n", rank_min, rank_max, num_threads);
115
116     for (rank = rank_min; rank <= rank_max; rank++)
117     {
118         printf("Starting rank %d\n", rank);
119         rankStartTime = time(0);
120         rankCountTuples = 0;
121         rankPerSecond = 0;
122
123         paramRank = PGint32(rank);
124         paramValues[0] = (char *)&paramRank;
125         paramLengths[0] = sizeof(paramRank);
126         paramFormats[0] = 1;
127         resSectors = PQexecPrepared(conn, "index_sectors", 1, paramValues, paramLengths, paramFormats, 1);
128         if (PQresultStatus(resSectors) != PGRES_TUPLES_OK)
129         {
130             fprintf(stderr, "index_sectors: SELECT failed: %s", PQerrorMessage(conn));
131             PQclear(resSectors);
132             exit(EXIT_FAILURE);
133         }
134                 if (PQftype(resSectors, 0) != PG_OID_INT4)
135                 {
136             fprintf(stderr, "Sector value has unexpected type\n");
137             PQclear(resSectors);
138             exit(EXIT_FAILURE);
139                 }
140                 if (PQftype(resSectors, 1) != PG_OID_INT8)
141                 {
142             fprintf(stderr, "Sector value has unexpected type\n");
143             PQclear(resSectors);
144             exit(EXIT_FAILURE);
145                 }
146
147                 rankTotalTuples = 0;
148         for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
149         {
150                 rankTotalTuples += PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1)));
151         }
152
153         for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
154         {
155                         sector = PGint32(*((uint32_t *)PQgetvalue(resSectors, iSector, 0)));
156                 //printf("\n Starting sector %d size %ld\n", sector, PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1))));
157
158                         // Get all the place_id's for this sector
159                 paramRank = PGint32(rank);
160                 paramValues[0] = (char *)&paramRank;
161                 paramLengths[0] = sizeof(paramRank);
162                 paramFormats[0] = 1;
163                 paramSector = PGint32(sector);
164                 paramValues[1] = (char *)&paramSector;
165                 paramLengths[1] = sizeof(paramSector);
166                 paramFormats[1] = 1;
167                 resPlaces = PQexecPrepared(conn, "index_sector_places", 2, paramValues, paramLengths, paramFormats, 1);
168                 if (PQresultStatus(resPlaces) != PGRES_TUPLES_OK)
169                 {
170                     fprintf(stderr, "index_sector_places: SELECT failed: %s", PQerrorMessage(conn));
171                     PQclear(resPlaces);
172                     exit(EXIT_FAILURE);
173                 }
174                         if (PQftype(resPlaces, 0) != PG_OID_INT4)
175                         {
176                     fprintf(stderr, "Place_id value has unexpected type\n");
177                     PQclear(resPlaces);
178                     exit(EXIT_FAILURE);
179                         }
180
181                         count = 0;
182                         rankPerSecond = 0;
183                         tuples = PQntuples(resPlaces);
184
185                         if (tuples > 0)
186                 {
187                                 // Spawn threads
188                                 for (i = 0; i < num_threads; i++)
189                                 {
190                                         thread_data[i].res = resPlaces;
191                                         thread_data[i].tuples = tuples;
192                                         thread_data[i].count = &count;
193                                         thread_data[i].count_mutex = &count_mutex;
194                                         thread_data[i].writer = writer;
195                                         thread_data[i].writer_mutex = &writer_mutex;
196                                         pthread_create(&thread_data[i].thread, NULL, &nominatim_indexThread, (void *)&thread_data[i]);
197                                 }
198
199                                 // Monitor threads to give user feedback
200                                 sleepcount = 0;
201                                 while(count < tuples)
202                                 {
203                                         usleep(1000);
204
205                                         // Aim for one update per second
206                                         if (sleepcount++ > 500)
207                                         {
208                                                 rankPerSecond = ((float)rankCountTuples + (float)count) / MAX(difftime(time(0), rankStartTime),1);
209                                                 printf("  Done %i in %i @ %f per second - Rank %i ETA (seconds): %f\n", (rankCountTuples + count), (int)(difftime(time(0), rankStartTime)), rankPerSecond, rank, ((float)(rankTotalTuples - (rankCountTuples + count)))/rankPerSecond);
210                                                 sleepcount = 0;
211                                         }
212                                 }
213
214                                 // Wait for everything to finish
215                                 for (i = 0; i < num_threads; i++)
216                                 {
217                                         pthread_join(thread_data[i].thread, NULL);
218                                 }
219
220                                 rankCountTuples += tuples;
221                 }
222
223                         // Finished sector
224                         rankPerSecond = (float)rankCountTuples / MAX(difftime(time(0), rankStartTime),1);
225                         printf("  Done %i in %i @ %f per second - ETA (seconds): %f\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond, ((float)(rankTotalTuples - rankCountTuples))/rankPerSecond);
226
227             PQclear(resPlaces);
228
229         }
230         // Finished rank
231                 printf("\r  Done %i in %i @ %f per second - FINISHED                      \n\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond);
232
233         PQclear(resSectors);
234     }
235
236     if (writer)
237     {
238         nominatim_exportXMLEnd(writer);
239     }
240 }
241
242 void *nominatim_indexThread(void * thread_data_in)
243 {
244         struct index_thread_data * thread_data = (struct index_thread_data * )thread_data_in;
245
246         PGresult * res;
247
248     const char *paramValues[1];
249     int         paramLengths[1];
250     int         paramFormats[1];
251     uint32_t    paramPlaceID;
252     uint32_t place_id;
253
254         while(1)
255         {
256                 pthread_mutex_lock( thread_data->count_mutex );
257                 if (*(thread_data->count) >= thread_data->tuples)
258                 {
259                         pthread_mutex_unlock( thread_data->count_mutex );
260                         break;
261                 }
262
263                 place_id = PGint32(*((uint32_t *)PQgetvalue(thread_data->res, *thread_data->count, 0)));
264                 (*thread_data->count)++;
265
266                 pthread_mutex_unlock( thread_data->count_mutex );
267
268                 //printf("  Processing place_id %ld\n", place_id);
269                 paramPlaceID = PGint32(place_id);
270         paramValues[0] = (char *)&paramPlaceID;
271         paramLengths[0] = sizeof(paramPlaceID);
272         paramFormats[0] = 1;
273         res = PQexecPrepared(thread_data->conn, "index_placex", 1, paramValues, paramLengths, paramFormats, 1);
274         if (PQresultStatus(res) != PGRES_COMMAND_OK)
275         {
276             fprintf(stderr, "index_placex: UPDATE failed: %s", PQerrorMessage(thread_data->conn));
277             PQclear(res);
278             exit(EXIT_FAILURE);
279         }
280         PQclear(res);
281
282         if (thread_data->writer)
283         {
284                 nominatim_exportPlace(place_id, thread_data->conn, thread_data->writer, thread_data->writer_mutex);
285         }
286         }
287
288         return NULL;
289 }