xyuanlu commented on code in PR #2623:
URL: https://github.com/apache/helix/pull/2623#discussion_r1340480236
##########
meta-client/src/main/java/org/apache/helix/metaclient/impl/zk/ZkMetaClientCache.java:
##########
@@ -102,14 +102,90 @@ public List<T> get(List<String> keys) {
return dataList;
}
+ /**
+ * Get the direct children for a given key.
+ * @param key For metadata storage that has hierarchical key space (e.g.
ZK), the key would be
+ * a parent key,
+ * For metadata storage that has non-hierarchical key space
(e.g. etcd), the key would
+ * be a prefix key.
+ * @return list of direct children or null if key doesn't exist / cache is
not populated yet.
+ */
@Override
public List<String> getDirectChildrenKeys(final String key) {
- throw new MetaClientException("Not implemented yet.");
+ if (_cacheChildren) {
+ TrieNode node = getTree(key);
+ if (node == null) {
+ LOG.debug("Children not found in cache for key: {}. This could
be because the cache is still being populated.", key);
+ return null;
+ }
+ return new ArrayList<>(node.getChildren().keySet());
+ }
+ return super.getDirectChildrenKeys(key);
}
+ /**
+ * Get the number of direct children for a given key.
+ * @param key For metadata storage that has hierarchical key space (e.g.
ZK), the key would be
+ * a parent key,
+ * For metadata storage that has non-hierarchical key space
(e.g. etcd), the key would
+ * be a prefix key.
+ * @return number of direct children or 0 if key doesn't exist / has no
children / cache is not populated yet.
+ */
@Override
public int countDirectChildren(final String key) {
- throw new MetaClientException("Not implemented yet.");
+ if (_cacheChildren) {
+ TrieNode node = getTree(key);
+ if (node == null) {
+ LOG.debug("Children not found in cache for key: {}. This could
be because the cache is still being populated.", key);
+ return 0;
+ }
+ return node.getChildren().size();
+ }
+ return super.countDirectChildren(key);
+ }
+
+ private TrieNode getTree(String path) {
+ String[] pathComponents = path.split("/");
+ TrieNode currentNode = _childrenCacheTree;
+ for (int i = 1; i < pathComponents.length; i++) {
+ String component = pathComponents[i];
+ if (!currentNode.getChildren().containsKey(component)) {
+ return currentNode;
+ } else {
+ currentNode = currentNode.getChildren().get(component);
+ }
+ }
+ return currentNode;
+ }
+
+ private void updateCache(String path, boolean isCreate) {
+ if (_cacheChildren) {
+ String[] pathComponents = path.split("/");
+ TrieNode currentNode = _childrenCacheTree;
+ TrieNode previousNode = null;
Review Comment:
Same as above. AddingNoe should be a function in TrieNode.
--
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]