]> git.cworth.org Git - apitrace/blob - trace_snappyfile.cpp
Share enum signatures.
[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 <assert.h>
32 #include <string.h>
33
34 using namespace Trace;
35
36 /*
37  * Snappy file format.
38  * -------------------
39  *
40  * Snappy at its core is just a compressoin algorithm so we're
41  * creating a new file format which uses snappy compression
42  * to hold the trace data.
43  *
44  * The file is composed of a number of chunks, they are:
45  * chunk {
46  *     uint32 - specifying the length of the compressed data
47  *     compressed data
48  * }
49  * File can contain any number of such chunks.
50  * The default size of an uncompressed chunk is specified in
51  * SNAPPY_CHUNK_SIZE.
52  *
53  * Note:
54  * Currently the default size for a a to-be-compressed data is
55  * 1mb, meaning that the compressed data will be <= 1mb.
56  * The reason it's 1mb is because it seems
57  * to offer a pretty good compression/disk io speed ratio
58  * but that might change.
59  *
60  */
61
62 SnappyFile::SnappyFile(const std::string &filename,
63                               File::Mode mode)
64     : File(),
65       m_cache(0),
66       m_cachePtr(0),
67       m_cacheSize(0)
68 {
69     m_compressedCache = new char[SNAPPY_CHUNK_SIZE];
70 }
71
72 SnappyFile::~SnappyFile()
73 {
74     delete [] m_compressedCache;
75 }
76
77 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
78 {
79     std::ios_base::openmode fmode = std::fstream::binary;
80     if (mode == File::Write) {
81         fmode |= (std::fstream::out | std::fstream::trunc);
82         createCache(SNAPPY_CHUNK_SIZE);
83     } else if (mode == File::Read) {
84         fmode |= std::fstream::in;
85     }
86
87     m_stream.open(filename.c_str(), fmode);
88
89     //read in the initial buffer if we're reading
90     if (m_stream.is_open() && mode == File::Read) {
91         // read the snappy file identifier
92         unsigned char byte1, byte2;
93         m_stream >> byte1;
94         m_stream >> byte2;
95         assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
96
97         flushCache();
98     } else if (m_stream.is_open() && mode == File::Write) {
99         // write the snappy file identifier
100         m_stream << SNAPPY_BYTE1;
101         m_stream << SNAPPY_BYTE2;
102     }
103     return m_stream.is_open();
104 }
105
106 bool SnappyFile::rawWrite(const void *buffer, int length)
107 {
108     if (freeCacheSize() > length) {
109         memcpy(m_cachePtr, buffer, length);
110         m_cachePtr += length;
111     } else if (freeCacheSize() == length) {
112         memcpy(m_cachePtr, buffer, length);
113         m_cachePtr += length;
114         flushCache();
115     } else {
116         int sizeToWrite = length;
117
118         while (sizeToWrite >= freeCacheSize()) {
119             int endSize = freeCacheSize();
120             int offset = length - sizeToWrite;
121             memcpy(m_cachePtr, (char*)buffer + offset, endSize);
122             sizeToWrite -= endSize;
123             m_cachePtr += endSize;
124             flushCache();
125         }
126         if (sizeToWrite) {
127             int offset = length - sizeToWrite;
128             memcpy(m_cachePtr, (char*)buffer + offset, sizeToWrite);
129             m_cachePtr += sizeToWrite;
130         }
131     }
132
133     return true;
134 }
135
136 bool SnappyFile::rawRead(void *buffer, int length)
137 {
138     if (endOfData()) {
139         return false;
140     }
141
142     if (freeCacheSize() >= length) {
143         memcpy(buffer, m_cachePtr, length);
144         m_cachePtr += length;
145     } else {
146         int sizeToRead = length;
147         int offset = 0;
148         while (sizeToRead) {
149             int chunkSize = std::min(freeCacheSize(), sizeToRead);
150             offset = length - sizeToRead;
151             memcpy((char*)buffer + offset, m_cachePtr, chunkSize);
152             m_cachePtr += chunkSize;
153             sizeToRead -= chunkSize;
154             if (sizeToRead > 0)
155                 flushCache();
156             if (!m_cacheSize)
157                 break;
158         }
159     }
160
161     return true;
162 }
163
164 int SnappyFile::rawGetc()
165 {
166     int c = 0;
167     if (!rawRead(&c, 1))
168         return -1;
169     return c;
170 }
171
172 void SnappyFile::rawClose()
173 {
174     flushCache();
175     m_stream.close();
176     delete [] m_cache;
177     m_cache = NULL;
178     m_cachePtr = NULL;
179 }
180
181 void SnappyFile::rawFlush()
182 {
183     flushCache();
184     m_stream.flush();
185 }
186
187 void SnappyFile::flushCache()
188 {
189     if (m_mode == File::Write) {
190         size_t compressedLength;
191
192         ::snappy::RawCompress(m_cache, SNAPPY_CHUNK_SIZE - freeCacheSize(),
193                               m_compressedCache, &compressedLength);
194
195         writeCompressedLength(compressedLength);
196         m_stream.write(m_compressedCache, compressedLength);
197         m_cachePtr = m_cache;
198     } else if (m_mode == File::Read) {
199         if (m_stream.eof())
200             return;
201         //assert(m_cachePtr == m_cache + m_cacheSize);
202         size_t compressedLength;
203         compressedLength = readCompressedLength();
204         m_stream.read((char*)m_compressedCache, compressedLength);
205         /*
206          * The reason we peek here is because the last read will
207          * read all the way until the last character, but that will not
208          * trigger m_stream.eof() to be set, so by calling peek
209          * we assure that if we in fact have read the entire stream
210          * then the m_stream.eof() is always set.
211          */
212         m_stream.peek();
213         ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
214                                         &m_cacheSize);
215         if (m_cache)
216             delete [] m_cache;
217         createCache(m_cacheSize);
218         ::snappy::RawUncompress(m_compressedCache, compressedLength,
219                                 m_cache);
220     }
221 }
222
223 void SnappyFile::createCache(size_t size)
224 {
225     m_cache = new char[size];
226     m_cachePtr = m_cache;
227     m_cacheSize = size;
228 }
229
230 void SnappyFile::writeCompressedLength(uint32_t value)
231 {
232     m_stream.write((const char*)&value, sizeof value);
233 }
234
235 uint32_t SnappyFile::readCompressedLength()
236 {
237     uint32_t len;
238     m_stream.read((char*)&len, sizeof len);
239     return len;
240 }