/// <summary> /// Sort a single partition in-memory. </summary> private FileInfo SortPartition(/*int len*/) // LUCENENET NOTE: made private, since protected is not valid in a sealed class. Also eliminated unused parameter. { var data = this.buffer; FileInfo tempFile = FileSupport.CreateTempFile("sort", "partition", DefaultTempDir()); long start = Environment.TickCount; sortInfo.SortTime += (Environment.TickCount - start); using (var @out = new ByteSequencesWriter(tempFile)) { IBytesRefEnumerator iter = buffer.GetEnumerator(comparer); while (iter.MoveNext()) { if (Debugging.AssertsEnabled) { Debugging.Assert(iter.Current.Length <= ushort.MaxValue); } @out.Write(iter.Current); } } // Clean up the buffer for the next partition. data.Clear(); return(tempFile); }
/// <summary> /// Sort a single partition in-memory. </summary> private FileInfo SortPartition(/*int len*/) // LUCENENET NOTE: made private, since protected is not valid in a sealed class. Also eliminated unused parameter. { var data = this.buffer; FileInfo tempFile = FileSupport.CreateTempFile("sort", "partition", DefaultTempDir()); long start = J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond; // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results sortInfo.SortTime += ((J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond) - start); // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results using (var @out = new ByteSequencesWriter(tempFile)) { IBytesRefEnumerator iter = buffer.GetEnumerator(comparer); while (iter.MoveNext()) { if (Debugging.AssertsEnabled) { Debugging.Assert(iter.Current.Length <= ushort.MaxValue); } @out.Write(iter.Current); } } // Clean up the buffer for the next partition. data.Clear(); return(tempFile); }
/// <summary> /// Sort a single partition in-memory. </summary> private FileInfo SortPartition(int len) // LUCENENET NOTE: made private, since protected is not valid in a sealed class { var data = this.buffer; FileInfo tempFile = FileSupport.CreateTempFile("sort", "partition", DefaultTempDir()); long start = Environment.TickCount; sortInfo.SortTime += (Environment.TickCount - start); using (var @out = new ByteSequencesWriter(tempFile)) { BytesRef spare; IBytesRefIterator iter = buffer.GetIterator(comparer); while ((spare = iter.Next()) != null) { Debug.Assert(spare.Length <= ushort.MaxValue); @out.Write(spare); } } // Clean up the buffer for the next partition. data.Clear(); return(tempFile); }
/// <summary> /// Sort a single partition in-memory. </summary> internal FileInfo SortPartition(int len) { var data = this.Buffer; FileInfo tempFile = FileSupport.CreateTempFile("sort", "partition", DefaultTempDir()); long start = DateTime.Now.Millisecond; sortInfo.SortTime += (DateTime.Now.Millisecond - start); using (var @out = new ByteSequencesWriter(tempFile)) { BytesRef spare; BytesRefIterator iter = Buffer.Iterator(comparator); while ((spare = iter.Next()) != null) { Debug.Assert(spare.Length <= short.MaxValue); @out.Write(spare); } } // Clean up the buffer for the next partition. data.Clear(); return(tempFile); }
/// <summary> /// Merge a list of sorted temporary files (partitions) into an output file. </summary> internal void MergePartitions(IList <FileInfo> merges, FileInfo outputFile) { long start = Environment.TickCount; var @out = new ByteSequencesWriter(outputFile); PriorityQueue <FileAndTop> queue = new PriorityQueueAnonymousInnerClassHelper(this, merges.Count); var streams = new ByteSequencesReader[merges.Count]; try { // Open streams and read the top for each file for (int i = 0; i < merges.Count; i++) { streams[i] = new ByteSequencesReader(merges[i]); byte[] line = streams[i].Read(); if (line != null) { queue.InsertWithOverflow(new FileAndTop(i, line)); } } // Unix utility sort() uses ordered array of files to pick the next line from, updating // it as it reads new lines. The PQ used here is a more elegant solution and has // a nicer theoretical complexity bound :) The entire sorting process is I/O bound anyway // so it shouldn't make much of a difference (didn't check). FileAndTop top; while ((top = queue.Top) != null) { @out.Write(top.Current); if (!streams[top.Fd].Read(top.Current)) { queue.Pop(); } else { queue.UpdateTop(); } } sortInfo.MergeTime += Environment.TickCount - start; sortInfo.MergeRounds++; } finally { // The logic below is: if an exception occurs in closing out, it has a priority over exceptions // happening in closing streams. try { IOUtils.Dispose(streams); } finally { IOUtils.Dispose(@out); } } }