Implement value_filter for dump

old-master
Vitaliy Filippov 2024-05-07 15:24:43 +03:00
parent b0cc255623
commit 693c49403e
1 changed files with 15 additions and 5 deletions

View File

@ -134,10 +134,10 @@ class EtcTree
} }
// create a snapshot of all data including leases // create a snapshot of all data including leases
dump(persistent_only) dump(persistent_only, value_filter)
{ {
const snapshot = { const snapshot = {
state: this._copy_tree(this.state, persistent_only) || {}, state: this._copy_tree(this.state, persistent_only, value_filter) || {},
mod_revision: this.mod_revision, mod_revision: this.mod_revision,
}; };
if (!persistent_only) if (!persistent_only)
@ -152,17 +152,27 @@ class EtcTree
return snapshot; return snapshot;
} }
_copy_tree(cur, no_lease) _copy_tree(cur, no_lease, value_filter)
{ {
const nonempty = cur.value != null && (!no_lease || !copy.lease); let nonempty = cur.value != null && (!no_lease || !cur.lease);
let filtered;
if (nonempty && value_filter)
{
filtered = value_filter(cur.value);
nonempty = nonempty && filtered != null;
}
const copy = (nonempty ? { ...cur } : {}); const copy = (nonempty ? { ...cur } : {});
copy.children = {}; copy.children = {};
if (nonempty && value_filter)
{
copy.value = filtered;
}
delete copy.watchers; delete copy.watchers;
delete copy.key_watchers; delete copy.key_watchers;
let has_children = false; let has_children = false;
for (const k in cur.children) for (const k in cur.children)
{ {
const child = this._copy_tree(cur.children[k], no_lease); const child = this._copy_tree(cur.children[k], no_lease, value_filter);
if (child) if (child)
{ {
copy.children[k] = child; copy.children[k] = child;