//todo public void Reorganize() { Console.WriteLine("###REORGANIZATION###"); var NewMainReader = new MainReader("newmain", MainPageSize, Mode.New); var NewMainWriter = new MainWriter("newmain", MainPageSize, Mode.New); TempLong = OverflowAddress; ResetVariables(); AllocateEmptyPageAtTheEnd(ref NewMainWriter); MainPages++; FilePage newPage = NewMainReader.ReadNextPage(); Tuple <Record, long> entry; MainReader.Reader.Position = 0; //MainReader._count = false; while ((entry = MainReader.ReadNextEntryWithChaining()) != null) { if (entry.Item1.Key == long.MaxValue) { continue; } if (entry.Item1.Deleted) { continue; } if (newPage.Count >= Alpha * MainPageSize) { // insert new element to index // allocate new page NewMainWriter.WritePage(newPage); AllocateEmptyPageAtTheEnd(ref NewMainWriter); newPage = NewMainReader.ReadNextPage(); MainPages++; if (entry.Item1.Key != long.MaxValue) { IndexUnit.Entries.Add(new Tuple <long, long>(entry.Item1.Key, MainPages - 1)); } } newPage.Entries[(int)newPage.Count++] = new Tuple <Record, long>(entry.Item1, -1); MainRecordCount++; } NewMainWriter.WritePage(newPage); OverflowAddress = NewMainWriter.Writer.Position; AllocateEmptyPageAtTheEnd(ref NewMainWriter); // allocate overflow OverflowPages++; IndexUnit.Sort(); IndexUnit.WriteIndex(); MetaData.Save(); MainReader.Dispose(); MainWriter.Dispose(); NewMainReader.Dispose(); NewMainWriter.Dispose(); File.Delete(_filePath); File.Move("newmain", _filePath); MainReader = new MainReader(_filePath, MainPageSize, Mode.Read); MainWriter = new MainWriter(_filePath, MainPageSize, Mode.Read); }
private void AllocateEmptyPageAtTheEnd(ref MainWriter mw) { mw.Writer.Position = mw.Writer.Length; var page = new FilePage { Address = MainReader.PageNumberFromAddress(mw.Writer.Position) }; mw.WritePage(page); }
private void Initialize() { //fill primary area with empty pages //set overflow area address long addrI = 0L, addrM = 0L; try { File.Delete("meta"); } catch { } IndexUnit.Init(true); var firstMainPage = new FilePage { Address = addrM++, Count = 1 }; firstMainPage.Entries[0] = new Tuple <Record, long>(new Record(), -1); MainWriter.WritePage(firstMainPage); MainPages = 1; MainRecordCount = 1; OverflowAddress = MainWriter.Writer.Position; OverflowFirstPageNumber = MainReader.PageNumberFromAddress(MainWriter.Writer.Position); var newPage = new FilePage { Address = addrM }; MainWriter.WritePage(newPage); OverflowEndAddress = MainWriter.Writer.Position; OverflowPages = 1; MainWriter.Reset(); Program.MainWrites = 0L; MetaData.Save(); IndexUnit.WriteIndex(); }