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();
}
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);
}
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();
}
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);
}
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);
}
Aggregations