Search in sources :

Example 1 with GetChildrenResponse

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

the class ZooKeeper method getChildren.

/**
 * Return the list of the children 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 deletes the node of the given
 * path or creates/delete a child under the node.
 * <p>
 * The list of children returned is not sorted and no guarantee is provided
 * as to its natural or lexical order.
 * <p>
 * A KeeperException with error code KeeperException.NoNode will be thrown
 * if no node with the given path exists.
 *
 * @param path
 * @param watcher explicit watcher
 * @return an unordered array of children of the node with the given path
 * @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 List<String> getChildren(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 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();
    ReplyHeader r = cnxn.submitRequest(h, request, response, wcb);
    if (r.getErr() != 0) {
        throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath);
    }
    return response.getChildren();
}
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 2 with GetChildrenResponse

use of org.apache.zookeeper.proto.GetChildrenResponse 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 3 with GetChildrenResponse

use of org.apache.zookeeper.proto.GetChildrenResponse 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)

Example 4 with GetChildrenResponse

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

the class MultiResponse method serialize.

@Override
public void serialize(OutputArchive archive, String tag) throws IOException {
    archive.startRecord(this, tag);
    for (OpResult result : results) {
        int err = result.getType() == ZooDefs.OpCode.error ? ((OpResult.ErrorResult) result).getErr() : 0;
        new MultiHeader(result.getType(), false, err).serialize(archive, tag);
        switch(result.getType()) {
            case ZooDefs.OpCode.create:
                new CreateResponse(((OpResult.CreateResult) result).getPath()).serialize(archive, tag);
                break;
            case ZooDefs.OpCode.create2:
                OpResult.CreateResult createResult = (OpResult.CreateResult) result;
                new Create2Response(createResult.getPath(), createResult.getStat()).serialize(archive, tag);
                break;
            case ZooDefs.OpCode.delete:
            case ZooDefs.OpCode.check:
                break;
            case ZooDefs.OpCode.setData:
                new SetDataResponse(((OpResult.SetDataResult) result).getStat()).serialize(archive, tag);
                break;
            case ZooDefs.OpCode.getChildren:
                new GetChildrenResponse(((OpResult.GetChildrenResult) result).getChildren()).serialize(archive, tag);
                break;
            case ZooDefs.OpCode.getData:
                new GetDataResponse(((OpResult.GetDataResult) result).getData(), ((OpResult.GetDataResult) result).getStat()).serialize(archive, tag);
                break;
            case ZooDefs.OpCode.error:
                new ErrorResponse(((OpResult.ErrorResult) result).getErr()).serialize(archive, tag);
                break;
            default:
                throw new IOException("Invalid type " + result.getType() + " in MultiResponse");
        }
    }
    new MultiHeader(-1, true, -1).serialize(archive, tag);
    archive.endRecord(this, 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)

Example 5 with GetChildrenResponse

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

the class FinalRequestProcessor method handleGetChildrenRequest.

private Record handleGetChildrenRequest(Record request, ServerCnxn cnxn, List<Id> authInfo) throws KeeperException, IOException {
    GetChildrenRequest getChildrenRequest = (GetChildrenRequest) request;
    String path = getChildrenRequest.getPath();
    DataNode n = zks.getZKDatabase().getNode(path);
    if (n == null) {
        throw new KeeperException.NoNodeException();
    }
    zks.checkACL(cnxn, zks.getZKDatabase().aclForNode(n), ZooDefs.Perms.READ, authInfo, path, null);
    List<String> children = zks.getZKDatabase().getChildren(path, null, getChildrenRequest.getWatch() ? cnxn : null);
    return new GetChildrenResponse(children);
}
Also used : GetChildrenRequest(org.apache.zookeeper.proto.GetChildrenRequest) GetChildrenResponse(org.apache.zookeeper.proto.GetChildrenResponse)

Aggregations

GetChildrenResponse (org.apache.zookeeper.proto.GetChildrenResponse)5 GetChildrenRequest (org.apache.zookeeper.proto.GetChildrenRequest)3 IOException (java.io.IOException)2 Create2Response (org.apache.zookeeper.proto.Create2Response)2 CreateResponse (org.apache.zookeeper.proto.CreateResponse)2 ErrorResponse (org.apache.zookeeper.proto.ErrorResponse)2 GetDataResponse (org.apache.zookeeper.proto.GetDataResponse)2 MultiHeader (org.apache.zookeeper.proto.MultiHeader)2 ReplyHeader (org.apache.zookeeper.proto.ReplyHeader)2 RequestHeader (org.apache.zookeeper.proto.RequestHeader)2 SetDataResponse (org.apache.zookeeper.proto.SetDataResponse)2