Question

In my code, I have two types of writers: regular writer, class Writer and compressing writer, class ZlibWriter. I need a way to combine both writers.
Please have a look at the code below. sorry that it is a bit too long. I tried posting just the very relevant.

typedef std::vector<int> int_vector;

class Writer
{
public:
    void open(string path);
    void close();

    void write(string v);
    void write(bool v);
    void write(int v);
    void write(int_vector v);

private:
      std::ofstream m_fstream; ///< file output stream
};


class ZlibWriter
{
public:
    void open();
    void close();

    void write(int v);
    void write(int_vector v);

private:
      ozstream m_zstream; ///< ozstream is a wrapper class that extends ofstream and compress data using zlib format prior to writing
};


class Storage
{
public:
    string     m_name;
    bool       m_compressed;
    int_vector m_data;
};


class StorageWriter
{
public:

    void init(string path)
    {
        m_writer.open(path)
    }

    void write(const Storage& p)
    {
        m_writer.write(p.m_name);
        m_writer.write(p.m_compressed);
        if( !p.m_compressed )
            m_writer.write(p.m_data);
        else
        {
            // TBD - how to use ZlibWriter here?
        }
    }

private:
    Writer m_writer;
};



Constraints
StorageWriter::write() is the bottleneck of my program. it is called countless of times and responsible for the writing of many gigabytes of data.

Question
I need to implement the //TBD section in StorageWriter::write().
I would like to have a simple way of "switching" between Writer and ZlibWriter.

Suggestion 1
Don't use ZlibWriter. each time I need to compress data, just compress it to temporary variable and then write it to file.
I don't like this because it will make me duplicate the whole compress mechanism already implemented in class ZlibWriter.

Suggestion 2

  1. create instance of class ZlibWriter on the fly ( or even as a member of StorageWriter).
  2. move position of ZlibWriter stream to the position class Writer has.
  3. write data using ZlibWriter.
  4. move position of Writer to the end of the file.

Not sure it is wise to have two live streams to the same file. In addition all those seek operation are not very efficient at a critical section of the program.



Thanks a lot for your time.

Was it helpful?

Solution

You can overload your ZlibWriter ctor, such that you will be able to provide a custom stream your writer will write to. Use std::ostream::rdbuf to replace your output buffer with an output buffer of another stream.

OTHER TIPS

I am not exactly sure what you are looking for (do you always write to one file, or does every writer write to its own?) but its perfectly fine to have both writers as member variables. you could even go as far as having a vector of Writers. so make an abstract interface that all writers include so that you can loop over the vector and simply call the write() function with your data. the writer itself will handle the writing, and thus the zlib writer will compress the data and then write it, without you having to worry about anything :)

if you need to toggle between them, you can just add a seperate member variable for your zlibwriter, and in the else case write the compressed data with the zlibwriter.

an idea to speed up the process is to share the file between writers, since only 1 process at a time will write to it, if you move the file outside the writer, and give both writers a reference to it, you can handle the seek etc from the writers, which means the next writer can just write, instead of having to do unnescessary file operations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top