3232
3333--- Update or insert a tuple in the specified space
3434--
35- -- @function call
35+ -- @function tuple
3636--
3737-- @param string space_name
3838-- A space name
3939--
40- -- @param table obj
41- -- Tuple object (according to space format)
40+ -- @param table tuple
41+ -- Tuple
4242--
4343-- @param table user_operations
4444-- user_operations to be performed.
45- -- See `space:update ` operations in Tarantool doc
45+ -- See `space:upsert() ` operations in Tarantool doc
4646--
4747-- @tparam ?number opts.timeout
4848-- Function call timeout
4949--
50- -- @return [1] object
50+ -- @return [1] tuple
5151-- @treturn [2] nil
5252-- @treturn [2] table Error description
5353--
54- function upsert .call (space_name , obj , user_operations , opts )
54+ function upsert .tuple (space_name , tuple , user_operations , opts )
5555 checks (' string' , ' ?' , ' table' , {
5656 timeout = ' ?number' ,
5757 })
@@ -69,12 +69,6 @@ function upsert.call(space_name, obj, user_operations, opts)
6969 return nil , UpsertError :new (" Wrong operations are specified: %s" , err )
7070 end
7171
72- -- compute default bucket_id
73- local tuple , err = utils .flatten (obj , space_format )
74- if err ~= nil then
75- return nil , UpsertError :new (" Object is specified in bad format: %s" , err )
76- end
77-
7872 local key = utils .extract_key (tuple , space .index [0 ].parts )
7973
8074 local bucket_id = vshard .router .bucket_id_strcrc32 (key )
@@ -83,11 +77,17 @@ function upsert.call(space_name, obj, user_operations, opts)
8377 return nil , UpsertError :new (" Failed to get replicaset for bucket_id %s: %s" , bucket_id , err .err )
8478 end
8579
86- local tuple , err = utils .flatten ( obj , space_format , bucket_id )
80+ local bucket_id_fieldno , err = utils .get_bucket_id_fieldno ( space )
8781 if err ~= nil then
88- return nil , UpsertError :new (" Object is specified in bad format: %s" , err )
82+ return nil , err
83+ end
84+
85+ if tuple [bucket_id_fieldno ] ~= nil then
86+ return nil , UpsertError :new (" Unexpected value (%s) at field %s (sharding key)" ,
87+ tuple [bucket_id_fieldno ], bucket_id_fieldno )
8988 end
9089
90+ tuple [bucket_id_fieldno ] = bucket_id
9191 local _ , err = call .rw (UPSERT_FUNC_NAME , {space_name , tuple , operations }, {
9292 replicasets = {replicaset },
9393 timeout = opts .timeout ,
@@ -104,4 +104,47 @@ function upsert.call(space_name, obj, user_operations, opts)
104104 }
105105end
106106
107+ --- Update or insert an object in the specified space
108+ --
109+ -- @function object
110+ --
111+ -- @param string space_name
112+ -- A space name
113+ --
114+ -- @param table obj
115+ -- Object
116+ --
117+ -- @param table user_operations
118+ -- user_operations to be performed.
119+ -- See `space:upsert()` operations in Tarantool doc
120+ --
121+ -- @tparam ?number opts.timeout
122+ -- Function call timeout
123+ --
124+ -- @return [1] object
125+ -- @treturn [2] nil
126+ -- @treturn [2] table Error description
127+ --
128+ function upsert .object (space_name , obj , user_operations , opts )
129+ checks (' string' , ' ?' , ' table' , {
130+ timeout = ' ?number' ,
131+ })
132+
133+ opts = opts or {}
134+
135+ local space = utils .get_space (space_name , vshard .router .routeall ())
136+ if space == nil then
137+ return nil , UpsertError :new (" Space %q doesn't exists" , space_name )
138+ end
139+
140+ local space_format = space :format ()
141+
142+ local tuple , err = utils .flatten (obj , space_format )
143+ if err ~= nil then
144+ return nil , UpsertError :new (" Object is specified in bad format: %s" , err )
145+ end
146+
147+ return upsert .tuple (space_name , tuple , user_operations , opts )
148+ end
149+
107150return upsert
0 commit comments