Exemplo n.º 1
0
        /// <summary>
        /// </summary>
        /// <param name="Path"></param>
        public VirtualFileSystemNode InsertNode(string Path, DateTime CreateTime, object Metadata = null)
        {
            VirtualFileSystemNode ParentNode = Root;

            string ParentPath = GetParentPath(Path);

            if (ParentPath.Length > 0)
            {
                ParentNode = InsertNode(ParentPath, CreateTime);
            }

            string NodeName             = GetNodeName(Path);
            VirtualFileSystemNode Child = ParentNode.GetChildByName(NodeName);

            if (Child == null)
            {
                Child = new VirtualFileSystemNode(NodeName, Path, CreateTime, ParentNode, Metadata);
                ParentNode.Children.Add(Child);
                ParentNode.SortChildren();

                OnNodeAdded?.Invoke(this, Child);
            }
            else
            {
                Child.Metadata = Metadata;
            }

            return(Child);
        }
Exemplo n.º 2
0
        /// <summary>
        /// </summary>
        /// <returns></returns>
        public VirtualFileSystemNode GetNodeByPath(string Path)
        {
            if (Path.Length == 0)
            {
                RefreshNodeChildren(Root);
                return(Root);
            }

            VirtualFileSystemNode ParentNode = Root;

            string ParentPath = GetParentPath(Path);

            if (ParentPath.Length > 0)
            {
                ParentNode = GetNodeByPath(ParentPath);
            }

            if (ParentNode == null)
            {
                return(null);
            }

            RefreshNodeChildren(ParentNode);

            string NodeName = GetNodeName(Path);

            return(ParentNode.GetChildByName(NodeName));
        }
Exemplo n.º 3
0
 /// <summary>
 /// </summary>
 /// <param name="Naem"></param>
 public VirtualFileSystemNode(string InName, string InPath, DateTime InCreateTime, VirtualFileSystemNode InParent = null, object InMetadata = null)
 {
     Name       = InName;
     Path       = InPath;
     Parent     = InParent;
     Metadata   = InMetadata;
     CreateTime = InCreateTime;
 }
Exemplo n.º 4
0
        /// <summary>
        /// </summary>
        /// <param name="Node"></param>
        private void RefreshNodeChildren(VirtualFileSystemNode Node)
        {
            ulong ElapsedTime = TimeUtils.Ticks - Node.LastChildRefreshTime;

            if (Node.LastChildRefreshTime == 0 || ElapsedTime > ChildrenRefreshInterval && AutoRefreshChildren)
            {
                OnRequestChildren?.Invoke(this, Node.Path);
                Node.LastChildRefreshTime = TimeUtils.Ticks;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// </summary>
        /// <param name="Path"></param>
        public void RemoveNode(string Path)
        {
            VirtualFileSystemNode Node = GetNodeByPath(Path);

            if (Node != null && Node.Parent != null)
            {
                OnNodeRemoved?.Invoke(this, Node);
                Node.Parent.Children.Remove(Node);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// </summary>
        public void ForceRefresh(VirtualFileSystemNode Node = null)
        {
            if (Node == null)
            {
                Node = Root;
            }

            Node.LastChildRefreshTime = 0;
            foreach (VirtualFileSystemNode Child in Node.Children)
            {
                ForceRefresh(Child);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// </summary>
        /// <param name="Path"></param>
        public void RemoveChildNodes(string Path)
        {
            VirtualFileSystemNode Node = GetNodeByPath(Path);

            if (Node != null)
            {
                foreach (VirtualFileSystemNode Child in Node.Children)
                {
                    OnNodeRemoved?.Invoke(this, Child);
                }

                Node.Children.Clear();
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// </summary>
        /// <param name="Path"></param>
        public List <string> GetChildrenNames(string Path)
        {
            List <string> Result = new List <string>();

            VirtualFileSystemNode Node = GetNodeByPath(Path);

            if (Node != null)
            {
                RefreshNodeChildren(Node);

                foreach (VirtualFileSystemNode Child in Node.Children)
                {
                    Result.Add(Child.Path);
                }
            }

            return(Result);
        }
Exemplo n.º 9
0
        /// <summary>
        /// </summary>
        /// <param name="Path"></param>
        /// <param name="Children"></param>
        public void ReconcileChildren(string Path, List <VirtualFileSystemInsertChild> Children)
        {
            VirtualFileSystemNode BaseNode = GetNodeByPath(Path);

            if (BaseNode == null)
            {
                BaseNode = InsertNode(Path, DateTime.UtcNow);
            }

            List <VirtualFileSystemNode>        ChildrenToRemove = new List <VirtualFileSystemNode>();
            List <VirtualFileSystemInsertChild> ChildrenToAdd    = new List <VirtualFileSystemInsertChild>();

            // Find children to remove.
            foreach (VirtualFileSystemNode Child in BaseNode.Children)
            {
                bool Exists = false;

                foreach (VirtualFileSystemInsertChild NewChild in Children)
                {
                    if (Child.Path == NewChild.VirtualPath)
                    {
                        Exists = true;
                        break;
                    }
                }

                if (!Exists)
                {
                    ChildrenToRemove.Add(Child);
                }
            }

            // Find children to add.
            foreach (VirtualFileSystemInsertChild NewChild in Children)
            {
                bool Exists = false;

                foreach (VirtualFileSystemNode Child in BaseNode.Children)
                {
                    if (Child.Path == NewChild.VirtualPath)
                    {
                        // Update metadata while we are here in case it changed.
                        Child.Metadata   = NewChild.Metadata;
                        Child.CreateTime = NewChild.CreateTime;

                        OnNodeUpdated?.Invoke(this, Child);

                        Exists = true;
                        break;
                    }
                }

                if (!Exists)
                {
                    ChildrenToAdd.Add(NewChild);
                }
            }

            // Remove children.
            foreach (VirtualFileSystemNode Child in ChildrenToRemove)
            {
                BaseNode.Children.Remove(Child);
            }

            // Add children.
            List <VirtualFileSystemNode> NewChildren = new List <VirtualFileSystemNode>();

            foreach (VirtualFileSystemInsertChild NewChild in ChildrenToAdd)
            {
                string NodeName = GetNodeName(NewChild.VirtualPath);

                VirtualFileSystemNode Child = new VirtualFileSystemNode(NodeName, NewChild.VirtualPath, NewChild.CreateTime, BaseNode, NewChild.Metadata);
                BaseNode.Children.Add(Child);
                NewChildren.Add(Child);
            }

            /*
             *
             * foreach (VirtualFileSystemNode Child in ChildrenToRemove)
             * {
             *  Console.WriteLine("PreSort: Remove: {0}", Child.Path);
             * }
             *
             * foreach (VirtualFileSystemNode Child in NewChildren)
             * {
             *  Console.WriteLine("PreSort: Add: {0}", Child.Path);
             * }*/

            // Sort all children by create time.
            BaseNode.Children.Sort((Item1, Item2) => - Item1.CreateTime.CompareTo(Item2.CreateTime));

            // Fire events.
            foreach (VirtualFileSystemNode Child in ChildrenToRemove)
            {
                //Console.WriteLine("PostSort: Remove: {0}", Child.Path);
                OnNodeRemoved?.Invoke(this, Child);
            }

            foreach (VirtualFileSystemNode Child in NewChildren)
            {
                //Console.WriteLine("PostSort: Add: {0}", Child.Path);
                OnNodeAdded?.Invoke(this, Child);
            }
        }
Exemplo n.º 10
0
 public VirtualFileSystem()
 {
     Root = new VirtualFileSystemNode("", "", DateTime.UtcNow);
     RefreshNodeChildren(Root);
 }