//The function which can implement ExportTreeToDirectory.
        private void CreateDirectoryFromTree(IndexingNode curNode, string curPath)
        {
            if (_root != null && curNode.Equals(_root))
            {
                foreach (var child in curNode.Children)
                {
                    CreateDirectoryFromTree(child, curPath);
                }
                return;
            }
            var objectPath    = Project.FindObjectPath(curNode.NameSHA256, curNode.ContentSHA256, _tempMode);
            var nextLayerPath = "";

            if (objectPath != null && curNode.ContentSHA256 != ObjectHelper.EmptyZeroes)
            {
                ObjectHelper.CreateFile(curPath, objectPath);
            }

            if (objectPath != null && curNode.ContentSHA256 == ObjectHelper.EmptyZeroes)
            {
                nextLayerPath = ObjectHelper.CreateDirectory(curPath, objectPath);
            }

            foreach (var child in curNode.Children)
            {
                CreateDirectoryFromTree(child, nextLayerPath);
            }
        }
        //The function which can implement ImportTreeFromDirectory.
        private void CreateTreeFromDirectory(string path, IndexingNode parent)
        {
            var directoryInfo = new DirectoryInfo(path);

            foreach (var f in directoryInfo.GetFiles())
            {
                var fileNameHash = HashHelper.HashString(f.Name);
                var contentHash  = HashHelper.HashFile(Path.Combine(path, f.Name));
                var subFileNode  = new IndexingNode(fileNameHash, contentHash, parent);
                _allNodes.Add(subFileNode);
                parent.AddChild(subFileNode);
                Project.CreateObject(f, fileNameHash, contentHash, _tempMode);
            }

            foreach (var d in directoryInfo.GetDirectories())
            {
                if (d.Name == Project.PrivateFolderName)
                {
                    // do not index directory of ourself
                    // TODO: handle nested LocalVersionControlSystem
                    continue;
                }

                var directoryNameHash = HashHelper.HashString(d.Name);
                var subDirectoryNode  = new IndexingNode(directoryNameHash, parent);
                _allNodes.Add(subDirectoryNode);
                parent.AddChild(subDirectoryNode);
                Project.CreateObject(d, directoryNameHash, _tempMode);
                CreateTreeFromDirectory(Path.Combine(path, d.Name), subDirectoryNode);
            }
        }
 //The function which can implement ExportTreeToIndexing.
 private void CreateIndexingFromTree(List <String> result, IndexingNode curNode, string pathHash)
 {
     result.Add(pathHash + "\\" + curNode.ToString());
     foreach (IndexingNode n in curNode.Children)
     {
         CreateIndexingFromTree(result, n, pathHash + "\\" + curNode.ToString());
     }
 }
        //The function which can implement ImportTreeFromIndexing.
        private void CreateTreeFromIndexing(string[] indexing, int curLine, IndexingNode parent, int curLayer)
        {
            while (curLine + 1 < indexing.Length)
            {
                //Update to the current line.
                curLine++;
                var line = indexing[curLine];

                //Get the node information.
                var layer = line.Length / 131;
                if (layer != 1)
                {
                    if (line.Substring((layer - 2) * 131, 131).Substring(1, 64) != parent.NameSHA256)
                    {
                        continue;
                    }
                }
                if (layer != curLayer + 1)
                {
                    continue;
                }
                line = line.Substring((layer - 1) * 131, 131);

                var nameHash    = line.Substring(1, 64);
                var contentHash = line.Substring(67, 64);

                if (contentHash == ObjectHelper.EmptyZeroes)
                {
                    var tempNode = new IndexingNode(nameHash, parent);
                    _allNodes.Add(tempNode);
                    parent.AddChild(tempNode);
                    CreateTreeFromIndexing(indexing, curLine, tempNode, layer);
                }
                else
                {
                    var tempNode = new IndexingNode(nameHash, contentHash, parent);
                    _allNodes.Add(tempNode);
                    parent.AddChild(tempNode);
                }
            }
        }
        //Step 1: Copy Tree 1 to result
        public static void CopyFromTree1(IndexingNode?resultTreeParent, IndexingNode originalTreeCurrent)
        {
            if (ResultTree == null)
            {
                throw new Exception("Uninitialized tree");
            }
            IndexingNode temp = new IndexingNode(originalTreeCurrent.NameSHA256, originalTreeCurrent.ContentSHA256, resultTreeParent);

            ResultIndexingNodes.Add(temp);

            if (resultTreeParent != null)
            {
                resultTreeParent.AddChild(temp);
            }
            else
            {
                ResultTree.SetRoot(temp);
            }
            foreach (IndexingNode node in originalTreeCurrent.Children)
            {
                CopyFromTree1(temp, node);
            }
        }
Exemplo n.º 6
0
        public bool Equals(IndexingNode node)
        {
            if (node == null)
            {
                return(false);
            }
            //If root
            IndexingNode?parent = node.GetParent();

            if (_parent == null)
            {
                return(parent == null);
            }
            else if (parent == null)
            {
                return(_parent == null);
            }
            else if (_parent.ToString().Equals(parent.ToString(), StringComparison.InvariantCultureIgnoreCase))
            {
                return(ToString().Equals(node.ToString(), StringComparison.InvariantCultureIgnoreCase));
            }
            return(false);
        }
        //Step 2: Insert node only in Tree 2 without conflicts
        public static void Insert(IndexingNode curNode, IndexingNode insertNode)
        {
            IndexingNode parent = insertNode.GetParent();

            if (parent == null)
            {
                throw new Exception();
            }
            if (curNode.NameSHA256.Equals(parent.NameSHA256, StringComparison.InvariantCulture))
            {
                IndexingNode temp = new IndexingNode(insertNode.NameSHA256, insertNode.ContentSHA256, curNode);
                foreach (IndexingNode n in curNode.Children)
                {
                    //Same name, different content
                    if (n.NameSHA256.Equals(insertNode.NameSHA256, StringComparison.InvariantCulture) &&
                        !n.ContentSHA256.Equals(insertNode.ContentSHA256, StringComparison.InvariantCulture))
                    {
                        UpdateNodeIn1.Add(n);
                        UpdateNodeIn2.Add(temp);
                        return;
                    }
                    if (n.NameSHA256.Equals(insertNode.NameSHA256, StringComparison.InvariantCulture) &&
                        n.ContentSHA256.Equals(insertNode.ContentSHA256, StringComparison.InvariantCulture))
                    {
                        return;
                    }
                }
                curNode.AddChild(temp);
                ResultIndexingNodes.Add(temp);
                return;
            }
            foreach (IndexingNode n in curNode.Children)
            {
                Insert(n, insertNode);
            }
        }
 public void SetRoot(IndexingNode root)
 {
     _root = root;
 }
Exemplo n.º 9
0
 public void AddChild(IndexingNode newChild)
 {
     _children.Add(newChild);
 }