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 pg_prepare_params[1] = PG_OID_INT4;
73 res = PQprepare(conn, "index_sector_places",
74 "select place_id from placex where rank_search = $1 and geometry_sector = $2 and indexed_status > 0",
75 2, pg_prepare_params);
76 if (PQresultStatus(res) != PGRES_COMMAND_OK)
78 fprintf(stderr, "Failed preparing index_sector_places: %s\n", PQerrorMessage(conn));
83 // Build the data for each thread
84 thread_data = (struct index_thread_data *)malloc(sizeof(struct index_thread_data)*num_threads);
85 for (i = 0; i < num_threads; i++)
87 thread_data[i].conn = PQconnectdb(conninfo);
88 if (PQstatus(thread_data[i].conn) != CONNECTION_OK) {
89 fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(thread_data[i].conn));
93 pg_prepare_params[0] = PG_OID_INT4;
94 res = PQprepare(thread_data[i].conn, "index_placex",
95 "update placex set indexed_status = 0 where place_id = $1",
96 1, pg_prepare_params);
97 if (PQresultStatus(res) != PGRES_COMMAND_OK)
99 fprintf(stderr, "Failed preparing index_placex: %s\n", PQerrorMessage(conn));
104 nominatim_exportCreatePreparedQueries(thread_data[i].conn);
107 // Create the output file
109 if (structuredoutputfile)
111 writer = nominatim_exportXMLStart(structuredoutputfile);
114 fprintf(stderr, "Starting indexing rank (%i > %i ) using %i treads\n", rank_min, rank_max, num_threads);
116 for (rank = rank_min; rank <= rank_max; rank++)
118 printf("Starting rank %d\n", rank);
119 rankStartTime = time(0);
123 paramRank = PGint32(rank);
124 paramValues[0] = (char *)¶mRank;
125 paramLengths[0] = sizeof(paramRank);
127 resSectors = PQexecPrepared(conn, "index_sectors", 1, paramValues, paramLengths, paramFormats, 1);
128 if (PQresultStatus(resSectors) != PGRES_TUPLES_OK)
130 fprintf(stderr, "index_sectors: SELECT failed: %s", PQerrorMessage(conn));
134 if (PQftype(resSectors, 0) != PG_OID_INT4)
136 fprintf(stderr, "Sector value has unexpected type\n");
140 if (PQftype(resSectors, 1) != PG_OID_INT8)
142 fprintf(stderr, "Sector value has unexpected type\n");
148 for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
150 rankTotalTuples += PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1)));
153 for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
155 sector = PGint32(*((uint32_t *)PQgetvalue(resSectors, iSector, 0)));
156 //printf("\n Starting sector %d size %ld\n", sector, PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1))));
158 // Get all the place_id's for this sector
159 paramRank = PGint32(rank);
160 paramValues[0] = (char *)¶mRank;
161 paramLengths[0] = sizeof(paramRank);
163 paramSector = PGint32(sector);
164 paramValues[1] = (char *)¶mSector;
165 paramLengths[1] = sizeof(paramSector);
167 resPlaces = PQexecPrepared(conn, "index_sector_places", 2, paramValues, paramLengths, paramFormats, 1);
168 if (PQresultStatus(resPlaces) != PGRES_TUPLES_OK)
170 fprintf(stderr, "index_sector_places: SELECT failed: %s", PQerrorMessage(conn));
174 if (PQftype(resPlaces, 0) != PG_OID_INT4)
176 fprintf(stderr, "Place_id value has unexpected type\n");
183 tuples = PQntuples(resPlaces);
188 for (i = 0; i < num_threads; i++)
190 thread_data[i].res = resPlaces;
191 thread_data[i].tuples = tuples;
192 thread_data[i].count = &count;
193 thread_data[i].count_mutex = &count_mutex;
194 thread_data[i].writer = writer;
195 thread_data[i].writer_mutex = &writer_mutex;
196 pthread_create(&thread_data[i].thread, NULL, &nominatim_indexThread, (void *)&thread_data[i]);
199 // Monitor threads to give user feedback
201 while(count < tuples)
205 // Aim for one update per second
206 if (sleepcount++ > 500)
208 rankPerSecond = ((float)rankCountTuples + (float)count) / MAX(difftime(time(0), rankStartTime),1);
209 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);
214 // Wait for everything to finish
215 for (i = 0; i < num_threads; i++)
217 pthread_join(thread_data[i].thread, NULL);
220 rankCountTuples += tuples;
224 rankPerSecond = (float)rankCountTuples / MAX(difftime(time(0), rankStartTime),1);
225 printf(" Done %i in %i @ %f per second - ETA (seconds): %f\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond, ((float)(rankTotalTuples - rankCountTuples))/rankPerSecond);
231 printf("\r Done %i in %i @ %f per second - FINISHED \n\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond);
238 nominatim_exportXMLEnd(writer);
242 void *nominatim_indexThread(void * thread_data_in)
244 struct index_thread_data * thread_data = (struct index_thread_data * )thread_data_in;
248 const char *paramValues[1];
251 uint32_t paramPlaceID;
256 pthread_mutex_lock( thread_data->count_mutex );
257 if (*(thread_data->count) >= thread_data->tuples)
259 pthread_mutex_unlock( thread_data->count_mutex );
263 place_id = PGint32(*((uint32_t *)PQgetvalue(thread_data->res, *thread_data->count, 0)));
264 (*thread_data->count)++;
266 pthread_mutex_unlock( thread_data->count_mutex );
268 //printf(" Processing place_id %ld\n", place_id);
269 paramPlaceID = PGint32(place_id);
270 paramValues[0] = (char *)¶mPlaceID;
271 paramLengths[0] = sizeof(paramPlaceID);
273 res = PQexecPrepared(thread_data->conn, "index_placex", 1, paramValues, paramLengths, paramFormats, 1);
274 if (PQresultStatus(res) != PGRES_COMMAND_OK)
276 fprintf(stderr, "index_placex: UPDATE failed: %s", PQerrorMessage(thread_data->conn));
282 if (thread_data->writer)
284 nominatim_exportPlace(place_id, thread_data->conn, thread_data->writer, thread_data->writer_mutex);