/* * [Clump.java] * * Summary: Assigning all files to a zip. * * Copyright: (c) 2003-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 8.5 2007-08-18 */ package com.mindprod.replicatorsender; import com.mindprod.sorted.Merge; /** * Assigning all files to a zip. * * @author Roedy Green, Canadian Mind Products * @version 8.5 2007-08-18 * @since 2003-03-23 */ final class Clump { /** * make sure all files marked deleted and files to distribute have unique clumping numbers. */ private static void ensureUniqueClumping() { ReplicatorSender.combinedFilesAndDeletions .sort( MaxiFD.BY_CLUMPING_AND_FILENAME ); long prevClump = ConfigForSender.NULL_TIMESTAMP; for ( MaxiFD fd : ReplicatorSender.combinedFilesAndDeletions ) { if ( fd.getClumping() <= prevClump ) { fd.setClumping( ++prevClump ); } else { prevClump = fd.getClumping(); } } } /** * Assign files to zip files so that zip files will be roughly the right size. */ public static void process() { // ensures/sorts filesToDistribute filesToDelete as side effect. ReplicatorSender.combinedFilesAndDeletions = Merge.union( ReplicatorSender.filesToDistribute, ReplicatorSender.filesToDelete, MaxiFD.BY_CLUMPING_AND_FILENAME ); ensureUniqueClumping(); final long limit = ConfigForSender.UNCOMPRESSED_IDEAL_ZIPSIZE; // Force a new jar right off the bat, will only be created if there were // new files. // force creating of a new zip on any new file or deletions to distribute MaxiZD zd = null; long fullness = Integer.MAX_VALUE / 2; for ( MaxiFD fd : ReplicatorSender.combinedFilesAndDeletions ) { if ( fd.getZipNumber() != 0 ) { continue; } final long length = fd.getFileLength(); // Put it in same jar if it will fit under the limit // or if going over would get us closer to the ideal limit than // going under. if ( fullness + length <= limit || fullness < limit && limit - fullness >= fullness + length - limit ) { // fit into this jar. fullness += length; } else { // fit into next jar fullness = length; zd = new MaxiZD();// will be an UNCREATED_A; } // this will assign objects in the filesToDistribute and filesToDelete list as well. fd.assignZip( zd ); } // end for // we leave filesToDistribute and filesToDelete sorted BY_CLUMPING_AND_FILENAME, // with all entries assigned to zips } } // end Clump.