From c5848a9c6d167b350b3b64d46d38c2a1fd06f565 Mon Sep 17 00:00:00 2001 From: hellwig Date: Thu, 24 May 2012 12:03:23 +0000 Subject: [PATCH] ProtoStream reviewed git-svn-id: https://www4.informatik.uni-erlangen.de/i4svn/danceos/trunk/devel/fail@1270 8c4709b5-6ec9-48aa-a5cd-a96041d1645a --- core/util/ProtoStream.cc | 65 +++++++++++++++------------------------ core/util/ProtoStream.hpp | 61 ++++++++++++++++++++++++------------ 2 files changed, 67 insertions(+), 59 deletions(-) diff --git a/core/util/ProtoStream.cc b/core/util/ProtoStream.cc index 725eab54..78978cc9 100644 --- a/core/util/ProtoStream.cc +++ b/core/util/ProtoStream.cc @@ -2,34 +2,21 @@ using namespace std; -ProtoStream::ProtoStream(ostream* outfile){ - - m_write_mode = true; +ProtoOStream::ProtoOStream(ostream* outfile) +{ m_outfile = outfile; m_log.setDescription("ProtoStream"); m_log.showTime(false); } -ProtoStream::ProtoStream(istream* infile){ - - m_write_mode = false; - m_infile = infile; - m_log.setDescription("ProtoStream"); - m_log.showTime(false); -} - -bool ProtoStream::writeMessage(google::protobuf::Message* m){ - - if(!m_write_mode){ - m_log << "To attach a message, please call the constructor of the ProtoStream-Object with a Pointer of an ostream." << endl; - return false; - } - +bool ProtoOStream::writeMessage(google::protobuf::Message* m) +{ m_size = htonl(m->ByteSize()); m_outfile->write(reinterpret_cast(&m_size), sizeof(m_size)); - if(m_outfile->bad()){ + if(m_outfile->bad()) { m_log << "Could not write to file!" << endl; + return false; } m->SerializeToOstream(m_outfile); @@ -38,40 +25,38 @@ bool ProtoStream::writeMessage(google::protobuf::Message* m){ } -bool ProtoStream:: reset(){ - - if(m_write_mode){ - m_log << "To reset, please call the constructor of the ProtoStream-Object with a Pointer of an istream." << endl; - return false; - } - +void ProtoIStream:: reset() +{ m_infile->seekg(0,ios::beg); - return true; } -bool ProtoStream::getNext(google::protobuf::Message* m){ + +//PROTOISTREAM + +ProtoIStream::ProtoIStream(istream* infile) +{ + m_infile = infile; + m_log.setDescription("ProtoStream"); + m_log.showTime(false); - if(m_write_mode){ - m_log << "To get a message, please call the constructor of the ProtoStream-Object with a Pointer of an istream." << endl; + m_infile->seekg(0, ios::end); + m_sizeOfInfile = m_infile->tellg(); + m_infile->seekg(0, ios::beg); +} + +bool ProtoIStream::getNext(google::protobuf::Message* m) +{ + if(m_infile->tellg() >= m_sizeOfInfile) { return false; } m_infile->read(reinterpret_cast(&m_size), sizeof(m_size)); - - if(m_infile->bad()){ - m_log << "Could not write to file!" << endl; - } - m_size = ntohl(m_size); - char *buf = new char[m_size]; - m_infile->read(buf, m_size); - std::string st(buf, m_size); - m->ParseFromString(st); - + delete[] buf; return true; } diff --git a/core/util/ProtoStream.hpp b/core/util/ProtoStream.hpp index d99de581..6a06d57f 100644 --- a/core/util/ProtoStream.hpp +++ b/core/util/ProtoStream.hpp @@ -1,3 +1,17 @@ +/** brief One way to manage large protobuf-messages + * + * Google protobuf is not designed for large messages. + * That leads to much memory which is consumed by processing large + * messages. To solve this problem the ProtoStream class stores the + * protobuf-messages in an other way. + * Instead of using the repeat-function the data is written + * sequentially in a file. + * Written in the format: + * |4 bytes length-information of the first message | first message + * |4 bytes length-information of the second message| second message + * | ... + */ + #ifndef __PROTOSTREAM_HPP__ #define __PROTOSTREAM_HPP__ @@ -11,34 +25,22 @@ using namespace std; /** - * \class ProtoStream + * \class ProtoOStream * - * - * \brief One way to manage large protobuf-messages - * - * Google protobuf is not designed for large messages. - * That leads to much memory which is consumed by processing large messages. - * To solve this problem the ProtoStream class stores the protobuf-messages in an other way. - * Instead of using the repeat-function the data is written sequentially in a file. - * Written in the format: - * |4 bytes length-information of the first message| first message | 4 bytes length-information of second message | second message | ... + * This class can be used to write messages in a file. * */ -class ProtoStream +class ProtoOStream { private: - bool m_write_mode; uint32_t m_size; Logger m_log; ostream* m_outfile; - istream* m_infile; - public: - ProtoStream(ostream * outfile); - ProtoStream(istream * infile); - virtual ~ProtoStream() {}; + ProtoOStream(ostream * outfile); + virtual ~ProtoOStream() {}; /** * Writes a message to a file. * This works only if the constructor was called wit a pointer @@ -47,14 +49,35 @@ class ProtoStream * @return Returns true if data was written. */ bool writeMessage(google::protobuf::Message* m); +}; + + +/** + * \class ProtoIStream + * + * This class can be used to read messages sequentially from a file. + * + */ +class ProtoIStream +{ + private: + uint32_t m_size; + long m_sizeOfInfile; + + Logger m_log; + istream* m_infile; + + + public: + ProtoIStream(istream * infile); + virtual ~ProtoIStream() {}; /** * Resets the position of the get pointer. After that getNext * delivers the first message again. * This works only if the constructor was called wit a pointer * of an istream. - * @return Returns true if the pointer is reset. */ - bool reset(); + void reset(); /** * Delivers the protobuf-messages sequentially from file. * This works only if the constructor was called wit a pointer