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;
42 const char *paramValues[2];
49 xmlTextWriterPtr writer;
50 pthread_mutex_t writer_mutex = PTHREAD_MUTEX_INITIALIZER;
52 Oid pg_prepare_params[2];
54 conn = PQconnectdb(conninfo);
55 if (PQstatus(conn) != CONNECTION_OK) {
56 fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn));
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)
66 fprintf(stderr, "Failed preparing index_sectors: %s\n", PQerrorMessage(conn));
71 pg_prepare_params[0] = PG_OID_INT4;
72 res = PQprepare(conn, "index_nosectors",
73 "select 0::integer,count(*) from placex where rank_search = $1 and indexed_status > 0",
74 1, pg_prepare_params);
75 if (PQresultStatus(res) != PGRES_COMMAND_OK)
77 fprintf(stderr, "Failed preparing index_sectors: %s\n", PQerrorMessage(conn));
82 pg_prepare_params[0] = PG_OID_INT4;
83 pg_prepare_params[1] = PG_OID_INT4;
84 res = PQprepare(conn, "index_sector_places",
85 "select place_id from placex where rank_search = $1 and geometry_sector = $2 and indexed_status > 0",
86 2, pg_prepare_params);
87 if (PQresultStatus(res) != PGRES_COMMAND_OK)
89 fprintf(stderr, "Failed preparing index_sector_places: %s\n", PQerrorMessage(conn));
94 pg_prepare_params[0] = PG_OID_INT4;
95 res = PQprepare(conn, "index_nosector_places",
96 "select place_id from placex where rank_search = $1 and indexed_status > 0 order by geometry_sector",
97 1, pg_prepare_params);
98 if (PQresultStatus(res) != PGRES_COMMAND_OK)
100 fprintf(stderr, "Failed preparing index_nosector_places: %s\n", PQerrorMessage(conn));
105 // Build the data for each thread
106 thread_data = (struct index_thread_data *)malloc(sizeof(struct index_thread_data)*num_threads);
107 for (i = 0; i < num_threads; i++)
109 thread_data[i].conn = PQconnectdb(conninfo);
110 if (PQstatus(thread_data[i].conn) != CONNECTION_OK) {
111 fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(thread_data[i].conn));
115 pg_prepare_params[0] = PG_OID_INT4;
116 res = PQprepare(thread_data[i].conn, "index_placex",
117 "update placex set indexed_status = 0 where place_id = $1",
118 1, pg_prepare_params);
119 if (PQresultStatus(res) != PGRES_COMMAND_OK)
121 fprintf(stderr, "Failed preparing index_placex: %s\n", PQerrorMessage(conn));
126 res = PQexec(thread_data[i].conn, "set enable_seqscan = false");
127 if (PQresultStatus(res) != PGRES_COMMAND_OK)
129 fprintf(stderr, "Failed disabling sequential scan: %s\n", PQerrorMessage(conn));
134 nominatim_exportCreatePreparedQueries(thread_data[i].conn);
137 // Create the output file
139 if (structuredoutputfile)
141 writer = nominatim_exportXMLStart(structuredoutputfile);
144 fprintf(stderr, "Starting indexing rank (%i to %i) using %i treads\n", rank_min, rank_max, num_threads);
146 for (rank = rank_min; rank <= rank_max; rank++)
148 printf("Starting rank %d\n", rank);
152 paramRank = PGint32(rank);
153 paramValues[0] = (char *)¶mRank;
154 paramLengths[0] = sizeof(paramRank);
157 resSectors = PQexecPrepared(conn, "index_nosectors", 1, paramValues, paramLengths, paramFormats, 1);
159 resSectors = PQexecPrepared(conn, "index_sectors", 1, paramValues, paramLengths, paramFormats, 1);
160 if (PQresultStatus(resSectors) != PGRES_TUPLES_OK)
162 fprintf(stderr, "index_sectors: SELECT failed: %s", PQerrorMessage(conn));
166 if (PQftype(resSectors, 0) != PG_OID_INT4)
168 fprintf(stderr, "Sector value has unexpected type\n");
172 if (PQftype(resSectors, 1) != PG_OID_INT8)
174 fprintf(stderr, "Sector value has unexpected type\n");
180 for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
182 rankTotalTuples += PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1)));
185 rankStartTime = time(0);
186 for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
188 sector = PGint32(*((uint32_t *)PQgetvalue(resSectors, iSector, 0)));
189 //printf("\n Starting sector %d size %ld\n", sector, PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1))));
191 // Get all the place_id's for this sector
192 paramRank = PGint32(rank);
193 paramValues[0] = (char *)¶mRank;
194 paramLengths[0] = sizeof(paramRank);
196 paramSector = PGint32(sector);
197 paramValues[1] = (char *)¶mSector;
198 paramLengths[1] = sizeof(paramSector);
201 resPlaces = PQexecPrepared(conn, "index_nosector_places", 1, paramValues, paramLengths, paramFormats, 1);
203 resPlaces = PQexecPrepared(conn, "index_sector_places", 2, paramValues, paramLengths, paramFormats, 1);
204 if (PQresultStatus(resPlaces) != PGRES_TUPLES_OK)
206 fprintf(stderr, "index_sector_places: SELECT failed: %s", PQerrorMessage(conn));
210 if (PQftype(resPlaces, 0) != PG_OID_INT4)
212 fprintf(stderr, "Place_id value has unexpected type\n");
219 tuples = PQntuples(resPlaces);
224 for (i = 0; i < num_threads; i++)
226 thread_data[i].res = resPlaces;
227 thread_data[i].tuples = tuples;
228 thread_data[i].count = &count;
229 thread_data[i].count_mutex = &count_mutex;
230 thread_data[i].writer = writer;
231 thread_data[i].writer_mutex = &writer_mutex;
232 pthread_create(&thread_data[i].thread, NULL, &nominatim_indexThread, (void *)&thread_data[i]);
235 // Monitor threads to give user feedback
237 while(count < tuples)
241 // Aim for one update per second
242 if (sleepcount++ > 500)
244 rankPerSecond = ((float)rankCountTuples + (float)count) / MAX(difftime(time(0), rankStartTime),1);
245 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);
250 // Wait for everything to finish
251 for (i = 0; i < num_threads; i++)
253 pthread_join(thread_data[i].thread, NULL);
256 rankCountTuples += tuples;
260 rankPerSecond = (float)rankCountTuples / MAX(difftime(time(0), rankStartTime),1);
261 printf(" Done %i in %i @ %f per second - ETA (seconds): %f\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond, ((float)(rankTotalTuples - rankCountTuples))/rankPerSecond);
267 printf("\r Done %i in %i @ %f per second - FINISHED \n\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond);
274 nominatim_exportXMLEnd(writer);
278 void *nominatim_indexThread(void * thread_data_in)
280 struct index_thread_data * thread_data = (struct index_thread_data * )thread_data_in;
284 const char *paramValues[1];
287 uint32_t paramPlaceID;
289 time_t updateStartTime;
293 pthread_mutex_lock( thread_data->count_mutex );
294 if (*(thread_data->count) >= thread_data->tuples)
296 pthread_mutex_unlock( thread_data->count_mutex );
300 place_id = PGint32(*((uint32_t *)PQgetvalue(thread_data->res, *thread_data->count, 0)));
301 (*thread_data->count)++;
303 pthread_mutex_unlock( thread_data->count_mutex );
305 if (verbose) printf(" Processing place_id %d\n", place_id);
307 updateStartTime = time(0);
308 paramPlaceID = PGint32(place_id);
309 paramValues[0] = (char *)¶mPlaceID;
310 paramLengths[0] = sizeof(paramPlaceID);
312 res = PQexecPrepared(thread_data->conn, "index_placex", 1, paramValues, paramLengths, paramFormats, 1);
313 if (PQresultStatus(res) != PGRES_COMMAND_OK)
315 fprintf(stderr, "index_placex: UPDATE failed: %s", PQerrorMessage(thread_data->conn));
320 if (difftime(time(0), updateStartTime) > 1) printf(" Slow place_id %d\n", place_id);
322 if (thread_data->writer)
324 nominatim_exportPlace(place_id, thread_data->conn, thread_data->writer, thread_data->writer_mutex);