Split txn_action

old-master
Vitaliy Filippov 2024-05-04 12:03:34 +03:00
parent 857cf668f2
commit 74a77a3974
2 changed files with 96 additions and 82 deletions

View File

@ -276,7 +276,9 @@ class AntiEtcd
unsubscribeClient(client_id)
{
if (!this.clients[client_id])
{
return;
}
for (const watch_id in this.clients[client_id].watches)
{
const mapped_id = this.clients[client_id].watches[watch_id];

View File

@ -377,7 +377,7 @@ class EtcTree
const notifications = [];
for (const key in this.leases[req.ID].keys)
{
this.txn_action({ request_delete_range: { key } }, next_revision, notifications);
this._delete_range({ key }, next_revision, notifications);
}
this.notify(notifications);
return { header: { revision: this.mod_revision } };
@ -483,20 +483,6 @@ class EtcTree
return { canceled: true };
}
api_txn({ compare, success, failure })
{
const failed = (compare || []).filter(chk => !this.check(chk)).length > 0;
const responses = [];
const notifications = [];
const next_revision = this.mod_revision + 1;
for (const req of (failed ? failure : success) || [])
{
responses.push(this.txn_action(req, next_revision, notifications));
}
this.notify(notifications);
return { header: { revision: this.mod_revision }, succeeded: !failed, responses };
}
notify(notifications)
{
if (!notifications.length)
@ -525,11 +511,39 @@ class EtcTree
}
}
txn_action(req, cur_revision, notifications)
api_txn({ compare, success, failure })
{
const failed = (compare || []).filter(chk => !this.check(chk)).length > 0;
const responses = [];
const notifications = [];
const next_revision = this.mod_revision + 1;
for (const req of (failed ? failure : success) || [])
{
responses.push(this._txn_action(req, next_revision, notifications));
}
this.notify(notifications);
return { header: { revision: this.mod_revision }, succeeded: !failed, responses };
}
_txn_action(req, cur_revision, notifications)
{
if (req.request_range || req.requestRange)
{
const request_range = req.request_range || req.requestRange;
return { response_range: this._range(req.request_range || req.requestRange) };
}
else if (req.request_put || req.requestPut)
{
return { response_put: this._put(req.request_put || req.requestPut, cur_revision, notifications) }
}
else if (req.request_delete_range || req.requestDeleteRange)
{
return { response_delete_range: this._delete_range(req.request_delete_range || req.requestDeleteRange, cur_revision, notifications) };
}
return {};
}
_range(request_range)
{
// 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);
@ -539,11 +553,11 @@ class EtcTree
{
this.get_all(kvs, cur, all, parts.join('/') || null, request_range);
}
return { response_range: { kvs } };
return { kvs };
}
else if (req.request_put || req.requestPut)
_put(request_put, cur_revision, notifications)
{
const request_put = req.request_put || req.requestPut;
// FIXME: prev_kv, ignore_value(?), ignore_lease(?)
const parts = this.key_parts(this.de64(request_put.key));
const key = parts.join('/');
@ -587,11 +601,11 @@ class EtcTree
}
notifications.push(notify);
}
return { response_put: {} };
return {};
}
else if (req.request_delete_range || req.requestDeleteRange)
_delete_range(request_delete_range, cur_revision, notifications)
{
const request_delete_range = req.request_delete_range || req.requestDeleteRange;
// FIXME: prev_kv
const { parts, all } = this.get_range(request_delete_range);
const { cur, watchers } = this.get_subtree(parts, false, true);
@ -600,9 +614,7 @@ class EtcTree
{
this.delete_all(notifications, watchers, cur, all, parts.join('/') || null, cur_revision);
}
return { response_delete_range: { deleted: notifications.length-prevcount } };
}
return {};
return { deleted: notifications.length-prevcount };
}
get_all(kvs, cur, all, prefix, req)