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 = false and name is not null group by geometry_sector order by geometry_sector",
63 1, pg_prepare_params);
64 if (PQresultStatus(res) != PGRES_COMMAND_OK) exit(EXIT_FAILURE);
67 pg_prepare_params[0] = PG_OID_INT4;
68 pg_prepare_params[1] = PG_OID_INT4;
69 res = PQprepare(conn, "index_sector_places",
70 "select place_id from placex where rank_search = $1 and geometry_index(geometry,indexed,name) = $2",
71 2, pg_prepare_params);
72 if (PQresultStatus(res) != PGRES_COMMAND_OK) exit(EXIT_FAILURE);
75 // Build the data for each thread
76 thread_data = (struct index_thread_data *)malloc(sizeof(struct index_thread_data)*num_threads);
77 for (i = 0; i < num_threads; i++)
79 thread_data[i].conn = PQconnectdb(conninfo);
80 if (PQstatus(thread_data[i].conn) != CONNECTION_OK) {
81 fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(thread_data[i].conn));
85 pg_prepare_params[0] = PG_OID_INT8;
86 res = PQprepare(thread_data[i].conn, "index_placex",
87 "update placex set indexed = true where place_id = $1",
88 1, pg_prepare_params);
89 if (PQresultStatus(res) != PGRES_COMMAND_OK) exit(EXIT_FAILURE);
92 nominatim_exportCreatePreparedQueries(thread_data[i].conn);
95 // Create the output file
97 if (structuredoutputfile)
99 writer = nominatim_exportXMLStart(structuredoutputfile);
102 fprintf(stderr, "Starting indexing rank (%i > %i ) using %i treads\n", rank_min, rank_max, num_threads);
104 for (rank = rank_min; rank <= rank_max; rank++)
106 printf("Starting rank %d\n", rank);
107 rankStartTime = time(0);
111 paramRank = PGint32(rank);
112 paramValues[0] = (char *)¶mRank;
113 paramLengths[0] = sizeof(paramRank);
115 resSectors = PQexecPrepared(conn, "index_sectors", 1, paramValues, paramLengths, paramFormats, 1);
116 if (PQresultStatus(resSectors) != PGRES_TUPLES_OK)
118 fprintf(stderr, "index_sectors: SELECT failed: %s", PQerrorMessage(conn));
122 if (PQftype(resSectors, 0) != PG_OID_INT4)
124 fprintf(stderr, "Sector value has unexpected type\n");
128 if (PQftype(resSectors, 1) != PG_OID_INT8)
130 fprintf(stderr, "Sector value has unexpected type\n");
136 for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
138 rankTotalTuples += PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1)));
141 for (iSector = 0; iSector < PQntuples(resSectors); iSector++)
143 sector = PGint32(*((uint32_t *)PQgetvalue(resSectors, iSector, 0)));
144 //printf("\n Starting sector %d size %ld\n", sector, PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1))));
146 // Get all the place_id's for this sector
147 paramRank = PGint32(rank);
148 paramValues[0] = (char *)¶mRank;
149 paramLengths[0] = sizeof(paramRank);
151 paramSector = PGint32(sector);
152 paramValues[1] = (char *)¶mSector;
153 paramLengths[1] = sizeof(paramSector);
155 resPlaces = PQexecPrepared(conn, "index_sector_places", 2, paramValues, paramLengths, paramFormats, 1);
156 if (PQresultStatus(resPlaces) != PGRES_TUPLES_OK)
158 fprintf(stderr, "index_sector_places: SELECT failed: %s", PQerrorMessage(conn));
162 if (PQftype(resPlaces, 0) != PG_OID_INT8)
164 fprintf(stderr, "Place_id value has unexpected type\n");
171 tuples = PQntuples(resPlaces);
176 for (i = 0; i < num_threads; i++)
178 thread_data[i].res = resPlaces;
179 thread_data[i].tuples = tuples;
180 thread_data[i].count = &count;
181 thread_data[i].count_mutex = &count_mutex;
182 thread_data[i].writer = writer;
183 thread_data[i].writer_mutex = &writer_mutex;
184 pthread_create(&thread_data[i].thread, NULL, &nominatim_indexThread, (void *)&thread_data[i]);
187 // Monitor threads to give user feedback
189 while(count < tuples)
193 // Aim for one update per second
194 if (sleepcount++ > 500)
196 rankPerSecond = ((float)rankCountTuples + (float)count) / MAX(difftime(time(0), rankStartTime),1);
197 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);
202 // Wait for everything to finish
203 for (i = 0; i < num_threads; i++)
205 pthread_join(thread_data[i].thread, NULL);
208 rankCountTuples += tuples;
212 rankPerSecond = (float)rankCountTuples / MAX(difftime(time(0), rankStartTime),1);
213 printf(" Done %i in %i @ %f per second - ETA (seconds): %f\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond, ((float)(rankTotalTuples - rankCountTuples))/rankPerSecond);
219 printf("\r Done %i in %i @ %f per second - FINISHED \n\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond);
226 nominatim_exportXMLEnd(writer);
230 void *nominatim_indexThread(void * thread_data_in)
232 struct index_thread_data * thread_data = (struct index_thread_data * )thread_data_in;
236 const char *paramValues[1];
239 uint64_t paramPlaceID;
244 pthread_mutex_lock( thread_data->count_mutex );
245 if (*(thread_data->count) >= thread_data->tuples)
247 pthread_mutex_unlock( thread_data->count_mutex );
251 place_id = PGint64(*((uint64_t *)PQgetvalue(thread_data->res, *thread_data->count, 0)));
252 (*thread_data->count)++;
254 pthread_mutex_unlock( thread_data->count_mutex );
256 //printf(" Processing place_id %ld\n", place_id);
257 paramPlaceID = PGint64(place_id);
258 paramValues[0] = (char *)¶mPlaceID;
259 paramLengths[0] = sizeof(paramPlaceID);
261 res = PQexecPrepared(thread_data->conn, "index_placex", 1, paramValues, paramLengths, paramFormats, 1);
262 if (PQresultStatus(res) != PGRES_COMMAND_OK)
264 fprintf(stderr, "index_placex: UPDATE failed: %s", PQerrorMessage(thread_data->conn));
270 if (thread_data->writer)
272 nominatim_exportPlace(place_id, thread_data->conn, thread_data->writer, thread_data->writer_mutex);