Skip to content

Commit

Permalink
Make file e implementación del algoritmo LRU
Browse files Browse the repository at this point in the history
  • Loading branch information
edmunoz committed Sep 4, 2015
1 parent 184f376 commit 4d7a094
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#https://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html

all:

compile:
time javac -sourcepath src -d build/classes/ src/filesystemcaches/FileSystemCaches.java
#' Para ejecutar siga el siguiente orden
#'

run:
time java -cp build/classes/ filesystemcaches.FileSystemCaches 5
#' Ejecutando...
70 changes: 70 additions & 0 deletions src/filesystemcaches/FileSystemCaches.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package filesystemcaches;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;

/**
*
* @author Esteban Muñoz <[email protected]>
*/
public class FileSystemCaches {

/**
* @param args the command line arguments
*/
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 algorit = args[1].toString();
String cacheSize = Integer.parseInt(args[2].toString());
switch(algorit) {
case "FIFO":
System.out.println("FIFO");
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");
break;
default:
throw new AssertionError();
}
}


public static void LRU(LRUCache 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());
}


}
32 changes: 32 additions & 0 deletions src/filesystemcaches/LRUCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package filesystemcaches;

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

/**
* @reference http://codeidol.com/java/javagenerics/Maps/Implementing-Map/
* @author Esteban Muñoz <[email protected]>
* @param <K> Key
* @param <V> Value
*/
public class LRUCache<K,V> extends LinkedHashMap<K,V> {
private final int cacheSize;

/**
* Constructor
* @param cacheSize Tamaño de la cache(Numero de entidades)
*/
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);
this.cacheSize = cacheSize;
}

@Override
protected boolean removeEldestEntry(Entry<K, V> eldest) {
return this.size() > this.cacheSize;
}

}

0 comments on commit 4d7a094

Please sign in to comment.