]> git.cworth.org Git - apitrace/blobdiff - trace_snappyfile.cpp
Implement scanning/skipping of fragments of the trace
[apitrace] / trace_snappyfile.cpp
index ad0a57749a784ae7148ee056e4115c6f360980b2..443ebe7c5040c131bf7eef72daeb84ace273b98b 100644 (file)
@@ -28,6 +28,8 @@
 
 #include <snappy.h>
 
+#include <iostream>
+
 #include <assert.h>
 #include <string.h>
 
@@ -66,7 +68,9 @@ SnappyFile::SnappyFile(const std::string &filename,
       m_cachePtr(0),
       m_cacheSize(0)
 {
-    m_compressedCache = new char[SNAPPY_CHUNK_SIZE];
+    size_t maxCompressedLength =
+        snappy::MaxCompressedLength(SNAPPY_CHUNK_SIZE);
+    m_compressedCache = new char[maxCompressedLength];
 }
 
 SnappyFile::~SnappyFile()
@@ -199,6 +203,7 @@ void SnappyFile::flushCache()
         if (m_stream.eof())
             return;
         //assert(m_cachePtr == m_cache + m_cacheSize);
+        m_currentOffset.chunk = m_stream.tellg();
         size_t compressedLength;
         compressedLength = readCompressedLength();
         m_stream.read((char*)m_compressedCache, compressedLength);
@@ -238,3 +243,52 @@ uint32_t SnappyFile::readCompressedLength()
     m_stream.read((char*)&len, sizeof len);
     return len;
 }
+
+bool SnappyFile::supportsOffsets() const
+{
+    return true;
+}
+
+File::Offset SnappyFile::currentOffset()
+{
+    m_currentOffset.offsetInChunk = m_cachePtr - m_cache;
+    return m_currentOffset;
+}
+
+void SnappyFile::setCurrentOffset(const File::Offset &offset)
+{
+    // to remove eof bit
+    m_stream.clear();
+    // seek to the start of a chunk
+    m_stream.seekg(offset.chunk, std::ios::beg);
+    // load the chunk
+    flushCache();
+    assert(m_cacheSize >= offset.offsetInChunk);
+    // seek within our cache to the correct location within the chunk
+    m_cachePtr = m_cache + offset.offsetInChunk;
+
+}
+
+bool SnappyFile::rawSkip(unsigned length)
+{
+    if (endOfData()) {
+        return false;
+    }
+
+    if (freeCacheSize() >= length) {
+        m_cachePtr += length;
+    } else {
+        int sizeToRead = length;
+        while (sizeToRead) {
+            int chunkSize = std::min(freeCacheSize(), sizeToRead);
+            m_cachePtr += chunkSize;
+            sizeToRead -= chunkSize;
+            if (sizeToRead > 0)
+                flushCache();
+            if (!m_cacheSize)
+                break;
+        }
+    }
+
+    return true;
+}