@Override public boolean[] setChildren(List<String> paths, List<T> records, int options) { final int size = paths.size(); List<String> serverPaths = prependChroot(paths); Cache<T> cache = getCache(serverPaths); if (cache != null) { try { cache.lockWrite(); List<Stat> setStats = new ArrayList<Stat>(); List<List<String>> pathsCreatedList = new ArrayList<List<String>>(Collections.<List<String>> nCopies(size, null)); boolean[] success = _baseAccessor.set(serverPaths, records, pathsCreatedList, setStats, options); for (int i = 0; i < size; i++) { updateCache(cache, pathsCreatedList.get(i), success[i], serverPaths.get(i), records.get(i), setStats.get(i)); } return success; } finally { cache.unlockWrite(); } } return _baseAccessor.setChildren(serverPaths, records, options); }
@Override public boolean[] setChildren(List<String> paths, List<T> records, int options) { final int size = paths.size(); List<String> serverPaths = prependChroot(paths); Cache<T> cache = getCache(serverPaths); if (cache != null) { try { cache.lockWrite(); List<Stat> setStats = new ArrayList<Stat>(); List<List<String>> pathsCreatedList = new ArrayList<List<String>>(Collections.<List<String>>nCopies(size, null)); boolean[] success = _baseAccessor.set(serverPaths, records, pathsCreatedList, setStats, options); for (int i = 0; i < size; i++) { updateCache(cache, pathsCreatedList.get(i), success[i], serverPaths.get(i), records.get(i), setStats.get(i)); } return success; } finally { cache.unlockWrite(); } } return _baseAccessor.setChildren(serverPaths, records, options); }
@Override public boolean exists(String path, int options) { String clientPath = path; String serverPath = prependChroot(clientPath); Cache<T> cache = getCache(serverPath); if (cache != null) { boolean exists = cache.exists(serverPath); if (exists) { return true; } } // if not exists in cache, always fall back to zk return _baseAccessor.exists(serverPath, options); }
@Override public boolean exists(String path, int options) { String clientPath = path; String serverPath = prependChroot(clientPath); Cache<T> cache = getCache(serverPath); if (cache != null) { boolean exists = cache.exists(serverPath); if (exists) { return true; } } // if not exists in cache, always fall back to zk return _baseAccessor.exists(serverPath, options); }
@Override public boolean[] createChildren(List<String> paths, List<T> records, int options) { final int size = paths.size(); List<String> serverPaths = prependChroot(paths); Cache<T> cache = getCache(serverPaths); if (cache != null) { try { cache.lockWrite(); boolean[] needCreate = new boolean[size]; Arrays.fill(needCreate, true); List<List<String>> pathsCreatedList = new ArrayList<List<String>>(Collections.<List<String>> nCopies(size, null)); CreateCallbackHandler[] createCbList = _baseAccessor.create(serverPaths, records, needCreate, pathsCreatedList, options); boolean[] success = new boolean[size]; for (int i = 0; i < size; i++) { CreateCallbackHandler cb = createCbList[i]; success[i] = (Code.get(cb.getRc()) == Code.OK); updateCache(cache, pathsCreatedList.get(i), success[i], serverPaths.get(i), records.get(i), ZNode.ZERO_STAT); } return success; } finally { cache.unlockWrite(); } } // no cache return _baseAccessor.createChildren(serverPaths, records, options); }
@Override public boolean[] createChildren(List<String> paths, List<T> records, int options) { final int size = paths.size(); List<String> serverPaths = prependChroot(paths); Cache<T> cache = getCache(serverPaths); if (cache != null) { try { cache.lockWrite(); boolean[] needCreate = new boolean[size]; Arrays.fill(needCreate, true); List<List<String>> pathsCreatedList = new ArrayList<List<String>>(Collections.<List<String>>nCopies(size, null)); CreateCallbackHandler[] createCbList = _baseAccessor.create(serverPaths, records, needCreate, pathsCreatedList, options); boolean[] success = new boolean[size]; for (int i = 0; i < size; i++) { CreateCallbackHandler cb = createCbList[i]; success[i] = (Code.get(cb.getRc()) == Code.OK); updateCache(cache, pathsCreatedList.get(i), success[i], serverPaths.get(i), records.get(i), ZNode.ZERO_STAT); } return success; } finally { cache.unlockWrite(); } } // no cache return _baseAccessor.createChildren(serverPaths, records, options); }
@Override public boolean[] remove(List<String> paths, int options) { final int size = paths.size(); List<String> serverPaths = prependChroot(paths); Cache<T> cache = getCache(serverPaths); if (cache != null) { try { cache.lockWrite(); boolean[] success = _baseAccessor.remove(serverPaths, options); for (int i = 0; i < size; i++) { if (success[i]) { cache.purgeRecursive(serverPaths.get(i)); } } return success; } finally { cache.unlockWrite(); } } // no cache return _baseAccessor.remove(serverPaths, options); }
@Override public boolean[] remove(List<String> paths, int options) { final int size = paths.size(); List<String> serverPaths = prependChroot(paths); Cache<T> cache = getCache(serverPaths); if (cache != null) { try { cache.lockWrite(); boolean[] success = _baseAccessor.remove(serverPaths, options); for (int i = 0; i < size; i++) { if (success[i]) { cache.purgeRecursive(serverPaths.get(i)); } } return success; } finally { cache.unlockWrite(); } } // no cache return _baseAccessor.remove(serverPaths, options); }
@Override public List<String> getChildNames(String parentPath, int options) { String serverParentPath = prependChroot(parentPath); Cache<T> cache = getCache(serverParentPath); if (cache != null) { // System.out.println("zk-cache"); ZNode znode = cache.get(serverParentPath); if (znode != null && znode.getChildSet() != Collections.<String> emptySet()) { // System.out.println("zk-cache-hit: " + parentPath); List<String> childNames = new ArrayList<String>(znode.getChildSet()); Collections.sort(childNames); return childNames; } else { // System.out.println("zk-cache-miss"); try { cache.lockWrite(); List<String> childNames = _baseAccessor.getChildNames(serverParentPath, options); // System.out.println("\t--" + childNames); cache.addToParentChildSet(serverParentPath, childNames); return childNames; } finally { cache.unlockWrite(); } } } // no cache return _baseAccessor.getChildNames(serverParentPath, options); }
@Override public List<String> getChildNames(String parentPath, int options) { String serverParentPath = prependChroot(parentPath); Cache<T> cache = getCache(serverParentPath); if (cache != null) { // System.out.println("zk-cache"); ZNode znode = cache.get(serverParentPath); if (znode != null && znode.getChildSet() != Collections.<String>emptySet()) { // System.out.println("zk-cache-hit: " + parentPath); List<String> childNames = new ArrayList<String>(znode.getChildSet()); Collections.sort(childNames); return childNames; } else { // System.out.println("zk-cache-miss"); try { cache.lockWrite(); List<String> childNames = _baseAccessor.getChildNames(serverParentPath, options); // System.out.println("\t--" + childNames); cache.addToParentChildSet(serverParentPath, childNames); return childNames; } finally { cache.unlockWrite(); } } } // no cache return _baseAccessor.getChildNames(serverParentPath, options); }
@Override public boolean remove(String path, int options) { String clientPath = path; String serverPath = prependChroot(clientPath); Cache<T> cache = getCache(serverPath); if (cache != null) { try { cache.lockWrite(); boolean success = _baseAccessor.remove(serverPath, options); if (success) { cache.purgeRecursive(serverPath); } return success; } finally { cache.unlockWrite(); } } // no cache return _baseAccessor.remove(serverPath, options); }
@Override public boolean create(String path, T data, int options) { String clientPath = path; String serverPath = prependChroot(clientPath); Cache<T> cache = getCache(serverPath); if (cache != null) { try { cache.lockWrite(); ZkBaseDataAccessor<T>.AccessResult result = _baseAccessor.doCreate(serverPath, data, options); boolean success = (result._retCode == RetCode.OK); updateCache(cache, result._pathCreated, success, serverPath, data, ZNode.ZERO_STAT); return success; } finally { cache.unlockWrite(); } } // no cache return _baseAccessor.create(serverPath, data, options); }
@Override public boolean create(String path, T data, int options) { String clientPath = path; String serverPath = prependChroot(clientPath); Cache<T> cache = getCache(serverPath); if (cache != null) { try { cache.lockWrite(); ZkBaseDataAccessor<T>.AccessResult result = _baseAccessor.doCreate(serverPath, data, options); boolean success = (result._retCode == RetCode.OK); updateCache(cache, result._pathCreated, success, serverPath, data, ZNode.ZERO_STAT); return success; } finally { cache.unlockWrite(); } } // no cache return _baseAccessor.create(serverPath, data, options); }
@Override public boolean remove(String path, int options) { String clientPath = path; String serverPath = prependChroot(clientPath); Cache<T> cache = getCache(serverPath); if (cache != null) { try { cache.lockWrite(); boolean success = _baseAccessor.remove(serverPath, options); if (success) { cache.purgeRecursive(serverPath); } return success; } finally { cache.unlockWrite(); } } // no cache return _baseAccessor.remove(serverPath, options); }
@Override public boolean update(String path, DataUpdater<T> updater, int options) { String clientPath = path; String serverPath = prependChroot(clientPath); Cache<T> cache = getCache(serverPath); if (cache != null) { try { cache.lockWrite(); ZkBaseDataAccessor<T>.AccessResult result = _baseAccessor.doUpdate(serverPath, updater, options); boolean success = (result._retCode == RetCode.OK); updateCache(cache, result._pathCreated, success, serverPath, result._updatedValue, result._stat); return success; } finally { cache.unlockWrite(); } } // no cache return _groupCommit.commit(_baseAccessor, options, serverPath, updater); // return _baseAccessor.update(serverPath, updater, options); }
@Override public boolean update(String path, DataUpdater<T> updater, int options) { String clientPath = path; String serverPath = prependChroot(clientPath); Cache<T> cache = getCache(serverPath); if (cache != null) { try { cache.lockWrite(); ZkBaseDataAccessor<T>.AccessResult result = _baseAccessor.doUpdate(serverPath, updater, options); boolean success = (result._retCode == RetCode.OK); updateCache(cache, result._pathCreated, success, serverPath, result._updatedValue, result._stat); return success; } finally { cache.unlockWrite(); } } // no cache return _groupCommit.commit(_baseAccessor, options, serverPath, updater); // return _baseAccessor.update(serverPath, updater, options); }
@Override public Stat getStat(String path, int options) { String clientPath = path; String serverPath = prependChroot(clientPath); Cache<T> cache = getCache(serverPath); if (cache != null) { Stat stat = new Stat(); ZNode znode = cache.get(serverPath); if (znode != null) { return znode.getStat(); } else { // if cache miss, fall back to zk and update cache try { cache.lockWrite(); T data = _baseAccessor.get(serverPath, stat, options); cache.update(serverPath, data, stat); } catch (ZkNoNodeException e) { return null; } finally { cache.unlockWrite(); } return stat; } } // no cache return _baseAccessor.getStat(serverPath, options); }
@Override public boolean set(String path, T data, int expectVersion, int options) { String clientPath = path; String serverPath = prependChroot(clientPath); Cache<T> cache = getCache(serverPath); boolean success = false; try { if (cache != null) { cache.lockWrite(); ZkBaseDataAccessor<T>.AccessResult result = _baseAccessor.doSet(serverPath, data, expectVersion, options); success = result._retCode == RetCode.OK; updateCache(cache, result._pathCreated, success, serverPath, data, result._stat); } else { // no cache success = _baseAccessor.set(serverPath, data, expectVersion, options); } } catch (Exception e) { } finally { if (cache != null) { cache.unlockWrite(); } } return success; }
@Override public Stat getStat(String path, int options) { String clientPath = path; String serverPath = prependChroot(clientPath); Cache<T> cache = getCache(serverPath); if (cache != null) { Stat stat = new Stat(); ZNode znode = cache.get(serverPath); if (znode != null) { return znode.getStat(); } else { // if cache miss, fall back to zk and update cache try { cache.lockWrite(); T data = _baseAccessor.get(serverPath, stat, options); cache.update(serverPath, data, stat); } catch (ZkNoNodeException e) { return null; } finally { cache.unlockWrite(); } return stat; } } // no cache return _baseAccessor.getStat(serverPath, options); }
@Override public boolean set(String path, T data, int expectVersion, int options) { String clientPath = path; String serverPath = prependChroot(clientPath); Cache<T> cache = getCache(serverPath); boolean success = false; try { if (cache != null) { cache.lockWrite(); ZkBaseDataAccessor<T>.AccessResult result = _baseAccessor.doSet(serverPath, data, expectVersion, options); success = result._retCode == RetCode.OK; updateCache(cache, result._pathCreated, success, serverPath, data, result._stat); } else { // no cache success = _baseAccessor.set(serverPath, data, expectVersion, options); } } catch (Exception e) { } finally { if (cache != null) { cache.unlockWrite(); } } return success; }