/** * 获取该节点的数据 * * @param path 节点路径 * @return 节点数据 */ protected ChildData getData(String path) { try { return new ChildData(path, EMPTY_STAT, client.getData().forPath(path)); } catch (Exception e) { throw new NiubiException(e); } }
/** * 获取该节点的数据 * * @param path 节点路径 * @return 节点数据 */ protected ChildData getData(String path) { try { return new ChildData(path, EMPTY_STAT, client.getData().forPath(path)); } catch (Exception e) { throw new NiubiException(e); } }
private void publishEvent(TreeCacheEvent.Type type, String path) { publishEvent(new TreeCacheEvent(type, new ChildData(path, null, null))); }
public abstract void notify(Path path, PathChildrenCacheEvent event);
@Override public List<ChildData> getCurrentData() { List<ChildData> childData = new ArrayList<>(); for (String childName : getChildren(path)) { Path childPath = path.append(childName); childData.add(new ChildData(childPath.getAbsolute(), null, getData(childPath).get())); } return childData; }
@Override public ChildData getCurrentData(Path fullPath) { if (!fullPath.getParentPath().equals(path)) { throw new IllegalArgumentException("Path '" + fullPath + "' is not a child path of '" + path + "'"); } return getData(fullPath).map(bytes -> new ChildData(fullPath.getAbsolute(), null, bytes)).orElse(null); }
private void internalRebuild() throws Exception { try { Stat stat = new Stat(); byte[] bytes = dataIsCompressed ? client.getData().decompressed().storingStatIn(stat).forPath(path) : client.getData().storingStatIn(stat).forPath(path); data.set(new ChildData(path, stat, bytes)); } catch ( KeeperException.NoNodeException e ) { data.set(null); } }
private void internalRebuild() throws Exception { try { Stat stat = new Stat(); byte[] bytes = dataIsCompressed ? client.getData().decompressed().storingStatIn(stat).forPath(path) : client.getData().storingStatIn(stat).forPath(path); data.set(new ChildData(path, stat, bytes)); } catch ( KeeperException.NoNodeException e ) { data.set(null); } }
private void internalRebuildNode(String fullPath) throws Exception { if ( cacheData ) { try { Stat stat = new Stat(); byte[] bytes = dataIsCompressed ? client.getData().decompressed().storingStatIn(stat).forPath(fullPath) : client.getData().storingStatIn(stat).forPath(fullPath); currentData.put(fullPath, new ChildData(fullPath, stat, bytes)); } catch ( KeeperException.NoNodeException ignore ) { // node no longer exists - remove it currentData.remove(fullPath); } } else { Stat stat = client.checkExists().forPath(fullPath); if ( stat != null ) { currentData.put(fullPath, new ChildData(fullPath, stat, null)); } else { // node no longer exists - remove it currentData.remove(fullPath); } } }
private void collectData(Node parent, Path parentPath, List<ChildData> data) { for (Node child : parent.children().values()) { Path childPath = parentPath.append(child.name()); data.add(new ChildData("/" + childPath.toString(), null, child.getContent())); } }
@Override public ChildData getCurrentData() { Node node = fileSystem.root().getNode(Paths.get(path.toString()), false); if (node == null) return null; return new ChildData("/" + path.toString(), null, node.getContent()); }
/** * As a memory optimization, you can clear the cached data bytes for a node. Subsequent * calls to {@link ChildData#getData()} for this node will return <code>null</code>. * * @param fullPath the path of the node to clear * @param ifVersion if non-negative, only clear the data if the data's version matches this version * @return true if the data was cleared */ public boolean clearDataBytes(String fullPath, int ifVersion) { ChildData data = currentData.get(fullPath); if ( data != null ) { if ( (ifVersion < 0) || (ifVersion == data.getStat().getVersion()) ) { if ( data.getData() != null ) { currentData.replace(fullPath, data, new ChildData(data.getPath(), data.getStat(), null)); } return true; } } return false; }
private void updateCache(String fullPath, Stat stat, byte[] bytes) { ChildData data = new ChildData(fullPath, stat, bytes); ChildData previousData = currentData.put(fullPath, data); if (previousData == null) { callListeners(new PathChildrenCacheEvent(PathChildrenCacheEvent.Type.CHILD_ADDED, data)); } else if (previousData.getStat().getVersion() != stat.getVersion()) { callListeners(new PathChildrenCacheEvent(PathChildrenCacheEvent.Type.CHILD_UPDATED, data)); } } }
childDataUpdater.compareAndSet(this, oldChildData, new ChildData(oldChildData.getPath(), newStat, oldChildData.getData())); if ( event.getResultCode() == KeeperException.Code.OK.intValue() ) ChildData toPublish = new ChildData(event.getPath(), newStat, event.getData()); ChildData oldChildData; if ( cacheData ) oldChildData = childDataUpdater.getAndSet(this, new ChildData(event.getPath(), newStat, null));
private void applyNewData(String fullPath, int resultCode, Stat stat, byte[] bytes) { if ( resultCode == KeeperException.Code.OK.intValue() ) // otherwise - node must have dropped or something - we should be getting another event { ChildData data = new ChildData(fullPath, stat, bytes); ChildData previousData = currentData.put(fullPath, data); if ( previousData == null ) // i.e. new { offerOperation(new EventOperation(this, new PathChildrenCacheEvent(PathChildrenCacheEvent.Type.CHILD_ADDED, data))); } else if ( previousData.getStat().getVersion() != stat.getVersion() ) { offerOperation(new EventOperation(this, new PathChildrenCacheEvent(PathChildrenCacheEvent.Type.CHILD_UPDATED, data))); } updateInitialSet(ZKPaths.getNodeFromPath(fullPath), data); } }
ChildData childData = new ChildData(path, event.getStat(), event.getData()); setNewData(childData);
ChildData childData = new ChildData(path, event.getStat(), event.getData()); setNewData(childData);