/// <summary> /// Get a page from cache or from disk (and put on cache) /// </summary> public T GetPage <T>(uint pageID, bool setDirty = false) where T : BasePage { var page = _cache.GetPage(pageID); // is not on cache? load from disk if (page == null) { var buffer = _disk.ReadPage(pageID); page = BasePage.ReadPage(buffer); _cache.AddPage(page); } #if DEBUG // if page is empty, convert to T if (page.PageType == PageType.Empty && typeof(T) != typeof(BasePage)) { throw new Exception("Pager.GetPage<T>() never shuld happend"); } #endif // set page as dirty if passing by param if (setDirty) { this.SetDirty((T)page); } return((T)page); }
/// <summary> /// This method must be called before read/write operation to avoid dirty reads. /// It's occurs when my cache contains pages that was changed in another process /// </summary> public bool AvoidDirtyRead() { var cache = (HeaderPage)_cache.GetPage(0); if (cache == null) { return(false); } // read change direct from disk var change = _disk.GetChangeID(); // if changeID was changed, file was changed by another process - clear all cache if (cache.ChangeID != change) { _cache.Clear(); return(true); } return(false); }