public String getString(String path) throws KeeperException, InterruptedException { try { byte[] data = getData(path, false, null); if (data == null) { return null; } return new String(data, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
public int getInt(String path) throws KeeperException, InterruptedException { return Integer.parseInt(new String(getData(path, false, new Stat()))); }
public long getLong(String path) throws KeeperException, InterruptedException { return Long.parseLong(new String(getData(path, false, new Stat()))); }
public Long getLongOrNull(String path) throws KeeperException, InterruptedException { if (exists(path, false) == null) { return null; } else { return Long.parseLong(new String(getData(path, false, new Stat()))); } }
public static Boolean get(ZooKeeperPlus zk, String nodePath) throws InterruptedException, KeeperException { return decodeValue(zk.getData(nodePath, null, null)); }
public static Integer get(ZooKeeperPlus zk, String nodePath) throws InterruptedException, KeeperException { return decodeValue(zk.getData(nodePath, null, null)); }
private boolean watchForData() throws InterruptedException, KeeperException { if (LOG.isTraceEnabled()) { LOG.trace(String.format("Getting value for %s", nodePath)); } try { synchronized (this) { value = decode(zk.getData(nodePath, watcher, stat)); return true; } } catch (KeeperException.NoNodeException e) { watchForCreation(); return false; } }
private void dumpZk(String parentPath, String nodeName, int depth) throws Exception { System.err.print(StringUtils.repeat(" ", depth)); System.err.print("/" + nodeName); String nodePath = ZkPath.append(parentPath, nodeName); nodePath = nodePath.replace("//", "/"); byte[] data = zk.getData(nodePath, false, null); if (data == null) { System.err.print(" -> null"); } else { System.err.print(" -> [bytes]"); } System.err.println(); List<String> children = zk.getChildren(nodePath, false); for (String child : children) { dumpZk(nodePath, child, depth + 1); } }
private int getNextDomainId() throws KeeperException, InterruptedException { final String domainIdCounterPath = ZkPath.append(domainsRoot, KEY_DOMAIN_ID_COUNTER); if (zk.exists(domainIdCounterPath, false) == null) { zk.create(domainIdCounterPath, Integer.toString(1).getBytes()); return 1; } while (true) { final Stat stat = new Stat(); final byte[] data = zk.getData(domainIdCounterPath, false, stat); int lastVersionNumber = Integer.parseInt(new String(data)); try { lastVersionNumber++; zk.setData(domainIdCounterPath, Integer.toString(lastVersionNumber).getBytes(), stat.getVersion()); return lastVersionNumber; } catch (KeeperException.BadVersionException e) { if (LOG.isDebugEnabled()) { LOG.debug("Tried to set the domain id counter to " + lastVersionNumber + " but was preempted by another writer. Retrying."); } } } }
@Override public String load(ZooKeeperPlus zk, String basePath, String relPath) { try { return new String(zk.getData(ZkPath.append(basePath, relPath), false, new Stat())); } catch (Exception e) { throw new RuntimeException(e); } } }
@Test public void testIt() throws Exception { final ZooKeeperPlus zk = getZk(); zk.ensureCreated("/", null, CreateMode.PERSISTENT); assertExists("/", zk); zk.ensureCreated("/simple", "1".getBytes(), CreateMode.PERSISTENT); assertExists("/simple", zk); zk.ensureCreated("/simple", "2".getBytes(), CreateMode.PERSISTENT); assertExists("/simple", zk); assertTrue(Arrays.equals(zk.getData("/simple", false, null), "1".getBytes())); zk.ensureCreated("/deeper/file", null, CreateMode.PERSISTENT); assertExists("/deeper/file", zk); assertExists("/deeper", zk); zk.ensureCreated("/simple/even/deeper", "3".getBytes(), CreateMode.PERSISTENT); assertTrue(Arrays.equals(zk.getData("/simple", false, null), "1".getBytes())); }
@Test public void testIt() throws Exception { final ZooKeeperPlus zk = getZk(); final String colRoot = ZkPath.append(getRoot(), "collection"); zk.create(colRoot, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); final ElementLoader<String> elementLoader = (zk1, basePath, relPath) -> { try { return new String(zk1.getData(ZkPath.append(basePath, relPath), false, new Stat())); } catch (Exception e) { throw new RuntimeException(e); } }; final WatchedMap<String> c1 = new WatchedMap<>(zk, colRoot, elementLoader); dumpZk(); WaitUntil.orDie(() -> 0 == c1.size()); assertEquals(0, c1.size()); zk.create(ZkPath.append(colRoot, "first"), "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); WaitUntil.orDie(() -> 1 == c1.size()); assertEquals(1, c1.size()); }