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 to %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);
144 paramRank = PGint32(rank);
145 paramValues[0] = (char *)¶mRank;
146 paramLengths[0] = sizeof(paramRank);
149 resSectors = PQexecPrepared(conn, "index_nosectors", 1, paramValues, paramLengths, paramFormats, 1);
151 resSectors = PQexecPrepared(conn, "index_sectors", 1, paramValues, paramLengths, paramFormats, 1);
152 if (PQresultStatus(resSectors) != PGRES_TUPLES_OK)
154 fprintf(stderr, "index_sectors: SELECT failed: %s", PQerrorMessage(conn));
158 if (PQftype(resSectors, 0) != PG_OID_INT4)
160 fprintf(stderr, "Sector value has unexpected type\n");
164 if (PQftype(resSectors, 1) != PG_OID_INT8)
166 fprintf(stderr, "Sector value has unexpected type\n");
172 for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
174 rankTotalTuples += PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1)));
177 rankStartTime = time(0);
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;
281 time_t updateStartTime;
285 pthread_mutex_lock( thread_data->count_mutex );
286 if (*(thread_data->count) >= thread_data->tuples)
288 pthread_mutex_unlock( thread_data->count_mutex );
292 place_id = PGint32(*((uint32_t *)PQgetvalue(thread_data->res, *thread_data->count, 0)));
293 (*thread_data->count)++;
295 pthread_mutex_unlock( thread_data->count_mutex );
297 if (verbose) printf(" Processing place_id %d\n", place_id);
299 updateStartTime = time(0);
300 paramPlaceID = PGint32(place_id);
301 paramValues[0] = (char *)¶mPlaceID;
302 paramLengths[0] = sizeof(paramPlaceID);
304 res = PQexecPrepared(thread_data->conn, "index_placex", 1, paramValues, paramLengths, paramFormats, 1);
305 if (PQresultStatus(res) != PGRES_COMMAND_OK)
307 fprintf(stderr, "index_placex: UPDATE failed: %s", PQerrorMessage(thread_data->conn));
312 if (difftime(time(0), updateStartTime) > 1) printf(" Slow place_id %d\n", place_id);
314 if (thread_data->writer)
316 nominatim_exportPlace(place_id, thread_data->conn, thread_data->writer, thread_data->writer_mutex);