Exemple #1
0
 public void SetStorage(int index, SubStorage storage)
 {
     Assert.SdkRequiresInRange(index, 0, StorageCount);
     DataStorage[index] = storage;
 }
Exemple #2
0
 public void SetStorage(int index, IStorage storage, long offset, long size)
 {
     Assert.SdkRequiresInRange(index, 0, StorageCount);
     DataStorage[index] = new SubStorage(storage, offset, size);
 }
Exemple #3
0
 public Result Initialize(SubStorage nodeStorage, SubStorage entryStorage, int entryCount)
 {
     return(Table.Initialize(nodeStorage, entryStorage, NodeSize, Unsafe.SizeOf <Entry>(), entryCount));
 }
            /// <summary>
            /// Initializes the bucket tree builder.
            /// </summary>
            /// <param name="headerStorage">The <see cref="SubStorage"/> the tree's header will be written to.Must be at least the size in bytes returned by <see cref="QueryHeaderStorageSize"/>.</param>
            /// <param name="nodeStorage">The <see cref="SubStorage"/> the tree's nodes will be written to. Must be at least the size in bytes returned by <see cref="QueryNodeStorageSize"/>.</param>
            /// <param name="entryStorage">The <see cref="SubStorage"/> the tree's entries will be written to. Must be at least the size in bytes returned by <see cref="QueryEntryStorageSize"/>.</param>
            /// <param name="nodeSize">The size of each node in the bucket tree. Must be a power of 2.</param>
            /// <param name="entrySize">The size of each entry that will be stored in the bucket tree.</param>
            /// <param name="entryCount">The exact number of entries that will be added to the bucket tree.</param>
            /// <returns>The <see cref="Result"/> of the operation.</returns>
            public Result Initialize(SubStorage headerStorage, SubStorage nodeStorage, SubStorage entryStorage,
                                     int nodeSize, int entrySize, int entryCount)
            {
                Assert.True(entrySize >= sizeof(long));
                Assert.True(nodeSize >= entrySize + Unsafe.SizeOf <NodeHeader>());
                Assert.True(NodeSizeMin <= nodeSize && nodeSize <= NodeSizeMax);
                Assert.True(BitUtil.IsPowerOfTwo(nodeSize));

                if (headerStorage is null || nodeStorage is null || entryStorage is null)
                {
                    return(ResultFs.NullptrArgument.Log());
                }

                // Set the builder parameters
                NodeSize   = nodeSize;
                EntrySize  = entrySize;
                EntryCount = entryCount;

                EntriesPerEntrySet   = GetEntryCount(nodeSize, entrySize);
                OffsetsPerNode       = GetOffsetCount(nodeSize);
                CurrentL2OffsetIndex = GetNodeL2Count(nodeSize, entrySize, entryCount);

                // Create and write the header
                var header = new Header();

                header.Format(entryCount);
                Result rc = headerStorage.Write(0, SpanHelpers.AsByteSpan(ref header));

                if (rc.IsFailure())
                {
                    return(rc);
                }

                // Allocate buffers for the L1 node and entry sets
                _l1Node.Allocate(nodeSize);
                _entrySet.Allocate(nodeSize);

                int entrySetCount = GetEntrySetCount(nodeSize, entrySize, entryCount);

                // Allocate an L2 node buffer if there are more entry sets than will fit in the L1 node
                if (OffsetsPerNode < entrySetCount)
                {
                    _l2Node.Allocate(nodeSize);
                }

                _l1Node.FillZero();
                _l2Node.FillZero();
                _entrySet.FillZero();

                NodeStorage  = nodeStorage;
                EntryStorage = entryStorage;

                // Set the initial position
                CurrentEntryIndex = 0;
                CurrentOffset     = -1;

                return(Result.Success);
            }
Exemple #5
0
        public Result Initialize(SubStorage nodeStorage, SubStorage entryStorage, int nodeSize, int entrySize,
                                 int entryCount)
        {
            Assert.AssertTrue(entrySize >= sizeof(long));
            Assert.AssertTrue(nodeSize >= entrySize + Unsafe.SizeOf <NodeHeader>());
            Assert.AssertTrue(NodeSizeMin <= nodeSize && nodeSize <= NodeSizeMax);
            Assert.AssertTrue(Utilities.IsPowerOfTwo(nodeSize));
            Assert.AssertTrue(!IsInitialized());

            // Ensure valid entry count.
            if (entryCount <= 0)
            {
                return(ResultFs.InvalidArgument.Log());
            }

            // Allocate node.
            if (!_nodeL1.Allocate(nodeSize))
            {
                return(ResultFs.BufferAllocationFailed.Log());
            }

            bool needFree = true;

            try
            {
                // Read node.
                Result rc = nodeStorage.Read(0, _nodeL1.GetBuffer());
                if (rc.IsFailure())
                {
                    return(rc);
                }

                // Verify node.
                rc = _nodeL1.GetHeader().Verify(0, nodeSize, sizeof(long));
                if (rc.IsFailure())
                {
                    return(rc);
                }

                // Validate offsets.
                int offsetCount            = GetOffsetCount(nodeSize);
                int entrySetCount          = GetEntrySetCount(nodeSize, entrySize, entryCount);
                BucketTreeNode <long> node = _nodeL1.GetNode <long>();

                long startOffset;
                if (offsetCount < entrySetCount && node.GetCount() < offsetCount)
                {
                    startOffset = node.GetL2BeginOffset();
                }
                else
                {
                    startOffset = node.GetBeginOffset();
                }

                long endOffset = node.GetEndOffset();

                if (startOffset < 0 || startOffset > node.GetBeginOffset() || startOffset >= endOffset)
                {
                    return(ResultFs.InvalidBucketTreeEntryOffset.Log());
                }

                NodeStorage   = nodeStorage;
                EntryStorage  = entryStorage;
                NodeSize      = nodeSize;
                EntrySize     = entrySize;
                OffsetCount   = offsetCount;
                EntrySetCount = entrySetCount;
                StartOffset   = startOffset;
                EndOffset     = endOffset;

                needFree = false;

                return(Result.Success);
            }
            finally
            {
                if (needFree)
                {
                    _nodeL1.Free();
                }
            }
        }
 public TruncatedSubStorage(SubStorage other) : base(other)
 {
 }
Exemple #7
0
 public SubStorage(SubStorage baseStorage, long offset, long length)
 {
     BaseStorage = baseStorage.BaseStorage;
     Offset      = baseStorage.Offset + offset;
     Length      = length;
 }