public static IEnumerable <PageRecord> MergeTables(RazorCache cache, Manifest mf, int destinationLevel, IEnumerable <PageRef> tableSpecs, ExceptionHandling exceptionHandling, Action <string> logger) { var orderedTableSpecs = tableSpecs.OrderByPagePriority(); var outputTables = new List <PageRecord>(); SortedBlockTableWriter writer = null; Key firstKey = new Key(); Key lastKey = new Key(); Key maxKey = new Key(); // Maximum key we can span with this table to avoid covering more than 10 pages in the destination Action <KeyValuePair <Key, Value> > OpenPage = (pair) => { writer = new SortedBlockTableWriter(mf.BaseFileName, destinationLevel, mf.NextVersion(destinationLevel)); firstKey = pair.Key; using (var m = mf.GetLatestManifest()) { maxKey = m.FindSpanningLimit(destinationLevel + 1, firstKey); } }; Action ClosePage = () => { writer.Close(); outputTables.Add(new PageRecord(destinationLevel, writer.Version, firstKey, lastKey)); writer = null; }; try { foreach (var pair in EnumerateMergedTablesPreCached(cache, mf.BaseFileName, orderedTableSpecs, exceptionHandling, logger)) { if (writer == null) { OpenPage(pair); } if (writer.WrittenSize >= Config.MaxSortedBlockTableSize || (!maxKey.IsEmpty && pair.Key.CompareTo(maxKey) >= 0)) { ClosePage(); } if (writer == null) { OpenPage(pair); } writer.WritePair(pair.Key, pair.Value); lastKey = pair.Key; } } finally { if (writer != null) { ClosePage(); } } return(outputTables); }
public void WriteToSortedBlockTable(string baseFileName, int level, int version) { lock (_tableLock) { SortedBlockTableWriter tableWriter = null; try { tableWriter = new SortedBlockTableWriter(baseFileName, level, version); foreach (var pair in this.Enumerate()) { tableWriter.WritePair(pair.Key, pair.Value); } } finally { if (tableWriter != null) { tableWriter.Close(); } } } }