]> git.openstreetmap.org Git - nominatim.git/blobdiff - nominatim/index.c
change default nominatim path for tests to obvious '..'
[nominatim.git] / nominatim / index.c
index 07c8daba396e8a0edde1f4adb3b071aac53a1151..13f292737082f5a4185c45086b6ff90d265c91eb 100644 (file)
@@ -116,7 +116,7 @@ void nominatim_index(int rank_min, int rank_max, int num_threads, const char *co
             exit(EXIT_FAILURE);
         }
 
             exit(EXIT_FAILURE);
         }
 
-        pg_prepare_params[0] = PG_OID_INT4;
+        pg_prepare_params[0] = PG_OID_INT8;
         res = PQprepare(thread_data[i].conn, "index_placex",
                         "update placex set indexed_status = 0 where place_id = $1",
                         1, pg_prepare_params);
         res = PQprepare(thread_data[i].conn, "index_placex",
                         "update placex set indexed_status = 0 where place_id = $1",
                         1, pg_prepare_params);
@@ -127,13 +127,13 @@ void nominatim_index(int rank_min, int rank_max, int num_threads, const char *co
         }
         PQclear(res);
 
         }
         PQclear(res);
 
-        res = PQexec(thread_data[i].conn, "set enable_seqscan = false");
+        /*res = PQexec(thread_data[i].conn, "set enable_seqscan = false");
         if (PQresultStatus(res) != PGRES_COMMAND_OK)
         {
             fprintf(stderr, "Failed disabling sequential scan: %s\n", PQerrorMessage(conn));
             exit(EXIT_FAILURE);
         }
         if (PQresultStatus(res) != PGRES_COMMAND_OK)
         {
             fprintf(stderr, "Failed disabling sequential scan: %s\n", PQerrorMessage(conn));
             exit(EXIT_FAILURE);
         }
-        PQclear(res);
+        PQclear(res);*/
 
         nominatim_exportCreatePreparedQueries(thread_data[i].conn);
     }
 
         nominatim_exportCreatePreparedQueries(thread_data[i].conn);
     }
@@ -145,11 +145,11 @@ void nominatim_index(int rank_min, int rank_max, int num_threads, const char *co
         writer = nominatim_exportXMLStart(structuredoutputfile);
     }
 
         writer = nominatim_exportXMLStart(structuredoutputfile);
     }
 
-    fprintf(stderr, "Starting indexing rank (%i to %i) using %i treads\n", rank_min, rank_max, num_threads);
+    fprintf(stderr, "Starting indexing rank (%i to %i) using %i threads\n", rank_min, rank_max, num_threads);
 
     for (rank = rank_min; rank <= rank_max; rank++)
     {
 
     for (rank = rank_min; rank <= rank_max; rank++)
     {
-        printf("Starting rank %d\n", rank);
+        fprintf(stderr, "Starting rank %d\n", rank);
         rankCountTuples = 0;
         rankPerSecond = 0;
 
         rankCountTuples = 0;
         rankPerSecond = 0;
 
@@ -157,10 +157,11 @@ void nominatim_index(int rank_min, int rank_max, int num_threads, const char *co
         paramValues[0] = (char *)&paramRank;
         paramLengths[0] = sizeof(paramRank);
         paramFormats[0] = 1;
         paramValues[0] = (char *)&paramRank;
         paramLengths[0] = sizeof(paramRank);
         paramFormats[0] = 1;
-        if (rank < 16)
-            resSectors = PQexecPrepared(conn, "index_nosectors", 1, paramValues, paramLengths, paramFormats, 1);
-        else
-            resSectors = PQexecPrepared(conn, "index_sectors", 1, paramValues, paramLengths, paramFormats, 1);
+//        if (rank < 16)
+//            resSectors = PQexecPrepared(conn, "index_nosectors", 1, paramValues, paramLengths, paramFormats, 1);
+//        else
+        resSectors = PQexecPrepared(conn, "index_sectors", 1, paramValues, paramLengths, paramFormats, 1);
+
         if (PQresultStatus(resSectors) != PGRES_TUPLES_OK)
         {
             fprintf(stderr, "index_sectors: SELECT failed: %s", PQerrorMessage(conn));
         if (PQresultStatus(resSectors) != PGRES_TUPLES_OK)
         {
             fprintf(stderr, "index_sectors: SELECT failed: %s", PQerrorMessage(conn));
@@ -198,7 +199,7 @@ void nominatim_index(int rank_min, int rank_max, int num_threads, const char *co
                     PQclear(resPlaces);
                     exit(EXIT_FAILURE);
                 }
                     PQclear(resPlaces);
                     exit(EXIT_FAILURE);
                 }
-                if (PQftype(resPlaces, 0) != PG_OID_INT4)
+                if (PQftype(resPlaces, 0) != PG_OID_INT8)
                 {
                     fprintf(stderr, "Place_id value has unexpected type\n");
                     PQclear(resPlaces);
                 {
                     fprintf(stderr, "Place_id value has unexpected type\n");
                     PQclear(resPlaces);
@@ -215,7 +216,7 @@ void nominatim_index(int rank_min, int rank_max, int num_threads, const char *co
             if (iSector < PQntuples(resSectors))
             {
                 sector = PGint32(*((uint32_t *)PQgetvalue(resSectors, iSector, 0)));
             if (iSector < PQntuples(resSectors))
             {
                 sector = PGint32(*((uint32_t *)PQgetvalue(resSectors, iSector, 0)));
-                //printf("\n Starting sector %d size %ld\n", sector, PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1))));
+//                fprintf(stderr, "\n Starting sector %d size %ld\n", sector, PGint64(*((uint64_t *)PQgetvalue(resSectors, iSector, 1))));
 
                 // Get all the place_id's for this sector
                 paramRank = PGint32(rank);
 
                 // Get all the place_id's for this sector
                 paramRank = PGint32(rank);
@@ -226,10 +227,14 @@ void nominatim_index(int rank_min, int rank_max, int num_threads, const char *co
                 paramValues[1] = (char *)&paramSector;
                 paramLengths[1] = sizeof(paramSector);
                 paramFormats[1] = 1;
                 paramValues[1] = (char *)&paramSector;
                 paramLengths[1] = sizeof(paramSector);
                 paramFormats[1] = 1;
-                if (rank < 16)
+                if (rankTotalTuples-rankCountTuples < num_threads*1000)
+               {
                     iResult = PQsendQueryPrepared(conn, "index_nosector_places", 1, paramValues, paramLengths, paramFormats, 1);
                     iResult = PQsendQueryPrepared(conn, "index_nosector_places", 1, paramValues, paramLengths, paramFormats, 1);
+               }
                 else
                 else
+               {
                     iResult = PQsendQueryPrepared(conn, "index_sector_places", 2, paramValues, paramLengths, paramFormats, 1);
                     iResult = PQsendQueryPrepared(conn, "index_sector_places", 2, paramValues, paramLengths, paramFormats, 1);
+               }
                 if (!iResult)
                 {
                     fprintf(stderr, "index_sector_places: SELECT failed: %s", PQerrorMessage(conn));
                 if (!iResult)
                 {
                     fprintf(stderr, "index_sector_places: SELECT failed: %s", PQerrorMessage(conn));
@@ -268,7 +273,7 @@ void nominatim_index(int rank_min, int rank_max, int num_threads, const char *co
                         if (sleepcount++ > 500)
                         {
                             rankPerSecond = ((float)rankCountTuples + (float)count) / MAX(difftime(time(0), rankStartTime),1);
                         if (sleepcount++ > 500)
                         {
                             rankPerSecond = ((float)rankCountTuples + (float)count) / MAX(difftime(time(0), rankStartTime),1);
-                            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);
+                            fprintf(stderr, "  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);
                             sleepcount = 0;
                         }
                     }
                             sleepcount = 0;
                         }
                     }
@@ -284,13 +289,17 @@ void nominatim_index(int rank_min, int rank_max, int num_threads, const char *co
 
                 // Finished sector
                 rankPerSecond = (float)rankCountTuples / MAX(difftime(time(0), rankStartTime),1);
 
                 // Finished sector
                 rankPerSecond = (float)rankCountTuples / MAX(difftime(time(0), rankStartTime),1);
-                printf("  Done %i in %i @ %f per second - ETA (seconds): %f\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond, ((float)(rankTotalTuples - rankCountTuples))/rankPerSecond);
+                fprintf(stderr, "  Done %i in %i @ %f per second - ETA (seconds): %f\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond, ((float)(rankTotalTuples - rankCountTuples))/rankPerSecond);
 
                 PQclear(resPlaces);
             }
 
                 PQclear(resPlaces);
             }
+            if (rankTotalTuples-rankCountTuples < num_threads*20 && iSector < PQntuples(resSectors))
+            {
+                iSector = PQntuples(resSectors) - 1;
+            }
         }
         // Finished rank
         }
         // Finished rank
-        printf("\r  Done %i in %i @ %f per second - FINISHED                      \n\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond);
+        fprintf(stderr, "\r  Done %i in %i @ %f per second - FINISHED                      \n\n", rankCountTuples, (int)(difftime(time(0), rankStartTime)), rankPerSecond);
 
         PQclear(resSectors);
     }
 
         PQclear(resSectors);
     }
@@ -299,19 +308,27 @@ void nominatim_index(int rank_min, int rank_max, int num_threads, const char *co
     {
         nominatim_exportXMLEnd(writer);
     }
     {
         nominatim_exportXMLEnd(writer);
     }
+
+    // Close all connections
+    for (i = 0; i < num_threads; i++)
+    {
+        PQfinish(thread_data[i].conn);
+    }
+    PQfinish(conn);
 }
 
 void *nominatim_indexThread(void * thread_data_in)
 {
     struct index_thread_data * thread_data = (struct index_thread_data * )thread_data_in;
 }
 
 void *nominatim_indexThread(void * thread_data_in)
 {
     struct index_thread_data * thread_data = (struct index_thread_data * )thread_data_in;
+    struct export_data querySet;
 
     PGresult   *res;
 
     const char *paramValues[1];
     int         paramLengths[1];
     int         paramFormats[1];
 
     PGresult   *res;
 
     const char *paramValues[1];
     int         paramLengths[1];
     int         paramFormats[1];
-    uint32_t    paramPlaceID;
-    uint32_t   place_id;
+    uint64_t    paramPlaceID;
+    uint64_t   place_id;
     time_t             updateStartTime;
 
     while (1)
     time_t             updateStartTime;
 
     while (1)
@@ -323,31 +340,53 @@ void *nominatim_indexThread(void * thread_data_in)
             break;
         }
 
             break;
         }
 
-        place_id = PGint32(*((uint32_t *)PQgetvalue(thread_data->res, *thread_data->count, 0)));
+        place_id = PGint64(*((uint64_t *)PQgetvalue(thread_data->res, *thread_data->count, 0)));
         (*thread_data->count)++;
 
         pthread_mutex_unlock( thread_data->count_mutex );
 
         (*thread_data->count)++;
 
         pthread_mutex_unlock( thread_data->count_mutex );
 
-        if (verbose) printf("  Processing place_id %d\n", place_id);
+        if (verbose) fprintf(stderr, "  Processing place_id %ld\n", place_id);
 
         updateStartTime = time(0);
 
         updateStartTime = time(0);
-        paramPlaceID = PGint32(place_id);
-        paramValues[0] = (char *)&paramPlaceID;
-        paramLengths[0] = sizeof(paramPlaceID);
-        paramFormats[0] = 1;
-        res = PQexecPrepared(thread_data->conn, "index_placex", 1, paramValues, paramLengths, paramFormats, 1);
-        if (PQresultStatus(res) != PGRES_COMMAND_OK)
+       int done = 0;
+
+        if (thread_data->writer)
         {
         {
-            fprintf(stderr, "index_placex: UPDATE failed: %s", PQerrorMessage(thread_data->conn));
-            PQclear(res);
-            exit(EXIT_FAILURE);
+             nominatim_exportPlaceQueries(place_id, thread_data->conn, &querySet);
+        }
+
+       while(!done)
+       {
+               paramPlaceID = PGint64(place_id);
+               paramValues[0] = (char *)&paramPlaceID;
+               paramLengths[0] = sizeof(paramPlaceID);
+               paramFormats[0] = 1;
+               res = PQexecPrepared(thread_data->conn, "index_placex", 1, paramValues, paramLengths, paramFormats, 1);
+               if (PQresultStatus(res) == PGRES_COMMAND_OK)
+                       done = 1;
+               else
+               {
+                       if (!strncmp(PQerrorMessage(thread_data->conn), "ERROR:  deadlock detected", 25))
+                       {
+                           fprintf(stderr, "index_placex: UPDATE failed - deadlock, retrying (%ld)\n", place_id);
+                           PQclear(res);
+                            sleep(rand() % 10);
+                       }
+                       else
+                       {
+                           fprintf(stderr, "index_placex: UPDATE failed: %s", PQerrorMessage(thread_data->conn));
+                           PQclear(res);
+                           exit(EXIT_FAILURE);
+                       }
+               }
         }
         PQclear(res);
         }
         PQclear(res);
-        if (difftime(time(0), updateStartTime) > 1) printf("  Slow place_id %d\n", place_id);
+        if (difftime(time(0), updateStartTime) > 1) fprintf(stderr, "  Slow place_id %ld\n", place_id);
 
         if (thread_data->writer)
         {
 
         if (thread_data->writer)
         {
-            nominatim_exportPlace(place_id, thread_data->conn, thread_data->writer, thread_data->writer_mutex);
+            nominatim_exportPlace(place_id, thread_data->conn, thread_data->writer, thread_data->writer_mutex, &querySet);
+            nominatim_exportFreeQueries(&querySet);
         }
     }
 
         }
     }