Search in sources :

Example 1 with GetDataResponse

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

the class ZooKeeper method getData.

/**
 * Return the data and the stat of the node of the given path.
 * <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 sets data on the node, or
 * deletes the node.
 * <p>
 * A KeeperException with error code KeeperException.NoNode will be thrown
 * if no node with the given path exists.
 *
 * @param path the given path
 * @param watcher explicit watcher
 * @param stat the stat of the node
 * @return the data of the node
 * @throws KeeperException If the server signals an error with a non-zero error code
 * @throws InterruptedException If the server transaction is interrupted.
 * @throws IllegalArgumentException if an invalid path is specified
 */
public byte[] getData(final String path, Watcher watcher, Stat stat) 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 DataWatchRegistration(watcher, clientPath);
    }
    final String serverPath = prependChroot(clientPath);
    RequestHeader h = new RequestHeader();
    h.setType(ZooDefs.OpCode.getData);
    GetDataRequest request = new GetDataRequest();
    request.setPath(serverPath);
    request.setWatch(watcher != null);
    GetDataResponse response = new GetDataResponse();
    ReplyHeader r = cnxn.submitRequest(h, request, response, wcb);
    if (r.getErr() != 0) {
        throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath);
    }
    if (stat != null) {
        DataTree.copyStat(response.getStat(), stat);
    }
    return response.getData();
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) RequestHeader(org.apache.zookeeper.proto.RequestHeader) GetDataResponse(org.apache.zookeeper.proto.GetDataResponse) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest)

Example 2 with GetDataResponse

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

the class ZooKeeper method getData.

/**
 * The asynchronous version of getData.
 *
 * @see #getData(String, Watcher, Stat)
 */
public void getData(final String path, Watcher watcher, DataCallback 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 DataWatchRegistration(watcher, clientPath);
    }
    final String serverPath = prependChroot(clientPath);
    RequestHeader h = new RequestHeader();
    h.setType(ZooDefs.OpCode.getData);
    GetDataRequest request = new GetDataRequest();
    request.setPath(serverPath);
    request.setWatch(watcher != null);
    GetDataResponse response = new GetDataResponse();
    cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, clientPath, serverPath, ctx, wcb);
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) RequestHeader(org.apache.zookeeper.proto.RequestHeader) GetDataResponse(org.apache.zookeeper.proto.GetDataResponse) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest)

Example 3 with GetDataResponse

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

the class ZooKeeper method getConfig.

/**
 * Return the last committed configuration (as known to the server to which the client is connected)
 * and the stat of the configuration.
 * <p>
 * If the watch is non-null and the call is successful (no exception is
 * thrown), a watch will be left on the configuration node (ZooDefs.CONFIG_NODE). The watch
 * will be triggered by a successful reconfig operation
 * <p>
 * A KeeperException with error code KeeperException.NoNode will be thrown
 * if the configuration node doesn't exists.
 *
 * @param watcher explicit watcher
 * @param stat the stat of the configuration node ZooDefs.CONFIG_NODE
 * @return configuration data stored in ZooDefs.CONFIG_NODE
 * @throws KeeperException If the server signals an error with a non-zero error code
 * @throws InterruptedException If the server transaction is interrupted.
 */
public byte[] getConfig(Watcher watcher, Stat stat) throws KeeperException, InterruptedException {
    final String configZnode = ZooDefs.CONFIG_NODE;
    // the watch contains the un-chroot path
    WatchRegistration wcb = null;
    if (watcher != null) {
        wcb = new DataWatchRegistration(watcher, configZnode);
    }
    RequestHeader h = new RequestHeader();
    h.setType(ZooDefs.OpCode.getData);
    GetDataRequest request = new GetDataRequest();
    request.setPath(configZnode);
    request.setWatch(watcher != null);
    GetDataResponse response = new GetDataResponse();
    ReplyHeader r = cnxn.submitRequest(h, request, response, wcb);
    if (r.getErr() != 0) {
        throw KeeperException.create(KeeperException.Code.get(r.getErr()), configZnode);
    }
    if (stat != null) {
        DataTree.copyStat(response.getStat(), stat);
    }
    return response.getData();
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) RequestHeader(org.apache.zookeeper.proto.RequestHeader) GetDataResponse(org.apache.zookeeper.proto.GetDataResponse) GetDataRequest(org.apache.zookeeper.proto.GetDataRequest)

Example 4 with GetDataResponse

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

the class ZooKeeperAdmin method reconfigure.

/**
 * The Asynchronous version of reconfig.
 *
 * @see #reconfigure
 */
public void reconfigure(String joiningServers, String leavingServers, String newMembers, long fromConfig, DataCallback cb, Object ctx) {
    RequestHeader h = new RequestHeader();
    h.setType(ZooDefs.OpCode.reconfig);
    ReconfigRequest request = new ReconfigRequest(joiningServers, leavingServers, newMembers, fromConfig);
    GetDataResponse response = new GetDataResponse();
    cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, ZooDefs.CONFIG_NODE, ZooDefs.CONFIG_NODE, ctx, null);
}
Also used : ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) RequestHeader(org.apache.zookeeper.proto.RequestHeader) GetDataResponse(org.apache.zookeeper.proto.GetDataResponse) ReconfigRequest(org.apache.zookeeper.proto.ReconfigRequest)

Example 5 with GetDataResponse

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

the class MultiResponse method deserialize.

@Override
public void deserialize(InputArchive archive, String tag) throws IOException {
    results = new ArrayList<OpResult>();
    archive.startRecord(tag);
    MultiHeader h = new MultiHeader();
    h.deserialize(archive, tag);
    while (!h.getDone()) {
        switch(h.getType()) {
            case ZooDefs.OpCode.create:
                CreateResponse cr = new CreateResponse();
                cr.deserialize(archive, tag);
                results.add(new OpResult.CreateResult(cr.getPath()));
                break;
            case ZooDefs.OpCode.create2:
                Create2Response cr2 = new Create2Response();
                cr2.deserialize(archive, tag);
                results.add(new OpResult.CreateResult(cr2.getPath(), cr2.getStat()));
                break;
            case ZooDefs.OpCode.delete:
                results.add(new OpResult.DeleteResult());
                break;
            case ZooDefs.OpCode.setData:
                SetDataResponse sdr = new SetDataResponse();
                sdr.deserialize(archive, tag);
                results.add(new OpResult.SetDataResult(sdr.getStat()));
                break;
            case ZooDefs.OpCode.check:
                results.add(new OpResult.CheckResult());
                break;
            case ZooDefs.OpCode.getChildren:
                GetChildrenResponse gcr = new GetChildrenResponse();
                gcr.deserialize(archive, tag);
                results.add(new OpResult.GetChildrenResult(gcr.getChildren()));
                break;
            case ZooDefs.OpCode.getData:
                GetDataResponse gdr = new GetDataResponse();
                gdr.deserialize(archive, tag);
                results.add(new OpResult.GetDataResult(gdr.getData(), gdr.getStat()));
                break;
            case ZooDefs.OpCode.error:
                // TODO: need way to more cleanly serialize/deserialize exceptions
                ErrorResponse er = new ErrorResponse();
                er.deserialize(archive, tag);
                results.add(new OpResult.ErrorResult(er.getErr()));
                break;
            default:
                throw new IOException("Invalid type " + h.getType() + " in MultiResponse");
        }
        h.deserialize(archive, tag);
    }
    archive.endRecord(tag);
}
Also used : CreateResponse(org.apache.zookeeper.proto.CreateResponse) IOException(java.io.IOException) Create2Response(org.apache.zookeeper.proto.Create2Response) SetDataResponse(org.apache.zookeeper.proto.SetDataResponse) MultiHeader(org.apache.zookeeper.proto.MultiHeader) ErrorResponse(org.apache.zookeeper.proto.ErrorResponse) GetChildrenResponse(org.apache.zookeeper.proto.GetChildrenResponse) GetDataResponse(org.apache.zookeeper.proto.GetDataResponse)

Aggregations

GetDataResponse (org.apache.zookeeper.proto.GetDataResponse)10 ReplyHeader (org.apache.zookeeper.proto.ReplyHeader)7 GetDataRequest (org.apache.zookeeper.proto.GetDataRequest)6 RequestHeader (org.apache.zookeeper.proto.RequestHeader)6 IOException (java.io.IOException)3 Create2Response (org.apache.zookeeper.proto.Create2Response)3 CreateResponse (org.apache.zookeeper.proto.CreateResponse)3 ErrorResponse (org.apache.zookeeper.proto.ErrorResponse)3 SetDataResponse (org.apache.zookeeper.proto.SetDataResponse)3 Stat (org.apache.zookeeper.data.Stat)2 GetChildrenResponse (org.apache.zookeeper.proto.GetChildrenResponse)2 MultiHeader (org.apache.zookeeper.proto.MultiHeader)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 Record (org.apache.jute.Record)1 KeeperException (org.apache.zookeeper.KeeperException)1 Code (org.apache.zookeeper.KeeperException.Code)1 SessionMovedException (org.apache.zookeeper.KeeperException.SessionMovedException)1