📄 cm.java
字号:
* @param dn contains the name of the folder * @param fn contains the name of the file * @return number of milliseconds until the file expires or -1 if the * file is not recognized or already expired. */ public synchronized long getExpirationtime(String dn, String fn) { try { Key key = new Key(dn + "/" + fn); Record record = cacheDB.readRecord(key); long expiration = calcExpiration(record); if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("Expiration for :" + fn + " " + expiration); } if (expiration < 0) { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("Removing expired record :" + fn); } try { remove(dn, fn); } catch (IOException e) { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.log(Level.FINE, "Failed to remove record", e); } } } return expiration; } catch (DBException de) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "failed to get " + dn + "/" + fn, de); } return -1; } } /** * Figures out expiration * * @param record record * @return expiration in ms */ private static long calcExpiration(Record record) { if (record == null) { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("Record is null returning expiration of -1"); } return -1; } Long exp = (Long) record.getMetaData(Record.EXPIRATION); Long life = (Long) record.getMetaData(Record.LIFETIME); long expiresin = life - System.currentTimeMillis(); if (expiresin <= 0) { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine( MessageFormat.format("Record expired lifetime : {0} expiration: {1} expires in: {2}", life, exp , expiresin)); LOG.fine(MessageFormat.format("Record expires on :{0}", new Date(life))); } return -1; } else { if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine(MessageFormat.format("Record lifetime: {0} expiration: {1} expires in: {2}", life, exp, expiresin)); LOG.fine(MessageFormat.format("Record expires on :{0}", new Date(life))); } return Math.min(expiresin, exp.longValue()); } } /** * Returns the inputStream of a specified file, in a specified dir * * @param dn directory name * @param fn file name * @return The inputStream value * @throws IOException if an I/O error occurs */ public InputStream getInputStream(String dn, String fn) throws IOException { Key key = new Key(dn + "/" + fn); try { Record record = cacheDB.readRecord(key); if (record == null) { return null; } if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("Restored record for " + key); } Value val = record.getValue(); if (val != null) { return val.getInputStream(); } else { return null; } } catch (DBException de) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "Failed to restore record for " + key, de); } IOException failure = new IOException("Failed to restore record for " + key); failure.initCause(de); throw failure; } } /** * Remove a file * * @param dn directory name * @param fn file name * @throws IOException if an I/O error occurs */ public synchronized void remove(String dn, String fn) throws IOException { try { if (fn == null) { return; } Key key = new Key(dn + "/" + fn); Record record = cacheDB.readRecord(key); long removePos = cacheDB.findValue(key); cacheDB.deleteRecord(key); if (record != null) { try { if (calcExpiration(record) > 0) { InputStream is = record.getValue().getInputStream(); XMLDocument asDoc = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, is); Advertisement adv = AdvertisementFactory.newAdvertisement(asDoc); Map<String, String> indexables = getIndexfields(adv.getIndexFields(), asDoc); indexer.removeFromIndex(addKey(dn, indexables), removePos); // add it to deltas to expire it in srdi addDelta(dn, indexables, 0); if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("removed " + record); } } } catch (Exception e) { // bad bits we are done if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "failed to remove " + dn + "/" + fn, e); } } } } catch (DBException de) { // entry does not exist if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("failed to remove " + dn + "/" + fn); } } } /** * Restore a saved StructuredDocument. * * @param dn directory name * @param fn file name * @return StructuredDocument containing the file * @throws IOException if an I/O error occurs * was not possible. */ public StructuredDocument restore(String dn, String fn) throws IOException { InputStream is = getInputStream(dn, fn); return StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, is); } /** * Restore an advetisement into a byte array. * * @param dn directory name * @param fn file name * @return byte [] containing the file * @throws IOException if an I/O error occurs */ public synchronized byte[] restoreBytes(String dn, String fn) throws IOException { try { Key key = new Key(dn + "/" + fn); Record record = cacheDB.readRecord(key); if (record == null) { return null; } if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("restored " + record); } Value val = record.getValue(); if (val != null) { return val.getData(); } else { return null; } } catch (DBException de) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "failed to restore " + dn + "/" + fn, de); } IOException failure = new IOException("failed to restore " + dn + "/" + fn); failure.initCause(de); throw failure; } } /** * Stores a StructuredDocument in specified dir, and file name * * @param dn directory name * @param fn file name * @param adv Advertisement to store * @throws IOException if an I/O error occurs */ public void save(String dn, String fn, Advertisement adv) throws IOException { save(dn, fn, adv, DiscoveryService.INFINITE_LIFETIME, DiscoveryService.NO_EXPIRATION); } /** * Stores a StructuredDocument in specified dir, and file name, and * associated doc timeouts * * @param dn directory name * @param fn file name * @param adv Advertisement to save * @param lifetime Document (local) lifetime in relative ms * @param expiration Document (global) expiration time in relative ms * @throws IOException Thrown if there is a problem saving the document. */ public synchronized void save(String dn, String fn, Advertisement adv, long lifetime, long expiration) throws IOException { try { if (expiration < 0 || lifetime <= 0) { throw new IllegalArgumentException("Bad expiration or lifetime."); } XMLDocument doc; try { doc = (XMLDocument) adv.getDocument(MimeMediaType.XMLUTF8); } catch (RuntimeException e) { IOException failure = new IOException("Advertisement couldn't be saved"); failure.initCause(e); throw failure; } Key key = new Key(dn + "/" + fn); // save the new version ByteArrayOutputStream baos = new ByteArrayOutputStream(); doc.sendToStream(baos); baos.close(); Value value = new Value(baos.toByteArray()); Long oldLife = null; Record record = cacheDB.readRecord(key); if (record != null) { // grab the old lifetime oldLife = (Long) record.getMetaData(Record.LIFETIME); } long absoluteLifetime = TimeUtils.toAbsoluteTimeMillis(lifetime); if (oldLife != null) { if (absoluteLifetime < oldLife) { // make sure we don't override the original value if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine(MessageFormat.format("Overriding attempt to decrease adv lifetime from : {0} to :{1}", new Date(oldLife), new Date(absoluteLifetime))); } absoluteLifetime = oldLife; } } // make sure expiration does not exceed lifetime if (expiration > lifetime) { expiration = lifetime; } long pos = cacheDB.writeRecord(key, value, absoluteLifetime, expiration); Map<String, String> indexables = getIndexfields(adv.getIndexFields(), doc); Map<String, String> keyedIdx = addKey(dn, indexables); if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine("Indexing " + keyedIdx + " at " + pos); } indexer.addToIndex(keyedIdx, pos); if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { // too noisy // LOG.debug("Wrote " + key + " = " + value); LOG.fine("Stored " + indexables + " at " + pos); } if (expiration > 0) { // Update for SRDI with our caches lifetime only if we are prepared to share the advertisement with others. addDelta(dn, indexables, TimeUtils.toRelativeTimeMillis(absoluteLifetime)); } } catch (DBException de) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, MessageFormat.format("Failed to write {0}/{1} {2} {3}", dn, fn, lifetime, expiration), de); } IOException failure = new IOException("Failed to write " + dn + "/" + fn + " " + lifetime + " " + expiration); failure.initCause(de); throw failure; } } /** * Store some bytes in specified dir, and file name, and * associated doc timeouts * * @param dn directory name * @param fn file name * @param data byte array to save * @param lifetime Document (local) lifetime in relative ms * @param expiration Document (global) expiration time in relative ms * @throws IOException Thrown if there is a problem saving the document. */ public synchronized void save(String dn, String fn, byte[] data, long lifetime, long expiration) throws IOException { try { if (expiration < 0 || lifetime <= 0) { throw new IllegalArgumentException("Bad expiration or lifetime."); } Key key = new Key(dn + "/" + fn); Value value = new Value(data); Long oldLife = null; Record record = cacheDB.readRecord(key); if (record != null) { // grab the old lifetime oldLife = (Long) record.getMetaData(Record.LIFETIME); } // save the new version long absoluteLifetime = TimeUtils.toAbsoluteTimeMillis(lifetime); if (oldLife != null) { if (absoluteLifetime < oldLife) { // make sure we don't override the original value if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) { LOG.fine(MessageFormat.format("Overriding attempt to decrease adv lifetime from : {0} to :{1}", new Date(oldLife), new Date(absoluteLifetime))); } absoluteLifetime = oldLife; } } // make sure expiration does not exceed lifetime if (expiration > lifetime) { expiration = lifetime; } cacheDB.writeRecord(key, value, absoluteLifetime, expiration); } catch (DBException de) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "Failed to write " + dn + "/" + fn + " " + lifetime + " " + expiration, de); } IOException failure = new IOException("Failed to write " + dn + "/" + fn + " " + lifetime + " " + expiration); failure.initCause(de); throw failure; } } private static Map<String, String> getIndexfields(String[] fields, StructuredDocument doc) { Map<String, String> map = new HashMap<String, String>(); if (doc == null) { if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) { LOG.warning("Null document"); } return map; } if (fields == null) { return map; } for (String field : fields) { Enumeration en = doc.getChildren(field); while (en.hasMoreElements()) { String val = (String) ((Element) en.nextElement()).getValue(); if (val != null) { map.put(field, val); } } } return map; } /* adds a primary index 'dn' to indexables */ private static Map<String, String> addKey(String dn, Map<String, String> map) { if (map == null) { return null; } Map<String, String> tmp = new HashMap<String, String>(); if (map.size() > 0) { Iterator<String> it = map.keySet().iterator(); while (it != null && it.hasNext()) { String name = it.next(); tmp.put(dn + name, map.get(name)); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -