Beispiel #1
0
        public void SetLength(CachedStream owner, long length)
        {
            IntArrayList obsoleteOffsets = null;

            try
            {
                if (owner.Length > length)
                {
                    foreach (CachedPage page in _pagesCache)
                    {
                        if (Object.ReferenceEquals(page.Owner, owner))
                        {
                            if (page.Offset >= length)
                            {
                                if (obsoleteOffsets == null)
                                {
                                    obsoleteOffsets = IntArrayListPool.Alloc();
                                }
                                obsoleteOffsets.Add((int)(page.Offset >> CachedPage._pageShiftBits));
                                page.ClearDirty();
                                if (_lastAccessedPage != null && _lastAccessedPage.Offset == page.Offset)
                                {
                                    _lastAccessedPage = null;
                                }
                            }
                            else if (page.Offset + page.Size > length)
                            {
                                int newSize = (int)(length - page.Offset);
                                if (newSize != page.Size)
                                {
                                    page.Size = newSize;
                                    page.SetDirty();
                                }
                            }
                        }
                    }
                    if (obsoleteOffsets != null)
                    {
                        _searchKey._owner = owner;
                        for (int i = 0; i < obsoleteOffsets.Count; ++i)
                        {
                            _searchKey._offset = obsoleteOffsets[i];
                            _pagesCache.Remove(_searchKey);
                        }
                    }
                }
                owner.SetLengthImpl(length);
            }
            finally
            {
                owner.GetUnderlyingStream().SetLength(length);
                if (obsoleteOffsets != null)
                {
                    IntArrayListPool.Dispose(obsoleteOffsets);
                }
            }
        }
Beispiel #2
0
 public void Flush(CachedStream owner)
 {
     if (_dirtyPages.Count > 0)
     {
         if (_dirtyPages.Count == 1)
         {
             foreach (HashSet.Entry e in _dirtyPages)
             {
                 CachedPageKey key = (CachedPageKey)e.Key;
                 if (Object.ReferenceEquals(key._owner, owner))
                 {
                     ((CachedPage)_pagesCache.TryKey(key)).Save();
                 }
             }
         }
         else
         {
             IntArrayList pageOffsets = IntArrayListPool.Alloc();
             try
             {
                 foreach (HashSet.Entry e in _dirtyPages)
                 {
                     CachedPageKey key = (CachedPageKey)e.Key;
                     if (Object.ReferenceEquals(key._owner, owner))
                     {
                         pageOffsets.Add(key._offset);
                     }
                 }
                 pageOffsets.Sort();
                 _searchKey._owner = owner;
                 for (int i = 0; i < pageOffsets.Count; ++i)
                 {
                     _searchKey._offset = pageOffsets[i];
                     ((CachedPage)_pagesCache.TryKey(_searchKey)).Save();
                 }
             }
             finally
             {
                 IntArrayListPool.Dispose(pageOffsets);
             }
         }
     }
     if (owner.GetUnderlyingStream().CanWrite)
     {
         owner.GetUnderlyingStream().Flush();
     }
 }
Beispiel #3
0
 public override void Flush()
 {
     if (_dirtyClusters.Count > 0)
     {
         IntArrayList handles = IntArrayListPool.Alloc();
         try
         {
             foreach (IntHashSet.Entry e in _dirtyClusters)
             {
                 handles.Add(e.Key);
             }
             for (int i = 0; i < handles.Count; ++i)
             {
                 ((Cluster)_clusterCache.TryKey(handles[i])).SaveHeader();
             }
         }
         finally
         {
             IntArrayListPool.Dispose(handles);
         }
         _stream.Flush();
     }
 }
Beispiel #4
0
 public void Flush(CachedStream owner)
 {
     if (_dirtyPages.Count > 0)
     {
         if (_dirtyPages.Count == 1)
         {
             foreach (IntHashSet.Entry e in _dirtyPages)
             {
                 ((CachedPage)_pagesCache.TryKey(e.Key)).Save();
             }
         }
         else
         {
             IntArrayList pageOffsets = IntArrayListPool.Alloc();
             try
             {
                 foreach (IntHashSet.Entry e in _dirtyPages)
                 {
                     pageOffsets.Add(e.Key);
                 }
                 pageOffsets.Sort();
                 for (int i = 0; i < pageOffsets.Count; ++i)
                 {
                     ((CachedPage)_pagesCache.TryKey(pageOffsets[i])).Save();
                 }
             }
             finally
             {
                 IntArrayListPool.Dispose(pageOffsets);
             }
         }
     }
     if (owner.GetUnderlyingStream().CanWrite)
     {
         owner.GetUnderlyingStream().Flush();
     }
 }