Esempio n. 1
0
        public async Task <string> CreateAsync(string path, byte[] data, List <ACL> acl, CreateMode createMode)
        {
            string currentPath    = string.Empty;
            var    lastCreatePath = string.Empty;
            var    pathList       = path.Split('/');

            for (int i = 0; i < pathList.Length; i++)
            {
                if (!currentPath.EndsWith('/'))
                {
                    currentPath += "/";
                }
                currentPath += pathList[i];
                if (i > 0 && i < pathList.Length)
                {
                    var isExists = await ExistsAsync(currentPath);

                    if (!isExists)
                    {
                        var createdPath = await ZK.createAsync(currentPath, path == currentPath?data : null, acl, createMode);

                        if (!string.IsNullOrWhiteSpace(createdPath))
                        {
                            lastCreatePath = createdPath;
                        }
                    }
                }
            }

            return(lastCreatePath);
        }
        // 关闭ZooKeeper连接
        // 释放资源
        public async Task Close()
        {
            if (this.ZK != null)
            {
                await ZK.closeAsync();
            }

            this.ZK = null;
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sourcePath"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task SetDataAsync(string sourcePath, byte[] data)
        {
            var exists = await ExistsAsync(sourcePath);

            if (exists)
            {
                await ZK.setDataAsync(sourcePath, data);
            }
        }
Esempio n. 4
0
        public async Task <bool> DeleteAsync(string sourcePath)
        {
            var exists = await ExistsAsync(sourcePath);

            if (exists)
            {
                await ZK.deleteAsync(sourcePath);
            }
            return(true);
        }
Esempio n. 5
0
        public async Task <byte[]> GetDataAsync(string sourcePath, bool watch = false)
        {
            var exists = await ExistsAsync(sourcePath);

            if (exists)
            {
                var dataResult = await ZK.getDataAsync(sourcePath, watch : watch);

                return(dataResult.Data);
            }
            return(null);
        }
Esempio n. 6
0
        public async Task <List <string> > GetChlidrenListAsync(string sourcePath, bool watch = false)
        {
            var isExists = await ExistsAsync(sourcePath, watch : watch);

            if (!isExists)
            {
                return(new List <string>());
            }
            var childrenResult = await ZK.getChildrenAsync(sourcePath, watch : watch);

            return(childrenResult.Children);
        }
        // 读取节点的配置数据
        public async Task <string> ReadConfigDataAsync()
        {
            if (this.ZK == null)
            {
                return(string.Empty);
            }

            var stat = await ZK.existsAsync(QueryPath, true);

            if (stat == null)
            {
                return(string.Empty);
            }

            this.Stat = stat;

            var dataResult = await ZK.getDataAsync(QueryPath, true);

            return(Encoding.UTF8.GetString(dataResult.Data));
        }
Esempio n. 8
0
        public async Task <bool> ExistsAsync(string path, bool watch = false)
        {
            var state = await ZK.existsAsync(path, watch : watch);

            return(state != null);
        }
Esempio n. 9
0
 public Task CloseAsync()
 {
     return(ZK?.closeAsync());
 }