Search in sources :

Example 31 with ReplyHeader

use of org.apache.zookeeper.proto.ReplyHeader in project zookeeper by apache.

the class ZooKeeper method setData.

/**
 * Set the data for the node of the given path if such a node exists and the
 * given version matches the version of the node (if the given version is
 * -1, it matches any node's versions). Return the stat of the node.
 * <p>
 * This operation, if successful, will trigger all the watches on the node
 * of the given path left by getData calls.
 * <p>
 * A KeeperException with error code KeeperException.NoNode will be thrown
 * if no node with the given path exists.
 * <p>
 * A KeeperException with error code KeeperException.BadVersion will be
 * thrown if the given version does not match the node's version.
 * <p>
 * The maximum allowable size of the data array is 1 MB (1,048,576 bytes).
 * Arrays larger than this will cause a KeeperException to be thrown.
 *
 * @param path
 *                the path of the node
 * @param data
 *                the data to set
 * @param version
 *                the expected matching version
 * @return the state of the node
 * @throws InterruptedException If the server transaction is interrupted.
 * @throws KeeperException If the server signals an error with a non-zero error code.
 * @throws IllegalArgumentException if an invalid path is specified
 */
public Stat setData(final String path, byte[] data, int version) throws KeeperException, InterruptedException {
    final String clientPath = path;
    PathUtils.validatePath(clientPath);
    final String serverPath = prependChroot(clientPath);
    RequestHeader h = new RequestHeader();
    h.setType(ZooDefs.OpCode.setData);
    SetDataRequest request = new SetDataRequest();
    request.setPath(serverPath);
    request.setData(data);
    request.setVersion(version);
    SetDataResponse response = new SetDataResponse();
    ReplyHeader r = cnxn.submitRequest(h, request, response, null);
    if (r.getErr() != 0) {
        throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath);
    }
    return response.getStat();
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) RequestHeader(org.apache.zookeeper.proto.RequestHeader) SetDataRequest(org.apache.zookeeper.proto.SetDataRequest) SetDataResponse(org.apache.zookeeper.proto.SetDataResponse)

Example 32 with ReplyHeader

use of org.apache.zookeeper.proto.ReplyHeader in project zookeeper by apache.

the class ZooKeeper method exists.

/**
 * Return the stat of the node of the given path. Return null if no such a
 * node exists.
 * <p>
 * If the watch is non-null and the call is successful (no exception is thrown),
 * a watch will be left on the node with the given path. The watch will be
 * triggered by a successful operation that creates/delete the node or sets
 * the data on the node.
 *
 * @param path the node path
 * @param watcher explicit watcher
 * @return the stat of the node of the given path; return null if no such a
 *         node exists.
 * @throws KeeperException If the server signals an error
 * @throws InterruptedException If the server transaction is interrupted.
 * @throws IllegalArgumentException if an invalid path is specified
 */
public Stat exists(final String path, Watcher watcher) throws KeeperException, InterruptedException {
    final String clientPath = path;
    PathUtils.validatePath(clientPath);
    // the watch contains the un-chroot path
    WatchRegistration wcb = null;
    if (watcher != null) {
        wcb = new ExistsWatchRegistration(watcher, clientPath);
    }
    final String serverPath = prependChroot(clientPath);
    RequestHeader h = new RequestHeader();
    h.setType(ZooDefs.OpCode.exists);
    ExistsRequest request = new ExistsRequest();
    request.setPath(serverPath);
    request.setWatch(watcher != null);
    SetDataResponse response = new SetDataResponse();
    ReplyHeader r = cnxn.submitRequest(h, request, response, wcb);
    if (r.getErr() != 0) {
        if (r.getErr() == KeeperException.Code.NONODE.intValue()) {
            return null;
        }
        throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath);
    }
    return response.getStat().getCzxid() == -1 ? null : response.getStat();
}
Also used : ExistsRequest(org.apache.zookeeper.proto.ExistsRequest) ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) RequestHeader(org.apache.zookeeper.proto.RequestHeader) SetDataResponse(org.apache.zookeeper.proto.SetDataResponse)

Example 33 with ReplyHeader

use of org.apache.zookeeper.proto.ReplyHeader in project zookeeper by apache.

the class ZooKeeper method getChildren.

/**
 * The asynchronous version of getChildren.
 *
 * @see #getChildren(String, Watcher)
 */
public void getChildren(final String path, Watcher watcher, ChildrenCallback cb, Object ctx) {
    final String clientPath = path;
    PathUtils.validatePath(clientPath);
    // the watch contains the un-chroot path
    WatchRegistration wcb = null;
    if (watcher != null) {
        wcb = new ChildWatchRegistration(watcher, clientPath);
    }
    final String serverPath = prependChroot(clientPath);
    RequestHeader h = new RequestHeader();
    h.setType(ZooDefs.OpCode.getChildren);
    GetChildrenRequest request = new GetChildrenRequest();
    request.setPath(serverPath);
    request.setWatch(watcher != null);
    GetChildrenResponse response = new GetChildrenResponse();
    cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, clientPath, serverPath, ctx, wcb);
}
Also used : GetChildrenRequest(org.apache.zookeeper.proto.GetChildrenRequest) ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) GetChildrenResponse(org.apache.zookeeper.proto.GetChildrenResponse) RequestHeader(org.apache.zookeeper.proto.RequestHeader)

Example 34 with ReplyHeader

use of org.apache.zookeeper.proto.ReplyHeader in project zookeeper by apache.

the class ZooKeeper method create.

/**
 * The asynchronous version of create.
 *
 * @see #create(String, byte[], List, CreateMode)
 */
public void create(final String path, byte[] data, List<ACL> acl, CreateMode createMode, StringCallback cb, Object ctx) {
    final String clientPath = path;
    PathUtils.validatePath(clientPath, createMode.isSequential());
    EphemeralType.validateTTL(createMode, -1);
    final String serverPath = prependChroot(clientPath);
    RequestHeader h = new RequestHeader();
    h.setType(createMode.isContainer() ? ZooDefs.OpCode.createContainer : ZooDefs.OpCode.create);
    CreateRequest request = new CreateRequest();
    CreateResponse response = new CreateResponse();
    ReplyHeader r = new ReplyHeader();
    request.setData(data);
    request.setFlags(createMode.toFlag());
    request.setPath(serverPath);
    request.setAcl(acl);
    cnxn.queuePacket(h, r, request, response, cb, clientPath, serverPath, ctx, null);
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) CreateRequest(org.apache.zookeeper.proto.CreateRequest) CreateResponse(org.apache.zookeeper.proto.CreateResponse) RequestHeader(org.apache.zookeeper.proto.RequestHeader)

Example 35 with ReplyHeader

use of org.apache.zookeeper.proto.ReplyHeader in project zookeeper by apache.

the class ZooKeeper method setACL.

/**
 * The asynchronous version of setACL.
 *
 * @see #setACL(String, List, int)
 */
public void setACL(final String path, List<ACL> acl, int version, StatCallback cb, Object ctx) {
    final String clientPath = path;
    PathUtils.validatePath(clientPath);
    final String serverPath = prependChroot(clientPath);
    RequestHeader h = new RequestHeader();
    h.setType(ZooDefs.OpCode.setACL);
    SetACLRequest request = new SetACLRequest();
    request.setPath(serverPath);
    request.setAcl(acl);
    request.setVersion(version);
    SetACLResponse response = new SetACLResponse();
    cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, clientPath, serverPath, ctx, null);
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) SetACLRequest(org.apache.zookeeper.proto.SetACLRequest) RequestHeader(org.apache.zookeeper.proto.RequestHeader) SetACLResponse(org.apache.zookeeper.proto.SetACLResponse)

Aggregations

ReplyHeader (org.apache.zookeeper.proto.ReplyHeader)40 RequestHeader (org.apache.zookeeper.proto.RequestHeader)34 GetDataResponse (org.apache.zookeeper.proto.GetDataResponse)7 Record (org.apache.jute.Record)6 GetDataRequest (org.apache.zookeeper.proto.GetDataRequest)5 SetDataResponse (org.apache.zookeeper.proto.SetDataResponse)5 IOException (java.io.IOException)4 CreateResponse (org.apache.zookeeper.proto.CreateResponse)4 ExistsRequest (org.apache.zookeeper.proto.ExistsRequest)4 AuthPacket (org.apache.zookeeper.proto.AuthPacket)3 Create2Response (org.apache.zookeeper.proto.Create2Response)3 GetACLRequest (org.apache.zookeeper.proto.GetACLRequest)3 GetACLResponse (org.apache.zookeeper.proto.GetACLResponse)3 GetChildren2Request (org.apache.zookeeper.proto.GetChildren2Request)3 GetChildrenRequest (org.apache.zookeeper.proto.GetChildrenRequest)3 GetChildrenResponse (org.apache.zookeeper.proto.GetChildrenResponse)3 SetACLResponse (org.apache.zookeeper.proto.SetACLResponse)3 KeeperException (org.apache.zookeeper.KeeperException)2 Code (org.apache.zookeeper.KeeperException.Code)2 ErrorResult (org.apache.zookeeper.OpResult.ErrorResult)2