From 316ef305dec157a3a795e565041f1d9a5dda4f9b Mon Sep 17 00:00:00 2001 From: Huyen Chau Nguyen Date: Tue, 18 Oct 2016 11:21:41 -0700 Subject: [PATCH] Refactor loading code for timestamp file --- .../engine/datafacade/internal_datafacade.hpp | 9 +++++++-- include/storage/io.hpp | 18 ++++++++++++++++-- src/storage/storage.cpp | 15 ++++++++------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/include/engine/datafacade/internal_datafacade.hpp b/include/engine/datafacade/internal_datafacade.hpp index 2ac70434b..c5fc17cde 100644 --- a/include/engine/datafacade/internal_datafacade.hpp +++ b/include/engine/datafacade/internal_datafacade.hpp @@ -13,6 +13,7 @@ #include "extractor/original_edge_data.hpp" #include "extractor/profile_properties.hpp" #include "extractor/query_node.hpp" +#include "storage/io.hpp" #include "storage/storage_config.hpp" #include "storage/io.hpp" #include "engine/geospatial_query.hpp" @@ -71,7 +72,6 @@ class InternalDataFacade final : public BaseDataFacade InternalDataFacade() {} unsigned m_check_sum; - unsigned m_number_of_nodes; std::unique_ptr m_query_graph; std::string m_timestamp; @@ -141,12 +141,17 @@ class InternalDataFacade final : public BaseDataFacade void LoadTimestamp(const boost::filesystem::path ×tamp_path) { util::SimpleLogger().Write() << "Loading Timestamp"; + boost::filesystem::ifstream timestamp_stream(timestamp_path); if (!timestamp_stream) { throw util::exception("Could not open " + timestamp_path.string() + " for reading."); } - getline(timestamp_stream, m_timestamp); + + auto timestamp_size = storage::io::readTimestampSize(timestamp_stream); + char *timestamp_ptr = new char[timestamp_size](); + storage::io::readTimestamp(timestamp_stream, timestamp_ptr, timestamp_size); + m_timestamp = std::string(timestamp_ptr); } void LoadGraph(const boost::filesystem::path &hsgr_path) diff --git a/include/storage/io.hpp b/include/storage/io.hpp index 4de872a32..02907f8b8 100644 --- a/include/storage/io.hpp +++ b/include/storage/io.hpp @@ -48,8 +48,7 @@ inline HSGRHeader readHSGRHeader(boost::filesystem::ifstream &input_stream) return header; } -// Needs to be called after getHSGRSize() to get the correct offset in the stream -// +// Needs to be called after HSGRHeader() to get the correct offset in the stream template void readHSGR(boost::filesystem::ifstream &input_stream, NodeT *node_buffer, @@ -61,6 +60,21 @@ void readHSGR(boost::filesystem::ifstream &input_stream, input_stream.read(reinterpret_cast(edge_buffer), number_of_edges * sizeof(EdgeT)); } +// Returns the size of the timestamp in a file +inline std::uint32_t readTimestampSize(boost::filesystem::ifstream ×tamp_input_stream) +{ + timestamp_input_stream.seekg (0, timestamp_input_stream.end); + auto length = timestamp_input_stream.tellg(); + timestamp_input_stream.seekg (0, timestamp_input_stream.beg); + return length; +} + +// Reads the timestamp in a file +inline void readTimestamp(boost::filesystem::ifstream ×tamp_input_stream, char *timestamp, std::size_t timestamp_length) +{ + timestamp_input_stream.read(timestamp, timestamp_length * sizeof(char)); +} + } } } diff --git a/src/storage/storage.cpp b/src/storage/storage.cpp index 48d3522c5..1c883db55 100644 --- a/src/storage/storage.cpp +++ b/src/storage/storage.cpp @@ -272,11 +272,14 @@ Storage::ReturnCode Storage::Run(int max_wait) // load profile properties shared_layout_ptr->SetBlockSize(SharedDataLayout::PROPERTIES, 1); - // load timestamp size + // read timestampsize boost::filesystem::ifstream timestamp_stream(config.timestamp_path); - std::string m_timestamp; - getline(timestamp_stream, m_timestamp); - shared_layout_ptr->SetBlockSize(SharedDataLayout::TIMESTAMP, m_timestamp.length()); + if (!timestamp_stream) + { + throw util::exception("Could not open " + config.timestamp_path.string() + " for reading."); + } + std::size_t timestamp_size = io::readTimestampSize(timestamp_stream); + shared_layout_ptr->SetBlockSize(SharedDataLayout::TIMESTAMP, timestamp_size); // load core marker size boost::filesystem::ifstream core_marker_file(config.core_data_path, std::ios::binary); @@ -659,7 +662,7 @@ Storage::ReturnCode Storage::Run(int max_wait) // store timestamp char *timestamp_ptr = shared_layout_ptr->GetBlockPtr(shared_memory_ptr, SharedDataLayout::TIMESTAMP); - std::copy(m_timestamp.c_str(), m_timestamp.c_str() + m_timestamp.length(), timestamp_ptr); + io::readTimestamp(timestamp_stream, timestamp_ptr, timestamp_size); // store search tree portion of rtree char *rtree_ptr = shared_layout_ptr->GetBlockPtr(shared_memory_ptr, @@ -771,7 +774,6 @@ Storage::ReturnCode Storage::Run(int max_wait) static_cast(data_type_memory->Ptr()); { - boost::interprocess::scoped_lock current_regions_exclusive_lock; @@ -801,7 +803,6 @@ Storage::ReturnCode Storage::Run(int max_wait) } util::SimpleLogger().Write() << "Ok."; - data_timestamp_ptr->layout = layout_region; data_timestamp_ptr->data = data_region; data_timestamp_ptr->timestamp += 1;