15 #include "nominatim.h"
18 #include "postgresql.h"
22 void nominatim_index(int rank_min, int rank_max, int num_threads, const char *conninfo, const char *structuredoutputfile)
24 struct index_thread_data * thread_data;
25 pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
26 int tuples, count, sleepcount;
35 PGresult * resSectors;
44 const char *paramValues[2];
51 xmlTextWriterPtr writer;
52 pthread_mutex_t writer_mutex = PTHREAD_MUTEX_INITIALIZER;
54 Oid pg_prepare_params[2];
56 conn = PQconnectdb(conninfo);
57 if (PQstatus(conn) != CONNECTION_OK) {
58 fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn));
62 pg_prepare_params[0] = PG_OID_INT4;
63 res = PQprepare(conn, "index_sectors",
64 "select geometry_sector,count(*) from placex where rank_search = $1 and indexed_status > 0 group by geometry_sector order by geometry_sector",
65 1, pg_prepare_params);
66 if (PQresultStatus(res) != PGRES_COMMAND_OK)
68 fprintf(stderr, "Failed preparing index_sectors: %s\n", PQerrorMessage(conn));
73 pg_prepare_params[0] = PG_OID_INT4;
74 res = PQprepare(conn, "index_nosectors",
75 "select 0::integer,count(*) from placex where rank_search = $1 and indexed_status > 0",
76 1, pg_prepare_params);
77 if (PQresultStatus(res) != PGRES_COMMAND_OK)
79 fprintf(stderr, "Failed preparing index_sectors: %s\n", PQerrorMessage(conn));
84 pg_prepare_params[0] = PG_OID_INT4;
85 pg_prepare_params[1] = PG_OID_INT4;
86 res = PQprepare(conn, "index_sector_places",
87 "select place_id from placex where rank_search = $1 and geometry_sector = $2 and indexed_status > 0",
88 2, pg_prepare_params);
89 if (PQresultStatus(res) != PGRES_COMMAND_OK)
91 fprintf(stderr, "Failed preparing index_sector_places: %s\n", PQerrorMessage(conn));
96 pg_prepare_params[0] = PG_OID_INT4;
97 res = PQprepare(conn, "index_nosector_places",
98 "select place_id from placex where rank_search = $1 and indexed_status > 0 order by geometry_sector",
99 1, pg_prepare_params);
100 if (PQresultStatus(res) != PGRES_COMMAND_OK)
102 fprintf(stderr, "Failed preparing index_nosector_places: %s\n", PQerrorMessage(conn));
107 // Build the data for each thread
108 thread_data = (struct index_thread_data *)malloc(sizeof(struct index_thread_data)*num_threads);
109 for (i = 0; i < num_threads; i++)
111 thread_data[i].conn = PQconnectdb(conninfo);
112 if (PQstatus(thread_data[i].conn) != CONNECTION_OK) {
113 fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(thread_data[i].conn));
117 pg_prepare_params[0] = PG_OID_INT4;
118 res = PQprepare(thread_data[i].conn, "index_placex",
119 "update placex set indexed_status = 0 where place_id = $1",
120 1, pg_prepare_params);
121 if (PQresultStatus(res) != PGRES_COMMAND_OK)
123 fprintf(stderr, "Failed preparing index_placex: %s\n", PQerrorMessage(conn));
128 res = PQexec(thread_data[i].conn, "set enable_seqscan = false");
129 if (PQresultStatus(res) != PGRES_COMMAND_OK)
131 fprintf(stderr, "Failed disabling sequential scan: %s\n", PQerrorMessage(conn));
136 nominatim_exportCreatePreparedQueries(thread_data[i].conn);
139 // Create the output file
141 if (structuredoutputfile)
143 writer = nominatim_exportXMLStart(structuredoutputfile);
146 fprintf(stderr, "Starting indexing rank (%i to %i) using %i treads\n", rank_min, rank_max, num_threads);
148 for (rank = rank_min; rank <= rank_max; rank++)
150 printf("Starting rank %d\n", rank);
154 paramRank = PGint32(rank);
155 paramValues[0] = (char *)¶mRank;
156 paramLengths[0] = sizeof(paramRank);
159 resSectors = PQexecPrepared(conn, "index_nosectors", 1, paramValues, paramLengths, paramFormats, 1);
161 resSectors = PQexecPrepared(conn, "index_sectors", 1, paramValues, paramLengths, paramFormats, 1);
162 if (PQresultStatus(resSectors) != PGRES_TUPLES_OK)
164 fprintf(stderr, "index_sectors: SELECT failed: %s", PQerrorMessage(conn));
168 if (PQftype(resSectors, 0) != PG_OID_INT4)
170 fprintf(stderr, "Sector value has unexpected type\n");
174 if (PQftype(resSectors, 1) != PG_OID_INT8)
176 fprintf(stderr, "Sector value has unexpected type\n");
182 for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
184 rankTotalTuples += PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1)));
187 rankStartTime = time(0);
188 for (iSector = 0; iSector <= PQntuples(resSectors); iSector++)
192 resPlaces = PQgetResult(conn);
193 if (PQresultStatus(resPlaces) != PGRES_TUPLES_OK)
195 fprintf(stderr, "index_sector_places: SELECT failed: %s", PQerrorMessage(conn));
199 if (PQftype(resPlaces, 0) != PG_OID_INT4)
201 fprintf(stderr, "Place_id value has unexpected type\n");
205 resNULL = PQgetResult(conn);
208 fprintf(stderr, "Unexpected non-null response\n");
213 if (iSector < PQntuples(resSectors))
215 sector = PGint32(*((uint32_t *)PQgetvalue(resSectors, iSector, 0)));
216 //printf("\n Starting sector %d size %ld\n", sector, PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1))));
218 // Get all the place_id's for this sector
219 paramRank = PGint32(rank);
220 paramValues[0] = (char *)¶mRank;
221 paramLengths[0] = sizeof(paramRank);
223 paramSector = PGint32(sector);
224 paramValues[1] = (char *)¶mSector;
225 paramLengths[1] = sizeof(paramSector);
228 iResult = PQsendQueryPrepared(conn, "index_nosector_places", 1, paramValues, paramLengths, paramFormats, 1);
230 iResult = PQsendQueryPrepared(conn, "index_sector_places", 2, paramValues, paramLengths, paramFormats, 1);
233 fprintf(stderr, "index_sector_places: SELECT failed: %s", PQerrorMessage(conn));
243 tuples = PQntuples(resPlaces);
248 for (i = 0; i < num_threads; i++)
250 thread_data[i].res = resPlaces;
251 thread_data[i].tuples = tuples;
252 thread_data[i].count = &count;
253 thread_data[i].count_mutex = &count_mutex;
254 thread_data[i].writer = writer;
255 thread_data[i].writer_mutex = &writer_mutex;
256 pthread_create(&thread_data[i].thread, NULL, &nominatim_indexThread, (void *)&thread_data[i]);
259 // Monitor threads to give user feedback
261 while(count < tuples)
265 // Aim for one update per second
266 if (sleepcount++ > 500)
268 rankPerSecond = ((float)rankCountTuples + (float)count) / MAX(difftime(time(0), rankStartTime),1);
269 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);
274 // Wait for everything to finish
275 for (i = 0; i < num_threads; i++)
277 pthread_join(thread_data[i].thread, NULL);
280 rankCountTuples += tuples;
284 rankPerSecond = (float)rankCountTuples / MAX(difftime(time(0), rankStartTime),1);
285 printf(" Done %i in %i @ %f per second - ETA (seconds): %f\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond, ((float)(rankTotalTuples - rankCountTuples))/rankPerSecond);
291 printf("\r Done %i in %i @ %f per second - FINISHED \n\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond);
298 nominatim_exportXMLEnd(writer);
302 void *nominatim_indexThread(void * thread_data_in)
304 struct index_thread_data * thread_data = (struct index_thread_data * )thread_data_in;
308 const char *paramValues[1];
311 uint32_t paramPlaceID;
313 time_t updateStartTime;
317 pthread_mutex_lock( thread_data->count_mutex );
318 if (*(thread_data->count) >= thread_data->tuples)
320 pthread_mutex_unlock( thread_data->count_mutex );
324 place_id = PGint32(*((uint32_t *)PQgetvalue(thread_data->res, *thread_data->count, 0)));
325 (*thread_data->count)++;
327 pthread_mutex_unlock( thread_data->count_mutex );
329 if (verbose) printf(" Processing place_id %d\n", place_id);
331 updateStartTime = time(0);
332 paramPlaceID = PGint32(place_id);
333 paramValues[0] = (char *)¶mPlaceID;
334 paramLengths[0] = sizeof(paramPlaceID);
336 res = PQexecPrepared(thread_data->conn, "index_placex", 1, paramValues, paramLengths, paramFormats, 1);
337 if (PQresultStatus(res) != PGRES_COMMAND_OK)
339 fprintf(stderr, "index_placex: UPDATE failed: %s", PQerrorMessage(thread_data->conn));
344 if (difftime(time(0), updateStartTime) > 1) printf(" Slow place_id %d\n", place_id);
346 if (thread_data->writer)
348 nominatim_exportPlace(place_id, thread_data->conn, thread_data->writer, thread_data->writer_mutex);