Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

expose Stat for writes

  • Loading branch information...
commit e72d8fd682907004d0b1488e47d8fe55d405b751 1 parent 15a1464
@jzillmann jzillmann authored
View
2  src/main/java/org/I0Itec/zkclient/IZkConnection.java
@@ -39,7 +39,7 @@
public byte[] readData(String path, Stat stat, boolean watch) throws KeeperException, InterruptedException;
- public void writeData(String path, byte[] data, int expectedVersion) throws KeeperException, InterruptedException;
+ public Stat writeData(String path, byte[] data, int expectedVersion) throws KeeperException, InterruptedException;
public States getZookeeperState();
View
37 src/main/java/org/I0Itec/zkclient/InMemoryConnection.java
@@ -43,8 +43,23 @@
public class InMemoryConnection implements IZkConnection {
+ public static class DataAndVersion {
+ private byte[] _data;
+ private int _version;
+ public DataAndVersion(byte[] data, int version) {
+ _data = data;
+ _version = version;
+ }
+ public byte[] getData() {
+ return _data;
+ }
+ public int getVersion() {
+ return _version;
+ }
+ }
+
private Lock _lock = new ReentrantLock(true);
- private Map<String, byte[]> _data = new HashMap<String, byte[]>();
+ private Map<String, DataAndVersion> _data = new HashMap<String, DataAndVersion>();
private Map<String, Long> _creationTime = new HashMap<String, Long>();
private final AtomicInteger sequence = new AtomicInteger(0);
@@ -130,7 +145,7 @@ public String create(String path, byte[] data, CreateMode mode) throws KeeperExc
if (exists(path, false)) {
throw new KeeperException.NodeExistsException();
}
- _data.put(path, data);
+ _data.put(path, new DataAndVersion(data, 0));
_creationTime.put(path, System.currentTimeMillis());
checkWatch(_nodeWatches, path, EventType.NodeCreated);
// we also need to send a child change event for the parent
@@ -235,10 +250,13 @@ public States getZookeeperState() {
}
_lock.lock();
try {
- byte[] bs = _data.get(path);
- if (bs == null) {
+ DataAndVersion dataAndVersion = _data.get(path);
+ if (dataAndVersion == null) {
throw new ZkNoNodeException(new KeeperException.NoNodeException());
}
+ byte[] bs = dataAndVersion.getData();
+ if (stat != null)
+ stat.setVersion(dataAndVersion.getVersion());
return bs;
} finally {
_lock.unlock();
@@ -246,14 +264,16 @@ public States getZookeeperState() {
}
@Override
- public void writeData(String path, byte[] data, int expectedVersion) throws KeeperException, InterruptedException {
- _lock.lock();
+ public Stat writeData(String path, byte[] data, int expectedVersion) throws KeeperException, InterruptedException {
+ int newVersion=-1;
+ _lock.lock();
try {
checkWatch(_dataWatches, path, EventType.NodeDataChanged);
if (!exists(path, false)) {
throw new KeeperException.NoNodeException();
}
- _data.put(path, data);
+ newVersion = _data.get(path).getVersion() + 1;
+ _data.put(path, new DataAndVersion(data, newVersion));
String parentPath = getParentPath(path);
if (parentPath != null) {
checkWatch(_nodeWatches, parentPath, EventType.NodeChildrenChanged);
@@ -261,6 +281,9 @@ public void writeData(String path, byte[] data, int expectedVersion) throws Keep
} finally {
_lock.unlock();
}
+ Stat stat = new Stat();
+ stat.setVersion(newVersion);
+ return stat;
}
private void checkWatch(Set<String> watches, String path, EventType eventType) {
View
12 src/main/java/org/I0Itec/zkclient/ZkClient.java
@@ -773,8 +773,8 @@ public Object call() throws Exception {
return (T) derializable(data);
}
- public void writeData(String path, Object object) {
- writeData(path, object, -1);
+ public Stat writeData(String path, Object object) {
+ return writeData(path, object, -1);
}
/**
@@ -804,14 +804,14 @@ public void writeData(String path, Object object) {
} while (retry);
}
- public void writeData(final String path, Object datat, final int expectedVersion) {
+ public Stat writeData(final String path, Object datat, final int expectedVersion) {
final byte[] data = serialize(datat);
- retryUntilConnected(new Callable<Object>() {
+ return (Stat) retryUntilConnected(new Callable<Object>() {
@Override
public Object call() throws Exception {
- _connection.writeData(path, data, expectedVersion);
- return null;
+ Stat stat =_connection.writeData(path, data, expectedVersion);
+ return stat;
}
});
}
View
8 src/main/java/org/I0Itec/zkclient/ZkConnection.java
@@ -103,12 +103,12 @@ public boolean exists(String path, boolean watch) throws KeeperException, Interr
return _zk.getData(path, watch, stat);
}
- public void writeData(String path, byte[] data) throws KeeperException, InterruptedException {
- writeData(path, data, -1);
+ public Stat writeData(String path, byte[] data) throws KeeperException, InterruptedException {
+ return writeData(path, data, -1);
}
- public void writeData(String path, byte[] data, int version) throws KeeperException, InterruptedException {
- _zk.setData(path, data, version);
+ public Stat writeData(String path, byte[] data, int version) throws KeeperException, InterruptedException {
+ return _zk.setData(path, data, version);
}
public States getZookeeperState() {
Please sign in to comment.
Something went wrong with that request. Please try again.