]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/index.c
index on geometry of interpolation lines, and more improvements.
[nominatim.git] / nominatim / index.c
1 /*
2  * triggers indexing (reparenting etc.) through setting resetting indexed_status: update placex/osmline set indexed_status = 0 where indexed_status > 0
3  * triggers placex_update and osmline_update
4 */
5
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <assert.h>
11 #include <pthread.h>
12 #include <time.h>
13 #include <stdint.h>
14
15 #include <libpq-fe.h>
16
17 #include "nominatim.h"
18 #include "index.h"
19 #include "export.h"
20 #include "postgresql.h"
21
22 extern int verbose;
23
24 void run_indexing(int rank, int interpolation, PGconn *conn, int num_threads, 
25 struct index_thread_data * thread_data, const char *structuredoutputfile)
26 {
27     int tuples, count, sleepcount;
28     pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
29     
30     time_t rankStartTime;
31     int rankTotalTuples;
32     int rankCountTuples;
33     float rankPerSecond;
34     
35     PGresult * resSectors;
36     PGresult * resPlaces;
37     PGresult * resNULL;
38     
39     int i;
40     int iSector;
41     int iResult;
42     
43     const char *paramValues[2];
44     int         paramLengths[2];
45     int         paramFormats[2];
46     uint32_t    paramRank;
47     uint32_t    paramSector;
48     uint32_t    sector;
49     
50     xmlTextWriterPtr writer;
51     pthread_mutex_t writer_mutex = PTHREAD_MUTEX_INITIALIZER;
52     
53     // Create the output file
54     writer = NULL;
55     if (structuredoutputfile)
56     {
57         writer = nominatim_exportXMLStart(structuredoutputfile);
58     }
59     
60     if (interpolation)
61     {
62         fprintf(stderr, "Starting interpolation lines (location_property_osmline)\n");
63     }
64     else
65     {
66         fprintf(stderr, "Starting rank %d\n", rank);
67     }
68     
69     rankCountTuples = 0;
70     rankPerSecond = 0;
71
72     paramRank = PGint32(rank);
73     paramValues[0] = (char *)&paramRank;
74     paramLengths[0] = sizeof(paramRank);
75     paramFormats[0] = 1;
76     
77     if (interpolation)
78     {
79         resSectors = PQexecPrepared(conn, "index_sectors_osmline", 0, NULL, 0, NULL, 1);
80     }
81     else
82     {
83         resSectors = PQexecPrepared(conn, "index_sectors", 1, paramValues, paramLengths, paramFormats, 1);
84     }
85     if (PQresultStatus(resSectors) != PGRES_TUPLES_OK)
86     {
87         fprintf(stderr, "index_sectors: SELECT failed: %s", PQerrorMessage(conn));
88         PQclear(resSectors);
89         exit(EXIT_FAILURE);
90     }
91     if (PQftype(resSectors, 0) != PG_OID_INT4)
92     {
93         fprintf(stderr, "Sector value has unexpected type\n");
94         PQclear(resSectors);
95         exit(EXIT_FAILURE);
96     }
97     if (PQftype(resSectors, 1) != PG_OID_INT8)
98     {
99         fprintf(stderr, "Sector value has unexpected type\n");
100         PQclear(resSectors);
101         exit(EXIT_FAILURE);
102     }
103     
104     rankTotalTuples = 0;
105     for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
106     {
107         rankTotalTuples += PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1)));
108     }
109
110     rankStartTime = time(0);
111     for (iSector = 0; iSector <= PQntuples(resSectors); iSector++)
112     {
113         if (iSector > 0)
114         {
115             resPlaces = PQgetResult(conn);
116             if (PQresultStatus(resPlaces) != PGRES_TUPLES_OK)
117             {
118                 fprintf(stderr, "index_sector_places: SELECT failed: %s", PQerrorMessage(conn));
119                 PQclear(resPlaces);
120                 exit(EXIT_FAILURE);
121             }
122             if (PQftype(resPlaces, 0) != PG_OID_INT8)
123             {
124                 fprintf(stderr, "Place_id value has unexpected type\n");
125                 PQclear(resPlaces);
126                 exit(EXIT_FAILURE);
127             }
128             resNULL = PQgetResult(conn);
129             if (resNULL != NULL)
130             {
131                 fprintf(stderr, "Unexpected non-null response\n");
132                 exit(EXIT_FAILURE);
133             }
134         }
135
136         if (iSector < PQntuples(resSectors))
137         {
138             sector = PGint32(*((uint32_t *)PQgetvalue(resSectors, iSector, 0)));
139 //                fprintf(stderr, "\n Starting sector %d size %ld\n", sector, PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1))));
140
141             // Get all the place_id's for this sector
142             paramRank = PGint32(rank);
143             paramValues[0] = (char *)&paramRank;
144             paramLengths[0] = sizeof(paramRank);
145             paramFormats[0] = 1;
146             paramSector = PGint32(sector);
147             paramValues[1] = (char *)&paramSector;
148             paramLengths[1] = sizeof(paramSector);
149             paramFormats[1] = 1;
150             if (rankTotalTuples-rankCountTuples < num_threads*1000)
151             {
152                 // no sectors
153                 if (interpolation)
154                 {
155                     iResult = PQsendQueryPrepared(conn, "index_nosector_places_osmline", 0, NULL, 0, NULL, 1);
156                 }
157                 else
158                 {
159                     iResult = PQsendQueryPrepared(conn, "index_nosector_places", 1, paramValues, paramLengths, paramFormats, 1);
160                 }
161             }
162             else
163             {
164                 if (interpolation)
165                 {
166                     iResult = PQsendQueryPrepared(conn, "index_sector_places", 2, paramValues, paramLengths, paramFormats, 1);
167                 }
168                 else
169                 {
170                     iResult = PQsendQueryPrepared(conn, "index_sector_places_osmline", 1, paramValues, paramLengths, paramFormats, 1);
171                 }
172             }
173             if (!iResult)
174             {
175                 fprintf(stderr, "index_sector_places: SELECT failed: %s", PQerrorMessage(conn));
176                 PQclear(resPlaces);
177                 exit(EXIT_FAILURE);
178             }
179         }
180         if (iSector > 0)
181         {
182             count = 0;
183             rankPerSecond = 0;
184             tuples = PQntuples(resPlaces);
185
186             if (tuples > 0)
187             {
188                 // Spawn threads
189                 for (i = 0; i < num_threads; i++)
190                 {
191                     thread_data[i].res = resPlaces;
192                     thread_data[i].tuples = tuples;
193                     thread_data[i].count = &count;
194                     thread_data[i].count_mutex = &count_mutex;
195                     thread_data[i].writer = writer;
196                     thread_data[i].writer_mutex = &writer_mutex;
197                     if (interpolation)
198                     {
199                         thread_data[i].table = 0;  // use interpolations table
200                     }
201                     else
202                     {
203                         thread_data[i].table = 1;  // use placex table
204                     }
205                     pthread_create(&thread_data[i].thread, NULL, &nominatim_indexThread, (void *)&thread_data[i]);
206                 }
207
208                 // Monitor threads to give user feedback
209                 sleepcount = 0;
210                 while (count < tuples)
211                 {
212                     usleep(1000);
213
214                     // Aim for one update per second
215                     if (sleepcount++ > 500)
216                     {
217                         rankPerSecond = ((float)rankCountTuples + (float)count) / MAX(difftime(time(0), rankStartTime),1);
218                         fprintf(stderr, "  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);
219                         sleepcount = 0;
220                     }
221                 }
222
223                 // Wait for everything to finish
224                 for (i = 0; i < num_threads; i++)
225                 {
226                     pthread_join(thread_data[i].thread, NULL);
227                 }
228
229                 rankCountTuples += tuples;
230             }
231
232             // Finished sector
233             rankPerSecond = (float)rankCountTuples / MAX(difftime(time(0), rankStartTime),1);
234             fprintf(stderr, "  Done %i in %i @ %f per second - ETA (seconds): %f\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond, ((float)(rankTotalTuples - rankCountTuples))/rankPerSecond);
235
236             PQclear(resPlaces);
237         }
238         if (rankTotalTuples-rankCountTuples < num_threads*20 && iSector < PQntuples(resSectors))
239         {
240             iSector = PQntuples(resSectors) - 1;
241         }
242     }
243     // Finished rank
244     fprintf(stderr, "\r  Done %i in %i @ %f per second - FINISHED\n\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond);
245
246     PQclear(resSectors);
247     
248     
249 }
250
251 void nominatim_index(int rank_min, int rank_max, int num_threads, const char *conninfo, const char *structuredoutputfile)
252 {
253     struct index_thread_data * thread_data;
254
255     PGconn *conn;
256     PGresult * res;
257
258     int rank;
259     
260     int i;
261
262     xmlTextWriterPtr writer;
263     pthread_mutex_t writer_mutex = PTHREAD_MUTEX_INITIALIZER;
264
265     Oid pg_prepare_params[2];
266
267     conn = PQconnectdb(conninfo);
268     if (PQstatus(conn) != CONNECTION_OK)
269     {
270         fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn));
271         exit(EXIT_FAILURE);
272     }
273
274     pg_prepare_params[0] = PG_OID_INT4;
275     res = PQprepare(conn, "index_sectors",
276                     "select geometry_sector,count(*) from placex where rank_search = $1 and indexed_status > 0 group by geometry_sector order by geometry_sector",
277                     1, pg_prepare_params);
278     if (PQresultStatus(res) != PGRES_COMMAND_OK)
279     {
280         fprintf(stderr, "Failed preparing index_sectors: %s\n", PQerrorMessage(conn));
281         exit(EXIT_FAILURE);
282     }
283     PQclear(res);
284     
285     res = PQprepare(conn, "index_sectors_osmline",
286                     "select geometry_sector,count(*) from location_property_osmline where indexed_status > 0 group by geometry_sector order by geometry_sector",
287                     0, NULL);
288     if (PQresultStatus(res) != PGRES_COMMAND_OK)
289     {
290         fprintf(stderr, "Failed preparing index_sectors: %s\n", PQerrorMessage(conn));
291         exit(EXIT_FAILURE);
292     }
293     PQclear(res);
294
295     pg_prepare_params[0] = PG_OID_INT4;
296     res = PQprepare(conn, "index_nosectors",
297                     "select 0::integer,count(*) from placex where rank_search = $1 and indexed_status > 0",
298                     1, pg_prepare_params);
299     if (PQresultStatus(res) != PGRES_COMMAND_OK)
300     {
301         fprintf(stderr, "Failed preparing index_sectors: %s\n", PQerrorMessage(conn));
302         exit(EXIT_FAILURE);
303     }
304     PQclear(res);
305
306     pg_prepare_params[0] = PG_OID_INT4;
307     pg_prepare_params[1] = PG_OID_INT4;
308     res = PQprepare(conn, "index_sector_places",
309                     "select place_id from placex where rank_search = $1 and geometry_sector = $2 and indexed_status > 0",
310                     2, pg_prepare_params);
311     if (PQresultStatus(res) != PGRES_COMMAND_OK)
312     {
313         fprintf(stderr, "Failed preparing index_sector_places: %s\n", PQerrorMessage(conn));
314         exit(EXIT_FAILURE);
315     }
316     PQclear(res);
317
318     pg_prepare_params[0] = PG_OID_INT4;
319     res = PQprepare(conn, "index_nosector_places",
320                     "select place_id from placex where rank_search = $1 and indexed_status > 0 order by geometry_sector",
321                     1, pg_prepare_params);
322     if (PQresultStatus(res) != PGRES_COMMAND_OK)
323     {
324         fprintf(stderr, "Failed preparing index_nosector_places: %s\n", PQerrorMessage(conn));
325         exit(EXIT_FAILURE);
326     }
327     PQclear(res);
328     
329     pg_prepare_params[0] = PG_OID_INT4;
330     res = PQprepare(conn, "index_sector_places_osmline",
331                     "select place_id from location_property_osmline where geometry_sector = $1 and indexed_status > 0",
332                     1, pg_prepare_params);
333     if (PQresultStatus(res) != PGRES_COMMAND_OK)
334     {
335         fprintf(stderr, "Failed preparing index_sector_places: %s\n", PQerrorMessage(conn));
336         exit(EXIT_FAILURE);
337     }
338     PQclear(res);
339     
340     res = PQprepare(conn, "index_nosector_places_osmline",
341                     "select place_id from location_property_osmline where indexed_status > 0 order by geometry_sector",
342                     0, NULL);
343     if (PQresultStatus(res) != PGRES_COMMAND_OK)
344     {
345         fprintf(stderr, "Failed preparing index_nosector_places: %s\n", PQerrorMessage(conn));
346         exit(EXIT_FAILURE);
347     }
348     PQclear(res);
349     
350     // Build the data for each thread
351     thread_data = (struct index_thread_data *)malloc(sizeof(struct index_thread_data)*num_threads);
352     for (i = 0; i < num_threads; i++)
353     {
354         thread_data[i].conn = PQconnectdb(conninfo);
355         if (PQstatus(thread_data[i].conn) != CONNECTION_OK)
356         {
357             fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(thread_data[i].conn));
358             exit(EXIT_FAILURE);
359         }
360
361         pg_prepare_params[0] = PG_OID_INT8;
362         res = PQprepare(thread_data[i].conn, "index_placex",
363                         "update placex set indexed_status = 0 where place_id = $1",
364                         1, pg_prepare_params);
365         if (PQresultStatus(res) != PGRES_COMMAND_OK)
366         {
367             fprintf(stderr, "Failed preparing index_placex: %s\n", PQerrorMessage(conn));
368             exit(EXIT_FAILURE);
369         }
370         PQclear(res);
371         
372         pg_prepare_params[0] = PG_OID_INT8;
373         res = PQprepare(thread_data[i].conn, "index_osmline",
374                         "update location_property_osmline set indexed_status = 0 where place_id = $1",
375                         1, pg_prepare_params);
376         if (PQresultStatus(res) != PGRES_COMMAND_OK)
377         {
378             fprintf(stderr, "Failed preparing index_osmline: %s\n", PQerrorMessage(conn));
379             exit(EXIT_FAILURE);
380         }
381         PQclear(res);
382
383         /*res = PQexec(thread_data[i].conn, "set enable_seqscan = false");
384         if (PQresultStatus(res) != PGRES_COMMAND_OK)
385         {
386             fprintf(stderr, "Failed disabling sequential scan: %s\n", PQerrorMessage(conn));
387             exit(EXIT_FAILURE);
388         }
389         PQclear(res);*/
390
391         nominatim_exportCreatePreparedQueries(thread_data[i].conn);
392     }
393
394
395     fprintf(stderr, "Starting indexing rank (%i to %i) using %i threads\n", rank_min, rank_max, num_threads);
396
397     for (rank = rank_min; rank <= rank_max; rank++)
398     {
399         // OSMLINE: do reindexing (=> reparenting) for interpolation lines at rank 30, but before all other objects of rank 30
400         // reason: houses (rank 30) depend on the updated interpolation line, when reparenting (see placex_update in functions.sql)
401         if (rank == 30)
402         {
403             run_indexing(rank, 1, conn, num_threads, thread_data, structuredoutputfile);
404         }
405         run_indexing(rank, 0, conn, num_threads, thread_data, structuredoutputfile);
406     }
407             
408
409     if (rank == 30)
410     {
411         // Close all connections
412         for (i = 0; i < num_threads; i++)
413         {
414             PQfinish(thread_data[i].conn);
415         }
416         PQfinish(conn);
417     }
418 }
419
420 void *nominatim_indexThread(void * thread_data_in)
421 {
422     struct index_thread_data * thread_data = (struct index_thread_data * )thread_data_in;
423     struct export_data  querySet;
424
425     PGresult   *res;
426
427     const char  *paramValues[1];
428     int         paramLengths[1];
429     int         paramFormats[1];
430     uint64_t    paramPlaceID;
431     uint64_t    place_id;
432     time_t      updateStartTime;
433     uint        table;
434     
435     table = (uint)(thread_data->table);
436
437     while (1)
438     {
439         pthread_mutex_lock( thread_data->count_mutex );
440         if (*(thread_data->count) >= thread_data->tuples)
441         {
442             pthread_mutex_unlock( thread_data->count_mutex );
443             break;
444         }
445
446         place_id = PGint64(*((uint64_t *)PQgetvalue(thread_data->res, *thread_data->count, 0)));
447         (*thread_data->count)++;
448
449         pthread_mutex_unlock( thread_data->count_mutex );
450
451         if (verbose) fprintf(stderr, "  Processing place_id %ld\n", place_id);
452
453         updateStartTime = time(0);
454         int done = 0;
455
456         if (thread_data->writer)
457         {
458              nominatim_exportPlaceQueries(place_id, thread_data->conn, &querySet);
459         }
460
461         while(!done)
462         {
463             paramPlaceID = PGint64(place_id);
464             paramValues[0] = (char *)&paramPlaceID;
465             paramLengths[0] = sizeof(paramPlaceID);
466             paramFormats[0] = 1;
467             if (table == 1) // table=1 for placex
468             {
469                 res = PQexecPrepared(thread_data->conn, "index_placex", 1, paramValues, paramLengths, paramFormats, 1);
470             }
471             else // table=0 for osmline
472             {
473                 res = PQexecPrepared(thread_data->conn, "index_osmline", 1, paramValues, paramLengths, paramFormats, 1);
474             }
475             if (PQresultStatus(res) == PGRES_COMMAND_OK)
476                 done = 1;
477             else
478             {
479                 if (!strncmp(PQerrorMessage(thread_data->conn), "ERROR:  deadlock detected", 25))
480                 {
481                     if (table == 1)
482                     {
483                         fprintf(stderr, "index_placex: UPDATE failed - deadlock, retrying (%ld)\n", place_id);
484                     }
485                     else
486                     {
487                         fprintf(stderr, "index_osmline: UPDATE failed - deadlock, retrying (%ld)\n", place_id);
488                     }
489                     PQclear(res);
490                     sleep(rand() % 10);
491                 }
492                 else
493                 {
494                     if (table == 1)
495                     {
496                         fprintf(stderr, "index_placex: UPDATE failed: %s", PQerrorMessage(thread_data->conn));
497                     }
498                     else
499                     {
500                         fprintf(stderr, "index_osmline: UPDATE failed: %s", PQerrorMessage(thread_data->conn));
501                     }
502                     PQclear(res);
503                     exit(EXIT_FAILURE);
504                 }
505             }
506         }
507         PQclear(res);
508         if (difftime(time(0), updateStartTime) > 1) fprintf(stderr, "  Slow place_id %ld\n", place_id);
509
510         if (thread_data->writer)
511         {
512             nominatim_exportPlace(place_id, thread_data->conn, thread_data->writer, thread_data->writer_mutex, &querySet);
513             nominatim_exportFreeQueries(&querySet);
514         }
515     }
516
517     return NULL;
518 }