s243a

ArchiveContent.java (freenet.client)

Oct 22nd, 2014
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. //s243a pearltree node: http://www.pearltrees.com/s243a/archivecontent-java/id12827343
  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 java.util.HashSet;
  8.  
  9. import com.db4o.ObjectContainer;
  10.  
  11. import freenet.keys.FreenetURI;
  12.  
  13. /**
  14.  * @author amphibian (Matthew Toseland)
  15.  *
  16.  * Object passed down a full fetch, including all the recursion.
  17.  * Used, at present, for detecting archive fetch loops, hence the
  18.  * name.
  19.  */
  20. // WARNING: THIS CLASS IS STORED IN DB4O -- THINK TWICE BEFORE ADD/REMOVE/RENAME FIELDS
  21. public class ArchiveContext {
  22.  
  23.     private HashSet<FreenetURI> soFar;
  24.     final int maxArchiveLevels;
  25.     final long maxArchiveSize;
  26.    
  27.     public ArchiveContext(long maxArchiveSize, int max) {
  28.         this.maxArchiveLevels = max;
  29.         this.maxArchiveSize = maxArchiveSize;
  30.     }
  31.    
  32.     /**
  33.      * Check for a loop.
  34.      *
  35.      * The URI provided is expected to be a reasonably unique identifier for the archive.
  36.      */
  37.     public synchronized void doLoopDetection(FreenetURI key, ObjectContainer container) throws ArchiveFailureException {
  38.         if(container != null)
  39.             container.activate(soFar, Integer.MAX_VALUE);
  40.         if(soFar == null) {
  41.             soFar = new HashSet<FreenetURI>();
  42.             if(container != null)
  43.                 container.store(soFar);
  44.         }
  45.         if(soFar.size() > maxArchiveLevels)
  46.             throw new ArchiveFailureException(ArchiveFailureException.TOO_MANY_LEVELS);
  47.         FreenetURI uri = key;
  48.         if(container != null)
  49.             uri = uri.clone();
  50.         if(!soFar.add(uri)) {
  51.             throw new ArchiveFailureException(ArchiveFailureException.ARCHIVE_LOOP_DETECTED);
  52.         }
  53.         if(container != null) {
  54.             container.store(uri);
  55.             container.store(soFar);
  56.         }
  57.     }
  58.  
  59.     public void removeFrom(ObjectContainer container) {
  60.         if(soFar != null) {
  61.             for(FreenetURI uri : soFar) {
  62.                 uri.removeFrom(container);
  63.             }
  64.             container.delete(soFar);
  65.         }
  66.         container.delete(this);
  67.     }
  68. }
Add Comment
Please, Sign In to add comment