Skip to content

Commit

Permalink
modified cache strategy for zim clusters
Browse files Browse the repository at this point in the history
  • Loading branch information
Orbiter committed Nov 3, 2023
1 parent fdc6311 commit 496f768
Showing 1 changed file with 3 additions and 27 deletions.
30 changes: 3 additions & 27 deletions source/org/openzim/ZIMReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,28 +337,18 @@ public DirectoryEntry getDirectoryInfo(final char namespace, String articleName)
public Cluster getCluster(int clusterNumber) throws IOException {
for (int i = 0; i < this.clusterCache.size(); i++) {
Cluster c = clusterCache.get(i);
if (c.cluster_number == clusterNumber) {
c.incUsage(); // cache hit
return c;
}
if (c.cluster_number == clusterNumber) return c;
}

// cache miss
Cluster c = new Cluster(clusterNumber);

// check cache size
if (clusterCache.size() >= MAX_CLUSTER_CACHE_SIZE) {
// remove one entry
double maxEntry = Double.MIN_VALUE;
int pos = -1;
for (int i = 0; i < clusterCache.size(); i++) {
double r = this.clusterCache.get(i).getUsageRatio();
if (r > maxEntry) {maxEntry = r; pos = i;}
}
if (pos >= 0) this.clusterCache.remove(pos);
// remove one entry: the first entry is the oldest entry
this.clusterCache.remove(0);
}

c.incUsage();
this.clusterCache.add(c);
return c;
}
Expand All @@ -378,12 +368,10 @@ private class Cluster {

private int cluster_number; // used to identify the correct cache entry
private List<byte[]> blobs;
private int usageCounter; // used for efficient caching and cache stale detection
private boolean extended;

public Cluster(int cluster_number) throws IOException {
this.cluster_number = cluster_number;
this.usageCounter = 0;

// open the cluster and make a Input Stream with the proper decompression type
final long clusterPos = mFile.geClusterPtr(cluster_number);
Expand Down Expand Up @@ -444,21 +432,9 @@ public byte[] getBlob(int i) {
return this.blobs.get(i);
}

public void incUsage() {
this.usageCounter++;
}

public int getUsage() {
return this.usageCounter;
}

public int getSize() {
return this.blobs.size();
}

public double getUsageRatio() {
return ((double) this.usageCounter) / ((double) this.blobs.size());
}
}

public byte[] getArticleData(final DirectoryEntry directoryInfo) throws IOException {
Expand Down

0 comments on commit 496f768

Please sign in to comment.