public static void Sort(Table Element, RecordMatcher Comparer) { // Check that this is a heap table // if (!(Element is HeapTable)) { throw new Exception("Can only sort heap tables"); } if (Element.Header.IndexHeaders.Count != 0) { throw new Exception("Cannot sort a table with indexes"); } // Sort each element // SortEach(Element, Comparer); // Merge sort // Table.PageWalker x = new Table.PageWalker(Element); Table.PageWalker y = new Table.PageWalker(Element); while (x.CanAdvance) { // Get x's page // Page A = x.SelectNext(); // Break if we on the last page // if (A.NextPageID == -1) { break; } // Open a new page walker that starts at the next page // y.ToPage(A.NextPageID); // Loop through y // while (y.CanAdvance) { // Get the second page // Page B = y.SelectNext(); // Merge // Merge(A, B, Comparer); // Set B // Element.SetPage(B); } // A is final at this point // Element.SetPage(A); } }
private static void SortEach(Table Element, RecordMatcher Comparer) { if (Element.Header.ClusterKey.Count != 0) { throw new Exception("Can only sort a heap table"); } if (Element.RecordCount == 0) { return; } Table.PageWalker walker = new Table.PageWalker(Element); while (walker.CanAdvance) { Page p = walker.SelectNext(); Sort(p, Comparer); Element.SetPage(p); } }