diff --git a/src/box/box.lua b/src/box/box.lua index 4a5a3f02ffe350bf1de48673cd24e2da77bd8bd4..02fa0e1e0b931baa73ac2885a01a9b7b99380a9e 100644 --- a/src/box/box.lua +++ b/src/box/box.lua @@ -259,11 +259,15 @@ function box.bless_space(space) space_mt.replace = function(space, ...) return box.replace(space.n, ...) end space_mt.delete = function(space, ...) return box.delete(space.n, ...) end space_mt.truncate = function(space) - local pk = space.index[0].idx - local part_count = pk:part_count() - while #pk > 0 do - for v in pk:iterator() do - space:delete(v:slice(0, part_count)) + local pk = space.index[0] + while #pk.idx > 0 do + for t in pk:iterator() do + local key = {}; + -- ipairs does not work because pk.key_field is zero-indexed + for _k2, key_field in pairs(pk.key_field) do + table.insert(key, t[key_field.fieldno]) + end + space:delete(unpack(key)) end end end diff --git a/test/big/lua.result b/test/big/lua.result index 8153bc52eb3d1ae0159a02129185989ec43c3e76..b0beddbe2dfac305dab8c64b0a356c33bd5e7bcf 100644 --- a/test/big/lua.result +++ b/test/big/lua.result @@ -549,3 +549,37 @@ error: 'Duplicate key exists in unique index 1' lua box.space[1]:truncate() --- ... +# +# A test case for Bug #1042798 +# Truncate hangs when primary key is not in linear or starts at the first field +# https://bugs.launchpad.net/tarantool/+bug/1042798 +# +lua for k, f in pairs(box.space[23].index[0].key_field) do print(k, ' => ', f.fieldno) end +--- +0 => 2 +1 => 1 +... +lua box.insert(23, 1, 2, 3, 4) +--- + - 1: {2, 3, 4} +... +lua box.insert(23, 10, 20, 30, 40) +--- + - 10: {20, 30, 40} +... +lua box.insert(23, 20, 30, 40, 50) +--- + - 20: {30, 40, 50} +... +lua for _k, v in box.space[23]:pairs() do print(v) end +--- +1: {2, 3, 4} +10: {20, 30, 40} +20: {30, 40, 50} +... +lua box.space[23]:truncate() +--- +... +lua for _k, v in box.space[23]:pairs() do print(v) end +--- +... diff --git a/test/big/lua.test b/test/big/lua.test index 115a30e68e799fb09ca634ba9b44e1b79e2b2324..e4c1c237efca9eb78ba066c2ed17952cd8272430 100644 --- a/test/big/lua.test +++ b/test/big/lua.test @@ -203,3 +203,20 @@ print """# Test that we print index number in error ER_INDEX_VIOLATION""" exec admin "lua box.space[1]:insert(1, 'hello', 'world')" exec admin "lua box.space[1]:insert(2, 'hello', 'world')" exec admin "lua box.space[1]:truncate()" + +print """# +# A test case for Bug #1042798 +# Truncate hangs when primary key is not in linear or starts at the first field +# https://bugs.launchpad.net/tarantool/+bug/1042798 +#""" + +# Print key fields in pk +exec admin "lua for k, f in pairs(box.space[23].index[0].key_field) do print(k, ' => ', f.fieldno) end" +exec admin "lua box.insert(23, 1, 2, 3, 4)" +exec admin "lua box.insert(23, 10, 20, 30, 40)" +exec admin "lua box.insert(23, 20, 30, 40, 50)" +exec admin "lua for _k, v in box.space[23]:pairs() do print(v) end" +# Truncate must not hang +exec admin "lua box.space[23]:truncate()" +# Empty result +exec admin "lua for _k, v in box.space[23]:pairs() do print(v) end" diff --git a/test/big/tarantool.cfg b/test/big/tarantool.cfg index b3334affc2dfc6e962f41ba0fe9bbbf8de2b063d..0d26c1670ffbb5a505a1777ba0fee83cdf68e872 100644 --- a/test/big/tarantool.cfg +++ b/test/big/tarantool.cfg @@ -328,3 +328,12 @@ space[22].index[3].type = "TREE" space[22].index[3].unique = true space[22].index[3].key_field[0].fieldno = 3 space[22].index[3].key_field[0].type = "NUM" + +# Space #23, https://bugs.launchpad.net/tarantool/+bug/1042798 +space[23].enabled = 1 +space[23].index[0].type = "TREE" +space[23].index[0].unique = 1 +space[23].index[0].key_field[0].fieldno = 2 +space[23].index[0].key_field[0].type = "NUM" +space[23].index[0].key_field[1].fieldno = 1 +space[23].index[0].key_field[1].type = "NUM"