From 7f7e36c4a8c863780694840ff6ca120cecdb3ab0 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sun, 16 Mar 2025 01:32:30 +0700 Subject: [PATCH] batch_clear/destroy for multiple queries --- README.md | 4 +- develop/all.lua | 4 +- develop/untests.lua | 90 ++++++++++++++++++++++ evolved.lua | 180 +++++++++++++++++++++++++++++++++++++------- 4 files changed, 248 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 7e24652..52744ac 100644 --- a/README.md +++ b/README.md @@ -84,8 +84,8 @@ batch_set :: query, fragment, any... -> integer, boolean batch_assign :: query, fragment, any... -> integer, boolean batch_insert :: query, fragment, any... -> integer, boolean batch_remove :: query, fragment... -> integer, boolean -batch_clear :: query -> integer, boolean -batch_destroy :: query -> integer, boolean +batch_clear :: query... -> integer, boolean +batch_destroy :: query... -> integer, boolean batch_multi_set :: query, fragment[], component[]? -> integer, boolean batch_multi_assign :: query, fragment[], component[]? -> integer, boolean diff --git a/develop/all.lua b/develop/all.lua index 49a0913..7d7536f 100644 --- a/develop/all.lua +++ b/develop/all.lua @@ -1,4 +1,4 @@ require 'develop.example' -require 'develop.unbench' +-- require 'develop.unbench' require 'develop.untests' -require 'develop.usbench' +-- require 'develop.usbench' diff --git a/develop/untests.lua b/develop/untests.lua index 9e142a2..1c067b7 100644 --- a/develop/untests.lua +++ b/develop/untests.lua @@ -7553,3 +7553,93 @@ do assert(not evo.is_alive(e1) and evo.is_empty(e1)) assert(not evo.is_alive(e2) and evo.is_empty(e2)) end + +do + local f1, f2 = evo.id(2) + + local q1, q2 = evo.id(2) + assert(evo.insert(q1, evo.INCLUDES, f1)) + assert(evo.insert(q2, evo.INCLUDES, f2)) + + local e1, e2 = evo.id(2) + assert(evo.insert(e1, f1, f1)) + assert(evo.insert(e2, f2, f2)) + + assert(evo.batch_clear() == 0) + + assert(evo.is_alive(e1) and not evo.is_empty(e1)) + assert(evo.is_alive(e2) and not evo.is_empty(e2)) + + assert(evo.batch_clear(q1, q2, q1, q1) == 2) + + assert(evo.is_alive(e1) and evo.is_empty(e1)) + assert(evo.is_alive(e2) and evo.is_empty(e2)) +end + +do + local f1, f2 = evo.id(2) + + local q1, q2 = evo.id(2) + assert(evo.insert(q1, evo.INCLUDES, f1)) + assert(evo.insert(q2, evo.INCLUDES, f2)) + + local e1, e2 = evo.id(2) + assert(evo.insert(e1, f1, f1)) + assert(evo.insert(e2, f2, f2)) + + assert(evo.defer()) + do + assert(evo.batch_clear(q2, q1, q2, q2) == 0) + assert(evo.is_alive(e1) and not evo.is_empty(e1)) + assert(evo.is_alive(e2) and not evo.is_empty(e2)) + end + assert(evo.commit()) + + assert(evo.is_alive(e1) and evo.is_empty(e1)) + assert(evo.is_alive(e2) and evo.is_empty(e2)) +end + +do + local f1, f2 = evo.id(2) + + local q1, q2 = evo.id(2) + assert(evo.insert(q1, evo.INCLUDES, f1)) + assert(evo.insert(q2, evo.INCLUDES, f2)) + + local e1, e2 = evo.id(2) + assert(evo.insert(e1, f1, f1)) + assert(evo.insert(e2, f2, f2)) + + assert(evo.batch_destroy() == 0) + + assert(evo.is_alive(e1) and not evo.is_empty(e1)) + assert(evo.is_alive(e2) and not evo.is_empty(e2)) + + assert(evo.batch_destroy(q1, q2, q1, q1) == 2) + + assert(not evo.is_alive(e1) and evo.is_empty(e1)) + assert(not evo.is_alive(e2) and evo.is_empty(e2)) +end + +do + local f1, f2 = evo.id(2) + + local q1, q2 = evo.id(2) + assert(evo.insert(q1, evo.INCLUDES, f1)) + assert(evo.insert(q2, evo.INCLUDES, f2)) + + local e1, e2 = evo.id(2) + assert(evo.insert(e1, f1, f1)) + assert(evo.insert(e2, f2, f2)) + + assert(evo.defer()) + do + assert(evo.batch_destroy(q2, q1, q2, q2) == 0) + assert(evo.is_alive(e1) and not evo.is_empty(e1)) + assert(evo.is_alive(e2) and not evo.is_empty(e2)) + end + assert(evo.commit()) + + assert(not evo.is_alive(e1) and evo.is_empty(e1)) + assert(not evo.is_alive(e2) and evo.is_empty(e2)) +end diff --git a/evolved.lua b/evolved.lua index 6a6145a..c7a73f7 100644 --- a/evolved.lua +++ b/evolved.lua @@ -4342,38 +4342,146 @@ __defer_ops[__defer_op.batch_remove] = function(bytes, index) return 2 + fragment_count end ----@param query evolved.query -__defer_batch_clear = function(query) +---@param ... evolved.query queries +__defer_batch_clear = function(...) + local query_count = __lua_select('#', ...) + if query_count == 0 then return end + local length = __defer_length local bytecode = __defer_bytecode bytecode[length + 1] = __defer_op.batch_clear - bytecode[length + 2] = query + bytecode[length + 2] = query_count - __defer_length = length + 2 + if query_count == 0 then + -- nothing + elseif query_count == 1 then + local q1 = ... + bytecode[length + 3] = q1 + elseif query_count == 2 then + local q1, q2 = ... + bytecode[length + 3] = q1 + bytecode[length + 4] = q2 + elseif query_count == 3 then + local q1, q2, q3 = ... + bytecode[length + 3] = q1 + bytecode[length + 4] = q2 + bytecode[length + 5] = q3 + elseif query_count == 4 then + local q1, q2, q3, q4 = ... + bytecode[length + 3] = q1 + bytecode[length + 4] = q2 + bytecode[length + 5] = q3 + bytecode[length + 6] = q4 + else + local q1, q2, q3, q4 = ... + bytecode[length + 3] = q1 + bytecode[length + 4] = q2 + bytecode[length + 5] = q3 + bytecode[length + 6] = q4 + for i = 5, query_count do + bytecode[length + 2 + i] = __lua_select(i, ...) + end + end + + __defer_length = length + 2 + query_count end __defer_ops[__defer_op.batch_clear] = function(bytes, index) - local query = bytes[index + 0] - __evolved_batch_clear(query) - return 1 + local query_count = bytes[index + 0] + + if query_count == 0 then + -- nothing + elseif query_count == 1 then + local q1 = bytes[index + 1] + __evolved_batch_clear(q1) + elseif query_count == 2 then + local q1, q2 = bytes[index + 1], bytes[index + 2] + __evolved_batch_clear(q1, q2) + elseif query_count == 3 then + local q1, q2, q3 = bytes[index + 1], bytes[index + 2], bytes[index + 3] + __evolved_batch_clear(q1, q2, q3) + elseif query_count == 4 then + local q1, q2, q3, q4 = bytes[index + 1], bytes[index + 2], bytes[index + 3], bytes[index + 4] + __evolved_batch_clear(q1, q2, q3, q4) + else + local q1, q2, q3, q4 = bytes[index + 1], bytes[index + 2], bytes[index + 3], bytes[index + 4] + __evolved_batch_clear(q1, q2, q3, q4, + __lua_table_unpack(bytes, index + 5, index + 0 + query_count)) + end + + return 1 + query_count end ----@param query evolved.query -__defer_batch_destroy = function(query) +---@param ... evolved.query queries +__defer_batch_destroy = function(...) + local query_count = __lua_select('#', ...) + if query_count == 0 then return end + local length = __defer_length local bytecode = __defer_bytecode bytecode[length + 1] = __defer_op.batch_destroy - bytecode[length + 2] = query + bytecode[length + 2] = query_count - __defer_length = length + 2 + if query_count == 0 then + -- nothing + elseif query_count == 1 then + local q1 = ... + bytecode[length + 3] = q1 + elseif query_count == 2 then + local q1, q2 = ... + bytecode[length + 3] = q1 + bytecode[length + 4] = q2 + elseif query_count == 3 then + local q1, q2, q3 = ... + bytecode[length + 3] = q1 + bytecode[length + 4] = q2 + bytecode[length + 5] = q3 + elseif query_count == 4 then + local q1, q2, q3, q4 = ... + bytecode[length + 3] = q1 + bytecode[length + 4] = q2 + bytecode[length + 5] = q3 + bytecode[length + 6] = q4 + else + local q1, q2, q3, q4 = ... + bytecode[length + 3] = q1 + bytecode[length + 4] = q2 + bytecode[length + 5] = q3 + bytecode[length + 6] = q4 + for i = 5, query_count do + bytecode[length + 2 + i] = __lua_select(i, ...) + end + end + + __defer_length = length + 2 + query_count end __defer_ops[__defer_op.batch_destroy] = function(bytes, index) - local query = bytes[index + 0] - __evolved_batch_destroy(query) - return 1 + local query_count = bytes[index + 0] + + if query_count == 0 then + -- nothing + elseif query_count == 1 then + local q1 = bytes[index + 1] + __evolved_batch_destroy(q1) + elseif query_count == 2 then + local q1, q2 = bytes[index + 1], bytes[index + 2] + __evolved_batch_destroy(q1, q2) + elseif query_count == 3 then + local q1, q2, q3 = bytes[index + 1], bytes[index + 2], bytes[index + 3] + __evolved_batch_destroy(q1, q2, q3) + elseif query_count == 4 then + local q1, q2, q3, q4 = bytes[index + 1], bytes[index + 2], bytes[index + 3], bytes[index + 4] + __evolved_batch_destroy(q1, q2, q3, q4) + else + local q1, q2, q3, q4 = bytes[index + 1], bytes[index + 2], bytes[index + 3], bytes[index + 4] + __evolved_batch_destroy(q1, q2, q3, q4, + __lua_table_unpack(bytes, index + 5, index + 0 + query_count)) + end + + return 1 + query_count end ---@param query evolved.query @@ -6329,12 +6437,18 @@ __evolved_batch_remove = function(query, ...) return removed_count, false end ----@param query evolved.query +---@param ... evolved.query queries ---@return integer cleared_count ---@return boolean is_deferred -__evolved_batch_clear = function(query) +__evolved_batch_clear = function(...) + local argument_count = select('#', ...) + + if argument_count == 0 then + return 0, false + end + if __defer_depth > 0 then - __defer_batch_clear(query) + __defer_batch_clear(...) return 0, true end @@ -6346,9 +6460,13 @@ __evolved_batch_clear = function(query) local chunk_list = __acquire_table(__table_pool_tag.chunk_stack) local chunk_count = 0 - for chunk in __evolved_execute(query) do - chunk_count = chunk_count + 1 - chunk_list[chunk_count] = chunk + for argument_index = 1, argument_count do + local query = __lua_select(argument_index, ...) + + for chunk in __evolved_execute(query) do + chunk_count = chunk_count + 1 + chunk_list[chunk_count] = chunk + end end for i = 1, chunk_count do @@ -6363,12 +6481,18 @@ __evolved_batch_clear = function(query) return cleared_count, false end ----@param query evolved.query +---@param ... evolved.query queries ---@return integer destroyed_count ---@return boolean is_deferred -__evolved_batch_destroy = function(query) +__evolved_batch_destroy = function(...) + local argument_count = select('#', ...) + + if argument_count == 0 then + return 0, false + end + if __defer_depth > 0 then - __defer_batch_destroy(query) + __defer_batch_destroy(...) return 0, true end @@ -6380,9 +6504,13 @@ __evolved_batch_destroy = function(query) local chunk_list = __acquire_table(__table_pool_tag.chunk_stack) local chunk_count = 0 - for chunk in __evolved_execute(query) do - chunk_count = chunk_count + 1 - chunk_list[chunk_count] = chunk + for argument_index = 1, argument_count do + local query = __lua_select(argument_index, ...) + + for chunk in __evolved_execute(query) do + chunk_count = chunk_count + 1 + chunk_list[chunk_count] = chunk + end end for i = 1, chunk_count do