| 2244 | } |
| 2245 | |
| 2246 | uint64_t StreamIndexedIO::Index::write() |
| 2247 | { |
| 2248 | StreamIndexedIO::StreamFile &f = *m_stream; |
| 2249 | |
| 2250 | /// Write index at end |
| 2251 | std::streampos indexStart = m_next; |
| 2252 | |
| 2253 | f.seekp( m_next, std::ios::beg ); |
| 2254 | |
| 2255 | m_offset = indexStart; |
| 2256 | |
| 2257 | // todo estimate the memory required to avoid re-allocations? |
| 2258 | MemoryStreamSink sink; |
| 2259 | io::filtering_ostream indexOutStream; |
| 2260 | indexOutStream.push( sink ); |
| 2261 | |
| 2262 | assert( indexOutStream.is_complete() ); |
| 2263 | |
| 2264 | m_stringCache.write( indexOutStream ); |
| 2265 | |
| 2266 | writeNode( m_root, indexOutStream ); |
| 2267 | |
| 2268 | assert( m_freePagesOffset.size() == m_freePagesSize.size() ); |
| 2269 | uint64_t numFreePages = m_freePagesSize.size(); |
| 2270 | |
| 2271 | // Write out number of free "pages" |
| 2272 | writeLittleEndian( indexOutStream, numFreePages); |
| 2273 | |
| 2274 | /// Write out each free page |
| 2275 | for ( FreePagesSizeMap::const_iterator it = m_freePagesSize.begin(); it != m_freePagesSize.end(); ++it) |
| 2276 | { |
| 2277 | writeLittleEndian( indexOutStream, it->second->m_offset ); |
| 2278 | writeLittleEndian( indexOutStream, it->second->m_size ); |
| 2279 | } |
| 2280 | |
| 2281 | /// To synchronize/close, etc. |
| 2282 | indexOutStream.pop(); |
| 2283 | |
| 2284 | // compress the output stream using blosc before writing |
| 2285 | char* indexData = nullptr; |
| 2286 | std::streamsize indexDataSize; |
| 2287 | |
| 2288 | sink.get(indexData, indexDataSize); |
| 2289 | |
| 2290 | std::vector<char> compressedIndex; |
| 2291 | compress( indexData, indexDataSize, compressedIndex, indexCompressionLevel, indexCompressor, 1, BLOSC_MAX_BUFFERSIZE, 0); |
| 2292 | |
| 2293 | f.write( &compressedIndex[0], compressedIndex.size() ); |
| 2294 | |
| 2295 | writeLittleEndian( f, getCompressionCode( m_compressor )); |
| 2296 | writeLittleEndian( f, m_compressionLevel ); |
| 2297 | |
| 2298 | writeLittleEndian( f, m_offset ); |
| 2299 | writeLittleEndian( f, g_currentVersion ); |
| 2300 | writeLittleEndian( f, g_versionedMagicNumber ); |
| 2301 | |
| 2302 | m_hasChanged = false; |
| 2303 |
no test coverage detected