1 #include "trace_file.hpp"
11 using namespace Trace;
13 File::File(const std::string &filename,
15 : m_filename(filename),
19 if (!m_filename.empty()) {
20 open(m_filename, m_mode);
24 static size_t writtenRef = 0;
28 std::cerr << "written ref = "<<writtenRef << std::endl;
32 bool File::isOpened() const
37 File::Mode File::mode() const
42 std::string File::filename() const
47 bool File::open(const std::string &filename, File::Mode mode)
52 m_isOpened = rawOpen(filename, mode);
57 bool File::write(const void *buffer, int length)
59 if (!m_isOpened || m_mode != File::Write) {
63 return rawWrite(buffer, length);
66 bool File::read(void *buffer, int length)
68 if (!m_isOpened || m_mode != File::Read) {
71 return rawRead(buffer, length);
89 if (!m_isOpened || m_mode != File::Read) {
95 ZLibFile::ZLibFile(const std::string &filename,
97 : File(filename, mode),
102 ZLibFile::~ZLibFile()
106 bool ZLibFile::rawOpen(const std::string &filename, File::Mode mode)
108 m_gzFile = gzopen(filename.c_str(),
109 (mode == File::Write) ? "wb" : "rb");
110 return m_gzFile != NULL;
113 bool ZLibFile::rawWrite(const void *buffer, int length)
115 return gzwrite(m_gzFile, buffer, length);
118 bool ZLibFile::rawRead(void *buffer, int length)
120 return gzread(m_gzFile, buffer, length);
123 char ZLibFile::rawGetc()
125 return gzgetc(m_gzFile);
128 void ZLibFile::rawClose()
136 void ZLibFile::rawFlush()
138 gzflush(m_gzFile, Z_SYNC_FLUSH);
141 SnappyFile::SnappyFile(const std::string &filename,
148 m_compressedCache = new char[SNAPPY_CHUNK_SIZE];
151 SnappyFile::~SnappyFile()
153 delete [] m_compressedCache;
156 static size_t written = 0;
157 static size_t writtenComp = 0;
159 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
161 std::ios_base::openmode fmode = std::fstream::binary;
162 if (mode == File::Write) {
163 fmode |= (std::fstream::out | std::fstream::trunc);
164 createCache(SNAPPY_CHUNK_SIZE);
165 } else if (mode == File::Read) {
166 fmode |= std::fstream::in;
169 m_stream.open(filename.c_str(), fmode);
171 //read in the initial buffer if we're reading
172 if (m_stream.is_open() && mode == File::Read) {
175 return m_stream.is_open();
178 bool SnappyFile::rawWrite(const void *buffer, int length)
180 static int bufWritten = 0;
181 if (bufWritten < 500) {
182 const char *cbuffer = (const char*)buffer;
183 for (int i = 0; i < length; ++i) {
184 std::cerr << "--- "<<bufWritten << ") "<< (int) cbuffer[i]
189 if (freeCacheSize() > length) {
190 memcpy(m_cachePtr, buffer, length);
191 m_cachePtr += length;
192 } else if (freeCacheSize() == length) {
193 memcpy(m_cachePtr, buffer, length);
194 m_cachePtr += length;
197 int sizeToWrite = length;
199 while (sizeToWrite >= freeCacheSize()) {
200 int endSize = freeCacheSize();
201 int offset = length - sizeToWrite;
202 memcpy(m_cachePtr, (char*)buffer + offset, endSize);
203 sizeToWrite -= endSize;
204 m_cachePtr += endSize;
208 int offset = length - sizeToWrite;
209 memcpy(m_cachePtr, (char*)buffer + offset, sizeToWrite);
210 m_cachePtr += sizeToWrite;
217 bool SnappyFile::rawRead(void *buffer, int length)
219 std::cerr << "Reading byte = "<< (m_cachePtr - m_cache) << std::endl;
220 if (freeCacheSize() > length) {
221 memcpy(buffer, m_cachePtr, length);
222 m_cachePtr += length;
223 } else if (freeCacheSize() == length) {
224 memcpy(buffer, m_cachePtr, length);
225 m_cachePtr += length;
229 int sizeToRead = length;
233 int chunkSize = std::min(freeCacheSize(), sizeToRead);
234 offset = length - sizeToRead;
235 memcpy((char*)buffer + offset, m_cachePtr, chunkSize);
236 m_cachePtr += chunkSize;
237 sizeToRead -= chunkSize;
246 char SnappyFile::rawGetc()
253 void SnappyFile::rawClose()
256 std::cerr << "written = "<< written
257 <<", comp = "<< writtenComp
265 void SnappyFile::rawFlush()
270 void SnappyFile::flushCache()
272 if (m_mode == File::Write) {
273 size_t compressedLength;
275 static bool first = true;
277 std::cerr << "Buffer = [";
278 for (int i = 0; i < 512; ++i) {
279 std::cerr << i << " ) "<< (int)m_cache[i] << std::endl;
281 std::cerr << "]"<<std::endl;
285 ::snappy::RawCompress(m_cache, SNAPPY_CHUNK_SIZE - freeCacheSize(),
286 m_compressedCache, &compressedLength);
288 m_stream << compressedLength;
289 m_stream.write(m_compressedCache, compressedLength);
290 std::cerr << "compressed length = "<<compressedLength
291 <<" cache size = "<<(SNAPPY_CHUNK_SIZE - freeCacheSize())
292 <<" (ref = " << SNAPPY_CHUNK_SIZE <<")"
294 written += SNAPPY_CHUNK_SIZE - freeCacheSize();
295 writtenComp += compressedLength;
296 m_cachePtr = m_cache;
297 } else if (m_mode == File::Read) {
300 //assert(m_cachePtr == m_cache + m_cacheSize);
301 size_t compressedLength;
302 m_stream >> compressedLength;
303 m_stream.read((char*)m_compressedCache, compressedLength);
304 ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
306 std::cerr << "compressed length = "<<compressedLength
307 <<" cache size = "<<m_cacheSize
308 <<" (ref = " << SNAPPY_CHUNK_SIZE <<")"
312 createCache(m_cacheSize);
313 ::snappy::RawUncompress(m_compressedCache, compressedLength,
315 static bool first = true;
317 std::cerr << "Buffer = [";
318 for (int i = 0; i < 512; ++i) {
319 std::cerr << i << " ) "<< (int)m_cache[i] << std::endl;
321 std::cerr << "]"<<std::endl;
327 void SnappyFile::createCache(size_t size)
329 m_cache = new char[size];
330 m_cachePtr = m_cache;