Пример #1
0
        /// <summary>
        /// Sort input to output, explicit hint for the buffer size. The amount of allocated
        /// memory may deviate from the hint (may be smaller or larger).
        /// </summary>
        public SortInfo Sort(FileInfo input, FileInfo output)
        {
            sortInfo = new SortInfo(this)
            {
                TotalTime = Environment.TickCount
            };

            output.Delete();

            var  merges   = new List <FileInfo>();
            bool success2 = false;

            try
            {
                var  inputStream = new ByteSequencesReader(input);
                bool success     = false;
                try
                {
                    int lines = 0;
                    while ((lines = ReadPartition(inputStream)) > 0)
                    {
                        merges.Add(SortPartition(/*lines*/)); // LUCENENET specific - removed unused parameter
                        sortInfo.TempMergeFiles++;
                        sortInfo.Lines += lines;

                        // Handle intermediate merges.
                        if (merges.Count == maxTempFiles)
                        {
                            var intermediate = new FileInfo(Path.GetTempFileName());
                            try
                            {
                                MergePartitions(merges, intermediate);
                            }
                            finally
                            {
                                foreach (var file in merges)
                                {
                                    file.Delete();
                                }
                                merges.Clear();
                                merges.Add(intermediate);
                            }
                            sortInfo.TempMergeFiles++;
                        }
                    }
                    success = true;
                }
                finally
                {
                    if (success)
                    {
                        IOUtils.Dispose(inputStream);
                    }
                    else
                    {
                        IOUtils.DisposeWhileHandlingException(inputStream);
                    }
                }

                // One partition, try to rename or copy if unsuccessful.
                if (merges.Count == 1)
                {
                    FileInfo single = merges[0];
                    Copy(single, output);
                    try
                    {
                        File.Delete(single.FullName);
                    }
#pragma warning disable CA1031 // Do not catch general exception types
                    catch
                    {
                        // ignored
                    }
#pragma warning restore CA1031 // Do not catch general exception types
                }
                else
                {
                    // otherwise merge the partitions with a priority queue.
                    MergePartitions(merges, output);
                }
                success2 = true;
            }
            finally
            {
                foreach (FileInfo file in merges)
                {
                    file.Delete();
                }
                if (!success2)
                {
                    output.Delete();
                }
            }

            sortInfo.TotalTime = (Environment.TickCount - sortInfo.TotalTime);
            return(sortInfo);
        }
Пример #2
0
        private void TestCase(int itrsWithVal, int specifiedValsOnItr, bool removeDups)
        {
            // Build a random number of lists
            IList <int> expected = new JCG.List <int>();
            Random      random   = new J2N.Randomizer(Random.NextInt64());
            int         numLists = itrsWithVal + random.Next(1000 - itrsWithVal);

            IList <int>[] lists = new IList <int> [numLists];
            for (int i = 0; i < numLists; i++)
            {
                lists[i] = new JCG.List <int>();
            }
            int start = random.Next(1000000);
            int end   = start + VALS_TO_MERGE / itrsWithVal / Math.Abs(specifiedValsOnItr);

            for (int i = start; i < end; i++)
            {
                int maxList      = lists.Length;
                int maxValsOnItr = 0;
                int sumValsOnItr = 0;
                for (int itrWithVal = 0; itrWithVal < itrsWithVal; itrWithVal++)
                {
                    int list      = random.Next(maxList);
                    int valsOnItr = specifiedValsOnItr < 0 ? (1 + random.Next(-specifiedValsOnItr)) : specifiedValsOnItr;
                    maxValsOnItr  = Math.Max(maxValsOnItr, valsOnItr);
                    sumValsOnItr += valsOnItr;
                    for (int valOnItr = 0; valOnItr < valsOnItr; valOnItr++)
                    {
                        lists[list].Add(i);
                    }
                    maxList = maxList - 1;
                    ArrayUtil.Swap(lists, list, maxList);
                }
                int maxCount = removeDups ? maxValsOnItr : sumValsOnItr;
                for (int count = 0; count < maxCount; count++)
                {
                    expected.Add(i);
                }
            }
            // Now check that they get merged cleanly
            IEnumerator <int>[] itrs = new IEnumerator <int> [numLists];
            for (int i = 0; i < numLists; i++)
            {
                itrs[i] = lists[i].GetEnumerator();
            }
            try
            {
                MergedEnumerator <int> mergedItr    = new MergedEnumerator <int>(removeDups, itrs);
                using IEnumerator <int> expectedItr = expected.GetEnumerator();
                while (expectedItr.MoveNext())
                {
                    Assert.IsTrue(mergedItr.MoveNext());
                    Assert.AreEqual(expectedItr.Current, mergedItr.Current);
                }
                Assert.IsFalse(mergedItr.MoveNext());
            }
            finally
            {
                IOUtils.Dispose(itrs);
            }
        }