Esempio 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);
        }
Esempio 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);
        }
Esempio 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;
		}
Esempio 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);
        }
Esempio 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;
        }
Esempio 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);
        }
Esempio 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;
		}
Esempio 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);
        }
        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));
            }
        }
Esempio 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);
        }
Esempio 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);
        }
Esempio n. 12
0
 private static void InvalidAttemptToShrinkPageThatWasntAllocated(PageFromScratchBuffer value)
 {
     throw new InvalidOperationException($"Attempt to shrink a page that wasn't currently allocated: {value.PositionInScratchBuffer}");
 }
Esempio 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);
 }
Esempio 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);
        }
Esempio n. 15
0
        public void BreakLargeAllocationToSeparatePages(PageFromScratchBuffer value)
        {
            var item = GetScratchBufferFile(value.ScratchFileNumber);

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

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

            return(item.File.ShrinkOverflowPage(value, newNumberOfPages));
        }
Esempio 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;
		}
Esempio 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;
		}