Skip to content

Commit e6fbbbf

Browse files
committed
Correctly test that changing the cache size on live archive has effect.
1 parent 5a4fd26 commit e6fbbbf

File tree

7 files changed

+101
-1
lines changed

7 files changed

+101
-1
lines changed

include/zim/archive.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,12 +534,18 @@ namespace zim
534534
*/
535535
std::shared_ptr<FileImpl> getImpl() const { return m_impl; }
536536

537-
/** Get the size of the cluster cache.
537+
/** Get the maximum size of the cluster cache.
538538
*
539539
* @return The maximum number of clusters stored in the cache.
540540
*/
541541
size_t get_cluster_cache_max_size() const;
542542

543+
/** Get the current size of the cluster cache.
544+
*
545+
* @return The number of clusters currently stored in the cache.
546+
*/
547+
size_t get_cluster_cache_current_size() const;
548+
543549
/** Set the size of the cluster cache.
544550
*
545551
* If the new size is lower than the number of currently stored clusters
@@ -555,6 +561,12 @@ namespace zim
555561
*/
556562
size_t get_dirent_cache_max_size() const;
557563

564+
/** Get the current size of the dirent cache.
565+
*
566+
* @return The number of dirents currently stored in the cache.
567+
*/
568+
size_t get_dirent_cache_current_size() const;
569+
558570
/** Set the size of the dirent cache.
559571
*
560572
* If the new size is lower than the number of currently stored dirents

src/archive.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,11 @@ namespace zim
509509
return m_impl->get_cluster_cache_max_size();
510510
}
511511

512+
size_t Archive::get_cluster_cache_current_size() const
513+
{
514+
return m_impl->get_cluster_cache_current_size();
515+
}
516+
512517
void Archive::set_cluster_cache_max_size(size_t nb_clusters)
513518
{
514519
m_impl->set_cluster_cache_max_size(nb_clusters);
@@ -519,6 +524,11 @@ namespace zim
519524
return m_impl->get_dirent_cache_max_size();
520525
}
521526

527+
size_t Archive::get_dirent_cache_current_size() const
528+
{
529+
return m_impl->get_dirent_cache_current_size();
530+
}
531+
522532
void Archive::set_dirent_cache_max_size(size_t nb_dirents)
523533
{
524534
m_impl->set_dirent_cache_max_size(nb_dirents);

src/concurrent_cache.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ class ConcurrentCache
9090
return impl_.get_max_size();
9191
}
9292

93+
size_t get_current_size() const {
94+
std::unique_lock<std::mutex> l(lock_);
95+
return impl_.size();
96+
}
97+
9398
void set_max_size(size_t new_size) {
9499
std::unique_lock<std::mutex> l(lock_);
95100
return impl_.set_max_size(new_size);

src/dirent_accessor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class LIBZIM_PRIVATE_API DirectDirentAccessor
5656
entry_index_t getDirentCount() const { return m_direntCount; }
5757

5858
size_t get_max_cache_size() const { return m_direntCache.get_max_size(); }
59+
size_t get_current_cache_size() const { return m_direntCache.size(); }
5960
void set_max_cache_size(size_t nb_dirents) const { m_direntCache.set_max_size(nb_dirents); }
6061

6162
private: // functions

src/fileimpl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,13 +795,19 @@ bool checkTitleListing(const IndirectDirentAccessor& accessor, entry_index_type
795795
size_t FileImpl::get_cluster_cache_max_size() const {
796796
return clusterCache.get_max_size();
797797
}
798+
size_t FileImpl::get_cluster_cache_current_size() const {
799+
return clusterCache.get_current_size();
800+
}
798801
void FileImpl::set_cluster_cache_max_size(size_t nb_clusters) {
799802
clusterCache.set_max_size(nb_clusters);
800803
}
801804

802805
size_t FileImpl::get_dirent_cache_max_size() const {
803806
return mp_pathDirentAccessor->get_max_cache_size();
804807
}
808+
size_t FileImpl::get_dirent_cache_current_size() const {
809+
return mp_pathDirentAccessor->get_current_cache_size();
810+
}
805811
void FileImpl::set_dirent_cache_max_size(size_t nb_dirents) {
806812
mp_pathDirentAccessor->set_max_cache_size(nb_dirents);
807813
}

src/fileimpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,10 @@ namespace zim
154154
bool checkIntegrity(IntegrityCheck checkType);
155155

156156
size_t get_cluster_cache_max_size() const;
157+
size_t get_cluster_cache_current_size() const;
157158
void set_cluster_cache_max_size(size_t nb_clusters);
158159
size_t get_dirent_cache_max_size() const;
160+
size_t get_dirent_cache_current_size() const;
159161
void set_dirent_cache_max_size(size_t nb_dirents);
160162
size_t get_dirent_lookup_cache_max_size() const;
161163
void set_dirent_lookup_cache_max_size(size_t nb_ranges) { m_direntLookupSize = nb_ranges; };

test/archive.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,70 @@ TEST(ZimArchive, cacheDontImpactReading)
335335
}
336336
}
337337

338+
339+
TEST(ZimArchive, cacheChange)
340+
{
341+
342+
for (auto& testfile: getDataFilePath("wikibooks_be_all_nopic_2017-02.zim")) {
343+
auto archive = zim::Archive(testfile.path);
344+
345+
archive.set_dirent_cache_max_size(30);
346+
archive.set_cluster_cache_max_size(5);
347+
348+
auto range = archive.iterEfficient();
349+
auto it = range.begin();
350+
for (auto i = 0; i<50 && it != range.end(); i++, it++) {
351+
// Be sure to search by path to populate the dirent cache
352+
auto entry = archive.getEntryByPath(it->getPath());
353+
auto item = entry.getItem(true);
354+
auto data = item.getData();
355+
}
356+
EXPECT_EQ(archive.get_dirent_cache_current_size(), 30);
357+
EXPECT_EQ(archive.get_cluster_cache_current_size(), 2); // Only 2 clusters in the file
358+
359+
// Reduce cache size
360+
archive.set_dirent_cache_max_size(10);
361+
archive.set_cluster_cache_max_size(1);
362+
363+
EXPECT_EQ(archive.get_dirent_cache_current_size(), 10);
364+
EXPECT_EQ(archive.get_cluster_cache_current_size(), 1);
365+
366+
for (auto i = 0; i<50 && it != range.end(); i++, it++) {
367+
// Be sure to search by path to populate the dirent cache
368+
auto entry = archive.getEntryByPath(it->getPath());
369+
auto item = entry.getItem(true);
370+
auto data = item.getData();
371+
}
372+
373+
EXPECT_EQ(archive.get_dirent_cache_current_size(), 10);
374+
EXPECT_EQ(archive.get_cluster_cache_current_size(), 1);
375+
376+
// Clean cache
377+
// (More than testing the value, this is needed as we want to be sure the cache is actually populated later)
378+
archive.set_dirent_cache_max_size(0);
379+
archive.set_cluster_cache_max_size(0);
380+
381+
EXPECT_EQ(archive.get_dirent_cache_current_size(), 0);
382+
EXPECT_EQ(archive.get_cluster_cache_current_size(), 0);
383+
384+
// Increase the cache
385+
archive.set_dirent_cache_max_size(20);
386+
archive.set_cluster_cache_max_size(1);
387+
EXPECT_EQ(archive.get_dirent_cache_current_size(), 0);
388+
EXPECT_EQ(archive.get_cluster_cache_current_size(), 0);
389+
390+
for (auto it = range.begin(); it != range.end(); it++) {
391+
// Be sure to search by path to populate the dirent cache
392+
auto entry = archive.getEntryByPath(it->getPath());
393+
auto item = entry.getItem(true);
394+
auto data = item.getData();
395+
}
396+
397+
EXPECT_EQ(archive.get_dirent_cache_current_size(), 20);
398+
EXPECT_EQ(archive.get_cluster_cache_current_size(), 1);
399+
}
400+
}
401+
338402
TEST(ZimArchive, openDontFallbackOnNonSplitZimArchive)
339403
{
340404
const char* fname = "wikibooks_be_all_nopic_2017-02.zim";

0 commit comments

Comments
 (0)