ProtoStream reviewed

git-svn-id: https://www4.informatik.uni-erlangen.de/i4svn/danceos/trunk/devel/fail@1270 8c4709b5-6ec9-48aa-a5cd-a96041d1645a
This commit is contained in:
hellwig
2012-05-24 12:03:23 +00:00
parent 03d8688241
commit c5848a9c6d
2 changed files with 67 additions and 59 deletions

View File

@ -2,34 +2,21 @@
using namespace std; using namespace std;
ProtoStream::ProtoStream(ostream* outfile){ ProtoOStream::ProtoOStream(ostream* outfile)
{
m_write_mode = true;
m_outfile = outfile; m_outfile = outfile;
m_log.setDescription("ProtoStream"); m_log.setDescription("ProtoStream");
m_log.showTime(false); m_log.showTime(false);
} }
ProtoStream::ProtoStream(istream* infile){ bool ProtoOStream::writeMessage(google::protobuf::Message* m)
{
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;
}
m_size = htonl(m->ByteSize()); m_size = htonl(m->ByteSize());
m_outfile->write(reinterpret_cast<char*>(&m_size), sizeof(m_size)); m_outfile->write(reinterpret_cast<char*>(&m_size), sizeof(m_size));
if(m_outfile->bad()){ if(m_outfile->bad()) {
m_log << "Could not write to file!" << endl; m_log << "Could not write to file!" << endl;
return false;
} }
m->SerializeToOstream(m_outfile); m->SerializeToOstream(m_outfile);
@ -38,40 +25,38 @@ bool ProtoStream::writeMessage(google::protobuf::Message* m){
} }
bool ProtoStream:: reset(){ void ProtoIStream:: 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;
}
m_infile->seekg(0,ios::beg); 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_infile->seekg(0, ios::end);
m_log << "To get a message, please call the constructor of the ProtoStream-Object with a Pointer of an istream." << endl; 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; return false;
} }
m_infile->read(reinterpret_cast<char*>(&m_size), sizeof(m_size)); m_infile->read(reinterpret_cast<char*>(&m_size), sizeof(m_size));
if(m_infile->bad()){
m_log << "Could not write to file!" << endl;
}
m_size = ntohl(m_size); m_size = ntohl(m_size);
char *buf = new char[m_size]; char *buf = new char[m_size];
m_infile->read(buf, m_size); m_infile->read(buf, m_size);
std::string st(buf, m_size); std::string st(buf, m_size);
m->ParseFromString(st); m->ParseFromString(st);
delete[] buf;
return true; return true;
} }

View File

@ -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__ #ifndef __PROTOSTREAM_HPP__
#define __PROTOSTREAM_HPP__ #define __PROTOSTREAM_HPP__
@ -11,34 +25,22 @@
using namespace std; using namespace std;
/** /**
* \class ProtoStream * \class ProtoOStream
* *
* * This class can be used to write messages in a file.
* \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 | ...
* *
*/ */
class ProtoStream class ProtoOStream
{ {
private: private:
bool m_write_mode;
uint32_t m_size; uint32_t m_size;
Logger m_log; Logger m_log;
ostream* m_outfile; ostream* m_outfile;
istream* m_infile;
public: public:
ProtoStream(ostream * outfile); ProtoOStream(ostream * outfile);
ProtoStream(istream * infile); virtual ~ProtoOStream() {};
virtual ~ProtoStream() {};
/** /**
* Writes a message to a file. * Writes a message to a file.
* This works only if the constructor was called wit a pointer * This works only if the constructor was called wit a pointer
@ -47,14 +49,35 @@ class ProtoStream
* @return Returns true if data was written. * @return Returns true if data was written.
*/ */
bool writeMessage(google::protobuf::Message* m); 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 * Resets the position of the get pointer. After that getNext
* delivers the first message again. * delivers the first message again.
* This works only if the constructor was called wit a pointer * This works only if the constructor was called wit a pointer
* of an istream. * of an istream.
* @return Returns true if the pointer is reset.
*/ */
bool reset(); void reset();
/** /**
* Delivers the protobuf-messages sequentially from file. * Delivers the protobuf-messages sequentially from file.
* This works only if the constructor was called wit a pointer * This works only if the constructor was called wit a pointer