]> git.cworth.org Git - apitrace/blob - trace_snappyfile.cpp
Some initial thoughts on the on-demand loading api.
[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
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     m_compressedCache = new char[SNAPPY_CHUNK_SIZE];
72 }
73
74 SnappyFile::~SnappyFile()
75 {
76     delete [] m_compressedCache;
77 }
78
79 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
80 {
81     std::ios_base::openmode fmode = std::fstream::binary;
82     if (mode == File::Write) {
83         fmode |= (std::fstream::out | std::fstream::trunc);
84         createCache(SNAPPY_CHUNK_SIZE);
85     } else if (mode == File::Read) {
86         fmode |= std::fstream::in;
87     }
88
89     m_stream.open(filename.c_str(), fmode);
90
91     //read in the initial buffer if we're reading
92     if (m_stream.is_open() && mode == File::Read) {
93         // read the snappy file identifier
94         unsigned char byte1, byte2;
95         m_stream >> byte1;
96         m_stream >> byte2;
97         assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
98
99         flushCache();
100     } else if (m_stream.is_open() && mode == File::Write) {
101         // write the snappy file identifier
102         m_stream << SNAPPY_BYTE1;
103         m_stream << SNAPPY_BYTE2;
104     }
105     return m_stream.is_open();
106 }
107
108 bool SnappyFile::rawWrite(const void *buffer, int length)
109 {
110     if (freeCacheSize() > length) {
111         memcpy(m_cachePtr, buffer, length);
112         m_cachePtr += length;
113     } else if (freeCacheSize() == length) {
114         memcpy(m_cachePtr, buffer, length);
115         m_cachePtr += length;
116         flushCache();
117     } else {
118         int sizeToWrite = length;
119
120         while (sizeToWrite >= freeCacheSize()) {
121             int endSize = freeCacheSize();
122             int offset = length - sizeToWrite;
123             memcpy(m_cachePtr, (char*)buffer + offset, endSize);
124             sizeToWrite -= endSize;
125             m_cachePtr += endSize;
126             flushCache();
127         }
128         if (sizeToWrite) {
129             int offset = length - sizeToWrite;
130             memcpy(m_cachePtr, (char*)buffer + offset, sizeToWrite);
131             m_cachePtr += sizeToWrite;
132         }
133     }
134
135     return true;
136 }
137
138 bool SnappyFile::rawRead(void *buffer, int length)
139 {
140     if (endOfData()) {
141         return false;
142     }
143
144     if (freeCacheSize() >= length) {
145         memcpy(buffer, m_cachePtr, length);
146         m_cachePtr += length;
147     } else {
148         int sizeToRead = length;
149         int offset = 0;
150         while (sizeToRead) {
151             int chunkSize = std::min(freeCacheSize(), sizeToRead);
152             offset = length - sizeToRead;
153             memcpy((char*)buffer + offset, m_cachePtr, chunkSize);
154             m_cachePtr += chunkSize;
155             sizeToRead -= chunkSize;
156             if (sizeToRead > 0)
157                 flushCache();
158             if (!m_cacheSize)
159                 break;
160         }
161     }
162
163     return true;
164 }
165
166 int SnappyFile::rawGetc()
167 {
168     int c = 0;
169     if (!rawRead(&c, 1))
170         return -1;
171     return c;
172 }
173
174 void SnappyFile::rawClose()
175 {
176     flushCache();
177     m_stream.close();
178     delete [] m_cache;
179     m_cache = NULL;
180     m_cachePtr = NULL;
181 }
182
183 void SnappyFile::rawFlush()
184 {
185     flushCache();
186     m_stream.flush();
187 }
188
189 void SnappyFile::flushCache()
190 {
191     if (m_mode == File::Write) {
192         size_t compressedLength;
193
194         ::snappy::RawCompress(m_cache, SNAPPY_CHUNK_SIZE - freeCacheSize(),
195                               m_compressedCache, &compressedLength);
196
197         writeCompressedLength(compressedLength);
198         m_stream.write(m_compressedCache, compressedLength);
199         m_cachePtr = m_cache;
200     } else if (m_mode == File::Read) {
201         if (m_stream.eof())
202             return;
203         //assert(m_cachePtr == m_cache + m_cacheSize);
204         m_currentOffset.chunk = m_stream.tellg();
205         size_t compressedLength;
206         compressedLength = readCompressedLength();
207         m_stream.read((char*)m_compressedCache, compressedLength);
208         /*
209          * The reason we peek here is because the last read will
210          * read all the way until the last character, but that will not
211          * trigger m_stream.eof() to be set, so by calling peek
212          * we assure that if we in fact have read the entire stream
213          * then the m_stream.eof() is always set.
214          */
215         m_stream.peek();
216         ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
217                                         &m_cacheSize);
218         if (m_cache)
219             delete [] m_cache;
220         createCache(m_cacheSize);
221         ::snappy::RawUncompress(m_compressedCache, compressedLength,
222                                 m_cache);
223     }
224 }
225
226 void SnappyFile::createCache(size_t size)
227 {
228     m_cache = new char[size];
229     m_cachePtr = m_cache;
230     m_cacheSize = size;
231 }
232
233 void SnappyFile::writeCompressedLength(uint32_t value)
234 {
235     m_stream.write((const char*)&value, sizeof value);
236 }
237
238 uint32_t SnappyFile::readCompressedLength()
239 {
240     uint32_t len;
241     m_stream.read((char*)&len, sizeof len);
242     return len;
243 }
244
245 bool SnappyFile::supportsOffsets() const
246 {
247     return true;
248 }
249
250 File::Offset SnappyFile::currentOffset()
251 {
252     m_currentOffset.offsetInChunk = m_cachePtr - m_cache;
253     return m_currentOffset;
254 }
255
256 void SnappyFile::setCurrentOffset(const File::Offset &offset)
257 {
258     // to remove eof bit
259     m_stream.clear();
260     // seek to the start of a chunk
261     m_stream.seekg(offset.chunk, std::ios::beg);
262     // load the chunk
263     flushCache();
264     assert(m_cacheSize >= offset.offsetInChunk);
265     // seek within our cache to the correct location within the chunk
266     m_cachePtr = m_cache + offset.offsetInChunk;
267
268 }