ReadTree() public method

public ReadTree ( string treeName ) : Voron.Trees.Tree
treeName string
return Voron.Trees.Tree
Esempio n. 1
0
        public Searcher(FullTextIndex index)
        {
            _index = index;
            _tx = _index.StorageEnvironment.NewTransaction(TransactionFlags.Read);

            _docs = _tx.ReadTree("Docs");
        }
Esempio n. 2
0
		private static void SetCurrentTopologyInternal(Topology currentTopology, long index, Transaction tx)
		{
			if (currentTopology.TopologyId == Guid.Empty)
				throw new InvalidOperationException("Cannot set topology with an empty TopologyId");

			var metadata = tx.ReadTree(MetadataTreeName);

			var current = metadata.Read("current-topology");
			metadata.Add("previous-topology", current == null ? "{}" : current.Reader.ToStringValue());
			metadata.Add("current-topology", JsonConvert.SerializeObject(currentTopology));
			metadata.Add("current-topology-index", EndianBitConverter.Little.GetBytes(index));
		}
Esempio n. 3
0
		public static Tree GetTree(this StorageEnvironmentState state, Transaction tx, string treeName)
		{			
			if (String.IsNullOrEmpty(treeName))
				throw new InvalidOperationException("Cannot fetch tree with empty name");

            if (treeName.Equals(Constants.RootTreeName, StringComparison.InvariantCultureIgnoreCase))
                return state.Root;

            if (treeName.Equals(Constants.FreeSpaceTreeName, StringComparison.InvariantCultureIgnoreCase))
                return state.FreeSpaceRoot;

		    Tree tree = tx.ReadTree(treeName);

			if (tree != null)
				return tree;

			throw new InvalidOperationException("No such tree: " + treeName);			
		}
        public Tree GetTree(Transaction tx, string treeName)
        {
            if (String.IsNullOrEmpty(treeName))
                throw new InvalidOperationException("Cannot fetch tree with empty name");

            Tree tree = tx.ReadTree(treeName);

            if (tree != null)
                return tree;

            if (treeName.Equals(Constants.RootTreeName, StringComparison.InvariantCultureIgnoreCase))
                return Root;

            if (treeName.Equals(Constants.FreeSpaceTreeName, StringComparison.InvariantCultureIgnoreCase))
                return FreeSpaceRoot;

            if (tx.Flags == TransactionFlags.ReadWrite)
                return tx.Environment.CreateTree(tx, treeName, Options.ShouldUseKeyPrefix(treeName));

            throw new InvalidOperationException("No such tree: " + treeName);
        }
Esempio n. 5
0
        public unsafe Tree CreateTree(Transaction tx, string name, bool keysPrefixing = false)
        {
            if (tx.Flags == (TransactionFlags.ReadWrite) == false)
                throw new ArgumentException("Cannot create a new tree with a read only transaction");

            Tree tree = tx.ReadTree(name);
            if (tree != null)
                return tree;


	        if (name.Equals(Constants.RootTreeName, StringComparison.InvariantCultureIgnoreCase) ||
	            name.Equals(Constants.FreeSpaceTreeName, StringComparison.InvariantCultureIgnoreCase))
		        throw new InvalidOperationException("Cannot create a tree with reserved name: " + name);


            Slice key = (Slice)name;

            // we are in a write transaction, no need to handle locks
            var header = (TreeRootHeader*)tx.State.Root.DirectRead(key);
            if (header != null)
            {
                tree = Tree.Open(tx, header);
                tree.Name = name;
                tx.AddTree(name, tree);
                return tree;
            }

            tree = Tree.Create(tx, keysPrefixing);
            tree.Name = name;
            var space = tx.State.Root.DirectAdd(key, sizeof(TreeRootHeader));

            tree.State.CopyTo((TreeRootHeader*)space);
            tree.State.IsModified = true;
            tx.AddTree(name, tree);

			if(IsDebugRecording)
				DebugJournal.RecordWriteAction(DebugActionType.CreateTree, tx, Slice.Empty,name,Stream.Null);

            return tree;
        }
Esempio n. 6
0
	    public unsafe void RenameTree(Transaction tx, string fromName, string toName)
	    {
			if (tx.Flags == (TransactionFlags.ReadWrite) == false)
				throw new ArgumentException("Cannot rename a new tree with a read only transaction");

			if (toName.Equals(Constants.RootTreeName, StringComparison.InvariantCultureIgnoreCase) ||
				toName.Equals(Constants.FreeSpaceTreeName, StringComparison.InvariantCultureIgnoreCase))
				throw new InvalidOperationException("Cannot create a tree with reserved name: " + toName);

		    if (tx.ReadTree(toName) != null)
			    throw new ArgumentException("Cannot rename a tree with the name of an existing tree: " + toName);

		    Tree fromTree = tx.ReadTree(fromName);
		    if (fromTree == null)
			    throw new ArgumentException("Tree " + fromName + " does not exists");

            Slice key = (Slice)toName;

	        tx.State.Root.Delete((Slice)fromName);
			var ptr = tx.State.Root.DirectAdd(key, sizeof(TreeRootHeader));
		    fromTree.State.CopyTo((TreeRootHeader*) ptr);
		    fromTree.Name = toName;
		    fromTree.State.IsModified = true;
		    
			tx.RemoveTree(fromName);
			tx.RemoveTree(toName);

			tx.AddTree(toName, fromTree);

			if (IsDebugRecording)
                DebugJournal.RecordWriteAction(DebugActionType.RenameTree, tx, (Slice)toName, fromName, Stream.Null);
	    }
Esempio n. 7
0
	    public void DeleteTree(Transaction tx, string name)
        {
            if (tx.Flags == (TransactionFlags.ReadWrite) == false)
                throw new ArgumentException("Cannot create a new newRootTree with a read only transaction");

	        Tree tree = tx.ReadTree(name);
	        if (tree == null)
	            return;

            foreach (var page in tree.AllPages())
            {
                tx.FreePage(page);
            }

            tx.State.Root.Delete((Slice) name);

            tx.RemoveTree(name);
        }
Esempio n. 8
0
        private void ReplayWriteAction(WriteActivityEntry activityEntry, ref Transaction tx)
        {
            var tree = tx.ReadTree(activityEntry.TreeName);

            switch (activityEntry.ActionType)
            {
                case DebugActionType.Add:
                    tree.Add(activityEntry.Key, activityEntry.ValueStream);
                    break;
                case DebugActionType.Delete:
                    tree.Delete(activityEntry.Key);
                    break;
                case DebugActionType.MultiAdd:
                    tree.MultiAdd(activityEntry.Key, new Slice(Encoding.UTF8.GetBytes(activityEntry.Value.ToString())));
                    break;
                case DebugActionType.MultiDelete:
                    tree.MultiDelete(activityEntry.Key, new Slice(Encoding.UTF8.GetBytes(activityEntry.Value.ToString())));
                    break;
                case DebugActionType.CreateTree:
                    _env.CreateTree(tx, activityEntry.TreeName);
                    break;
                case DebugActionType.Increment:
                    var buffer = new byte[sizeof(long)];
                    activityEntry.ValueStream.Read(buffer, 0, buffer.Length);
                    var delta = EndianBitConverter.Little.ToInt64(buffer, 0);
                    tree.Increment(activityEntry.Key, delta);
                    break;
                case DebugActionType.AddStruct:
                    tree.Add(activityEntry.Key, activityEntry.ValueStream);
                    break;
                case DebugActionType.RenameTree:
                    _env.RenameTree(tx, activityEntry.TreeName, activityEntry.Key.ToString());
                    break;
                default: //precaution against newly added action types
                    throw new InvalidOperationException("unsupported tree action type: " + activityEntry.ActionType);
            }
        }
Esempio n. 9
0
        public StorageReport GenerateReport(Transaction tx, bool computeExactSizes = false)
        {
            var numberOfAllocatedPages = Math.Max(_dataPager.NumberOfAllocatedPages, NextPageNumber - 1); // async apply to data file task
            var numberOfFreePages = _freeSpaceHandling.AllPages(tx).Count;

            var trees = new List<Tree>();
            using (var rootIterator = tx.Root.Iterate())
            {
                if (rootIterator.Seek(Slice.BeforeAllKeys))
                {
                    do
                    {
                        var tree = tx.ReadTree(rootIterator.CurrentKey.ToString());
                        trees.Add(tree);

                    }
                    while (rootIterator.MoveNext());
                }
            }

            var generator = new StorageReportGenerator(tx);

            return generator.Generate(new ReportInput
            {
                NumberOfAllocatedPages = numberOfAllocatedPages,
                NumberOfFreePages = numberOfFreePages,
                NextPageNumber = NextPageNumber,
                Journals = Journal.Files.ToList(),
                Trees = trees,
                IsLightReport = !computeExactSizes
            });
        }
Esempio n. 10
0
        public unsafe Tree CreateTree(Transaction tx, string name, bool keysPrefixing = false)
        {
            Tree tree = tx.ReadTree(name);
            if (tree != null)
                return tree;

            if (name.Equals(Constants.RootTreeName, StringComparison.InvariantCultureIgnoreCase))
                return tx.Root;
            if (name.Equals(Constants.FreeSpaceTreeName, StringComparison.InvariantCultureIgnoreCase))
                return tx.FreeSpaceRoot;

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

            Slice key = name;

            tree = Tree.Create(tx, keysPrefixing);
            tree.Name = name;
            var space = tx.Root.DirectAdd(key, sizeof(TreeRootHeader));

            tree.State.CopyTo((TreeRootHeader*)space);
            tree.State.IsModified = true;
            tx.AddTree(name, tree);

            if(IsDebugRecording)
                DebugJournal.RecordWriteAction(DebugActionType.CreateTree, tx, Slice.Empty,name,Stream.Null);

            return tree;
        }
Esempio n. 11
0
		private static LogEntry GetLastLogEntryFromSnapshot(Transaction tx)
		{
			var metadata = tx.ReadTree(MetadataTreeName);
			// maybe there is a snapshot?
			var snapshotTerm = metadata.Read("snapshot-term");
			var snapshotIndex = metadata.Read("snapshot-index");

			if (snapshotIndex == null || snapshotTerm == null)
				return new LogEntry();

			return new LogEntry
			{
				Term = snapshotTerm.Reader.ReadLittleEndianInt64(),
				Index = snapshotIndex.Reader.ReadLittleEndianInt64()
			};
		}
Esempio n. 12
0
		private static long GetLastEntryNumber(Tree logs, Transaction tx)
		{
			long lastEntry;
			var lastKey = logs.LastKeyOrDefault();
			if (lastKey != null)
			{
				lastEntry = lastKey.CreateReader().ReadBigEndianInt64();
			}
			else
			{
				var metadata = tx.ReadTree(MetadataTreeName);
				// maybe there is a snapshot?
				var snapshotIndex = metadata.Read("snapshot-index");
				if (snapshotIndex != null)
					lastEntry = snapshotIndex.Reader.ReadLittleEndianInt64();
				else
					lastEntry = 0;
			}
			return lastEntry;
		}
Esempio n. 13
0
        public unsafe Tree CreateTree(Transaction tx, string name)
        {
            if (tx.Flags == (TransactionFlags.ReadWrite) == false)
                throw new ArgumentException("Cannot create a new tree with a read only transaction");

            Tree tree = tx.ReadTree(name);
            if (tree != null)
                return tree;

            Slice key = name;

            // we are in a write transaction, no need to handle locks
            var header = (TreeRootHeader*)tx.State.Root.DirectRead(key);
            if (header != null)
            {
                tree = Tree.Open(tx, _sliceComparer, header);
                tree.Name = name;
                tx.AddTree(name, tree);
                return tree;
            }

            tree = Tree.Create(tx, _sliceComparer);
            tree.Name = name;
            var space = tx.State.Root.DirectAdd(key, sizeof(TreeRootHeader));

            tree.State.CopyTo((TreeRootHeader*)space);
            tree.State.IsModified = true;
            tx.AddTree(name, tree);

			if(IsDebugRecording)
				DebugJournal.RecordAction(DebugActionType.CreateTree, Slice.Empty,name,Stream.Null);

            return tree;
        }
Esempio n. 14
0
 public static void RenderFreeSpace(Transaction tx)
 {
     RenderAndShow(tx, tx.ReadTree(tx.Environment.State.FreeSpaceRoot.Name).State.RootPageNumber, 1);
 }