Branch: refs/heads/master
  Home:   https://github.com/mailru/tarantool
  Commit: 26428df17be5e877bed6363f7209ad3169795356
      
https://github.com/mailru/tarantool/commit/26428df17be5e877bed6363f7209ad3169795356
  Author: Konstantin Osipov <[email protected]>
  Date:   2012-05-05 (Sat, 05 May 2012)

  Changed paths:
    M doc/user/stored-procedures.xml
    M mod/box/box.lua
    M test/box_big/tree_pk_multipart.test

  Log Message:
  -----------
  box.update(): make it work with multipart keys. Add a test.


diff --git a/doc/user/stored-procedures.xml b/doc/user/stored-procedures.xml
index 8d86e36..734c193 100644
--- a/doc/user/stored-procedures.xml
+++ b/doc/user/stored-procedures.xml
@@ -363,7 +363,8 @@ localhost> lua box.select(5, 1, 'firstname', 'lastname')
         <listitem>
             <para>
                 Update a tuple identified by a primary
-                <code>key</code>. Update arguments follow,
+                <code>key</code>. If a key is multipart,
+                it is passed in as a Lua table. Update arguments follow,
                 described by <code>format</code>.
                 The format and arguments are passed to
                 <code>box.pack()</code> and the result is sent
diff --git a/mod/box/box.lua b/mod/box/box.lua
index 94f31c6..f88782e 100644
--- a/mod/box/box.lua
+++ b/mod/box/box.lua
@@ -2,17 +2,13 @@
 -- A run-time error will be raised on attempt to change
 -- table members.
 local function create_const_table(table)
-    return setmetatable ({}, {
-                            __index = table,
-                            __newindex = function(table_arg,
-                                                  name_arg,
-                                                  value_arg)
-                                error("attempting to change constant " ..
-                                      tostring(name_arg) ..
-                                      " to "
-                                      .. tostring(value_arg), 2)
-                            end
-                            })
+    local function newindex(table, name, value)
+        error("Attempt to change constant "..tostring(name)..
+              " to "..tostring(value))
+    end
+    return setmetatable({}, { __index = table,
+                              __newindex = newindex,
+                              __metatable = false })
 end
 
 --- box flags
@@ -115,14 +111,19 @@ end
 --
 function box.update(space, key, format, ...)
     local op_count = select('#', ...)/2
-    return box.process(19,
-                       box.pack('iiipi'..format,
-                                  space,
-                                  1, -- flags, BOX_RETURN_TUPLE
-                                  1, -- primary key part count
-                                  key, -- primary key
-                                  op_count, -- op count
-                                  ...))
+    if type(key) == 'table' then
+        part_count = #key
+        return box.process(19,
+                    box.pack('iii'..string.rep('p', part_count),
+                        space, box.flags.BOX_RETURN_TUPLE, part_count,
+                        unpack(key))..
+                    box.pack('i'..format, op_count, ...))
+    else
+        return box.process(19,
+                    box.pack('iiipi'..format,
+                        space, box.flags.BOX_RETURN_TUPLE, 1,
+                        key, op_count, ...))
+    end
 end
 
 box.upd = {}
@@ -332,3 +333,5 @@ os.rename = nil
 os.tmpname = nil
 os.remove = nil
 require = nil
+
+-- vim: set et ts=4 sts
diff --git a/test/box_big/tree_pk_multipart.test 
b/test/box_big/tree_pk_multipart.test
index 648b2bf..5995946 100644
--- a/test/box_big/tree_pk_multipart.test
+++ b/test/box_big/tree_pk_multipart.test
@@ -78,6 +78,8 @@ exec admin "lua box.delete(9, 'The Wolf!', 'Vincent', 0)"
 exec admin "lua box.delete(9, 'The Wolf!', 'Vincent', 3)"
 exec admin "lua box.delete(9, 'Vincent', 'The Wolf!', 0)"
 
+exec admin "lua box.update(9, {'Vincent', 'The Wolf!', 1}, '=p=p', 0, 
'Updated', 4, 'New')"
+exec admin "lua box.update(9, {'Updated', 'The Wolf!', 1}, '=p#p', 0, 
'Vincent', 4, '')"
 # Checking Vincent's last messages
 exec admin "lua box.select(9, 0, 'Vincent', 'The Wolf!')"
 # Checking The Wolf's last messages


================================================================

_______________________________________________
Mailing list: https://launchpad.net/~tarantool-developers
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~tarantool-developers
More help   : https://help.launchpad.net/ListHelp

Reply via email to