From 4cdbd72ca0d79f2f1694203b8cc0372fada7d422 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Tue, 7 May 2024 15:19:07 +0300 Subject: [PATCH] Remove unused on_expire_lease, rename internal methods to _+name --- etctree.js | 105 ++++++++++++++++++++++------------------------------- 1 file changed, 44 insertions(+), 61 deletions(-) diff --git a/etctree.js b/etctree.js index 5438128..fae8bb1 100644 --- a/etctree.js +++ b/etctree.js @@ -23,7 +23,6 @@ class EtcTree this.mod_revision = 0; this.use_base64 = use_base64; this.paused = false; - this.on_expire_lease = null; } de64(k) @@ -40,10 +39,10 @@ class EtcTree return this.use_base64 ? Buffer.from(k).toString('base64') : k; } - check(chk) + _check(chk) { - const parts = this.key_parts(this.de64(chk.key)); - const { cur } = this.get_subtree(parts, false, false); + const parts = this._key_parts(this.de64(chk.key)); + const { cur } = this._get_subtree(parts, false, false); let check_value, ref_value; if (chk.target === 'MOD') { @@ -80,13 +79,13 @@ class EtcTree return check_value == ref_value; } - key_parts(key) + _key_parts(key) { const parts = key.replace(/\/\/+/g, '/').replace(/\/$/g, ''); // trim beginning? return parts === '' ? [] : parts.split('/'); } - get_range(req) + _get_range(req) { const key = this.de64(req.key); const end = this.de64(req.range_end); @@ -95,11 +94,11 @@ class EtcTree { throw new Error('Non-directory range queries are unsupported'); } - const parts = this.key_parts(key); + const parts = this._key_parts(key); return { parts, all: end != null }; } - get_subtree(parts, create, notify) + _get_subtree(parts, create, notify) { let cur = this.state; let watchers = notify ? [] : null; @@ -163,7 +162,7 @@ class EtcTree let has_children = false; for (const k in cur.children) { - const child = this._copy_tree(cur.children[k]); + const child = this._copy_tree(cur.children[k], no_lease); if (child) { copy.children[k] = child; @@ -216,13 +215,13 @@ class EtcTree } if (!this.paused) { - this.leases[id].timer_id = setTimeout(() => this.expire_lease(id), this.leases[id].expires - Date.now()); + this.leases[id].timer_id = setTimeout(() => this.api_revoke_lease({ ID: id }), this.leases[id].expires - Date.now()); } } // Then find and apply the difference in data const notifications = []; this._restore_diff(this.state, snapshot.state, null, this.state.watchers || [], notifications); - this.notify(notifications); + this._notify(notifications); } _restore_diff(cur_old, cur_new, prefix, watchers, notifications) @@ -246,17 +245,15 @@ class EtcTree if (!eq(cur_old.value, cur_new.value)) { cur_old.value = cur_new.value; - for (const w of (cur_old.key_watchers ? [ ...watchers, ...(cur_old.key_watchers||[]) ] : watchers)) + const key_watchers = (cur_old.key_watchers ? [ ...watchers, ...(cur_old.key_watchers||[]) ] : watchers); + const notify = { watchers: key_watchers, key, value: cur_old.value, mod_revision: cur_old.mod_revision }; + if (cur_old.lease) { - const notify = { watchers, key, value: cur_old.value, mod_revision: cur_old.mod_revision }; - if (cur_old.lease) - { - notify.lease = cur_old.lease; - } - notifications.push(notify); + notify.lease = cur_old.lease; } + notifications.push(notify); } - cur_old.children ||= {}; + cur_old.children = cur_old.children || {}; for (const k in cur_new.children) { if (!cur_old.children[k]) @@ -278,7 +275,7 @@ class EtcTree if (!cur_new.children[k]) { // Delete subtree - this.delete_all( + this._delete_all( notifications, cur_old.children[k].watchers ? [ ...watchers, ...cur_old.children[k].watchers ] : watchers, cur_old.children[k], true, @@ -312,16 +309,11 @@ class EtcTree const lease = this.leases[id]; if (!lease.timer_id) { - lease.timer_id = setTimeout(() => this.expire_lease(id), lease.expires - Date.now()); + lease.timer_id = setTimeout(() => this.api_revoke_lease({ ID: id }), lease.expires - Date.now()); } } } - set_on_expire_lease(cb) - { - this.on_expire_lease = cb; - } - api_grant_lease(req) { let id; @@ -330,7 +322,7 @@ class EtcTree id = crypto.randomBytes(8).toString('hex'); } const expires = Date.now() + req.TTL*1000; - const timer_id = this.paused ? null : setTimeout(() => this.expire_lease(id), req.TTL*1000); + const timer_id = this.paused ? null : setTimeout(() => this.api_revoke_lease({ ID: id }), req.TTL*1000); this.leases[id] = { ttl: req.TTL, expires, timer_id, keys: {} }; return { header: { revision: this.mod_revision }, ID: id, TTL: req.TTL }; } @@ -352,21 +344,12 @@ class EtcTree lease.expires = Date.now() + ttl*1000; if (!this.paused) { - lease.timer_id = setTimeout(() => this.expire_lease(id), ttl*1000); + lease.timer_id = setTimeout(() => this.api_revoke_lease({ ID: id }), ttl*1000); } // extra wrapping in { result: ... } return { result: { header: { revision: this.mod_revision }, ID: id, TTL: ''+ttl } }; } - expire_lease(id) - { - this.api_revoke_lease({ ID: id }) - if (this.on_expire_lease) - { - this.on_expire_lease(id); - } - } - api_revoke_lease(req) { if (!this.leases[req.ID]) @@ -379,13 +362,13 @@ class EtcTree { this._delete_range({ key }, next_revision, notifications); } - this.notify(notifications); + this._notify(notifications); return { header: { revision: this.mod_revision } }; } api_create_watch(req, send) { - const { parts, all } = this.get_range(req); + const { parts, all } = this._get_range(req); if (req.start_revision && this.compact_revision && this.compact_revision > req.start_revision) { // Deletions up to this.compact_revision are forgotten @@ -408,7 +391,7 @@ class EtcTree }; } this.watchers[watch_id].paths.push(parts); - const { cur } = this.get_subtree(parts, true, false); + const { cur } = this._get_subtree(parts, true, false); if (all) { cur.watchers = cur.watchers || []; @@ -425,15 +408,15 @@ class EtcTree setImmediate(() => { const events = []; - const { cur } = this.get_subtree([], false, false); - this.get_modified(events, cur, null, req.start_revision); + const { cur } = this._get_subtree([], false, false); + this._get_modified(events, cur, null, req.start_revision); send({ result: { header: { revision: this.mod_revision }, events } }); }); } return { watch_id, created: true }; } - get_modified(events, cur, prefix, min_rev) + _get_modified(events, cur, prefix, min_rev) { if (cur.mod_revision >= min_rev) { @@ -449,7 +432,7 @@ class EtcTree { for (const k in cur.children) { - this.get_modified(events, cur.children[k], prefix === null ? k : prefix+'/'+k, min_rev); + this._get_modified(events, cur.children[k], prefix === null ? k : prefix+'/'+k, min_rev); } } } @@ -460,7 +443,7 @@ class EtcTree { for (const parts of this.watchers[watch_id].paths) { - const { cur } = this.get_subtree(parts, false, false); + const { cur } = this._get_subtree(parts, false, false); if (cur) { if (cur.watchers) @@ -483,7 +466,7 @@ class EtcTree return { canceled: true }; } - notify(notifications) + _notify(notifications) { if (!notifications.length) { @@ -513,7 +496,7 @@ class EtcTree api_txn({ compare, success, failure }) { - const failed = (compare || []).filter(chk => !this.check(chk)).length > 0; + const failed = (compare || []).filter(chk => !this._check(chk)).length > 0; const responses = []; const notifications = []; const next_revision = this.mod_revision + 1; @@ -521,7 +504,7 @@ class EtcTree { responses.push(this._txn_action(req, next_revision, notifications)); } - this.notify(notifications); + this._notify(notifications); return { header: { revision: this.mod_revision }, succeeded: !failed, responses }; } @@ -533,7 +516,7 @@ class EtcTree } else if (req.request_put || req.requestPut) { - return { response_put: this._put(req.request_put || req.requestPut, cur_revision, notifications) } + return { response_put: this._put(req.request_put || req.requestPut, cur_revision, notifications) }; } else if (req.request_delete_range || req.requestDeleteRange) { @@ -546,12 +529,12 @@ class EtcTree { // FIXME: limit, revision(-), sort_order, sort_target, serializable(-), // count_only, min_mod_revision, max_mod_revision, min_create_revision, max_create_revision - const { parts, all } = this.get_range(request_range); - const { cur } = this.get_subtree(parts, false, false); + const { parts, all } = this._get_range(request_range); + const { cur } = this._get_subtree(parts, false, false); const kvs = []; if (cur) { - this.get_all(kvs, cur, all, parts.join('/') || null, request_range); + this._get_all(kvs, cur, all, parts.join('/') || null, request_range); } return { kvs }; } @@ -559,10 +542,10 @@ class EtcTree _put(request_put, cur_revision, notifications) { // FIXME: prev_kv, ignore_value(?), ignore_lease(?) - const parts = this.key_parts(this.de64(request_put.key)); + const parts = this._key_parts(this.de64(request_put.key)); const key = parts.join('/'); const value = this.de64(request_put.value); - const { cur, watchers } = this.get_subtree(parts, true, true); + const { cur, watchers } = this._get_subtree(parts, true, true); if (cur.key_watchers) { watchers.push.apply(watchers, cur.key_watchers); @@ -607,17 +590,17 @@ class EtcTree _delete_range(request_delete_range, cur_revision, notifications) { // FIXME: prev_kv - const { parts, all } = this.get_range(request_delete_range); - const { cur, watchers } = this.get_subtree(parts, false, true); + const { parts, all } = this._get_range(request_delete_range); + const { cur, watchers } = this._get_subtree(parts, false, true); const prevcount = notifications.length; if (cur) { - this.delete_all(notifications, watchers, cur, all, parts.join('/') || null, cur_revision); + this._delete_all(notifications, watchers, cur, all, parts.join('/') || null, cur_revision); } return { deleted: notifications.length-prevcount }; } - get_all(kvs, cur, all, prefix, req) + _get_all(kvs, cur, all, prefix, req) { if (req.limit && kvs.length > req.limit) { @@ -643,12 +626,12 @@ class EtcTree { for (let k in cur.children) { - this.get_all(kvs, cur.children[k], true, prefix === null ? k : prefix+'/'+k, req); + this._get_all(kvs, cur.children[k], true, prefix === null ? k : prefix+'/'+k, req); } } } - delete_all(notifications, watchers, cur, all, prefix, cur_revision) + _delete_all(notifications, watchers, cur, all, prefix, cur_revision) { if (cur.value != null) { @@ -674,7 +657,7 @@ class EtcTree for (let k in cur.children) { const subw = cur.children[k].watchers ? [ ...watchers, ...cur.children[k].watchers ] : watchers; - this.delete_all(notifications, subw, cur.children[k], true, prefix === null ? k : prefix+'/'+k, cur_revision); + this._delete_all(notifications, subw, cur.children[k], true, prefix === null ? k : prefix+'/'+k, cur_revision); } } }