1 /**************************************************************************
3 * Copyright 2011 Zack Rusin
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
27 #include "trace_snappyfile.hpp"
36 using namespace Trace;
42 * Snappy at its core is just a compressoin algorithm so we're
43 * creating a new file format which uses snappy compression
44 * to hold the trace data.
46 * The file is composed of a number of chunks, they are:
48 * uint32 - specifying the length of the compressed data
49 * compressed data, in little endian
51 * File can contain any number of such chunks.
52 * The default size of an uncompressed chunk is specified in
56 * Currently the default size for a a to-be-compressed data is
57 * 1mb, meaning that the compressed data will be <= 1mb.
58 * The reason it's 1mb is because it seems
59 * to offer a pretty good compression/disk io speed ratio
60 * but that might change.
64 SnappyFile::SnappyFile(const std::string &filename,
71 size_t maxCompressedLength =
72 snappy::MaxCompressedLength(SNAPPY_CHUNK_SIZE);
73 m_compressedCache = new char[maxCompressedLength];
76 SnappyFile::~SnappyFile()
78 delete [] m_compressedCache;
82 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
84 std::ios_base::openmode fmode = std::fstream::binary;
85 if (mode == File::Write) {
86 fmode |= (std::fstream::out | std::fstream::trunc);
87 createCache(SNAPPY_CHUNK_SIZE);
88 } else if (mode == File::Read) {
89 fmode |= std::fstream::in;
92 m_stream.open(filename.c_str(), fmode);
94 //read in the initial buffer if we're reading
95 if (m_stream.is_open() && mode == File::Read) {
96 // read the snappy file identifier
97 unsigned char byte1, byte2;
100 assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
103 } else if (m_stream.is_open() && mode == File::Write) {
104 // write the snappy file identifier
105 m_stream << SNAPPY_BYTE1;
106 m_stream << SNAPPY_BYTE2;
108 return m_stream.is_open();
111 bool SnappyFile::rawWrite(const void *buffer, size_t length)
113 if (freeCacheSize() > length) {
114 memcpy(m_cachePtr, buffer, length);
115 m_cachePtr += length;
116 } else if (freeCacheSize() == length) {
117 memcpy(m_cachePtr, buffer, length);
118 m_cachePtr += length;
121 int sizeToWrite = length;
123 while (sizeToWrite >= freeCacheSize()) {
124 int endSize = freeCacheSize();
125 int offset = length - sizeToWrite;
126 memcpy(m_cachePtr, (const char*)buffer + offset, endSize);
127 sizeToWrite -= endSize;
128 m_cachePtr += endSize;
132 int offset = length - sizeToWrite;
133 memcpy(m_cachePtr, (const char*)buffer + offset, sizeToWrite);
134 m_cachePtr += sizeToWrite;
141 bool SnappyFile::rawRead(void *buffer, size_t length)
147 if (freeCacheSize() >= length) {
148 memcpy(buffer, m_cachePtr, length);
149 m_cachePtr += length;
151 size_t sizeToRead = length;
154 size_t chunkSize = std::min(freeCacheSize(), sizeToRead);
155 offset = length - sizeToRead;
156 memcpy((char*)buffer + offset, m_cachePtr, chunkSize);
157 m_cachePtr += chunkSize;
158 sizeToRead -= chunkSize;
159 if (sizeToRead > 0) {
171 int SnappyFile::rawGetc()
179 void SnappyFile::rawClose()
181 if (m_mode == File::Write) {
190 void SnappyFile::rawFlush()
192 assert(m_mode == File::Write);
197 void SnappyFile::flushWriteCache()
199 size_t inputLength = usedCacheSize();
202 size_t compressedLength;
204 ::snappy::RawCompress(m_cache, inputLength,
205 m_compressedCache, &compressedLength);
207 writeCompressedLength(compressedLength);
208 m_stream.write(m_compressedCache, compressedLength);
209 m_cachePtr = m_cache;
211 assert(m_cachePtr == m_cache);
214 void SnappyFile::flushReadCache()
216 //assert(m_cachePtr == m_cache + m_cacheSize);
217 m_currentOffset.chunk = m_stream.tellg();
218 size_t compressedLength;
219 compressedLength = readCompressedLength();
221 if (compressedLength) {
222 m_stream.read((char*)m_compressedCache, compressedLength);
223 ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
225 createCache(m_cacheSize);
226 ::snappy::RawUncompress(m_compressedCache, compressedLength,
233 void SnappyFile::createCache(size_t size)
235 // TODO: only re-allocate if the current buffer is not big enough
242 m_cache = new char[size];
247 m_cachePtr = m_cache;
251 void SnappyFile::writeCompressedLength(size_t length)
253 unsigned char buf[4];
254 buf[0] = length & 0xff; length >>= 8;
255 buf[1] = length & 0xff; length >>= 8;
256 buf[2] = length & 0xff; length >>= 8;
257 buf[3] = length & 0xff; length >>= 8;
259 m_stream.write((const char *)buf, sizeof buf);
262 size_t SnappyFile::readCompressedLength()
264 unsigned char buf[4];
266 m_stream.read((char *)buf, sizeof buf);
267 if (m_stream.fail()) {
270 length = (size_t)buf[0];
271 length |= ((size_t)buf[1] << 8);
272 length |= ((size_t)buf[2] << 16);
273 length |= ((size_t)buf[3] << 24);
278 bool SnappyFile::supportsOffsets() const
283 File::Offset SnappyFile::currentOffset()
285 m_currentOffset.offsetInChunk = m_cachePtr - m_cache;
286 return m_currentOffset;
289 void SnappyFile::setCurrentOffset(const File::Offset &offset)
293 // seek to the start of a chunk
294 m_stream.seekg(offset.chunk, std::ios::beg);
297 assert(m_cacheSize >= offset.offsetInChunk);
298 // seek within our cache to the correct location within the chunk
299 m_cachePtr = m_cache + offset.offsetInChunk;
303 bool SnappyFile::rawSkip(unsigned length)
309 if (freeCacheSize() >= length) {
310 m_cachePtr += length;
312 size_t sizeToRead = length;
314 size_t chunkSize = std::min(freeCacheSize(), sizeToRead);
315 m_cachePtr += chunkSize;
316 sizeToRead -= chunkSize;
317 if (sizeToRead > 0) {