Exemplo n.º 1
0
        //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);
        }
Exemplo n.º 2
0
 public void Dispose()
 {
     MainReader.Dispose();
     MainWriter.Dispose();
     MetaData.Dispose();
     IndexUnit.Dispose();
 }
Exemplo n.º 3
0
        public Index(string fileName, string indexName, int mainPageSize = 0, Mode mode = Mode.Read)
        {
            MainPageSize = mainPageSize;
            _indexPath   = indexName;
            _filePath    = fileName;

            IndexUnit.Path = indexName;

            MainReader = new MainReader(fileName, MainPageSize, mode);
            MainWriter = new MainWriter(fileName, MainPageSize, mode);

            FilePage.PageSize = mainPageSize;

            if (mode == Mode.New)
            {
                Initialize();
            }
            if (mode == Mode.Read)
            {
                MetaData.Read();
                IndexUnit.Init(false);
                IndexUnit.ReadIndex();
                FilePage.PageSize = MainPageSize;
                MainReader        = new MainReader(fileName, MainPageSize, mode);
                MainWriter        = new MainWriter(fileName, MainPageSize, mode);
                IndexUnit.WriteIndex();
            }
            MetaData.Save();
        }
Exemplo n.º 4
0
        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();
        }