From: <ku...@us...> - 2008-01-18 07:49:33
|
Revision: 383 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=383&view=rev Author: kurzum Date: 2008-01-17 23:49:29 -0800 (Thu, 17 Jan 2008) Log Message: ----------- commented cache Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java 2008-01-16 15:42:53 UTC (rev 382) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java 2008-01-18 07:49:29 UTC (rev 383) @@ -29,36 +29,61 @@ import java.util.HashMap; /** - * SPARQL query cache to avoid possibly expensive multiple queries. + * SPARQL query cache to avoid possibly expensive multiple queries. An object of + * this class can be the cache itself or a cache object(one entry), We could + * split that in two classes, but one entry o object only has contains data and + * one additional function and would just be a data class * + * it writes the files according to one resource in the basedir and saves the + * cache object in it. + * * @author Sebastian Hellmann - * + * */ public class Cache implements Serializable { - // Object can be the cache itself - // or a cache object(one entry) - // it now uses a hashmap and can contain different queries at once - private HashMap<String, String> hm; + /** + * This maps sparql query to sparql result + */ + protected HashMap<String, String> hm; + final static long serialVersionUID = 104; transient String basedir = ""; transient String fileending = ".cache"; + transient boolean debug_print_flag = false; long timestamp; - long daysoffreshness = 15; - long multiplier = 24 * 60 * 60 * 1000;// h m s ms + /** + * After how many days cache entries get invalid + */ + protected long daysoffreshness = 15; + protected long multiplier = 24 * 60 * 60 * 1000;// h m s ms + // private HashMap<String, String> inmem_cache; - // constructor for the cache itself + // + /** + * constructor for the cache itself + * + * @param path + * where the cache files will be + */ public Cache(String path) { this.basedir = path + File.separator; if (!new File(path).exists()) { - System.out.println(new File(path).mkdir()); - ; + System.out.println("created directory: " + path + " : " + + new File(path).mkdir()); + } } // constructor for single cache object(one entry) - public Cache(String sparql, String content) { + /** + * @param sparql + * query + * @param content + * that is the sparql query result as xml + */ + protected Cache(String sparql, String content) { // this.content = c; // this.sparqlquery = sparql; this.timestamp = System.currentTimeMillis(); @@ -66,7 +91,17 @@ hm.put(sparql, content); } - public String get(String key, String sparql) { + /** + * gets a chached sparqlquery for a resource(key) and returns the + * sparqlXMLResult or null, if none is found. + * + * @param key + * is the resource, the identifier + * @param sparqlquery + * is a special sparql query about that resource + * @return sparqlXMLResult + */ + public String get(String key, String sparqlquery) { // System.out.println("get From "+key); String ret = null; try { @@ -79,7 +114,7 @@ // System.out.println("fresh"); String xml = ""; try { - xml = c.hm.get(sparql); + xml = c.hm.get(sparqlquery); } catch (Exception e) { return null; } @@ -90,22 +125,37 @@ e.printStackTrace(); } return ret; - }; + } - public void put(String key, String sparql, String content) { + /** + * @param key + * is the resource, the identifier + * @param sparqlquery + * is the query used as another identifier + * @param content + * is the result of the query + */ + public void put(String key, String sparqlquery, String content) { // System.out.println("put into "+key); Cache c = readFromFile(makeFilename(key)); if (c == null) { - c = new Cache(sparql, content); + c = new Cache(sparqlquery, content); putIntoFile(makeFilename(key), c); } else { - c.hm.put(sparql, content); + c.hm.put(sparqlquery, content); putIntoFile(makeFilename(key), c); } } - String makeFilename(String key) { + /** + * this function takes a resource string and then URIencodes it and makes a + * filename out of it for the use in the hashmap + * + * @param key + * @return the complete key for filename in the hashmap + */ + protected String makeFilename(String key) { String ret = ""; try { ret = basedir + URLEncoder.encode(key, "UTF-8") + fileending; @@ -115,14 +165,6 @@ return ret; } - boolean checkFreshness() { - if ((System.currentTimeMillis() - this.timestamp) <= (daysoffreshness * multiplier)) - // fresh - return true; - else - return false; - } - public void checkFile(String Filename) { if (!new File(Filename).exists()) { try { @@ -135,12 +177,18 @@ } - public void putIntoFile(String Filename, Cache content) { + /** + * puts a cache entry in a file + * + * @param Filename + * @param c + */ + protected void putIntoFile(String Filename, Cache c) { try { // FileWriter fw=new FileWriter(new File(Filename),true); FileOutputStream fos = new FileOutputStream(Filename, false); ObjectOutputStream o = new ObjectOutputStream(fos); - o.writeObject(content); + o.writeObject(c); fos.flush(); fos.close(); } catch (Exception e) { @@ -148,7 +196,13 @@ } } - public Cache readFromFile(String Filename) { + /** + * reads a cache entry from a file + * + * @param Filename + * @return cache entry + */ + protected Cache readFromFile(String Filename) { Cache content = null; try { FileInputStream fos = new FileInputStream(Filename); @@ -161,5 +215,13 @@ return content; } - + + protected boolean checkFreshness() { + if ((System.currentTimeMillis() - this.timestamp) <= (daysoffreshness * multiplier)) + // fresh + return true; + else + return false; + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jen...@us...> - 2008-01-29 20:05:04
|
Revision: 458 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=458&view=rev Author: jenslehmann Date: 2008-01-29 12:04:57 -0800 (Tue, 29 Jan 2008) Log Message: ----------- finished cache implementation (probably still not working - needs to be tested) Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java 2008-01-29 19:24:02 UTC (rev 457) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java 2008-01-29 20:04:57 UTC (rev 458) @@ -28,7 +28,7 @@ import java.io.Serializable; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import java.util.HashMap; +import java.util.LinkedList; import org.apache.log4j.Logger; @@ -49,6 +49,10 @@ * If a cached result of a SPARQL query exists, but is too old, the cache * behaves as if the cached result would not exist. * + * TODO: We are doing md5 hashing at the moment, so in rare cases different + * SPARQL queries can be mapped to the same file. Support for such scenarios + * needs to be included. + * * @author Sebastian Hellmann * @author Sebastian Knappe * @author Jens Lehmann @@ -60,12 +64,12 @@ private static final long serialVersionUID = 843308736471742205L; // maps hash of a SPARQL queries to JSON representation - // of its results; this - private HashMap<String, String> hm; + // of its results; this + // private HashMap<String, String> hm; private transient String cacheDir = ""; private transient String fileEnding = ".cache"; - private long timestamp; + // private long timestamp; // specifies after how many seconds a cached result becomes invalid private long freshnessSeconds = 15 * 24 * 60 * 60; @@ -85,22 +89,7 @@ } } - /** - * constructor for single cache object(one entry) - * - * @param sparqlQuery - * query - * @param content - * that is the sparql query result as xml - */ - private Cache(String sparqlQuery, String content) { - // this.content = c; - // this.sparqlquery = sparql; - this.timestamp = System.currentTimeMillis(); - this.hm = new HashMap<String, String>(); - hm.put(sparqlQuery, content); - } - + // compute md5-hash private String getHash(String string) { // calculate md5 hash of the string (code is somewhat // difficult to read, but there doesn't seem to be a @@ -122,110 +111,92 @@ return hexString.toString(); } + // return filename where the query result should be saved private String getFilename(String sparqlQuery) { - return getHash(sparqlQuery) + fileEnding; + return cacheDir + getHash(sparqlQuery) + fileEnding; } - + /** - * Gets the query result for a SPARQL query. + * Gets a result for a query if it is in the cache. * * @param sparqlQuery * SPARQL query to check. * @return Query result or null if no result has been found or it is * outdated. */ - public String get(String sparqlQuery) { - Cache c = readFromFile(getFilename(sparqlQuery)); - if (c == null) + @SuppressWarnings({"unchecked"}) + private String getCacheEntry(String sparqlQuery) { + String filename = getFilename(sparqlQuery); + File file = new File(filename); + + // return null (indicating no result) if file does not exist + if(!file.exists()) return null; - // System.out.println(" file found"); - if (!c.checkFreshness()) - return null; - // System.out.println("fresh"); - String xml = ""; + + LinkedList<Object> entry = null; try { - xml = c.hm.get(sparqlQuery); - } catch (Exception e) { + FileInputStream fos = new FileInputStream(filename); + ObjectInputStream o = new ObjectInputStream(fos); + entry = (LinkedList<Object>) o.readObject(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + // TODO: we need to check whether the query is correct + // (may not always be the case due to md5 hashing) + + // determine whether query is outdated + long timestamp = (Long) entry.get(0); + boolean fresh = checkFreshness(timestamp); + + if(!fresh) { + // delete file + file.delete(); + // return null indicating no result return null; } - return xml; + + return (String) entry.get(2); } /** - * @param key - * is the resource, the identifier - * @param sparqlquery - * is the query used as another identifier - * @param content - * is the result of the query + * Adds an entry to the cache. + * + * @param sparqlQuery + * The SPARQL query. + * @param result + * Result of the SPARQL query. */ - public void put(String sparqlQuery, String content) { - String hash = getHash(sparqlQuery); - Cache c = readFromFile(hash); - if (c == null) { - c = new Cache(sparqlQuery, content); - putIntoFile(hash, c); - } else { - c.hm.put(sparqlQuery, content); - putIntoFile(hash, c); - } + private void addToCache(String sparqlQuery, String result) { + String filename = getFilename(sparqlQuery); + long timestamp = System.currentTimeMillis(); - } + // create the object which will be serialised + LinkedList<Object> list = new LinkedList<Object>(); + list.add(timestamp); + list.add(sparqlQuery); + list.add(result); - public void checkFile(String Filename) { - if (!new File(Filename).exists()) { - try { - new File(Filename).createNewFile(); - } catch (Exception e) { - e.printStackTrace(); - } + // create the file we want to use + File file = new File(filename); - } - - } - - /** - * puts a cache entry in a file - * - * @param filename - * @param c - */ - protected void putIntoFile(String filename, Cache c) { try { - // FileWriter fw=new FileWriter(new File(Filename),true); + file.createNewFile(); FileOutputStream fos = new FileOutputStream(filename, false); ObjectOutputStream o = new ObjectOutputStream(fos); - o.writeObject(c); + o.writeObject(list); fos.flush(); fos.close(); - } catch (Exception e) { - System.out.println("Not in cache creating: " + filename); - } - } - - /** - * reads a cache entry from a file - * - * @param Filename - * @return cache entry - */ - protected Cache readFromFile(String Filename) { - Cache content = null; - try { - FileInputStream fos = new FileInputStream(Filename); - ObjectInputStream o = new ObjectInputStream(fos); - content = (Cache) o.readObject(); } catch (IOException e) { e.printStackTrace(); - } catch (ClassNotFoundException e) { - e.printStackTrace(); } - return content; } - private boolean checkFreshness() { - if ((System.currentTimeMillis() - this.timestamp) <= (freshnessSeconds * 1000)) - // fresh + // check whether the given timestamp is fresh + private boolean checkFreshness(long timestamp) { + if ((System.currentTimeMillis() - timestamp) <= (freshnessSeconds * 1000)) return true; else return false; @@ -242,12 +213,15 @@ * @return Jena result set. */ public ResultSet executeSparqlQuery(SparqlQuery query) { - if (hm.containsKey(query.getQueryString())) { - String result = hm.get(query.getQueryString()); + String result = getCacheEntry(query.getQueryString()); + if (result != null) { return SparqlQuery.JSONtoResultSet(result); } else { query.send(); - return query.getResultSet(); + ResultSet rs = query.getResultSet(); + String json = SparqlQuery.getAsJSON(rs); + addToCache(query.getQueryString(), json); + return rs; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <sk...@us...> - 2008-01-30 15:37:22
|
Revision: 468 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=468&view=rev Author: sknappe Date: 2008-01-30 07:37:07 -0800 (Wed, 30 Jan 2008) Log Message: ----------- fixed a bug Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java 2008-01-30 15:27:18 UTC (rev 467) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java 2008-01-30 15:37:07 UTC (rev 468) @@ -222,8 +222,9 @@ if (rs!=null){ String json = SparqlQuery.getAsJSON(rs); addToCache(query.getQueryString(), json); + return SparqlQuery.JSONtoResultSet(json); } - return rs; + else return rs; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <sk...@us...> - 2008-02-18 17:28:25
|
Revision: 605 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=605&view=rev Author: sknappe Date: 2008-02-18 09:28:12 -0800 (Mon, 18 Feb 2008) Log Message: ----------- bugfix Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java Modified: trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java 2008-02-18 15:54:00 UTC (rev 604) +++ trunk/src/dl-learner/org/dllearner/kb/sparql/Cache.java 2008-02-18 17:28:12 UTC (rev 605) @@ -217,8 +217,7 @@ if (result != null) { return SparqlQuery.JSONtoResultSet(result); } else { - query.send(); - ResultSet rs = query.getResultSet(); + ResultSet rs = query.send(); if (rs!=null){ String json = SparqlQuery.getAsJSON(rs); addToCache(query.getQueryString(), json); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |