s243a

FECJob.java (freenet.client)

Oct 22nd, 2014
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.04 KB | None | 0 0
  1. // s243a pearltree node: http://www.pearltrees.com/s243a/freenet-client/id12827285
  2. /**
  3.  *
  4.  */
  5. package freenet.client;
  6.  
  7. import com.db4o.ObjectContainer;
  8.  
  9. import freenet.client.async.ClientContext;
  10. import freenet.keys.CHKBlock;
  11. import freenet.support.Logger;
  12. import freenet.support.api.Bucket;
  13. import freenet.support.api.BucketFactory;
  14.  
  15. /**
  16.  * A class bundling the data meant to be FEC processed
  17.  *
  18.  * @author Florent Daignière <[email protected]>
  19.  */
  20. // WARNING: THIS CLASS IS STORED IN DB4O -- THINK TWICE BEFORE ADD/REMOVE/RENAME FIELDS
  21. public class FECJob {
  22.    
  23.     private transient static volatile boolean logMINOR;
  24.    
  25.     static {
  26.         Logger.registerClass(FECJob.class);
  27.     }
  28.    
  29.     private transient FECCodec codec;
  30.     private final short fecAlgo;
  31.     final Bucket[] dataBlocks, checkBlocks;
  32.     final SplitfileBlock[] dataBlockStatus, checkBlockStatus;
  33.     final BucketFactory bucketFactory;
  34.     final int blockLength;
  35.     final FECCallback callback;
  36.     final boolean isADecodingJob;
  37.     final long addedTime;
  38.     final short priority;
  39.     final boolean persistent;
  40.     /** Parent queue */
  41.     final FECQueue queue;
  42.     // A persistent hash code helps with debugging.
  43.     private final int hashCode;
  44.     transient boolean running;
  45.    
  46.     @Override
  47.     public int hashCode() {
  48.         return hashCode;
  49.     }
  50.    
  51.     public FECJob(FECCodec codec, FECQueue queue, SplitfileBlock[] dataBlockStatus, SplitfileBlock[] checkBlockStatus,  int blockLength, BucketFactory bucketFactory, FECCallback callback, boolean isADecodingJob, short priority, boolean persistent) {
  52.         this.codec = codec;
  53.         this.fecAlgo = codec.getAlgorithm();
  54.         this.queue = queue;
  55.         this.priority = priority;
  56.         this.addedTime = System.currentTimeMillis();
  57.        
  58.         this.dataBlockStatus = new SplitfileBlock[dataBlockStatus.length];
  59.         this.checkBlockStatus = new SplitfileBlock[checkBlockStatus.length];
  60.         for(int i=0;i<dataBlockStatus.length;i++)
  61.             this.dataBlockStatus[i] = dataBlockStatus[i];
  62.         for(int i=0;i<checkBlockStatus.length;i++)
  63.             this.checkBlockStatus[i] = checkBlockStatus[i];
  64.        
  65. //      this.dataBlockStatus = dataBlockStatus;
  66. //      this.checkBlockStatus = checkBlockStatus;
  67.        
  68.         this.dataBlocks = new Bucket[dataBlockStatus.length];
  69.         this.checkBlocks = new Bucket[checkBlockStatus.length];
  70.         for(int i=0;i<dataBlocks.length;i++)
  71.             this.dataBlocks[i] = dataBlockStatus[i].getData();
  72.         for(int i=0;i<checkBlocks.length;i++)
  73.             this.checkBlocks[i] = checkBlockStatus[i].getData();
  74.        
  75.         this.blockLength = blockLength;
  76.         this.bucketFactory = bucketFactory;
  77.         if(bucketFactory == null)
  78.             throw new NullPointerException();
  79.         this.callback = callback;
  80.         this.isADecodingJob = isADecodingJob;
  81.         this.persistent = persistent;
  82.         this.hashCode = super.hashCode();
  83.     }
  84.    
  85.     @Override
  86.     public String toString() {
  87.         return super.toString()+":decode="+isADecodingJob+":callback="+callback+":persistent="+persistent;
  88.     }
  89.    
  90.     public FECJob(FECCodec codec, FECQueue queue, Bucket[] dataBlocks, Bucket[] checkBlocks, int blockLength, BucketFactory bucketFactory, FECCallback callback, boolean isADecodingJob, short priority, boolean persistent) {
  91.         this.hashCode = super.hashCode();
  92.         this.codec = codec;
  93.         this.fecAlgo = codec.getAlgorithm();
  94.         this.queue = queue;
  95.         this.priority = priority;
  96.         this.addedTime = System.currentTimeMillis();
  97.        
  98.         // Make sure it is a separate array, just in case it doesn't get copied transparently by db4o.
  99.         this.dataBlocks = new Bucket[dataBlocks.length];
  100.         this.checkBlocks = new Bucket[checkBlocks.length];
  101.         for(int i=0;i<dataBlocks.length;i++) {
  102.             this.dataBlocks[i] = dataBlocks[i];
  103.             if(!isADecodingJob) {
  104.                 if(dataBlocks[i] == null)
  105.                     throw new NullPointerException("Data block "+i+" is null for encode in FECJob constructor!");
  106.             }
  107.         }
  108.         for(int i=0;i<checkBlocks.length;i++)
  109.             this.checkBlocks[i] = checkBlocks[i];
  110.        
  111.         this.dataBlockStatus = null;
  112.         this.checkBlockStatus = null;
  113.         this.blockLength = blockLength;
  114.         this.bucketFactory = bucketFactory;
  115.         if(bucketFactory == null)
  116.             throw new NullPointerException();
  117.         this.callback = callback;
  118.         this.isADecodingJob = isADecodingJob;
  119.         this.persistent = persistent;
  120.     }
  121.  
  122.     public FECCodec getCodec() {
  123.         if(codec == null) {
  124.             codec = FECCodec.getCodec(fecAlgo, dataBlocks.length, checkBlocks.length);
  125.             if(codec == null)
  126.                 Logger.error(this, "No codec found for algo "+fecAlgo+" data blocks length "+dataBlocks.length+" check blocks length "+checkBlocks.length);
  127.         }
  128.         return codec;
  129.     }
  130.    
  131.     public boolean activateForExecution(ObjectContainer container) {
  132.         if(logMINOR) {
  133.             Logger.minor(this, "Activating FECJob... "+this);
  134.             if(dataBlockStatus != null) {
  135.                 for(int i=0;i<dataBlockStatus.length;i++)
  136.                     Logger.minor(this, "Data block status "+i+": "+dataBlockStatus[i]+" (before activation)");
  137.             }
  138.         }
  139.         container.activate(this, 2);
  140.         boolean hasDataBlocks = false;
  141.         int countDataBlocks = 0;
  142.         int countNullDataBlocks = 0;
  143.         if(dataBlockStatus != null) {
  144.             hasDataBlocks = true;
  145.             countDataBlocks = dataBlockStatus.length;
  146.             for(int i=0;i<dataBlockStatus.length;i++) {
  147.                 container.activate(dataBlockStatus[i], 2);
  148.                 if(dataBlockStatus[i] == null)
  149.                     countNullDataBlocks++;
  150.             }
  151.        
  152.             if(logMINOR) {
  153.                 for(int i=0;i<dataBlockStatus.length;i++)
  154.                     Logger.minor(this, "Data block status "+i+": "+dataBlockStatus[i]+" (after activation)");
  155.             }
  156.         }
  157.         if(checkBlockStatus != null) {
  158.             for(int i=0;i<checkBlockStatus.length;i++)
  159.                 container.activate(checkBlockStatus[i], 2);
  160.         }
  161.         if(dataBlocks != null) {
  162.             hasDataBlocks = true;
  163.             countDataBlocks = dataBlocks.length;
  164.             for(int i=0;i<dataBlocks.length;i++) {
  165.                 container.activate(dataBlocks[i], 1);
  166.                 if(logMINOR)
  167.                     Logger.minor(this, "Data bucket "+i+": "+dataBlocks[i]+" (after activation)");
  168.                 if(dataBlocks[i] == null)
  169.                     countNullDataBlocks++;
  170.             }
  171.         }
  172.         if(checkBlocks != null) {
  173.             for(int i=0;i<checkBlocks.length;i++) {
  174.                 container.activate(checkBlocks[i], 1);
  175.                 if(logMINOR)
  176.                     Logger.minor(this, "Check bucket "+i+": "+checkBlocks[i]+" (after activation)");
  177.             }
  178.         }
  179.         if(!isADecodingJob) {
  180.             // First find the target
  181.             if(!hasDataBlocks) {
  182.                 Logger.error(this, "Invalid job: Encoding: No data blocks or data block status");
  183.                 return false;
  184.             }
  185.             if(hasDataBlocks && countDataBlocks == 0) {
  186.                 Logger.error(this, "Invalid job: Encoding: "+countDataBlocks+" blocks");
  187.                 return false;
  188.             }
  189.             if(hasDataBlocks && countNullDataBlocks > 0) {
  190.                 Logger.error(this, "Invalid job: Encoding: "+countDataBlocks+" blocks but "+countNullDataBlocks+" are null!");
  191.                 return false;
  192.             }
  193.         }
  194.         return true;
  195.     }
  196.  
  197.     public void storeBlockStatuses(ObjectContainer container, boolean failure) {
  198.         if(logMINOR) Logger.minor(this, "Storing block statuses");
  199.         int countNullsData = -1;
  200.         int countNullsCheck = -1;
  201.         int dataLength = -1;
  202.         int checkLength = -1;
  203.         if(dataBlockStatus != null) {
  204.             dataLength = dataBlockStatus.length;
  205.             countNullsData = 0;
  206.             for(int i=0;i<dataBlockStatus.length;i++) {
  207.                 SplitfileBlock block = dataBlockStatus[i];
  208.                 if(logMINOR) Logger.minor(this, "Storing data block "+i+": "+block);
  209.                 if(block != null)
  210.                     block.storeTo(container);
  211.                 else
  212.                     countNullsData++;
  213.             }
  214.            
  215.         }
  216.         if(checkBlockStatus != null) {
  217.             checkLength = checkBlockStatus.length;
  218.             countNullsCheck = 0;
  219.             for(int i=0;i<checkBlockStatus.length;i++) {
  220.                 SplitfileBlock block = checkBlockStatus[i];
  221.                 if(logMINOR) Logger.minor(this, "Storing check block "+i+": "+block);
  222.                 if(block != null)
  223.                     block.storeTo(container);
  224.                 else
  225.                     countNullsCheck++;
  226.             }
  227.         }
  228.         // FIXME simplify debugging logging code here???
  229.         // We cannot however just assume they are non-null, this isn't always true.
  230.         if(failure && (countNullsData > 0 || countNullsCheck > 0))
  231.             Logger.normal(this, "After failed, storing block statuses, "+countNullsData+"/"+dataBlockStatus.length+" nulls in data blocks "+countNullsCheck+"/"+checkBlockStatus.length+" nulls in check blocks");
  232.         else if(failure) {
  233.             if(logMINOR) Logger.minor(this, "After failed, storing block statuses, "+countNullsData+"/"+dataBlockStatus.length+" nulls in data blocks "+countNullsCheck+"/"+checkBlockStatus.length+" nulls in check blocks");
  234.         } else {
  235.             // After a normal, successful job.
  236.             if(isADecodingJob) {
  237.                 if(countNullsData != 0)
  238.                     Logger.normal(this, "After successful decode, storing block statuses, "+countNullsData+"/"+dataLength+" nulls in data blocks "+countNullsCheck+"/"+checkLength+" nulls in check blocks");
  239.                 else {
  240.                     if(logMINOR)
  241.                         Logger.minor(this, "After successful decode, storing block statuses, "+countNullsData+"/"+dataLength+" nulls in data blocks "+countNullsCheck+"/"+checkLength+" nulls in check blocks");
  242.                 }
  243.             } else {
  244.                 if(countNullsCheck != 0 || countNullsData != 0)
  245.                     Logger.normal(this, "After successful decode, storing block statuses, "+countNullsData+"/"+dataLength+" nulls in data blocks "+countNullsCheck+"/"+checkLength+" nulls in check blocks");
  246.                 else {
  247.                     if(logMINOR)
  248.                         Logger.minor(this, "After successful decode, storing block statuses, "+countNullsData+"/"+dataLength+" nulls in data blocks "+countNullsCheck+"/"+checkLength+" nulls in check blocks");
  249.                 }
  250.             }
  251.         }
  252.     }
  253.  
  254.     public boolean isCancelled(ObjectContainer container) {
  255.         if(callback == null) {
  256.             for(Bucket data : dataBlocks) {
  257.                 if(data != null) {
  258.                     Logger.error(this, "Callback is null (deleted??) but data is valid: "+data);
  259.                     data.free();
  260.                     data.removeFrom(container);
  261.                 }
  262.             }
  263.             for(Bucket data : checkBlocks) {
  264.                 if(data != null) {
  265.                     Logger.error(this, "Callback is null (deleted??) but data is valid: "+data);
  266.                     data.free();
  267.                     data.removeFrom(container);
  268.                 }
  269.             }
  270.             for(SplitfileBlock block : dataBlockStatus) {
  271.                 if(block != null) {
  272.                     Logger.error(this, "Callback is null (deleted??) but data is valid: "+block);
  273.                     Bucket data = block.getData();
  274.                     if(data != null) {
  275.                         Logger.error(this, "Callback is null (deleted??) but data is valid: "+data);
  276.                         data.free();
  277.                         data.removeFrom(container);
  278.                     }
  279.                     container.delete(block);
  280.                 }
  281.             }
  282.             for(SplitfileBlock block : checkBlockStatus) {
  283.                 if(block != null) {
  284.                     Logger.error(this, "Callback is null (deleted??) but data is valid: "+block);
  285.                     Bucket data = block.getData();
  286.                     if(data != null) {
  287.                         Logger.error(this, "Callback is null (deleted??) but data is valid: "+data);
  288.                         data.free();
  289.                         data.removeFrom(container);
  290.                     }
  291.                     container.delete(block);
  292.                 }
  293.             }
  294.             return true;
  295.         }
  296.         return false;
  297.     }
  298.  
  299.     /**
  300.      * @param container
  301.      * @param context
  302.      * @return True unless we were unable to remove the job because it has already started.
  303.      */
  304.     public boolean cancel(ObjectContainer container, ClientContext context) {
  305.         return queue.cancel(this, container, context);
  306.     }
  307.  
  308.     /** Should already be activated to depth 1 by caller. */
  309.     public void dump(ObjectContainer container) {
  310.         System.err.println("FEC job: "+toString());
  311.         System.err.println("Algorithm: "+fecAlgo);
  312.         System.err.println("Bucket factory: "+bucketFactory);
  313.         System.err.println("Block length: "+blockLength);
  314.         System.err.println("Callback: "+callback);
  315.         System.err.println("Type: "+(isADecodingJob ? "DECODE" : "ENCODE"));
  316.         System.err.println("Added time: "+addedTime);
  317.         System.err.println("Priority: "+priority);
  318.         System.err.println("Persistent: "+persistent);
  319.         System.err.println("Queue: "+queue);
  320.         System.err.println("Hash code: "+hashCode);
  321.         System.err.println("Running: "+running);
  322.         if(dataBlocks != null) {
  323.             System.err.println("Has data blocks");
  324.             int dataCount = 0;
  325.             for(int i=0;i<dataBlocks.length;i++) {
  326.                 Bucket data = dataBlocks[i];
  327.                 if(data == null) {
  328.                     System.err.println("Data block "+i+" is null!");
  329.                 } else {
  330.                     container.activate(data, 5);
  331.                     if(data.size() != CHKBlock.DATA_LENGTH) {
  332.                         System.err.println("Size of data block "+i+" is "+data.size()+" should be "+CHKBlock.DATA_LENGTH);
  333.                     } else {
  334.                         dataCount++;
  335.                     }
  336.                     System.err.println(data.toString()+" : "+data.size());
  337.                     container.deactivate(data, 5);
  338.                 }
  339.             }
  340.             if(dataCount == dataBlocks.length)
  341.                 System.out.println("Has all data blocks");
  342.             else
  343.                 System.out.println("Does not have all data blocks: "+dataCount+" of "+dataBlocks.length);
  344.         }
  345.         if(checkBlocks != null) {
  346.             System.err.println("Has check blocks");
  347.             int dataCount = 0;
  348.             for(int i=0;i<checkBlocks.length;i++) {
  349.                 Bucket data = checkBlocks[i];
  350.                 if(data == null) {
  351.                     System.err.println("Check block "+i+" is null!");
  352.                 } else {
  353.                     container.activate(data, 5);
  354.                     if(data.size() != CHKBlock.DATA_LENGTH) {
  355.                         System.err.println("Size of check block "+i+" is "+data.size()+" should be "+CHKBlock.DATA_LENGTH);
  356.                     } else {
  357.                         dataCount++;
  358.                     }
  359.                     System.err.println(data.toString()+" : "+data.size());
  360.                     container.deactivate(data, 5);
  361.                 }
  362.             }
  363.             if(dataCount == checkBlocks.length)
  364.                 System.out.println("Has all check blocks");
  365.             else
  366.                 System.out.println("Does not have all check blocks: "+dataCount+" of "+checkBlocks.length);
  367.         }
  368.         if(dataBlockStatus != null) {
  369.             System.err.println("Has data block status");
  370.             int dataCount = 0;
  371.             for(int i=0;i<dataBlockStatus.length;i++) {
  372.                 SplitfileBlock status = dataBlockStatus[i];
  373.                 Bucket data = status == null ? null : status.getData();
  374.                 if(data == null) {
  375.                     System.err.println("Data block "+i+" is null!");
  376.                 } else {
  377.                     container.activate(data, 5);
  378.                     if(data.size() != CHKBlock.DATA_LENGTH) {
  379.                         System.err.println("Size of data block "+i+" is "+data.size()+" should be "+CHKBlock.DATA_LENGTH);
  380.                     } else {
  381.                         dataCount++;
  382.                     }
  383.                     System.err.println(data.toString()+" : "+data.size());
  384.                     container.deactivate(data, 5);
  385.                 }
  386.             }
  387.             if(dataCount == dataBlockStatus.length)
  388.                 System.out.println("Has all data block statuses");
  389.             else
  390.                 System.out.println("Does not have all data block statuses: "+dataCount+" of "+dataBlockStatus.length);
  391.         }
  392.         if(checkBlockStatus != null) {
  393.             System.err.println("Has check block status");
  394.             int dataCount = 0;
  395.             for(int i=0;i<checkBlockStatus.length;i++) {
  396.                 SplitfileBlock status = checkBlockStatus[i];
  397.                 Bucket data = status == null ? null : status.getData();
  398.                 if(data == null) {
  399.                     System.err.println("Check block "+i+" is null!");
  400.                 } else {
  401.                     container.activate(data, 5);
  402.                     if(data.size() != CHKBlock.DATA_LENGTH) {
  403.                         System.err.println("Size of check block "+i+" is "+data.size()+" should be "+CHKBlock.DATA_LENGTH);
  404.                     } else {
  405.                         dataCount++;
  406.                     }
  407.                     System.err.println(data.toString()+" : "+data.size());
  408.                     container.deactivate(data, 5);
  409.                 }
  410.             }
  411.             if(dataCount == checkBlockStatus.length)
  412.                 System.out.println("Has all data block statuses");
  413.             else
  414.                 System.out.println("Does not have all data block statuses: "+dataCount+" of "+checkBlockStatus.length);
  415.         }
  416.     }
  417.    
  418.     public boolean objectCanDeactivate(ObjectContainer container) {
  419.         if(running) {
  420.             Logger.error(this, "Tried to deactivate but running == true!");
  421.             return false;
  422.         }
  423.         return true;
  424.     }
  425. }
Add Comment
Please, Sign In to add comment