Ejemplo n.º 1
0
        public Tree ReadTree(string treeName, RootObjectType type = RootObjectType.VariableSizeTree, bool isIndexTree = false, NewPageAllocator newPageAllocator = null)
        {
            Slice treeNameSlice;

            Slice.From(Allocator, treeName, ByteStringType.Immutable, out treeNameSlice);
            return(ReadTree(treeNameSlice, type, isIndexTree, newPageAllocator));
        }
Ejemplo n.º 2
0
        public Tree CreateTree(Slice name, RootObjectType type = RootObjectType.VariableSizeTree, TreeFlags flags = TreeFlags.None, bool isIndexTree = false, NewPageAllocator newPageAllocator = null)
        {
            Tree tree = ReadTree(name, type, isIndexTree, newPageAllocator);

            if (tree != null)
            {
                return(tree);
            }

            if (_lowLevelTransaction.Flags == TransactionFlags.ReadWrite == false)
            {
                throw new InvalidOperationException("No such tree: '" + name +
                                                    "' and cannot create trees in read transactions");
            }

            tree = Tree.Create(_lowLevelTransaction, this, name, flags);
            tree.State.RootObjectType = type;

            byte *ptr;

            using (_lowLevelTransaction.RootObjects.DirectAdd(name, sizeof(TreeRootHeader), out ptr))
                tree.State.CopyTo((TreeRootHeader *)ptr);

            tree.State.IsModified = true;
            AddTree(name, tree);

            return(tree);
        }
Ejemplo n.º 3
0
        public Tree ReadTree(string treeName, RootObjectType type = RootObjectType.VariableSizeTree, PageLocator pageLocator = null)
        {
            Slice treeNameSlice;

            Slice.From(Allocator, treeName, ByteStringType.Immutable, out treeNameSlice);
            return(ReadTree(treeNameSlice, type, pageLocator));
        }
Ejemplo n.º 4
0
        public Tree CreateTree(Slice name, RootObjectType type = RootObjectType.VariableSizeTree, TreeFlags flags = TreeFlags.None, PageLocator pageLocator = null)
        {
            Tree tree = ReadTree(name, type, pageLocator);

            if (tree != null)
            {
                return(tree);
            }

            if (_lowLevelTransaction.Flags == TransactionFlags.ReadWrite == false)
            {
                throw new InvalidOperationException("No such tree: '" + name +
                                                    "' and cannot create trees in read transactions");
            }

            tree      = Tree.Create(_lowLevelTransaction, this, flags, pageLocator: pageLocator);
            tree.Name = name;
            tree.State.RootObjectType = type;

            var space = (TreeRootHeader *)_lowLevelTransaction.RootObjects.DirectAdd(name, sizeof(TreeRootHeader));

            tree.State.CopyTo(space);

            tree.State.IsModified = true;
            AddTree(name, tree);

            return(tree);
        }
Ejemplo n.º 5
0
        public Tree CreateTree(string name, RootObjectType type = RootObjectType.VariableSizeTree, TreeFlags flags = TreeFlags.None, PageLocator pageLocator = null)
        {
            Slice treeNameSlice;

            Slice.From(Allocator, name, ByteStringType.Immutable, out treeNameSlice);
            return(CreateTree(treeNameSlice, type, flags, pageLocator));
        }
Ejemplo n.º 6
0
        private static void Report(RootObjectType objectType, string objectName, long globalProgress, long globalTotal, long objectProgress, long objectTotal, Action <CompactionProgress> progressReport)
        {
            if (progressReport == null)
            {
                return;
            }

            progressReport(new CompactionProgress
            {
                ObjectType     = objectType,
                ObjectName     = objectName,
                ObjectProgress = objectProgress,
                ObjectTotal    = objectTotal,
                GlobalProgress = globalProgress,
                GlobalTotal    = globalTotal
            });
        }
Ejemplo n.º 7
0
        public Tree ReadTree(Slice treeName, RootObjectType type = RootObjectType.VariableSizeTree, bool isIndexTree = false, NewPageAllocator newPageAllocator = null)
        {
            EnsureTrees();

            Tree tree;

            if (_trees.TryGetValue(treeName, out tree))
            {
                if (newPageAllocator == null)
                {
                    return(tree);
                }

                if (tree.HasNewPageAllocator == false)
                {
                    tree.SetNewPageAllocator(newPageAllocator);
                }

                return(tree);
            }

            TreeRootHeader *header = (TreeRootHeader *)_lowLevelTransaction.RootObjects.DirectRead(treeName);

            if (header != null)
            {
                if (header->RootObjectType != type)
                {
                    ThrowInvalidTreeType(treeName, type, header);
                }

                tree = Tree.Open(_lowLevelTransaction, this, treeName, header, type, isIndexTree, newPageAllocator);

                if ((tree.State.Flags & TreeFlags.LeafsCompressed) == TreeFlags.LeafsCompressed)
                {
                    tree.InitializeCompression();
                }

                _trees.Add(treeName, tree);

                return(tree);
            }

            _trees.Add(treeName, null);
            return(null);
        }
Ejemplo n.º 8
0
        private IEnumerable <string> GetTreeNames(Transaction tx, RootObjectType type)
        {
            using (var rootIterator = tx.LowLevelTransaction.RootObjects.Iterate(false))
            {
                if (rootIterator.Seek(Slices.BeforeAllKeys) == false)
                {
                    yield break;
                }

                do
                {
                    if (tx.GetRootObjectType(rootIterator.CurrentKey) != type)
                    {
                        continue;
                    }

                    yield return(rootIterator.CurrentKey.ToString());
                } while (rootIterator.MoveNext());
            }
        }
Ejemplo n.º 9
0
        public Tree ReadTree(Slice treeName, RootObjectType type = RootObjectType.VariableSizeTree, PageLocator pageLocator = null)
        {
            EnsureTrees();

            Tree tree;

            if (_trees.TryGetValue(treeName, out tree))
            {
                return(tree);
            }

            TreeRootHeader *header = (TreeRootHeader *)_lowLevelTransaction.RootObjects.DirectRead(treeName);

            if (header != null)
            {
                if (header->RootObjectType != type)
                {
                    throw new InvalidOperationException($"Tried to open {treeName} as a {type}, but it is actually a " + header->RootObjectType);
                }

                tree      = Tree.Open(_lowLevelTransaction, this, header, type, pageLocator);
                tree.Name = treeName;

                if ((tree.State.Flags & TreeFlags.LeafsCompressed) == TreeFlags.LeafsCompressed)
                {
                    tree.InitializeCompression();
                }

                _trees.Add(treeName, tree);

                return(tree);
            }

            _trees.Add(treeName, null);
            return(null);
        }
Ejemplo n.º 10
0
 public static Tree Open(LowLevelTransaction llt, Transaction tx, TreeRootHeader *header, RootObjectType type = RootObjectType.VariableSizeTree, PageLocator pageLocator = null)
 {
     return(new Tree(llt, tx, header->RootPageNumber, pageLocator)
     {
         _state =
         {
             RootObjectType  = type,
             PageCount       = header->PageCount,
             BranchPages     = header->BranchPages,
             Depth           = header->Depth,
             OverflowPages   = header->OverflowPages,
             LeafPages       = header->LeafPages,
             NumberOfEntries = header->NumberOfEntries,
             Flags           = header->Flags
         }
     });
 }
Ejemplo n.º 11
0
 public IEnumerable <RootObjectState <TType, TState> > GetRoots <TType, TState>(RootObjectType <TType, TState> type)
     where TType : RootObjectType <TType, TState>, new()
 {
     return(GetRoots((ObjectType)type).Cast <RootObjectState <TType, TState> >());
 }
Ejemplo n.º 12
0
        private static long CopyFixedSizeTrees(StorageEnvironment compactedEnv, Action <CompactionProgress> progressReport, Transaction txr,
                                               TreeIterator rootIterator, string treeName, long copiedTrees, long totalTreesCount, RootObjectType type, TransactionPersistentContext context)
        {
            var fst = txr.FixedTreeFor(rootIterator.CurrentKey.Clone(txr.Allocator), 0);

            Report(type, treeName, copiedTrees, totalTreesCount, 0, fst.NumberOfEntries, progressReport);

            using (var it = fst.Iterate())
            {
                var copiedEntries = 0L;
                if (it.Seek(Int64.MinValue) == false)
                {
                    return(copiedTrees);
                }

                do
                {
                    using (var txw = compactedEnv.WriteTransaction(context))
                    {
                        var snd             = txw.FixedTreeFor(rootIterator.CurrentKey.Clone(txr.Allocator));
                        var transactionSize = 0L;
                        do
                        {
                            Slice val;
                            using (it.Value(out val))
                                snd.Add(it.CurrentKey, val);
                            transactionSize += fst.ValueSize + sizeof(long);
                            copiedEntries++;
                        } while (transactionSize < compactedEnv.Options.MaxScratchBufferSize / 2 && it.MoveNext());

                        txw.Commit();
                    }
                    if (fst.NumberOfEntries == copiedEntries)
                    {
                        copiedTrees++;
                    }

                    Report(type, treeName, copiedTrees, totalTreesCount, copiedEntries, fst.NumberOfEntries, progressReport);
                    compactedEnv.FlushLogToDataFile();
                } while (it.MoveNext());
            }
            return(copiedTrees);
        }
Ejemplo n.º 13
0
 public Tree CreateTree(string name, RootObjectType type = RootObjectType.VariableSizeTree, TreeFlags flags = TreeFlags.None, bool isIndexTree = false, NewPageAllocator newPageAllocator = null)
 {
     Slice.From(Allocator, name, ByteStringType.Immutable, out var treeNameSlice);
     return(CreateTree(treeNameSlice, type, flags, isIndexTree, newPageAllocator));
 }
Ejemplo n.º 14
0
        public static Tree Create(LowLevelTransaction llt, Transaction tx, Slice name, TreeFlags flags = TreeFlags.None, RootObjectType type = RootObjectType.VariableSizeTree,
                                  bool isIndexTree = false, NewPageAllocator newPageAllocator = null)
        {
            if (type != RootObjectType.VariableSizeTree && type != RootObjectType.Table)
            {
                ThrowInvalidTreeCreateType();
            }

            var newPage = newPageAllocator?.AllocateSinglePage(0) ?? llt.AllocatePage(1);

            TreePage newRootPage = PrepareTreePage(TreePageFlags.Leaf, 1, newPage);

            var tree = new Tree(llt, tx, newRootPage.PageNumber, name, isIndexTree, newPageAllocator)
            {
                _state =
                {
                    RootObjectType = type,
                    Depth          =     1,
                    Flags          = flags,
                }
            };

            if ((flags & TreeFlags.LeafsCompressed) == TreeFlags.LeafsCompressed)
            {
                tree.InitializeCompression();
            }

            tree.State.RecordNewPage(newRootPage, 1);
            return(tree);
        }
Ejemplo n.º 15
0
 private static void ThrowInvalidTreeType(Slice treeName, RootObjectType type, TreeRootHeader *header)
 {
     throw new InvalidOperationException($"Tried to open {treeName} as a {type}, but it is actually a " +
                                         header->RootObjectType);
 }
Ejemplo n.º 16
0
 internal Internal(Identifier identifier)
     : base(identifier)
 {
     RootObjectType <TType, TState> .CheckType(identifier);
 }
Ejemplo n.º 17
0
        public static Tree Open(LowLevelTransaction llt, Transaction tx, Slice name, TreeRootHeader *header, RootObjectType type = RootObjectType.VariableSizeTree,
                                bool isIndexTree = false, NewPageAllocator newPageAllocator = null)
        {
            var tree = new Tree(llt, tx, header->RootPageNumber, name, isIndexTree, newPageAllocator)
            {
                _state =
                {
                    RootObjectType  = type,
                    PageCount       = header->PageCount,
                    BranchPages     = header->BranchPages,
                    Depth           = header->Depth,
                    OverflowPages   = header->OverflowPages,
                    LeafPages       = header->LeafPages,
                    NumberOfEntries = header->NumberOfEntries,
                    Flags           = header->Flags
                }
            };

            if ((tree.State.Flags & TreeFlags.LeafsCompressed) == TreeFlags.LeafsCompressed)
            {
                tree.InitializeCompression();
            }

            return(tree);
        }
Ejemplo n.º 18
0
 public override bool ShouldSnapshot(Slice slice, RootObjectType type)
 {
     return(slice.ToString() == "values");
 }
Ejemplo n.º 19
0
        private static long CopyFixedSizeTrees(StorageEnvironment compactedEnv, Action <CompactionProgress> progressReport, Transaction txr,
                                               TreeIterator rootIterator, string treeName, long copiedTrees, long totalTreesCount, RootObjectType type, TransactionPersistentContext context, CancellationToken token)
        {
            var treeNameSlice = rootIterator.CurrentKey.Clone(txr.Allocator);

            var header = (FixedSizeTreeHeader.Embedded *)txr.LowLevelTransaction.RootObjects.DirectRead(treeNameSlice);

            var fst = txr.FixedTreeFor(treeNameSlice, header->ValueSize);

            Report(type, treeName, copiedTrees, totalTreesCount, 0, fst.NumberOfEntries, progressReport, "Copying fixed size trees");

            using (var it = fst.Iterate())
            {
                var copiedEntries = 0L;
                if (it.Seek(Int64.MinValue) == false)
                {
                    return(copiedTrees);
                }

                do
                {
                    token.ThrowIfCancellationRequested();
                    using (var txw = compactedEnv.WriteTransaction(context))
                    {
                        var snd             = txw.FixedTreeFor(treeNameSlice, header->ValueSize);
                        var transactionSize = 0L;
                        do
                        {
                            token.ThrowIfCancellationRequested();

                            Slice val;
                            using (it.Value(out val))
                                snd.Add(it.CurrentKey, val);
                            transactionSize += fst.ValueSize + sizeof(long);
                            copiedEntries++;
                        } while (transactionSize < compactedEnv.Options.MaxScratchBufferSize / 2 && it.MoveNext());

                        txw.Commit();
                    }
                    if (fst.NumberOfEntries == copiedEntries)
                    {
                        copiedTrees++;
                    }

                    Report(type, treeName, copiedTrees, totalTreesCount, copiedEntries, fst.NumberOfEntries, progressReport);
                    compactedEnv.FlushLogToDataFile();
                } while (it.MoveNext());
            }
            return(copiedTrees);
        }
Ejemplo n.º 20
0
 public abstract bool ShouldSnapshot(Slice slice, RootObjectType type);
Ejemplo n.º 21
0
        public static Tree Create(LowLevelTransaction llt, Transaction tx, TreeFlags flags = TreeFlags.None, RootObjectType type = RootObjectType.VariableSizeTree, PageLocator pageLocator = null)
        {
            if (type != RootObjectType.VariableSizeTree && type != RootObjectType.Table)
            {
                throw new ArgumentException($"Only valid types are {nameof(RootObjectType.VariableSizeTree)} or {nameof(RootObjectType.Table)}.", nameof(type));
            }

            var newRootPage = AllocateNewPage(llt, TreePageFlags.Leaf, 1);
            var tree        = new Tree(llt, tx, newRootPage.PageNumber, pageLocator)
            {
                _state =
                {
                    RootObjectType = type,
                    Depth          =     1,
                    Flags          = flags,
                }
            };

            if ((flags & TreeFlags.LeafsCompressed) == TreeFlags.LeafsCompressed)
            {
                tree.InitializeCompression();
            }

            tree.State.RecordNewPage(newRootPage, 1);
            return(tree);
        }