#define SNAPPY_CHUNK_SIZE (1 * 1024 * 1024)
-#define SNAPPY_BYTE1 'a'
-#define SNAPPY_BYTE2 't'
-using namespace Trace;
+using namespace trace;
class SnappyFile : public File {
protected:
virtual bool rawOpen(const std::string &filename, File::Mode mode);
virtual bool rawWrite(const void *buffer, size_t length);
- virtual bool rawRead(void *buffer, size_t length);
+ virtual size_t rawRead(void *buffer, size_t length);
virtual int rawGetc();
virtual void rawClose();
virtual void rawFlush();
size_t readCompressedLength();
private:
std::fstream m_stream;
+ size_t m_cacheMaxSize;
+ size_t m_cacheSize;
char *m_cache;
char *m_cachePtr;
- size_t m_cacheSize;
char *m_compressedCache;
SnappyFile::SnappyFile(const std::string &filename,
File::Mode mode)
: File(),
- m_cache(0),
- m_cachePtr(0),
- m_cacheSize(0)
+ m_cacheMaxSize(SNAPPY_CHUNK_SIZE),
+ m_cacheSize(m_cacheMaxSize),
+ m_cache(new char [m_cacheMaxSize]),
+ m_cachePtr(m_cache)
{
size_t maxCompressedLength =
snappy::MaxCompressedLength(SNAPPY_CHUNK_SIZE);
SnappyFile::~SnappyFile()
{
+ close();
delete [] m_compressedCache;
delete [] m_cache;
}
return true;
}
-bool SnappyFile::rawRead(void *buffer, size_t length)
+size_t SnappyFile::rawRead(void *buffer, size_t length)
{
if (endOfData()) {
- return false;
+ return 0;
}
if (freeCacheSize() >= length) {
flushReadCache();
}
if (!m_cacheSize) {
- break;
+ return length - sizeToRead;
}
}
}
- return true;
+ return length;
}
int SnappyFile::rawGetc()
{
- int c = 0;
- if (!rawRead(&c, 1))
+ unsigned char c = 0;
+ if (rawRead(&c, 1) != 1)
return -1;
return c;
}
void SnappyFile::createCache(size_t size)
{
- // TODO: only re-allocate if the current buffer is not big enough
+ if (size > m_cacheMaxSize) {
+ do {
+ m_cacheMaxSize <<= 1;
+ } while (size > m_cacheMaxSize);
- if (m_cache) {
delete [] m_cache;
- }
-
- if (size) {
m_cache = new char[size];
- } else {
- m_cache = NULL;
+ m_cacheMaxSize = size;
}
m_cachePtr = m_cache;
int SnappyFile::rawPercentRead()
{
- return 100 * (double(m_stream.tellg()) / double(m_endPos));
+ return int(100 * (double(m_stream.tellg()) / double(m_endPos)));
}
File* File::createSnappy(void) {
return new SnappyFile;
}
-
-bool File::isSnappyCompressed(const std::string &filename)
-{
- std::fstream stream(filename.c_str(),
- std::fstream::binary | std::fstream::in);
- if (!stream.is_open())
- return false;
-
- unsigned char byte1, byte2;
- stream >> byte1;
- stream >> byte2;
- stream.close();
-
- return (byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
-}