xyuanlu commented on code in PR #2623:
URL: https://github.com/apache/helix/pull/2623#discussion_r1333564449
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/ZkMetaClientCache.java:
##########
@@ -59,36 +60,141 @@ public ZkMetaClientCache(ZkMetaClientConfig config,
MetaClientCacheConfig cacheC
_lazyCaching = cacheConfig.getLazyCaching();
_cacheData = cacheConfig.getCacheData();
_cacheChildren = cacheConfig.getCacheChildren();
+
+ if (_cacheData) {
+ _dataCacheMap = new ConcurrentHashMap<>();
+ }
+ if (_cacheChildren) {
+ _childrenCacheTree = new TrieNode(_rootEntry,
_rootEntry.substring(1));
+ }
}
@Override
- public Stat exists(String key) {
+ public T get(final String key) {
throw new MetaClientException("Not implemented yet.");
}
@Override
- public T get(final String key) {
+ public List<T> get(List<String> keys) {
throw new MetaClientException("Not implemented yet.");
}
@Override
public List<String> getDirectChildrenKeys(final String key) {
- throw new MetaClientException("Not implemented yet.");
+ TrieNode node = getTree(key);
+ List<String> children;
+ if (node == null || !node.isChildrenCached()) {
+ try {
+ children = _cacheClient.getChildren(key);
+ populateChildrenCache(node, children);
+ return children;
+ } catch (ZkException e) {
+ throw translateZkExceptionToMetaclientException(e);
+ }
+ }
+ return new ArrayList<>(node.getChildren().keySet());
+ }
+
+ private void populateChildrenCache(TrieNode root, List<String> children) {
+ if (root == null) {
+ return;
+ }
+ for (String child : children) {
+ updateCache("/" + child, true);
+ }
+ root.setChildrenCached(true);
}
@Override
public int countDirectChildren(final String key) {
- throw new MetaClientException("Not implemented yet.");
+ TrieNode node = getTree(key);
+ if (node == null || !node.isChildrenCached()) {
+ try {
+ List<String> children = _cacheClient.getChildren(key);
+ populateChildrenCache(node, children);
+ return _cacheClient.countChildren(key);
+ } catch (ZkException e) {
+ throw translateZkExceptionToMetaclientException(e);
+ }
+ }
+ return node.getChildren().size();
}
- @Override
- public List<T> get(List<String> keys) {
- throw new MetaClientException("Not implemented yet.");
+ private void handleCacheUpdate(String path, ChildChangeListener.ChangeType
changeType) {
+ switch (changeType) {
+ case ENTRY_CREATED:
+ updateCache(path, true);
Review Comment:
If we haven't read the node before, this will populate cache, witch may not
follow our lazy initiate pattern. Maybe reconsider. (Or we could keep it as it
is for simplicity)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]