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 nominatim_exportCreatePreparedQueries(thread_data[i].conn);
129 // Create the output file
131 if (structuredoutputfile)
133 writer = nominatim_exportXMLStart(structuredoutputfile);
136 fprintf(stderr, "Starting indexing rank (%i > %i ) using %i treads\n", rank_min, rank_max, num_threads);
138 for (rank = rank_min; rank <= rank_max; rank++)
140 printf("Starting rank %d\n", rank);
141 rankStartTime = time(0);
145 paramRank = PGint32(rank);
146 paramValues[0] = (char *)¶mRank;
147 paramLengths[0] = sizeof(paramRank);
150 resSectors = PQexecPrepared(conn, "index_nosectors", 1, paramValues, paramLengths, paramFormats, 1);
152 resSectors = PQexecPrepared(conn, "index_sectors", 1, paramValues, paramLengths, paramFormats, 1);
153 if (PQresultStatus(resSectors) != PGRES_TUPLES_OK)
155 fprintf(stderr, "index_sectors: SELECT failed: %s", PQerrorMessage(conn));
159 if (PQftype(resSectors, 0) != PG_OID_INT4)
161 fprintf(stderr, "Sector value has unexpected type\n");
165 if (PQftype(resSectors, 1) != PG_OID_INT8)
167 fprintf(stderr, "Sector value has unexpected type\n");
173 for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
175 rankTotalTuples += PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1)));
178 for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
180 sector = PGint32(*((uint32_t *)PQgetvalue(resSectors, iSector, 0)));
181 //printf("\n Starting sector %d size %ld\n", sector, PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1))));
183 // Get all the place_id's for this sector
184 paramRank = PGint32(rank);
185 paramValues[0] = (char *)¶mRank;
186 paramLengths[0] = sizeof(paramRank);
188 paramSector = PGint32(sector);
189 paramValues[1] = (char *)¶mSector;
190 paramLengths[1] = sizeof(paramSector);
193 resPlaces = PQexecPrepared(conn, "index_nosector_places", 1, paramValues, paramLengths, paramFormats, 1);
195 resPlaces = PQexecPrepared(conn, "index_sector_places", 2, paramValues, paramLengths, paramFormats, 1);
196 if (PQresultStatus(resPlaces) != PGRES_TUPLES_OK)
198 fprintf(stderr, "index_sector_places: SELECT failed: %s", PQerrorMessage(conn));
202 if (PQftype(resPlaces, 0) != PG_OID_INT4)
204 fprintf(stderr, "Place_id value has unexpected type\n");
211 tuples = PQntuples(resPlaces);
216 for (i = 0; i < num_threads; i++)
218 thread_data[i].res = resPlaces;
219 thread_data[i].tuples = tuples;
220 thread_data[i].count = &count;
221 thread_data[i].count_mutex = &count_mutex;
222 thread_data[i].writer = writer;
223 thread_data[i].writer_mutex = &writer_mutex;
224 pthread_create(&thread_data[i].thread, NULL, &nominatim_indexThread, (void *)&thread_data[i]);
227 // Monitor threads to give user feedback
229 while(count < tuples)
233 // Aim for one update per second
234 if (sleepcount++ > 500)
236 rankPerSecond = ((float)rankCountTuples + (float)count) / MAX(difftime(time(0), rankStartTime),1);
237 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);
242 // Wait for everything to finish
243 for (i = 0; i < num_threads; i++)
245 pthread_join(thread_data[i].thread, NULL);
248 rankCountTuples += tuples;
252 rankPerSecond = (float)rankCountTuples / MAX(difftime(time(0), rankStartTime),1);
253 printf(" Done %i in %i @ %f per second - ETA (seconds): %f\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond, ((float)(rankTotalTuples - rankCountTuples))/rankPerSecond);
259 printf("\r Done %i in %i @ %f per second - FINISHED \n\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond);
266 nominatim_exportXMLEnd(writer);
270 void *nominatim_indexThread(void * thread_data_in)
272 struct index_thread_data * thread_data = (struct index_thread_data * )thread_data_in;
276 const char *paramValues[1];
279 uint32_t paramPlaceID;
284 pthread_mutex_lock( thread_data->count_mutex );
285 if (*(thread_data->count) >= thread_data->tuples)
287 pthread_mutex_unlock( thread_data->count_mutex );
291 place_id = PGint32(*((uint32_t *)PQgetvalue(thread_data->res, *thread_data->count, 0)));
292 (*thread_data->count)++;
294 pthread_mutex_unlock( thread_data->count_mutex );
296 // printf(" Processing place_id %d\n", place_id);
297 paramPlaceID = PGint32(place_id);
298 paramValues[0] = (char *)¶mPlaceID;
299 paramLengths[0] = sizeof(paramPlaceID);
301 res = PQexecPrepared(thread_data->conn, "index_placex", 1, paramValues, paramLengths, paramFormats, 1);
302 if (PQresultStatus(res) != PGRES_COMMAND_OK)
304 fprintf(stderr, "index_placex: UPDATE failed: %s", PQerrorMessage(thread_data->conn));
310 if (thread_data->writer)
312 nominatim_exportPlace(place_id, thread_data->conn, thread_data->writer, thread_data->writer_mutex);