-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make file e implementación del algoritmo LRU
- Loading branch information
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |