lua-resty-tarantool icon indicating copy to clipboard operation
lua-resty-tarantool copied to clipboard

Insert with sequence index

Open aiibe opened this issue 5 years ago • 2 comments

I tried to insert new tuples using the sequence index, but I got this error: Invalid MsgPack - packet body

local res, err = tar:insert('projects', { 16, { name = 'Festival 2018' } }) -- Works fine
...

With a nil primary index , I expected Tarantool to auto generate it...

local res, err = tar:insert('projects', { nil, { name = 'Festival 2018' } })
if err return nil, err  -- Got above error
...

which works with tarantoolctl using this command:

localhost:3301 >> box.space.projects:insert{nil, 'other stuff'}
--- [17, 'other stuff']

Do I have to specify a primary index everytime to insert new tuple? Does lua-resty-tarantool support Sequence ?

aiibe avatar Oct 10 '18 00:10 aiibe

Hi @aiibe. Did you solve the problem?

affair avatar Oct 11 '19 20:10 affair

This seems to be a problem with lua-MessagePack, which turns a table like {nil, "foo", "bar"} into a messagepack map. To illustrate, this is the JSON representation of the returned MessagePack data:

{
  "2": "foo",
  "3": "bar"
}

This is what ticks tarantool off, because it expects an array and gets a map.

Sadly though, this is incredibly tricky to fix. lua-MessagePack provides no way (at least no documented way) of hinting that a table is a sparse array. There's a global option to enable holes in arrays, but this seems to completely break most other parts of the protocol.

Why the author of that library decided it would be a good idea to set such options globally and not allow any overrides in the function call is beyond me.


Okay, so much for my investigation; how could this be fixed?

The only thing that seems realistic to me is to move away from lua-MessagePack and switch to another, less poorly designed alternative. I tried this a while ago and ultimately dropped the idea after openresty segfaulted while trying to load that library (yay C-extensions!).

One way or another, fixing that problem will most likely be a rather large pull-request and take lots of work. I wouldn't count on it happening anytime soon.


Might there be a workaround?

I don't know, but it might work to just increment the sequence manually and use the returned value as the index. It's ugly, but it might at least work.

DarkWiiPlayer avatar Jun 24 '20 09:06 DarkWiiPlayer