Ah, yes. I am posting below the code of my class, but as I mentioned
above, I will now go with sqlite first. When the project is up and
running then if there is need then I will think of this approach.
Anyway, if you have time then please go through the code and notify me
of any gotchas that I maybe doing. I am still a learner; and sorry for
the sparse comments, it is mostly a prototype.
------
import java.io.*;
import java.util.*;
/**
* Parses and reads specially created Template dumps.
* <u>File format of dump:-<u>
* <code>template1 name <template1 value>template2 name
<template2 value>...</code>
* New-lines and space don't bother, but they are read and stored.<br>
* <b>Note:</b> The dump file must specify a template only once, else
the class would generate
* Runtime error.
*/
public class TemplateFetcher {
private RandomAccessFile rafTemIn = null;
private Hashtable<String,TemVal> temMap;
private Vector<String> lruQ; //Least recently used templates
queue.
private int cacheLimit = 10240; //in B (default: 10kB)
private int currCacheSize = 0; //in in B
final private static long SIZEOF_CHAR = 2;
public TemplateFetcher(String templateDat){
boolean parsingTemName = true;
boolean parsingTemVal = false;
try{
String TemName = new String("");
long bloc = -1;
long eloc = -1;
rafTemIn = new RandomAccessFile(new
File(templateDat), "r");
temMap = new Hashtable<String,TemVal>();
lruQ = new Vector<String>();
while(true){
byte[] b = new byte[2];
rafTemIn.read(b);
String chr = new String(b,"UTF-8");
System.err.println("~>"+chr);
if( !chr.equals("`") &&
parsingTemName)
TemName = TemName + chr;
else
if(chr.equals("`") && parsingTemName){
parsingTemName = false;
parsingTemVal = true;
bloc =
rafTemIn.getFilePointer() + SIZEOF_CHAR;
} else
if(chr.equals("`") && parsingTemVal){
parsingTemName = true;
parsingTemVal = false;
eloc =
rafTemIn.getFilePointer() - SIZEOF_CHAR;
//Adding these values to the
map.
TemName = TemName.trim();
temMap.put(TemName,new
TemVal(bloc,eloc));
System.err.println(TemName+"--
>"+bloc+","+eloc);
TemName = "";
bloc = -1;
eloc = -1;
}
}
} catch (EOFException e) {
System.err.println("Template dump read.");
/* */
} catch (IOException e) {
System.err.println("Template Dump file input
error");
}
}
/**
* Relases any resources that needs explicit call.
*/
public void putToRest(){
try{
rafTemIn.close();
}catch(IOException e){
System.err.println("Error while closing
template dump file.");
}
}
/**
* Sets max cache size in KB to cache the templates.
*/
public void setCacheLimit(int cacheSize){
cacheLimit = 1024 * cacheSize;
}
public String getTemplateVal(String templateName){
if(temMap.get(templateName)!=null){
TemVal temVal = temMap.get(templateName);
if(temVal.content!=null){
int index =
lruQ.indexOf(templateName);
lruQ.removeElementAt(index);
lruQ.add(templateName);
return temVal.content;
} else {
String stemval=null;;
try {
rafTemIn.seek(temVal.bloc);
byte[] b = new byte[(int)
(temVal.eloc - temVal.bloc + 1L)];
rafTemIn.read(b,0,(int)
(temVal.eloc - temVal.bloc + 1L));
stemval = new
String(b,"UTF-8");
} catch(IOException eio) {
System.err.println("Template
Dump file IO error.");
}
currCacheSize += stemval.length() *
(int)SIZEOF_CHAR;
temVal.content = stemval;
while(currCacheSize > cacheLimit &&
lruQ.size()!=0){
int index =
lruQ.indexOf(templateName);
TemVal firstTem =
temMap.get(lruQ.get(0));
firstTem.content = null;
currCacheSize = currCacheSize
- firstTem.content.length() / (int)SIZEOF_CHAR;
lruQ.removeElementAt(0);
}
lruQ.add(templateName);
return temVal.content;
}
} else
return null;
}
private class TemVal{
long bloc = -1; //Beging location of value block.
long eloc = -1; //End location of the template value.
String content = null; //The value of the template.
TemVal() { }
TemVal(long Bloc,long Eloc){
bloc=Bloc;
eloc=Eloc;
}
}
}
------
On Feb 22, 2:01 am, Axel <
axel...@gmail.com> wrote: