📄 srdicache.java
字号:
* @param attribute Attribute String to query on * @param value value of the attribute string * @return an enumeration of canonical paths */ public synchronized Enumeration query(String primaryKey, String attribute, String value) { boolean endswith = false; boolean startswith = false; boolean allvalues = false; if (primaryKey == null || primaryKey.length() == 0) { throw new IllegalArgumentException("primaryKey is mandatory"); } if (value == null || value.length() == 0 || attribute == null || attribute.length() == 0) { allvalues = true; } else { value = value.toUpperCase(); if (value.charAt(0) == '*') { endswith = true; value = value.substring(1, value.length()); } if (value.length() == 0) { allvalues = true; } else if (value.charAt(value.length() - 1) == '*') { startswith = true; value = value.substring(0, value.indexOf("*")); } } Hashtable ptbl = (Hashtable) caches.get(primaryKey); if (ptbl == null) { return new Vector().elements(); } else if (attribute == null){ Vector result = new Vector(); Enumeration each = ptbl.elements(); if (each.hasMoreElements()) { Hashtable tb = (Hashtable)each.nextElement(); Enumeration vecs = tb.elements(); if (vecs.hasMoreElements()) { Vector tmp = (Vector)vecs.nextElement(); addTo(result,getPaths(tmp)); } } return result.elements(); } Hashtable tbl = (Hashtable) ptbl.get(attribute); if (tbl == null) return new Vector().elements(); if (allvalues) { Enumeration values = tbl.elements(); Vector result = new Vector(); Vector tmp; while (values.hasMoreElements()) { tmp = (Vector) values.nextElement(); addTo(result,getPaths(tmp)); } return result.elements(); } else if (!endswith && !startswith) { Vector res = (Vector) tbl.get(value); if (res != null) { if (LOG.isDebugEnabled()) { LOG.debug(attribute + " Found " + res.size()); } return getPaths(res); } // other cases } else { Vector result = new Vector(); Enumeration keys = tbl.keys(); while (keys.hasMoreElements()) { String val = (String) keys.nextElement(); if (startswith && !endswith) { if (val.startsWith(value)) { addTo(result, ((Vector) tbl.get(val)).elements()); } } else if (endswith && !startswith) { if (val.endsWith(value)) { addTo(result, ((Vector) tbl.get(val)).elements()); } } else if (startswith && endswith) { if (val.indexOf(value) >= 0) { addTo(result,((Vector) tbl.get(val)).elements()); } } } return getPaths(result); } // empty vector return new Vector().elements(); } /* returns vector of peerid strings * */ private Enumeration getPaths (Vector entries) { Vector result = new Vector(); for (int i=0; i<entries.size(); i++ ) { SrdiEntry entry = (SrdiEntry) entries.elementAt(i); if(! isExpired(entry) ) { result.addElement(entry.path); } } return result.elements(); } /** * Adds content of one vector to another * * @param to vector where elements to be copied into * @param from vector where elements to be copied from */ private void addTo(Vector to, Enumeration from) { while (from.hasMoreElements()) { Object obj = from.nextElement(); if(!to.contains(obj)) { to.add(obj); } } } /** * Purges some of the cache. * The entries are cleaned-up properly. * * @param fraction Description of the Parameter */ public void purge(int fraction) { if (size == 0) { return; } if (fraction == 0) { fraction = 1; } long nbToPurge = size / fraction; if (nbToPurge == 0) { nbToPurge = 1; } while (nbToPurge-- > 0) { SrdiEntry entry = (SrdiEntry) lru.next(); if (LOG.isEnabledFor(Level.DEBUG)) LOG.debug("SrdiCache Purging : "+entry.getPath()); remove(entry.getPath()); entry.unlink(); size--; } } /** * Empties the cache completely. * The entries are abandonned to the GC. */ public void clear() { lru.clear(); caches.clear(); } public void garbageCollect() { //primary key table Enumeration primaryKeyTbl = caches.elements(); if (LOG.isEnabledFor(Level.DEBUG)) LOG.debug("SrdiCache garbage collect"); while (primaryKeyTbl.hasMoreElements()) { // secondary key table Hashtable attributes = (Hashtable) primaryKeyTbl.nextElement(); Iterator secondaryKeyTbl = attributes.values().iterator(); while (secondaryKeyTbl.hasNext()) { Hashtable secondaryEntries = (Hashtable) secondaryKeyTbl.next(); // remove all expired entries Iterator values = secondaryEntries.values().iterator(); while (values.hasNext()) { Vector entries = ((Vector) values.next()); for (int i=0; i<entries.size(); i++) { if (isExpired((SrdiEntry)entries.elementAt(i))) { entries.removeElementAt(i); } } } } } } public void removeKey(String primaryKey, String secondaryKey) { Hashtable caTbl = (Hashtable) caches.get(primaryKey); if (LOG.isEnabledFor(Level.DEBUG)) LOG.debug("SrdiCache removing entries of pkey ["+primaryKey+"] skey["+secondaryKey +"]" ); caTbl.remove(secondaryKey); } private boolean isExpired(SrdiEntry entry) { return (entry.expiration < System.currentTimeMillis()); } /** * Description of the Class */ class SrdiEntry extends Dlink { private Object path; private long expiration; // The application interface. /** * Constructor for the SrdiEntry object * relative time is converted to absolute time * * @param path path * @param expiration expiration associated with this entry * relative time in milliseconds */ public SrdiEntry(Object path, long expiration) { this.path = path; this.expiration = TimeUtils.toAbsoluteTimeMillis( expiration ); } /** * Gets the path attribute of the SrdiEntry object * * @return The path value */ public Object getPath() { return path; } /** * Gets the expiration attribute of the SrdiEntry object * * @return The expiration value */ public long getExpiration() { return expiration; } } /** * stop the current running thread */ public synchronized void stop() { stop = true; // wakeup and die notify(); } public synchronized void run() { while (!stop) { try { if (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug("waiting for " + interval + " before garbage collection"); } wait(interval); if (stop) { //if asked to stop, return return; } } catch (InterruptedException e) { } if (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug("Garbage collection started"); } garbageCollect(); if (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug("Garbage collection completed"); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -