public IndexPage GetFreeIndexPage() { var freeIndexPage = CacheIndexPage.GetPage(Header.FreeIndexPageID); // Check if "free page" has no more index to be used if (freeIndexPage.NodeIndex >= IndexPage.NODES_PER_PAGE - 1) { Header.LastPageID++; // Take last page and increase Header.IsDirty = true; var newIndexPage = new IndexPage(Header.LastPageID); // Create a new index page freeIndexPage.NextPageID = newIndexPage.PageID; // Point older page to the new page Header.FreeIndexPageID = Header.LastPageID; // Update last index page CacheIndexPage.AddPage(freeIndexPage, true); return(newIndexPage); } else { // Has more free index on same index page? return them freeIndexPage.NodeIndex++; // Reserve space return(freeIndexPage); } }
public EntryInfo[] ListAllFiles() { // Get root index page from cache var pageIndex = CacheIndexPage.GetPage(Header.IndexRootPageID); bool cont = true; List <EntryInfo> list = new List <EntryInfo>(); while (cont) { for (int i = 0; i <= pageIndex.NodeIndex; i++) { // Convert node (if is not logicaly deleted) to Entry var node = pageIndex.Nodes[i]; if (!node.IsDeleted) { list.Add(new EntryInfo(node)); } } // Go to the next page if (pageIndex.NextPageID != uint.MaxValue) { pageIndex = CacheIndexPage.GetPage(pageIndex.NextPageID); } else { cont = false; } } return(list.ToArray()); }
public bool Delete(Guid id) { // Search index node from guid var indexNode = Search(id); // If null, not found (return false) if (indexNode == null) { return(false); } // Delete the index node logicaly indexNode.IsDeleted = true; // Add page (from index node) to cache and set as dirty CacheIndexPage.AddPage(indexNode.IndexPage, true); // Mark all data blocks (from data pages) as IsEmpty = true DataFactory.MarkAsEmpty(indexNode.DataPageID, this); // Set header as Dirty to be saved on dispose Header.IsDirty = true; return(true); // Confirma a exclusão }
public void PersistPages() { // Check if header is dirty and save to disk if (Header.IsDirty) { HeaderFactory.WriteToFile(Header, Writer); Header.IsDirty = false; } // Persist all index pages that are dirty CacheIndexPage.PersistPages(); }
public Engine(FileStream stream) { Reader = new BinaryReader(stream); if (stream.CanWrite) { Writer = new BinaryWriter(stream); Writer.Lock(Header.LOCKER_POS, 1); } Header = new Header(); HeaderFactory.ReadFromFile(Header, this.Reader); CacheIndexPage = new CacheIndexPage(Reader, Writer, Header.IndexRootPageID); }