]> git.cworth.org Git - apitrace/blob - trace_snappyfile.cpp
Remove some currently unused member variables.
[apitrace] / trace_snappyfile.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Zack Rusin
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include "trace_snappyfile.hpp"
28
29 #include <snappy.h>
30
31 #include <iostream>
32
33 #include <assert.h>
34 #include <string.h>
35
36 using namespace Trace;
37
38 /*
39  * Snappy file format.
40  * -------------------
41  *
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.
45  *
46  * The file is composed of a number of chunks, they are:
47  * chunk {
48  *     uint32 - specifying the length of the compressed data
49  *     compressed data, in little endian
50  * }
51  * File can contain any number of such chunks.
52  * The default size of an uncompressed chunk is specified in
53  * SNAPPY_CHUNK_SIZE.
54  *
55  * Note:
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.
61  *
62  */
63
64 SnappyFile::SnappyFile(const std::string &filename,
65                               File::Mode mode)
66     : File(),
67       m_cache(0),
68       m_cachePtr(0),
69       m_cacheSize(0)
70 {
71     size_t maxCompressedLength =
72         snappy::MaxCompressedLength(SNAPPY_CHUNK_SIZE);
73     m_compressedCache = new char[maxCompressedLength];
74 }
75
76 SnappyFile::~SnappyFile()
77 {
78     delete [] m_compressedCache;
79     delete [] m_cache;
80 }
81
82 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
83 {
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;
90     }
91
92     m_stream.open(filename.c_str(), fmode);
93
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;
98         m_stream >> byte1;
99         m_stream >> byte2;
100         assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
101
102         flushReadCache();
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;
107     }
108     return m_stream.is_open();
109 }
110
111 bool SnappyFile::rawWrite(const void *buffer, size_t length)
112 {
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;
119         flushWriteCache();
120     } else {
121         int sizeToWrite = length;
122
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;
129             flushWriteCache();
130         }
131         if (sizeToWrite) {
132             int offset = length - sizeToWrite;
133             memcpy(m_cachePtr, (const char*)buffer + offset, sizeToWrite);
134             m_cachePtr += sizeToWrite;
135         }
136     }
137
138     return true;
139 }
140
141 bool SnappyFile::rawRead(void *buffer, size_t length)
142 {
143     if (endOfData()) {
144         return false;
145     }
146
147     if (freeCacheSize() >= length) {
148         memcpy(buffer, m_cachePtr, length);
149         m_cachePtr += length;
150     } else {
151         size_t sizeToRead = length;
152         size_t offset = 0;
153         while (sizeToRead) {
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) {
160                 flushReadCache();
161             }
162             if (!m_cacheSize) {
163                 break;
164             }
165         }
166     }
167
168     return true;
169 }
170
171 int SnappyFile::rawGetc()
172 {
173     int c = 0;
174     if (!rawRead(&c, 1))
175         return -1;
176     return c;
177 }
178
179 void SnappyFile::rawClose()
180 {
181     if (m_mode == File::Write) {
182         flushWriteCache();
183     }
184     m_stream.close();
185     delete [] m_cache;
186     m_cache = NULL;
187     m_cachePtr = NULL;
188 }
189
190 void SnappyFile::rawFlush()
191 {
192     assert(m_mode == File::Write);
193     flushWriteCache();
194     m_stream.flush();
195 }
196
197 void SnappyFile::flushWriteCache()
198 {
199     size_t inputLength = usedCacheSize();
200
201     if (inputLength) {
202         size_t compressedLength;
203
204         ::snappy::RawCompress(m_cache, inputLength,
205                               m_compressedCache, &compressedLength);
206
207         writeCompressedLength(compressedLength);
208         m_stream.write(m_compressedCache, compressedLength);
209         m_cachePtr = m_cache;
210     }
211     assert(m_cachePtr == m_cache);
212 }
213
214 void SnappyFile::flushReadCache(size_t skipLength)
215 {
216     //assert(m_cachePtr == m_cache + m_cacheSize);
217     m_currentOffset.chunk = m_stream.tellg();
218     size_t compressedLength;
219     compressedLength = readCompressedLength();
220
221     if (compressedLength) {
222         m_stream.read((char*)m_compressedCache, compressedLength);
223         ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
224                                         &m_cacheSize);
225         createCache(m_cacheSize);
226         if (skipLength < m_cacheSize) {
227             ::snappy::RawUncompress(m_compressedCache, compressedLength,
228                                     m_cache);
229         }
230     } else {
231         createCache(0);
232     }
233 }
234
235 void SnappyFile::createCache(size_t size)
236 {
237     // TODO: only re-allocate if the current buffer is not big enough
238
239     if (m_cache) {
240         delete [] m_cache;
241     }
242
243     if (size) {
244         m_cache = new char[size];
245     } else {
246         m_cache = NULL;
247     }
248
249     m_cachePtr = m_cache;
250     m_cacheSize = size;
251 }
252
253 void SnappyFile::writeCompressedLength(size_t length)
254 {
255     unsigned char buf[4];
256     buf[0] = length & 0xff; length >>= 8;
257     buf[1] = length & 0xff; length >>= 8;
258     buf[2] = length & 0xff; length >>= 8;
259     buf[3] = length & 0xff; length >>= 8;
260     assert(length == 0);
261     m_stream.write((const char *)buf, sizeof buf);
262 }
263
264 size_t SnappyFile::readCompressedLength()
265 {
266     unsigned char buf[4];
267     size_t length;
268     m_stream.read((char *)buf, sizeof buf);
269     if (m_stream.fail()) {
270         length = 0;
271     } else {
272         length  =  (size_t)buf[0];
273         length |= ((size_t)buf[1] <<  8);
274         length |= ((size_t)buf[2] << 16);
275         length |= ((size_t)buf[3] << 24);
276     }
277     return length;
278 }
279
280 bool SnappyFile::supportsOffsets() const
281 {
282     return true;
283 }
284
285 File::Offset SnappyFile::currentOffset()
286 {
287     m_currentOffset.offsetInChunk = m_cachePtr - m_cache;
288     return m_currentOffset;
289 }
290
291 void SnappyFile::setCurrentOffset(const File::Offset &offset)
292 {
293     // to remove eof bit
294     m_stream.clear();
295     // seek to the start of a chunk
296     m_stream.seekg(offset.chunk, std::ios::beg);
297     // load the chunk
298     flushReadCache();
299     assert(m_cacheSize >= offset.offsetInChunk);
300     // seek within our cache to the correct location within the chunk
301     m_cachePtr = m_cache + offset.offsetInChunk;
302
303 }
304
305 bool SnappyFile::rawSkip(size_t length)
306 {
307     if (endOfData()) {
308         return false;
309     }
310
311     if (freeCacheSize() >= length) {
312         m_cachePtr += length;
313     } else {
314         size_t sizeToRead = length;
315         while (sizeToRead) {
316             size_t chunkSize = std::min(freeCacheSize(), sizeToRead);
317             m_cachePtr += chunkSize;
318             sizeToRead -= chunkSize;
319             if (sizeToRead > 0) {
320                 flushReadCache(sizeToRead);
321             }
322             if (!m_cacheSize) {
323                 break;
324             }
325         }
326     }
327
328     return true;
329 }