コード例 #1
0
ファイル: ZooKeeper.cs プロジェクト: wuyou201400/DotnetSpider
        /// <summary>
        /// Create a node with the given path. The node data will be the given data,
        /// and node acl will be the given acl.
        ///
        /// The flags argument specifies whether the created node will be ephemeral
        /// or not.
        ///
        /// An ephemeral node will be removed by the ZooKeeper automatically when the
        /// session associated with the creation of the node expires.
        ///
        /// The flags argument can also specify to create a sequential node. The
        /// actual path name of a sequential node will be the given path plus a
        /// suffix "i" where i is the current sequential number of the node. The sequence
        /// number is always fixed length of 10 digits, 0 padded. Once
        /// such a node is created, the sequential number will be incremented by one.
        ///
        /// If a node with the same actual path already exists in the ZooKeeper, a
        /// KeeperException with error code KeeperException.NodeExists will be
        /// thrown. Note that since a different actual path is used for each
        /// invocation of creating sequential node with the same path argument, the
        /// call will never throw "file exists" KeeperException.
        ///
        /// If the parent node does not exist in the ZooKeeper, a KeeperException
        /// with error code KeeperException.NoNode will be thrown.
        ///
        /// An ephemeral node cannot have children. If the parent node of the given
        /// path is ephemeral, a KeeperException with error code
        /// KeeperException.NoChildrenForEphemerals will be thrown.
        ///
        /// This operation, if successful, will trigger all the watches left on the
        /// node of the given path by exists and getData API calls, and the watches
        /// left on the parent node by getChildren API calls.
        ///
        /// If a node is created successfully, the ZooKeeper server will trigger the
        /// watches on the path left by exists calls, and the watches on the parent
        /// of the node by getChildren calls.
        ///
        /// The maximum allowable size of the data array is 1 MB (1,048,576 bytes).
        /// Arrays larger than this will cause a KeeperExecption to be thrown.
        /// </summary>
        /// <param name="path">The path for the node.</param>
        /// <param name="data">The data for the node.</param>
        /// <param name="acl">The acl for the node.</param>
        /// <param name="createMode">specifying whether the node to be created is ephemeral and/or sequential.</param>
        /// <returns></returns>
        public string Create(string path, byte[] data, IEnumerable <ACL> acl, CreateMode createMode)
        {
            string clientPath = path;

            PathUtils.ValidatePath(clientPath, createMode.Sequential);
            if (acl != null && acl.Count() == 0)
            {
                throw new KeeperException.InvalidACLException();
            }

            string serverPath = PrependChroot(clientPath);

            RequestHeader h = new RequestHeader();

            h.Type = (int)OpCode.Create;
            CreateRequest  request  = new CreateRequest(serverPath, data, acl, createMode.Flag);
            CreateResponse response = new CreateResponse();
            var            rst      = response.ToString();
            ReplyHeader    r        = cnxn.SubmitRequest(h, request, response, null);

            if (r.Err != 0)
            {
                throw KeeperException.Create((KeeperException.Code)Enum.ToObject(typeof(KeeperException.Code), r.Err), clientPath);
            }
            return(cnxn.ChrootPath == null ? response.Path : response.Path.Substring(cnxn.ChrootPath.Length));
        }