Exemplo n.º 1
0
        public bool TryGettingFromAllocatedBuffer(Transaction tx, int numberOfPages, long size, out PageFromScratchBuffer result)
        {
            result = null;
            LinkedList <PendingPage> list;

            if (!_freePagesBySize.TryGetValue(size, out list) || list.Count <= 0)
            {
                return(false);
            }
            var val = list.Last.Value;
            var oldestTransaction = tx.Environment.OldestTransaction;

            if (oldestTransaction != 0 && val.ValidAfterTransactionId >= oldestTransaction)             // OldestTransaction can be 0 when there are none other transactions and we are in process of new transaction header allocation
            {
                return(false);
            }

            list.RemoveLast();
            var pageFromScratchBuffer = new PageFromScratchBuffer
            {
                ScratchFileNumber       = _scratchNumber,
                PositionInScratchBuffer = val.Page,
                Size          = size,
                NumberOfPages = numberOfPages
            };

            _allocatedPages.Add(val.Page, pageFromScratchBuffer);

            result = pageFromScratchBuffer;
            return(true);
        }
Exemplo n.º 2
0
        public bool TryGettingFromAllocatedBuffer(LowLevelTransaction tx, int numberOfPages, long size, out PageFromScratchBuffer result)
        {
            result = null;

            LinkedList <long> listOfAvailableImmediately;

            if (_freePagesBySizeAvailableImmediately.TryGetValue(size, out listOfAvailableImmediately) && listOfAvailableImmediately.Count > 0)
            {
                var freeAndAvailablePageNumber = listOfAvailableImmediately.Last.Value;

                listOfAvailableImmediately.RemoveLast();

#if VALIDATE
                byte *freeAndAvailablePagePointer = _scratchPager.AcquirePagePointer(tx, freeAndAvailablePageNumber, PagerState);
                ulong freeAndAvailablePageSize    = (ulong)size * Constants.Storage.PageSize;
                // This has to be forced, as the list of available pages should be protected by default, but this
                // is a policy we implement inside the ScratchBufferFile only.
                _scratchPager.UnprotectPageRange(freeAndAvailablePagePointer, freeAndAvailablePageSize, true);
#endif

                result = new PageFromScratchBuffer(_scratchNumber, freeAndAvailablePageNumber, size, numberOfPages);

                _allocatedPagesCount += numberOfPages;
                _allocatedPages.Add(freeAndAvailablePageNumber, result);

                return(true);
            }

            LinkedList <PendingPage> list;
            if (!_freePagesBySize.TryGetValue(size, out list) || list.Count <= 0)
            {
                return(false);
            }

            var val = list.Last.Value;

            if (val.ValidAfterTransactionId >= tx.Environment.PossibleOldestReadTransaction(tx))
            {
                return(false);
            }

            list.RemoveLast();

#if VALIDATE
            byte *freePageBySizePointer = _scratchPager.AcquirePagePointer(tx, val.Page, PagerState);
            ulong freePageBySizeSize    = (ulong)size * Constants.Storage.PageSize;
            // This has to be forced, as the list of available pages should be protected by default, but this
            // is a policy we implement inside the ScratchBufferFile only.
            _scratchPager.UnprotectPageRange(freePageBySizePointer, freePageBySizeSize, true);
#endif

            result = new PageFromScratchBuffer(_scratchNumber, val.Page, size, numberOfPages);

            _allocatedPagesCount += numberOfPages;
            _allocatedPages.Add(val.Page, result);
            return(true);
        }
Exemplo n.º 3
0
		public PageFromScratchBuffer Allocate(Transaction tx, int numberOfPages, long size)
		{
			_scratchPager.EnsureContinuous(tx, _lastUsedPage, (int) size);

            var result = new PageFromScratchBuffer(_scratchNumber, _lastUsedPage, size, numberOfPages);

            _allocatedPagesUsedSize += numberOfPages;
			_allocatedPages.Add(_lastUsedPage, result);
			_lastUsedPage += size;

			return result;
		}
Exemplo n.º 4
0
        public PageFromScratchBuffer Allocate(Transaction tx, int numberOfPages, long size)
        {
            _scratchPager.EnsureContinuous(tx, _lastUsedPage, (int)size);

            var result = new PageFromScratchBuffer(_scratchNumber, _lastUsedPage, size, numberOfPages);

            _allocatedPagesCount += numberOfPages;
            _allocatedPages.Add(_lastUsedPage, result);
            _lastUsedPage += size;

            return(result);
        }
Exemplo n.º 5
0
        public PageFromScratchBuffer Allocate(LowLevelTransaction tx, int numberOfPages, int sizeToAllocate)
        {
            var pagerState = _scratchPager.EnsureContinuous(_lastUsedPage, sizeToAllocate);
            tx?.EnsurePagerStateReference(pagerState);

            var result = new PageFromScratchBuffer(_scratchNumber, _lastUsedPage, sizeToAllocate, numberOfPages);

            _allocatedPagesCount += numberOfPages;
            _allocatedPages.Add(_lastUsedPage, result);
            _lastUsedPage += sizeToAllocate;

            return result;
        }
Exemplo n.º 6
0
        public PageFromScratchBuffer ShrinkOverflowPage(PageFromScratchBuffer value, int newNumberOfPages)
        {
            if (_allocatedPages.Remove(value.PositionInScratchBuffer) == false)
            {
                InvalidAttemptToShrinkPageThatWasntAllocated(value);
            }

            Debug.Assert(value.NumberOfPages > 1);
            Debug.Assert(value.NumberOfPages > newNumberOfPages);

            var shrinked = new PageFromScratchBuffer(Number, value.PositionInScratchBuffer, value.Size, newNumberOfPages);

            _allocatedPages.Add(shrinked.PositionInScratchBuffer, shrinked);

            return(shrinked);
        }
Exemplo n.º 7
0
		public PageFromScratchBuffer Allocate(Transaction tx, int numberOfPages, long size)
		{
			_scratchPager.EnsureContinuous(tx, _lastUsedPage, (int) size);

			var result = new PageFromScratchBuffer
			{
				ScratchFileNumber = _scratchNumber,
				PositionInScratchBuffer = _lastUsedPage,
				Size = size,
				NumberOfPages = numberOfPages
			};

			_allocatedPages.Add(_lastUsedPage, result);
			_lastUsedPage += size;

			return result;
		}
Exemplo n.º 8
0
        public PageFromScratchBuffer Allocate(Transaction tx, int numberOfPages, long size)
        {
            _scratchPager.EnsureContinuous(tx, _lastUsedPage, (int)size);

            var result = new PageFromScratchBuffer
            {
                ScratchFileNumber       = _scratchNumber,
                PositionInScratchBuffer = _lastUsedPage,
                Size          = size,
                NumberOfPages = numberOfPages
            };

            _allocatedPages.Add(_lastUsedPage, result);
            _lastUsedPage += size;

            return(result);
        }
Exemplo n.º 9
0
        public void BreakLargeAllocationToSeparatePages(PageFromScratchBuffer value)
        {
            if (_allocatedPages.Remove(value.PositionInScratchBuffer) == false)
            {
                throw new InvalidOperationException("Attempt to break up a page that wasn't currently allocated: " +
                                                    value.PositionInScratchBuffer);
            }

            _allocatedPages.Add(value.PositionInScratchBuffer,
                                new PageFromScratchBuffer(value.ScratchFileNumber, value.PositionInScratchBuffer, value.Size, 1));

            for (int i = 1; i < value.NumberOfPages; i++)
            {
                _allocatedPages.Add(value.PositionInScratchBuffer + i,
                                    new PageFromScratchBuffer(value.ScratchFileNumber, value.PositionInScratchBuffer + i, 0, 1));
            }
        }
Exemplo n.º 10
0
        public bool TryGettingFromAllocatedBuffer(Transaction tx, int numberOfPages, long size, out PageFromScratchBuffer result)
        {
            result = null;

            LinkedList <long> listOfAvailableImmediately;

            if (_freePagesBySizeAvailableImmediately.TryGetValue(size, out listOfAvailableImmediately) && listOfAvailableImmediately.Count > 0)
            {
                var freeAndAvailablePageNumber = listOfAvailableImmediately.Last.Value;

                listOfAvailableImmediately.RemoveLast();

                result = new PageFromScratchBuffer(_scratchNumber, freeAndAvailablePageNumber, size, numberOfPages);

                _allocatedPagesUsedSize += numberOfPages;
                _allocatedPages.Add(freeAndAvailablePageNumber, result);
                return(true);
            }

            LinkedList <PendingPage> list;

            if (!_freePagesBySize.TryGetValue(size, out list) || list.Count <= 0)
            {
                return(false);
            }

            var val = list.Last.Value;
            var oldestTransaction = tx.Environment.OldestTransaction;

            if (oldestTransaction != 0 && val.ValidAfterTransactionId >= oldestTransaction) // OldestTransaction can be 0 when there are none other transactions and we are in process of new transaction header allocation
            {
                return(false);
            }

            list.RemoveLast();
            result = new PageFromScratchBuffer(_scratchNumber, val.Page, size, numberOfPages);

            _allocatedPagesUsedSize += numberOfPages;
            _allocatedPages.Add(val.Page, result);

            return(true);
        }
Exemplo n.º 11
0
        public bool TryGettingFromAllocatedBuffer(Transaction tx, int numberOfPages, long size, out PageFromScratchBuffer result)
        {
            result = null;

            LinkedList <long> listOfAvailableImmediately;

            if (_freePagesBySizeAvailableImmediately.TryGetValue(size, out listOfAvailableImmediately) && listOfAvailableImmediately.Count > 0)
            {
                var freeAndAvailablePageNumber = listOfAvailableImmediately.Last.Value;

                listOfAvailableImmediately.RemoveLast();

                result = new PageFromScratchBuffer(_scratchNumber, freeAndAvailablePageNumber, size, numberOfPages);

                _allocatedPagesCount += numberOfPages;
                _allocatedPages.Add(freeAndAvailablePageNumber, result);
                return(true);
            }

            LinkedList <PendingPage> list;

            if (!_freePagesBySize.TryGetValue(size, out list) || list.Count <= 0)
            {
                return(false);
            }

            var val = list.Last.Value;

            if (val.ValidAfterTransactionId >= tx.Environment.PossibleOldestReadTransaction)
            {
                return(false);
            }

            list.RemoveLast();
            result = new PageFromScratchBuffer(_scratchNumber, val.Page, size, numberOfPages);

            _allocatedPagesCount += numberOfPages;
            _allocatedPages.Add(val.Page, result);

            return(true);
        }
Exemplo n.º 12
0
 private static void InvalidAttemptToShrinkPageThatWasntAllocated(PageFromScratchBuffer value)
 {
     throw new InvalidOperationException($"Attempt to shrink a page that wasn't currently allocated: {value.PositionInScratchBuffer}");
 }
Exemplo n.º 13
0
 private static void InvalidAttemptToBreakupPageThatWasntAllocated(PageFromScratchBuffer value)
 {
     throw new InvalidOperationException("Attempt to break up a page that wasn't currently allocated: " +
                                         value.PositionInScratchBuffer);
 }
Exemplo n.º 14
0
        public void BreakLargeAllocationToSeparatePages(IPagerLevelTransactionState tx, PageFromScratchBuffer value)
        {
            if (_allocatedPages.Remove(value.PositionInScratchBuffer) == false)
            {
                InvalidAttemptToBreakupPageThatWasntAllocated(value);
            }

            _allocatedPages.Add(value.PositionInScratchBuffer,
                                new PageFromScratchBuffer(value.ScratchFileNumber, value.PositionInScratchBuffer, 1, 1));

            for (int i = 1; i < value.NumberOfPages; i++)
            {
                _allocatedPages.Add(value.PositionInScratchBuffer + i,
                                    new PageFromScratchBuffer(value.ScratchFileNumber, value.PositionInScratchBuffer + i, 1, 1));
            }

            _scratchPager.BreakLargeAllocationToSeparatePages(tx, value.PositionInScratchBuffer);
        }
Exemplo n.º 15
0
        public void BreakLargeAllocationToSeparatePages(PageFromScratchBuffer value)
        {
            var item = GetScratchBufferFile(value.ScratchFileNumber);

            item.File.BreakLargeAllocationToSeparatePages(value);
        }
Exemplo n.º 16
0
        public void BreakLargeAllocationToSeparatePages(LowLevelTransaction tx, PageFromScratchBuffer value)
        {
            var item = GetScratchBufferFile(value.ScratchFileNumber);

            item.File.BreakLargeAllocationToSeparatePages(tx, value);
        }
Exemplo n.º 17
0
        public PageFromScratchBuffer ShrinkOverflowPage(PageFromScratchBuffer value, int newNumberOfPages)
        {
            var item = GetScratchBufferFile(value.ScratchFileNumber);

            return(item.File.ShrinkOverflowPage(value, newNumberOfPages));
        }
Exemplo n.º 18
0
		public bool TryGettingFromAllocatedBuffer(Transaction tx, int numberOfPages, long size, out PageFromScratchBuffer result)
		{
			result = null;
			LinkedList<PendingPage> list;
			if (!_freePagesBySize.TryGetValue(size, out list) || list.Count <= 0)
				return false;
			var val = list.Last.Value;
			var oldestTransaction = tx.Environment.OldestTransaction;
			if (oldestTransaction != 0 && val.ValidAfterTransactionId >= oldestTransaction) // OldestTransaction can be 0 when there are none other transactions and we are in process of new transaction header allocation
				return false;

			list.RemoveLast();
			var pageFromScratchBuffer = new PageFromScratchBuffer
			{
				ScratchFileNumber = _scratchNumber,
				PositionInScratchBuffer = val.Page,
				Size = size,
				NumberOfPages = numberOfPages
			};

			_allocatedPages.Add(val.Page, pageFromScratchBuffer);
			
			result = pageFromScratchBuffer;
			return true;
		}
Exemplo n.º 19
0
		public bool TryGettingFromAllocatedBuffer(Transaction tx, int numberOfPages, long size, out PageFromScratchBuffer result)
		{
			result = null;

			LinkedList<long> listOfAvailableImmediately;
			if (_freePagesBySizeAvailableImmediately.TryGetValue(size, out listOfAvailableImmediately) && listOfAvailableImmediately.Count > 0)
			{
				var freeAndAvailablePageNumber = listOfAvailableImmediately.Last.Value;

				listOfAvailableImmediately.RemoveLast();

				result = new PageFromScratchBuffer ( _scratchNumber, freeAndAvailablePageNumber, size, numberOfPages );
                
                _allocatedPagesUsedSize += numberOfPages;
				_allocatedPages.Add(freeAndAvailablePageNumber, result);
				return true;
			}

			LinkedList<PendingPage> list;
			if (!_freePagesBySize.TryGetValue(size, out list) || list.Count <= 0)
				return false;

			var val = list.Last.Value;
			var oldestTransaction = tx.Environment.OldestTransaction;
			if (oldestTransaction != 0 && val.ValidAfterTransactionId >= oldestTransaction) // OldestTransaction can be 0 when there are none other transactions and we are in process of new transaction header allocation
				return false;

			list.RemoveLast();
			result = new PageFromScratchBuffer ( _scratchNumber, val.Page, size, numberOfPages );

            _allocatedPagesUsedSize += numberOfPages;
			_allocatedPages.Add(val.Page, result);
			return true;
		}