From a60d07ea9c7c67d051de3ecade2945bd2d7b9683 Mon Sep 17 00:00:00 2001
From: James Turner <zakalawe@mac.com>
Date: Tue, 9 Aug 2016 09:38:54 +0100
Subject: [PATCH] Incremental insert of NE data to the index.

---
 src/GUI/QtLauncher.cxx   | 19 ++++++++++++++-----
 src/Navaids/PolyLine.cxx |  5 +++--
 src/Navaids/PolyLine.hxx |  3 ++-
 3 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/src/GUI/QtLauncher.cxx b/src/GUI/QtLauncher.cxx
index 16a0a104d..9a1a23f90 100644
--- a/src/GUI/QtLauncher.cxx
+++ b/src/GUI/QtLauncher.cxx
@@ -263,7 +263,8 @@ class NaturalEarthDataLoaderThread : public QThread
     Q_OBJECT
 public:
 
-    NaturalEarthDataLoaderThread()
+    NaturalEarthDataLoaderThread() :
+        m_lineInsertCount(0)
     {
         connect(this, &QThread::finished, this, &NaturalEarthDataLoaderThread::onFinished);
     }
@@ -284,15 +285,22 @@ protected:
         loadNaturalEarthFile("ne_10m_urban_areas.shp", flightgear::PolyLine::URBAN, true);
 
         qDebug() << "loading urban areas took:" << st.elapsedMSec();
-
     }
 
 private:
     void onFinished()
     {
-        flightgear::PolyLine::bulkAddToSpatialIndex(m_parsedLines);
-        qDebug() << "finished loading Natural Earth data";
-        deleteLater(); // commit suicide
+        flightgear::PolyLineList::const_iterator begin = m_parsedLines.begin() + m_lineInsertCount;
+        unsigned int numToAdd = std::min(1000UL, m_parsedLines.size() - m_lineInsertCount);
+        flightgear::PolyLineList::const_iterator end = begin + numToAdd;
+        flightgear::PolyLine::bulkAddToSpatialIndex(begin, end);
+
+        if (end == m_parsedLines.end()) {
+            deleteLater(); // commit suicide
+        } else {
+            m_lineInsertCount += 1000;
+            QTimer::singleShot(50, this, &NaturalEarthDataLoaderThread::onFinished);
+        }
     }
 
     void loadNaturalEarthFile(const std::string& aFileName,
@@ -310,6 +318,7 @@ private:
     }
     
     flightgear::PolyLineList m_parsedLines;
+    unsigned int m_lineInsertCount;
 };
 
 } // of anonymous namespace
diff --git a/src/Navaids/PolyLine.cxx b/src/Navaids/PolyLine.cxx
index 38777c653..68c98de96 100644
--- a/src/Navaids/PolyLine.cxx
+++ b/src/Navaids/PolyLine.cxx
@@ -101,11 +101,12 @@ PolyLineRef PolyLine::create(PolyLine::Type aTy, const SGGeodVec &aRawPoints)
     return new PolyLine(aTy, aRawPoints);
 }
 
-void PolyLine::bulkAddToSpatialIndex(const PolyLineList &lines)
+void PolyLine::bulkAddToSpatialIndex(PolyLineList::const_iterator begin,
+                                     PolyLineList::const_iterator end)
 {
     NavDataCache::Transaction txn( NavDataCache::instance());
     flightgear::PolyLineList::const_iterator it;
-    for (it=lines.begin(); it != lines.end(); ++it) {
+    for (it=begin; it != end; ++it) {
         (*it)->addToSpatialIndex();
     }
     txn.commit();
diff --git a/src/Navaids/PolyLine.hxx b/src/Navaids/PolyLine.hxx
index 57e827c97..0a33acf9d 100644
--- a/src/Navaids/PolyLine.hxx
+++ b/src/Navaids/PolyLine.hxx
@@ -91,7 +91,8 @@ public:
     
     static PolyLineRef create(Type aTy, const SGGeodVec& aRawPoints);
 
-    static void bulkAddToSpatialIndex(const PolyLineList& lines);
+    static void bulkAddToSpatialIndex(PolyLineList::const_iterator begin,
+                                      PolyLineList::const_iterator end);
 
     /**
      * retrieve all the lines within a range of a search point.