package cache;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import disk.FileBlockRange;
import events.NFSReadEvent;
import score.ScoringFunction;
import simulation.CacheSimInterface;
import simulation.Simulation;
import simulation.Stats;
public class CacheSim implements CacheSimInterface{
private int fetchMax;
private int evictMax;
private Simulation sim;
private TreeSet<DataBlockInfo> fetchQueue;
private TreeSet<DataBlockInfo> evictionQueue;
private Map<Long,TreeSet<Long>> pendingRequests;
private Map<DataBlockInfo, DataBlock> cache;
private Stats stats;
private ScoringFunction scoreFunc;
public CacheSim(Simulation sim, int fetchSize, int evictSize) {
this.sim = sim;
fetchMax = fetchSize;
evictMax = evictSize;
fetchQueue = new TreeSet<DataBlockInfo>(DataScoreFunction.FORWARD);
evictionQueue = new TreeSet<DataBlockInfo> (DataScoreFunction.BACKWARD);
pendingRequests = new TreeMap<Long, TreeSet<Long>>();
cache = new HashMap<DataBlockInfo, DataBlock>();
stats = new Stats();
}
public boolean isCached(DataBlockInfo dbi) {
return cache.containsKey(dbi);
}
public void changeScore(DataBlockInfo dbi, double newScore) {
if(fetchQueue.contains(dbi)){
fetchQueue.remove(dbi);
dbi.setScore(newScore);
fetchQueue.add(dbi);
}
if(evictionQueue.contains(dbi)){
evictionQueue.remove(dbi);
dbi.setScore(newScore);
evictionQueue.add(dbi);
}
}
public void updateQueue() {
if(fetchQueue.isEmpty())return;
if(evictionQueue.isEmpty()){
evictionQueue.add(fetchQueue.pollFirst());
if(fetchQueue.isEmpty())return;
}
while((evictionQueue.size()<evictMax)
&& (fetchQueue.first().getScore()>evictionQueue.last().getScore())
){
evictionQueue.pollLast();
evictionQueue.add(fetchQueue.pollFirst());
}
}
public void addToFetch(DataBlockInfo dbi) {
fetchQueue.add(dbi);
System.out.println("Fetch queue: " + fetchQueue.size());
}
@Override
public void blockReady(long requestID, long diskBlock) {
stats.addMiss(responseTime);
}
@Override
public void handleReadRequest(NFSReadEvent e) {
long waitingOn = 0;
FileBlockRange range = sim.filesystemModel.getFileBlocks(e);
}
}