Example #1
0
            public SectionsIterator(FixedSizeTree.IFixedSizeIterator iterator)
            {
                this._iterator = iterator;
                this._index    = 0;
                this._isDone   = false;

                this.Current = -1;
            }
Example #2
0
 private static bool TryMoveNextCyclic(FixedSizeTree.IFixedSizeIterator it, long startPage)
 {
     if (it.MoveNext())
     {
         if (it.CurrentKey == startPage)
         {
             return(false); // we went full circle
         }
         return(true);
     }
     // we run out space to move forward, let us go to the start and search from there
     return(it.Seek(0) && it.CurrentKey != startPage);
 }
Example #3
0
        private TableValueReader GetTableValueReader(FixedSizeTree.IFixedSizeIterator it)
        {
            long id;

            it.Value.CopyTo((byte *)&id);
            int size;
            var ptr = DirectRead(id, out size);

            return(new TableValueReader(ptr, size)
            {
                Id = id
            });
        }
Example #4
0
        private long?TryFindLargeValue(LowLevelTransaction tx, FixedSizeTree freeSpaceTree, FixedSizeTree.IFixedSizeIterator it, int num)
        {
            int numberOfNeededFullSections = num / NumberOfPagesInSection;
            int numberOfExtraBitsNeeded    = num % NumberOfPagesInSection;

            var info = new FoundSectionsInfo();

            do
            {
                var stream = it.CreateReaderForCurrent();
                {
                    var current          = new StreamBitArray(stream);
                    var currentSectionId = it.CurrentKey;

                    //need to find full free pages
                    if (current.SetCount < NumberOfPagesInSection)
                    {
                        info.Clear();
                        continue;
                    }

                    //those sections are not following each other in the memory
                    if (info.StartSectionId != null && currentSectionId != info.StartSectionId + info.Sections.Count)
                    {
                        info.Clear();
                    }

                    //set the first section of the sequence
                    if (info.StartSection == -1)
                    {
                        info.StartSection   = it.CurrentKey;
                        info.StartSectionId = currentSectionId;
                    }

                    info.Sections.Add(it.CurrentKey);

                    if (info.Sections.Count != numberOfNeededFullSections)
                    {
                        continue;
                    }

                    //we found enough full sections now we need just a bit more
                    if (numberOfExtraBitsNeeded == 0)
                    {
                        foreach (var section in info.Sections)
                        {
                            freeSpaceTree.Delete(section);
                        }

                        return(info.StartSectionId * NumberOfPagesInSection);
                    }

                    StreamBitArray next;
                    var            nextSectionId = currentSectionId + 1;
                    Slice          read;
                    using (freeSpaceTree.Read(nextSectionId, out read))
                    {
                        if (!read.HasValue)
                        {
                            //not a following next section
                            info.Clear();
                            continue;
                        }

                        next = new StreamBitArray(read.CreateReader());
                    }

                    if (next.HasStartRangeCount(numberOfExtraBitsNeeded) == false)
                    {
                        //not enough start range count
                        info.Clear();
                        continue;
                    }

                    //mark selected bits to false
                    if (next.SetCount == numberOfExtraBitsNeeded)
                    {
                        freeSpaceTree.Delete(nextSectionId);
                    }
                    else
                    {
                        for (int i = 0; i < numberOfExtraBitsNeeded; i++)
                        {
                            next.Set(i, false);
                        }
                        Slice val;
                        using (next.ToSlice(tx.Allocator, out val))
                            freeSpaceTree.Add(nextSectionId, val);
                    }

                    foreach (var section in info.Sections)
                    {
                        freeSpaceTree.Delete(section);
                    }

                    return(info.StartSectionId * NumberOfPagesInSection);
                }
            } while (it.MoveNext());

            return(null);
        }
Example #5
0
        private bool TryFindContinuousRange(LowLevelTransaction tx, FixedSizeTree freeSpaceTree, FixedSizeTree.IFixedSizeIterator it, int num,
                                            StreamBitArray current, long currentSectionId, out long?page)
        {
            page = -1;
            var start = -1;
            var count = 0;

            for (int i = 0; i < NumberOfPagesInSection; i++)
            {
                if (current.Get(i))
                {
                    if (start == -1)
                    {
                        start = i;
                    }
                    count++;
                    if (count == num)
                    {
                        page = currentSectionId * NumberOfPagesInSection + start;
                        break;
                    }
                }
                else
                {
                    start = -1;
                    count = 0;
                }
            }

            if (count != num)
            {
                return(false);
            }

            if (current.SetCount == num)
            {
                freeSpaceTree.Delete(it.CurrentKey);
            }
            else
            {
                for (int i = 0; i < num; i++)
                {
                    current.Set(i + start, false);
                }

                Slice val;
                using (current.ToSlice(tx.Allocator, out val))
                    freeSpaceTree.Add(it.CurrentKey, val);
            }

            return(true);
        }
Example #6
0
        private long?TryFindSmallValue(LowLevelTransaction tx, FixedSizeTree freeSpaceTree, FixedSizeTree.IFixedSizeIterator it, int num)
        {
            do
            {
                var current = new StreamBitArray(it.CreateReaderForCurrent());

                long?page;
                if (current.SetCount < num)
                {
                    if (TryFindSmallValueMergingTwoSections(tx, freeSpaceTree, it.CurrentKey, num, current, out page))
                    {
                        return(page);
                    }
                    continue;
                }

                if (TryFindContinuousRange(tx, freeSpaceTree, it, num, current, it.CurrentKey, out page))
                {
                    return(page);
                }

                //could not find a continuous so trying to merge
                if (TryFindSmallValueMergingTwoSections(tx, freeSpaceTree, it.CurrentKey, num, current, out page))
                {
                    return(page);
                }
            }while (it.MoveNext());

            return(null);
        }