1 #include "trace_snappyfile.hpp"
10 SnappyFile::SnappyFile(const std::string &filename,
17 m_compressedCache = new char[SNAPPY_CHUNK_SIZE];
20 SnappyFile::~SnappyFile()
22 delete [] m_compressedCache;
25 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
27 std::ios_base::openmode fmode = std::fstream::binary;
28 if (mode == File::Write) {
29 fmode |= (std::fstream::out | std::fstream::trunc);
30 createCache(SNAPPY_CHUNK_SIZE);
31 } else if (mode == File::Read) {
32 fmode |= std::fstream::in;
35 m_stream.open(filename.c_str(), fmode);
37 //read in the initial buffer if we're reading
38 if (m_stream.is_open() && mode == File::Read) {
39 // read the snappy file identifier
40 unsigned char byte1, byte2;
43 assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
46 } else if (m_stream.is_open() && mode == File::Write) {
47 // write the snappy file identifier
48 m_stream << SNAPPY_BYTE1;
49 m_stream << SNAPPY_BYTE2;
51 return m_stream.is_open();
54 bool SnappyFile::rawWrite(const void *buffer, int length)
56 if (freeCacheSize() > length) {
57 memcpy(m_cachePtr, buffer, length);
59 } else if (freeCacheSize() == length) {
60 memcpy(m_cachePtr, buffer, length);
64 int sizeToWrite = length;
66 while (sizeToWrite >= freeCacheSize()) {
67 int endSize = freeCacheSize();
68 int offset = length - sizeToWrite;
69 memcpy(m_cachePtr, (char*)buffer + offset, endSize);
70 sizeToWrite -= endSize;
71 m_cachePtr += endSize;
75 int offset = length - sizeToWrite;
76 memcpy(m_cachePtr, (char*)buffer + offset, sizeToWrite);
77 m_cachePtr += sizeToWrite;
84 bool SnappyFile::rawRead(void *buffer, int length)
89 if (freeCacheSize() > length) {
90 memcpy(buffer, m_cachePtr, length);
92 } else if (freeCacheSize() == length) {
93 memcpy(buffer, m_cachePtr, length);
97 int sizeToRead = length;
100 int chunkSize = std::min(freeCacheSize(), sizeToRead);
101 offset = length - sizeToRead;
102 memcpy((char*)buffer + offset, m_cachePtr, chunkSize);
103 m_cachePtr += chunkSize;
104 sizeToRead -= chunkSize;
115 int SnappyFile::rawGetc()
123 void SnappyFile::rawClose()
132 void SnappyFile::rawFlush(FlushType type)
134 if (type == FlushDeep) {
140 void SnappyFile::flushCache()
142 if (m_mode == File::Write) {
143 size_t compressedLength;
145 ::snappy::RawCompress(m_cache, SNAPPY_CHUNK_SIZE - freeCacheSize(),
146 m_compressedCache, &compressedLength);
148 writeCompressedLength(compressedLength);
149 m_stream.write(m_compressedCache, compressedLength);
150 m_cachePtr = m_cache;
151 } else if (m_mode == File::Read) {
154 //assert(m_cachePtr == m_cache + m_cacheSize);
155 size_t compressedLength;
156 compressedLength = readCompressedLength();
157 m_stream.read((char*)m_compressedCache, compressedLength);
158 ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
162 createCache(m_cacheSize);
163 ::snappy::RawUncompress(m_compressedCache, compressedLength,
168 void SnappyFile::createCache(size_t size)
170 m_cache = new char[size];
171 m_cachePtr = m_cache;
175 void SnappyFile::writeCompressedLength(size_t value)
177 m_stream.write((char*)&value, sizeof value);
180 size_t SnappyFile::readCompressedLength()
183 m_stream.read((char*)&len, sizeof len);