コード例 #1
0
        public async Task <BizResult <bool> > CreateZNode(string path, bool isParent, ZNode node)
        {
            if (node.Key.IndexOf('/') > 0 || node.Key.Contains("$$"))
            {
                return(new BizResult <bool>(false, -1, "Key不能包含 '/' 或者 '$$' 符号!"));
            }

            node.Key = node.Key.ToLower();
            var paths = path.Split('/', StringSplitOptions.RemoveEmptyEntries);

            if (paths.Count() > 1)
            {
                var existParentPath = string.Empty;
                foreach (var p in paths.Take(paths.Count() - 1))
                {
                    existParentPath += "/" + p;
                    if (await _zookeeperClient.ExistsAsync($"{existParentPath}/{node.Key}"))
                    {
                        return(new BizResult <bool>(false, -1, $"路径 {existParentPath} 下已存在相同名称的节点!"));
                    }
                }
            }

            var zpath = path + "/" + (isParent ? "$$" + node.Key : node.Key);

            if (await _zookeeperClient.ExistsAsync(zpath))
            {
                return(new BizResult <bool>(false, -1, $"Path:'{zpath.Replace("$$", "")}'节点已存在!"));
            }

            await _zookeeperClient.CreatePersistentAsync(zpath, node);

            return(new BizResult <bool>(true));
        }