You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are cases when a user has a map inside tuple's data somewhere, but it is not always created from the beginning. For example, want to create the map on its first update, when a first key is set. Or worse - the map might be somewhere in-place and empty, or sometimes be not here at all. Easily can happen if the map is a nullable field. However it is not possible to create the map + add a key to it atomically. See example (it is about updating tuples directly, but same is true for space updates):
tarantool> t1:update({{'=', '[1].key', 123}})
---
- [{'key': 123}]
...
tarantool> t2:update({{'=', '[1].key', 123}})
---
- error: Field ''[1].key'' was not found in the tuple
...
Attempt to add a new first operation {'=', '[1]', {}} would loose data if the map was already here and wasn't empty.
As a workaround the user has to get the tuple and either update it manually + make a replace, or add an optional {'=', <path>, {}} operation if the map is not created yet.
All the same is true for arrays.
The possible solutions are below.
New operations to ensure map/array existence
Add new operations which ensure that there is a map/array created. They are nop, if the map/array are already here, or create them if there is nil. Their opcodes could be { and [. Examples:
Basically, { works like = {} if the map is not here or it is nop. Same for [ and arrays. The operations fail if the field exists but it is not of the needed type ({ on an existing number or an array or other non-map type). They also fail if their JSON path following fails earlier than on the last token.
Create the JSON path parts automatically
If a JSON path following fails during an update, the missing parts are created on fly automatically. Examples:
The second example demonstrates how multiple missing parts of the path were created. It breaks backward compatibility though. Perhaps could add a new per-operation option like whether it should auto-create the path.
Hybrid solution
Add a new operation which ensures a passed path exists. Can either work like the first option + create all the missing parts of the path instead of failing, or could make a new operation which takes the value for the last token:
-- Create all parts of the <path> if they are missing.-- Set the value in the end of the path to be a map. Or fail if it already exists and is not a map.tuple:update({{'/', <path>, '{'}})
-- Same, but the value is an array.tuple:update({{'/', <path>, '['}})
There are cases when a user has a map inside tuple's data somewhere, but it is not always created from the beginning. For example, want to create the map on its first update, when a first key is set. Or worse - the map might be somewhere in-place and empty, or sometimes be not here at all. Easily can happen if the map is a nullable field. However it is not possible to create the map + add a key to it atomically. See example (it is about updating tuples directly, but same is true for space updates):
Attempt to add a new first operation
{'=', '[1]', {}}
would loose data if the map was already here and wasn't empty.As a workaround the user has to get the tuple and either update it manually + make a replace, or add an optional
{'=', <path>, {}}
operation if the map is not created yet.All the same is true for arrays.
The possible solutions are below.
New operations to ensure map/array existence
Add new operations which ensure that there is a map/array created. They are nop, if the map/array are already here, or create them if there is nil. Their opcodes could be
{
and[
. Examples:Basically,
{
works like= {}
if the map is not here or it is nop. Same for[
and arrays. The operations fail if the field exists but it is not of the needed type ({
on an existing number or an array or other non-map type). They also fail if their JSON path following fails earlier than on the last token.Create the JSON path parts automatically
If a JSON path following fails during an update, the missing parts are created on fly automatically. Examples:
The second example demonstrates how multiple missing parts of the path were created. It breaks backward compatibility though. Perhaps could add a new per-operation option like whether it should auto-create the path.
Hybrid solution
Add a new operation which ensures a passed path exists. Can either work like the first option + create all the missing parts of the path instead of failing, or could make a new operation which takes the value for the last token:
Other tickets about improving JSON updates: #6006, #5214, #5152, #3388.
The text was updated successfully, but these errors were encountered: