RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/logger/compressor.h
00001 /* compressor.h                                                    -*- C++ -*-
00002    Jeremy Barnes, 19 September 2012
00003    Copyright (c) 2012 Datacratic.  All rights reserved.
00004 
00005    Interface to a compressor object.
00006 
00007    We prefer this to other solutions as we have full control over when a
00008    stream is flushed and we can use this to minimise the potential for
00009    data loss.
00010 
00011    It would be nice to use boost::iostreams for this, but their flush() is
00012    buggy and there is no way to have precise control over flushing.
00013 */
00014 
00015 #ifndef __logger__compressor_h__
00016 #define __logger__compressor_h__
00017 
00018 
00019 #include <memory>
00020 #include <functional>
00021 #include <string>
00022 
00023 namespace Datacratic {
00024 
00025 
00026 /*****************************************************************************/
00027 /* COMPRESSOR                                                                */
00028 /*****************************************************************************/
00029 
00030 struct Compressor {
00031 
00032     virtual ~Compressor();
00033 
00034     typedef std::function<size_t (const char * data, size_t len)> OnData;
00035 
00036     OnData onData;
00037 
00039     enum FlushLevel {
00040         FLUSH_NONE,     
00041         FLUSH_AVAILABLE,
00042         FLUSH_SYNC,     
00043         FLUSH_RESTART,  
00044     };
00045 
00052     virtual size_t compress(const char * data, size_t len,
00053                             const OnData & onData) = 0;
00054     
00058     virtual size_t flush(FlushLevel flushLevel, const OnData & onData) = 0;
00059 
00063     virtual size_t finish(const OnData & onData) = 0;
00064 
00066     static std::string filenameToCompression(const std::string & filename);
00067 
00069     static Compressor * create(const std::string & compression,
00070                                int level);
00071 };
00072 
00073 
00074 /*****************************************************************************/
00075 /* NULL COMPRESSOR                                                           */
00076 /*****************************************************************************/
00077 
00078 struct NullCompressor : public Compressor {
00079 
00080     NullCompressor();
00081 
00082     virtual ~NullCompressor();
00083 
00084     virtual size_t compress(const char * data, size_t len,
00085                             const OnData & onData);
00086     
00087     virtual size_t flush(FlushLevel flushLevel, const OnData & onData);
00088 
00089     virtual size_t finish(const OnData & onData);
00090 };
00091 
00092 /*****************************************************************************/
00093 /* GZIP COMPRESSOR                                                           */
00094 /*****************************************************************************/
00095 
00096 struct GzipCompressor : public Compressor {
00097 
00098     GzipCompressor(int level);
00099 
00100     virtual ~GzipCompressor();
00101 
00102     void open(int level);
00103 
00104     virtual size_t compress(const char * data, size_t len,
00105                             const OnData & onData);
00106     
00107     virtual size_t flush(FlushLevel flushLevel, const OnData & onData);
00108 
00109     virtual size_t finish(const OnData & onData);
00110 
00111 private:
00112     struct Itl;
00113     std::unique_ptr<Itl> itl;
00114 };
00115 
00116 /*****************************************************************************/
00117 /* LZMA COMPRESSOR                                                           */
00118 /*****************************************************************************/
00119 
00120 struct LzmaCompressor : public Compressor {
00121 
00122     LzmaCompressor();
00123 
00124     ~LzmaCompressor();
00125 
00126     virtual size_t compress(const char * data, size_t len,
00127                             const OnData & onData);
00128     
00129     virtual size_t flush(FlushLevel flushLevel, const OnData & onData);
00130 
00131     virtual size_t finish(const OnData & onData);
00132 
00133 private:
00134     struct Itl;
00135     std::unique_ptr<Itl> itl;
00136 };
00137 
00138 } // namespace Datacratic
00139 
00140 #endif /* __logger__compressor_h__ */
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator