]> git.cworth.org Git - apitrace/blobdiff - trace_snappyfile.cpp
Make sure that the size of the compressed length is constant.
[apitrace] / trace_snappyfile.cpp
index 457f7e9529604d211ba86ce82e3900a4d80486af..2dc5cb3c1ff30db001dd1d1e7d6d8800b5f83f9c 100644 (file)
@@ -7,6 +7,32 @@
 
 using namespace Trace;
 
+/*
+ * Snappy file format.
+ * -------------------
+ *
+ * Snappy at its core is just a compressoin algorithm so we're
+ * creating a new file format which uses snappy compression
+ * to hold the trace data.
+ *
+ * The file is composed of a number of chunks, they are:
+ * chunk {
+ *     uint32 - specifying the length of the compressed data
+ *     compressed data
+ * }
+ * File can contain any number of such chunks.
+ * The default size of an uncompressed chunk is specified in
+ * SNAPPY_CHUNK_SIZE.
+ *
+ * Note:
+ * Currently the default size for a a to-be-compressed data is
+ * 1mb, meaning that the compressed data will be <= 1mb.
+ * The reason it's 1mb is because it seems
+ * to offer a pretty good compression/disk io speed ratio
+ * but that might change.
+ *
+ */
+
 SnappyFile::SnappyFile(const std::string &filename,
                               File::Mode mode)
     : File(),
@@ -145,7 +171,7 @@ void SnappyFile::flushCache()
         ::snappy::RawCompress(m_cache, SNAPPY_CHUNK_SIZE - freeCacheSize(),
                               m_compressedCache, &compressedLength);
 
-        m_stream << compressedLength;
+        writeCompressedLength(compressedLength);
         m_stream.write(m_compressedCache, compressedLength);
         m_cachePtr = m_cache;
     } else if (m_mode == File::Read) {
@@ -153,7 +179,7 @@ void SnappyFile::flushCache()
             return;
         //assert(m_cachePtr == m_cache + m_cacheSize);
         size_t compressedLength;
-        m_stream >> compressedLength;
+        compressedLength = readCompressedLength();
         m_stream.read((char*)m_compressedCache, compressedLength);
         ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
                                         &m_cacheSize);
@@ -171,3 +197,15 @@ void SnappyFile::createCache(size_t size)
     m_cachePtr = m_cache;
     m_cacheSize = size;
 }
+
+void SnappyFile::writeCompressedLength(uint32_t value)
+{
+    m_stream.write((char*)&value, sizeof value);
+}
+
+uint32_t SnappyFile::readCompressedLength()
+{
+    uint32_t len;
+    m_stream.read((char*)&len, sizeof len);
+    return len;
+}