{"id":329,"date":"2022-09-26T02:25:49","date_gmt":"2022-09-26T02:25:49","guid":{"rendered":"https:\/\/www.bhoomabrsr.com\/blog\/?p=329"},"modified":"2023-12-27T04:22:49","modified_gmt":"2023-12-27T04:22:49","slug":"zookeeper-javascript-implementation","status":"publish","type":"post","link":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/2022\/09\/26\/zookeeper-javascript-implementation\/","title":{"rendered":"ZooKeeper JavaScript Implementation"},"content":{"rendered":"\n<p>ZooKeeper JavaScript Implementation<\/p>\n\n\n\n<p><em>Def from Apache<\/em>: ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services.<\/p>\n\n\n\n<p>ZooKeeper Node Class Structure:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nclass ZooNode {\n  key = null;\n  value = null;\n  watchers = &#x5B;];\n  children = &#x5B;];\n  constructor(key, value, watcherFn) {\n    this.key = key;\n    this.value = value;\n    if (watcherFn) {\n      this.watchers.push(watcherFn);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p>ZooKeeper Implementation:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\nclass ZooKeeper {\n  root = new ZooNode('root');\n\n  \/*get path segments*\/\n  getPathSegments(path) {\n    const ps = path.split('\/');\n    ps.shift();\n    return ps;\n  }\n\n  \/*call watchers*\/\n  callWatchers(fns) {\n    fns.forEach(fn =&gt; fn?.());\n  }\n\n  \/*find a node and it's watchers*\/\n  dfs(root, pathSegments, idx, watcherFns = &#x5B;]) {\n    if (!pathSegments.length) {\n      return { node: root, watcherFns: &#x5B;...root.watchers] };\n    }\n    const key = pathSegments&#x5B;idx];\n    watcherFns = watcherFns.concat(root.watchers);\n    const nodeIdx = root.children.map(p =&gt; p.key).indexOf(key);\n    if (nodeIdx &gt; -1) {\n      const nextRoot = root.children&#x5B;nodeIdx];\n      if (pathSegments.length === idx + 1) {\n        watcherFns = watcherFns.concat(nextRoot.watchers);\n        return { node: nextRoot, watcherFns };\n      }\n      return this.dfs(nextRoot, pathSegments, idx + 1, watcherFns);\n    }\n  }\n\n  create(path, value) {\n    const ps = this.getPathSegments(path, true);\n    const newChildKey = ps.pop();\n    const { node: parentNode, watcherFns } = this.dfs(this.root, ps, 0) || {};\n    if (parentNode) {\n      parentNode.children.push(new ZooNode(newChildKey, value));\n      this.callWatchers(watcherFns);\n    } else {\n      console.log(`unable to add, path is not exist: ${path}`);\n    }\n  }\n\n  setValue(path, value) {\n    const ps = this.getPathSegments(path, true);\n    const { node, watcherFns } = this.dfs(this.root, ps, 0) || {};\n    if (node) {\n      node.value = value;\n      this.callWatchers(watcherFns);\n    } else {\n      console.log(`unable to update, path is not exist: ${path}`);\n    }\n  }\n\n  getValue(path) {\n    const ps = this.getPathSegments(path, true);\n    const { node } = this.dfs(this.root, ps, 0) || {};\n    if (node) {\n      return node.value;\n    } else {\n      console.log(`unable to update, path is not exist: ${path}`);\n    }\n  }\n\n  watch(path, fn) {\n    const ps = this.getPathSegments(path, true);\n    const { node } = this.dfs(this.root, ps, 0) || {};\n    if (node) {\n      node.watchers = &#x5B;fn];\/\/concat for multiple listeners\n    } else {\n      console.log(`unable to update, path is not exist: ${path}`);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p>Execution:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nlet z = new ZooKeeper();\nz.create('\/a', 'foo');\nz.create('\/a\/c', 'bar');\nz.create('\/a\/d', 'foo_d');\nz.setValue('\/a\/d', 'foo_d_update');\nconsole.log(z.getValue('\/a\/d')); \n\/\/foo_d_update\n\nz.watch('\/a\/d', () =&gt; { console.log('a watch at \/a\/d') });\nz.watch('\/a', () =&gt; { console.log('a watch at \/a') });\nz.setValue('\/a\/d', 'foo_d_update_again'); \n\/\/1. a watch at \/a \n\/\/2. a watch at \/a\/d\n\nz.setValue('\/a', 'foo_a_update_again');\n\/\/a watch at \/a\n<\/pre><\/div>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ZooKeeper JavaScript Implementation Def from Apache: ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. ZooKeeper Node Class Structure: ZooKeeper Implementation: Execution:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,44,15,35,1],"tags":[38,39,37,41,42,40],"class_list":["post-329","post","type-post","status-publish","format-standard","hentry","category-algos","category-data-structures","category-javascript","category-nodejs","category-uncategorized","tag-algos","tag-distributed","tag-ds","tag-mq","tag-sqs","tag-synchronization"],"_links":{"self":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=329"}],"version-history":[{"count":3,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/329\/revisions"}],"predecessor-version":[{"id":333,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/329\/revisions\/333"}],"wp:attachment":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}