diff --git a/DataStructures/StaticRTree.h b/DataStructures/StaticRTree.h index cd6b88b94..4900f1d51 100644 --- a/DataStructures/StaticRTree.h +++ b/DataStructures/StaticRTree.h @@ -47,6 +47,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include #include @@ -60,7 +61,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. const static uint32_t RTREE_BRANCHING_FACTOR = 64; const static uint32_t RTREE_LEAF_NODE_SIZE = 1024; -thread_local boost::filesystem::ifstream thread_local_rtree_stream; +static boost::thread_specific_ptr thread_local_rtree_stream; // Implements a static, i.e. packed, R-tree template time1 = std::chrono::steady_clock::now(); - std::chrono::duration elapsed_seconds = time1-time0; + std::chrono::duration elapsed_seconds = time1 - time0; SimpleLogger().Write() << "finished r-tree construction in " << (elapsed_seconds.count()) << " seconds"; } @@ -478,7 +481,10 @@ class StaticRTree : boost::noncopyable leaf_node_file.read((char *)&m_element_count, sizeof(uint64_t)); leaf_node_file.close(); - thread_local_rtree_stream.close(); + if (thread_local_rtree_stream.get()) + { + thread_local_rtree_stream->close(); + } // SimpleLogger().Write() << tree_size << " nodes in search tree"; // SimpleLogger().Write() << m_element_count << " elements in leafs"; @@ -501,7 +507,8 @@ class StaticRTree : boost::noncopyable m_search_tree[0].minimum_bounding_rectangle.GetMinDist(input_coordinate); traversal_queue.emplace(0, current_min_dist); - BOOST_ASSERT_MSG(std::numeric_limits::epsilon() > std::abs(traversal_queue.top().min_dist), + BOOST_ASSERT_MSG(std::numeric_limits::epsilon() > + std::abs(traversal_queue.top().min_dist), "Root element in NN Search has min dist != 0."); while (!traversal_queue.empty()) @@ -526,11 +533,12 @@ class StaticRTree : boost::noncopyable continue; } - double current_minimum_distance = FixedPointCoordinate::ApproximateEuclideanDistance( - input_coordinate.lat, - input_coordinate.lon, - m_coordinate_list->at(current_edge.u).lat, - m_coordinate_list->at(current_edge.u).lon); + double current_minimum_distance = + FixedPointCoordinate::ApproximateEuclideanDistance( + input_coordinate.lat, + input_coordinate.lon, + m_coordinate_list->at(current_edge.u).lat, + m_coordinate_list->at(current_edge.u).lon); if (current_minimum_distance < min_dist) { // found a new minimum @@ -540,11 +548,12 @@ class StaticRTree : boost::noncopyable found_a_nearest_edge = true; } - current_minimum_distance = FixedPointCoordinate::ApproximateEuclideanDistance( - input_coordinate.lat, - input_coordinate.lon, - m_coordinate_list->at(current_edge.v).lat, - m_coordinate_list->at(current_edge.v).lon); + current_minimum_distance = + FixedPointCoordinate::ApproximateEuclideanDistance( + input_coordinate.lat, + input_coordinate.lon, + m_coordinate_list->at(current_edge.v).lat, + m_coordinate_list->at(current_edge.v).lon); if (current_minimum_distance < min_dist) { @@ -747,18 +756,19 @@ class StaticRTree : boost::noncopyable private: inline void LoadLeafFromDisk(const uint32_t leaf_id, LeafNode &result_node) { - if (!thread_local_rtree_stream.is_open()) + if (!thread_local_rtree_stream.get() || !thread_local_rtree_stream->is_open()) { - thread_local_rtree_stream.open(m_leaf_node_filename, std::ios::in | std::ios::binary); + thread_local_rtree_stream.reset(new boost::filesystem::ifstream( + m_leaf_node_filename, std::ios::in | std::ios::binary)); } - if (!thread_local_rtree_stream.good()) + if (!thread_local_rtree_stream->good()) { - thread_local_rtree_stream.clear(std::ios::goodbit); + thread_local_rtree_stream->clear(std::ios::goodbit); SimpleLogger().Write(logDEBUG) << "Resetting stale filestream"; } uint64_t seek_pos = sizeof(uint64_t) + leaf_id * sizeof(LeafNode); - thread_local_rtree_stream.seekg(seek_pos); - thread_local_rtree_stream.read((char *)&result_node, sizeof(LeafNode)); + thread_local_rtree_stream->seekg(seek_pos); + thread_local_rtree_stream->read((char *)&result_node, sizeof(LeafNode)); } inline bool EdgesAreEquivalent(const FixedPointCoordinate &a,