Skip to content

Commit

Permalink
Agregación de algoritmo FIFO
Browse files Browse the repository at this point in the history
  • Loading branch information
edmunoz committed Sep 4, 2015
1 parent 4d7a094 commit 596ef37
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 11 deletions.
81 changes: 81 additions & 0 deletions src/filesystemcaches/FIFOCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package filesystemcaches;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;


/**
*
* @author Esteban Muñoz <[email protected]>
*/
public class FIFOCache<K,V> extends LinkedHashMap<K,V>{
private final int cacheSize;
private long missRate = 0;
private long hitRate = 0;
/**
* Constructor
* @param cacheSize Tamaño de la cache(Numero de entidades)
*/
public FIFOCache(int cacheSize) {
super(cacheSize,0.75f,false);
this.cacheSize = cacheSize;
}

/**
* preguntamos si hay datos
* Si no hay agregamos el dato y miss++
* Si hay
*
*/
@Override
public V put(K key, V value) {
V result = null;
if(this.size() < this.cacheSize){//Validamos si la cache llego a su limite
if(containsValue(value)){//Preguntamos si el valor esta en la tabla
this.hitRate++;//Aumentamos el hit rate
}
else{//Si no se encuentra en la tabla
result = super.put(key, value);//Agregamos el valor
this.missRate++;//Aumentamos el miss rate
}
}
else{//Cuando la cache llego a su tamaño total
if(containsValue(value)){//Si el valor esta en la tabla
this.hitRate++;//Aumentamos el hit rate
}
else{
Object m = keySet().toArray()[0];//Obtenemos el primer valor en la tabla
remove(m);//Lo removemos
result = super.put(key, value);//Agregamos el nuevo valor al final
this.missRate++;//Aumentamos el miss ratr
}
}
return result;
}

public static <T, E> T getKeyByValue(Map<T, E> map, E value) {
for (Map.Entry<T, E> entry : map.entrySet()) {
if (Objects.equals(value, entry.getValue())) {
return entry.getKey();
}
}
return null;
}

public long getHitRate() {
return hitRate;
}

public long getMissRate() {
return missRate;
}

}
43 changes: 33 additions & 10 deletions src/filesystemcaches/FileSystemCaches.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;

/**
*
Expand All @@ -17,23 +16,23 @@ public class FileSystemCaches {
public static void main(String[] args) {
// TODO code application logic here
//$java cacheSimulator workload.txt LRU 50000
//time java -cp build/classes/ filesystemcaches.FileSystemCaches 5
//time java -cp build/classes/ filesystemcaches.FileSystemCaches file algorit cacheSize
String file = args[0].toString
();
String file = args[0].toString();
String algorit = args[1].toString();
String cacheSize = Integer.parseInt(args[2].toString());
int cacheSize = Integer.parseInt(args[2].toString());
switch(algorit) {
case "FIFO":
System.out.println("FIFO");
System.out.println("Runing FIFO cache simulator .....");
FIFOCache<Integer,String> cacheFIFO = new FIFOCache<>(cacheSize);
FIFO(cacheFIFO,file);
break;
case "OPTIMUS":
System.out.println("OPTIMUS");
break;
case "LRU":
System.out.println("LRU");
LRUCache<Integer,String> cache = new LRUCache<>(cacheSize);
LRU(cache,"/home/esteban/Descargas/libro.txt");
System.out.println("Runing LRU cache simulator .....");
LRUCache<Integer,String> cacheLRU = new LRUCache<>(cacheSize);
LRU(cacheLRU,file);
break;
default:
throw new AssertionError();
Expand All @@ -49,7 +48,6 @@ public static void LRU(LRUCache cache,String fileName){
br = new BufferedReader(new FileReader(fileName));
while ((sCurrentLine = br.readLine()) != null){
count++;
System.out.println(count);
cache.put(count,sCurrentLine);
}
} catch (IOException e) {
Expand All @@ -66,5 +64,30 @@ public static void LRU(LRUCache cache,String fileName){
System.out.println("Misses :"+cache.getMissRate());
}

public static void FIFO(FIFOCache cache,String fileName){
BufferedReader br = null;
int count = 0;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(fileName));
while ((sCurrentLine = br.readLine()) != null) {
count++;
System.out.println(count);
cache.put(count,sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
System.out.println(cache);
System.out.println("Hits : "+ cache.getHitRate());
System.out.println("Misses :"+cache.getMissRate());
}


}
56 changes: 55 additions & 1 deletion src/filesystemcaches/LRUCache.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package filesystemcaches;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;

/**
* @reference http://codeidol.com/java/javagenerics/Maps/Implementing-Map/
Expand All @@ -11,6 +13,8 @@
*/
public class LRUCache<K,V> extends LinkedHashMap<K,V> {
private final int cacheSize;
private long missRate = 0;
private long hitRate = 0;

/**
* Constructor
Expand All @@ -20,7 +24,7 @@ public LRUCache(int cacheSize) {
//int initialCapacity = 16 La capacidad inicial
//float loadFactor = 0.75f El factor de carga
//boolean accessOrder = true Modo de ordenamiento
super(cacheSize, 0.75f,true);
super(cacheSize, 0.75f,false);
this.cacheSize = cacheSize;
}

Expand All @@ -29,4 +33,54 @@ protected boolean removeEldestEntry(Entry<K, V> eldest) {
return this.size() > this.cacheSize;
}

@Override
public V put(K key, V value) {
V result = null;
int currentSize = this.size();
if(currentSize < this.cacheSize){
if(containsValue(value)){
remove(getKeyByValue(this, value));
super.put(key, value);
this.hitRate++;
}
else{
result = super.put(key, value);
this.missRate++;
}
}
else{
if(containsValue(value)){
Object x = getKeyByValue(this, value);
remove(x);
super.put(key, value);
this.hitRate++;
}
else{
Object m = keySet().toArray()[0];
remove(m);
super.put(key, value);
this.missRate++;
}
}
return result; //To change body of generated methods, choose Tools | Templates.
}


public static <T, E> T getKeyByValue(Map<T, E> map, E value) {
for (Entry<T, E> entry : map.entrySet()) {
if (Objects.equals(value, entry.getValue())) {
return entry.getKey();
}
}
return null;
}

public long getHitRate() {
return hitRate;
}

public long getMissRate() {
return missRate;
}

}

0 comments on commit 596ef37

Please sign in to comment.