示例#1
0
        /// <summary>
        ///   Aggregates all the supplied FILES into THE FIRST file (marking it as a PERMA file) using the supplied combineF
        ///   action to merge all the individual files into 1 combined output file.
        /// </summary>
        /// <remarks>
        ///   The first file is renamed to the resultFileNameTemplate, marked as a PERMA file and updated in place on the
        ///   file system.  This is more efficient because we do not make a copy of the first file, we rename the first file and
        ///   UPDATE IT IN PLACE.  This results in less file IO, which improves performance for large files.
        /// </remarks>
        /// <param name="files">The sequence of files to combine into 1 file.</param>
        /// <param name="resultFileNameTemplate">The template string to use for the name of the combined file.</param>
        /// <param name="combineF">
        ///   Action to combine 2 files into 1.
        ///   combineF :: (accumulator: TempFile, inputFile: TempFile) -> unit.
        /// </param>
        /// <returns>A TempFile instance for the new combined file.</returns>
        public TempFile AggregateFilesIntoFirstAsPerma(IEnumerable <TempFile> files, string resultFileNameTemplate, Action <TempFile, TempFile> combineF)
        {
            if (files.IsEmpty())
            {
                TempFile accumulatorFile = NewPermaFile(resultFileNameTemplate);
                accumulatorFile.CreateIfNotExists();
                return(accumulatorFile);
            }

            if (files.Count() == 1)
            {
                TempFile accumulatorFile = files.Single();
                accumulatorFile.Rename(resultFileNameTemplate);
                accumulatorFile.MakePermanent();

                return(accumulatorFile);
            }

            {
                TempFile accumulatorFile = files.First();
                accumulatorFile.Rename(resultFileNameTemplate);
                accumulatorFile.MakePermanent();

                files.Skip(1).Each(file => combineF(accumulatorFile, file));

                return(accumulatorFile);
            }
        }
示例#2
0
        /// <summary>
        ///   Creates a new TempFile object, relative to this TempDir, using the supplied fileNameTemplate.  Files created
        ///   with this method will NOT be deleted when calling the ClearTempFiles() method.
        ///
        ///   NOTE: This does NOT actually create the file on the file system.
        /// </summary>
        /// <param name="fileNameTemplate">
        ///   A template string that is used to determine the name of the new TempFile.
        ///   It can have 1 of 3 possible values:
        ///     (1.) Empty                - a fully random name will be generated that is unique within the TempDir.
        ///     (2.) "temp_file_{0}.tmp"  - a semi random name will be generated, replacing the {0} placeholder with some random, unique value.
        ///     (3.) "my_temp_file"       - an exact, non-random name that will always be the same.
        /// </param>
        public TempFile NewPermaFile(string fileNameTemplate = "")
        {
            string fileName  = TempUtils.GenerateFsEntryName(DirPath, fileNameTemplate);
            var    permaFile = new TempFile(this, fileName);

            permaFile.MakePermanent();

            // keep track of temp files so that we can selectively clean them up later
            _files.Add(permaFile);

            return(permaFile);
        }