s243a

ArchiveStoreContext.java (freenet.client)

Oct 22nd, 2014
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. //s243a pearltree node: http://www.pearltrees.com/s243a/archivestorecontext-freenet/id12827728
  2. /* This code is part of Freenet. It is distributed under the GNU General
  3.  * Public License, version 2 (or at your option any later version). See
  4.  * http://www.gnu.org/ for further details of the GPL. */
  5. package freenet.client;
  6.  
  7. import freenet.keys.FreenetURI;
  8. import freenet.support.DoublyLinkedListImpl;
  9. import freenet.support.LogThresholdCallback;
  10. import freenet.support.Logger;
  11. import freenet.support.Logger.LogLevel;
  12.  
  13. /**
  14.  * Tracks all files currently in the cache from a given key.
  15.  * Keeps the last known hash of the key (if this changes in a fetch, we flush the cache, unpack,
  16.  * then throw an ArchiveRestartedException).
  17.  * Provides fetch methods for Fetcher, which try the cache and then fetch if necessary,
  18.  * subject to the above.
  19.  *
  20.  * Always take the lock on ArchiveStoreContext before the lock on ArchiveManager, NOT the other way around.
  21.  */
  22. class ArchiveStoreContext {
  23.  
  24.     private FreenetURI key;
  25.     private final ArchiveManager.ARCHIVE_TYPE archiveType;
  26.     /** Archive size */
  27.     private long lastSize = -1;
  28.     /** Archive hash */
  29.     private byte[] lastHash;
  30.     /** Index of still-cached ArchiveStoreItems with this key.
  31.      * Note that we never ever hold this and then take another lock! In particular
  32.      * we must not take the ArchiveManager lock while holding this lock. It must be
  33.      * the inner lock to avoid deadlocks. */
  34.     private final DoublyLinkedListImpl<ArchiveStoreItem> myItems;
  35.  
  36.         private static volatile boolean logMINOR;
  37.     static {
  38.         Logger.registerLogThresholdCallback(new LogThresholdCallback(){
  39.             @Override
  40.             public void shouldUpdate(){
  41.                 logMINOR = Logger.shouldLog(LogLevel.MINOR, this);
  42.             }
  43.         });
  44.     }
  45.    
  46.     ArchiveStoreContext(FreenetURI key, ArchiveManager.ARCHIVE_TYPE archiveType) {
  47.         this.key = key;
  48.         this.archiveType = archiveType;
  49.         myItems = new DoublyLinkedListImpl<ArchiveStoreItem>();
  50.     }
  51.  
  52.     /** Returns the size of the archive last time we fetched it, or -1 */
  53.     long getLastSize() {
  54.         return lastSize;
  55.     }
  56.    
  57.     /** Sets the size of the archive - @see getLastSize() */
  58.     void setLastSize(long size) {
  59.         lastSize = size;
  60.     }
  61.  
  62.    
  63.     /** Returns the hash of the archive last time we fetched it, or null */
  64.     byte[] getLastHash() {
  65.         return lastHash;
  66.     }
  67.  
  68.     /** Sets the hash of the archive - @see getLastHash() */
  69.     void setLastHash(byte[] realHash) {
  70.         lastHash = realHash;
  71.     }
  72.  
  73.     /**
  74.      * Remove all ArchiveStoreItems with this key from the cache.
  75.      */
  76.     void removeAllCachedItems(ArchiveManager manager) {
  77.         ArchiveStoreItem item = null;
  78.         while(true) {
  79.             synchronized (myItems) {
  80.                 // removeCachedItem() will call removeItem(), so don't remove it here.
  81.                 item = myItems.head();
  82.             }
  83.             if(item == null) break;
  84.             manager.removeCachedItem(item);
  85.         }
  86.     }
  87.  
  88.     /** Notify that a new archive store item with this key has been added to the cache. */
  89.     void addItem(ArchiveStoreItem item) {
  90.         synchronized(myItems) {
  91.             myItems.push(item);
  92.         }
  93.     }
  94.  
  95.     /** Notify that an archive store item with this key has been expelled from the
  96.      * cache. Remove it from our local cache and ask it to free the bucket if
  97.      * necessary. */
  98.     void removeItem(ArchiveStoreItem item) {
  99.         synchronized(myItems) {
  100.             if(myItems.remove(item) == null) {
  101.                 if(logMINOR)
  102.                     Logger.minor(this, "Not removing: "+item+" for "+this+" - already removed");
  103.                 return; // only removed once
  104.             }
  105.         }
  106.         item.innerClose();
  107.     }
  108.  
  109.     public short getArchiveType() {
  110.         return archiveType.metadataID;
  111.     }
  112.    
  113.     public FreenetURI getKey() {
  114.         return key;
  115.     }
  116.  
  117. }
Add Comment
Please, Sign In to add comment