Skip to content

Commit

Permalink
Lazy/on-demand decompression of clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
veloman-yunkan committed Sep 2, 2020
1 parent 3dcd79e commit c610baa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,6 @@ CompressedCluster::CompressedCluster(std::shared_ptr<const Reader> reader, Compr
readHeader<uint64_t>(*ds_);
else
readHeader<uint32_t>(*ds_);

readBlobs(*ds_);
}

bool
Expand Down Expand Up @@ -284,16 +282,17 @@ CompressedCluster::readHeader(IDataStream& ds)
}

void
CompressedCluster::readBlobs(IDataStream& ds)
CompressedCluster::ensureBlobIsDecompressed(blob_index_t n) const
{
const size_t n = count().v;
for ( size_t i = 0; i < n; ++i )
blobs_.push_back(ds.readBlob(getBlobSize(blob_index_t(i)).v));
for ( size_t i = blobs_.size(); i <= n.v; ++i )
blobs_.push_back(ds_->readBlob(getBlobSize(blob_index_t(i)).v));
}

Blob
CompressedCluster::getBlob(blob_index_t n) const
{
std::lock_guard<std::mutex> lock(blobAccessMutex_);
ensureBlobIsDecompressed(n);
ASSERT(n.v, <, blobs_.size());
const IDataStream::Blob& blob = blobs_[n.v];
return idsBlob2zimBlob(blob, 0, blob.size());
Expand All @@ -302,6 +301,8 @@ CompressedCluster::getBlob(blob_index_t n) const
Blob
CompressedCluster::getBlob(blob_index_t n, offset_t offset, zsize_t size) const
{
std::lock_guard<std::mutex> lock(blobAccessMutex_);
ensureBlobIsDecompressed(n);
ASSERT(n.v, <, blobs_.size());
return idsBlob2zimBlob(blobs_[n.v], offset.v, size.v);
}
Expand Down
7 changes: 5 additions & 2 deletions src/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <iosfwd>
#include <vector>
#include <memory>
#include <mutex>

#include "zim_types.h"
#include "zim/error.h"
Expand Down Expand Up @@ -110,13 +111,15 @@ namespace zim
template<typename OFFSET_TYPE>
void readHeader(IDataStream& ds);

void readBlobs(IDataStream& ds);
void ensureBlobIsDecompressed(blob_index_t i) const;

private: // data
const std::shared_ptr<IDataStream> ds_;
const CompressionType compression_;
std::vector<size_t> blobSizes_;
std::vector<IDataStream::Blob> blobs_;

mutable std::mutex blobAccessMutex_;
mutable std::vector<IDataStream::Blob> blobs_;
};

}
Expand Down

0 comments on commit c610baa

Please sign in to comment.