From 2124636cbe5d616174949b95e6f6a9a446ec9bf8 Mon Sep 17 00:00:00 2001
From: Dmitry Isaikin <isaikin@corp.mail.ru>
Date: Wed, 20 Feb 2013 17:05:52 +0400
Subject: [PATCH] - box.auto_increment works with NUM64 keys - auto_increment
 key start from '1' now

Conflicts:

	test/big/tarantool.cfg
---
 src/box/box.lua        | 14 +++++++++++---
 test/big/lua.result    | 24 +++++++++++++++++++++++-
 test/big/lua.test      | 10 ++++++++++
 test/big/tarantool.cfg |  7 +++++++
 4 files changed, 51 insertions(+), 4 deletions(-)

diff --git a/src/box/box.lua b/src/box/box.lua
index 372c0792f0..1ee6e13af4 100644
--- a/src/box/box.lua
+++ b/src/box/box.lua
@@ -102,15 +102,23 @@ function box.update(space, key, format, ...)
                                 ...))
 end
 
--- Assumes that spaceno has a TREE int32 (NUM) primary key
+-- Assumes that spaceno has a TREE int32 (NUM) or int64 (NUM64) primary key
 -- inserts a tuple after getting the next value of the
 -- primary key and returns it back to the user
 function box.auto_increment(spaceno, ...)
     spaceno = tonumber(spaceno)
     local max_tuple = box.space[spaceno].index[0].idx:max()
-    local max = -1
+    local max = 0
     if max_tuple ~= nil then
-        max = box.unpack('i', max_tuple[0])
+        max = max_tuple[0]
+        local fmt = 'i'
+        if #max == 8 then fmt = 'l' end
+        max = box.unpack(fmt, max)
+    else
+        -- first time
+        if box.space[spaceno].index[0].key_field[0].type == "NUM64" then
+            max = tonumber64(max)
+        end
     end
     return box.insert(spaceno, max + 1, ...)
 end
diff --git a/test/big/lua.result b/test/big/lua.result
index e6b2e32616..ec58da48ab 100644
--- a/test/big/lua.result
+++ b/test/big/lua.result
@@ -327,7 +327,7 @@ lua box.space[18]:truncate()
 ...
 lua box.auto_increment(18, 'a')
 ---
- - 0: {'a'}
+ - 1: {'a'}
 ...
 lua box.insert(18, 5)
 ---
@@ -344,6 +344,28 @@ lua box.auto_increment(18, 'c')
 lua box.space[18]:truncate()
 ---
 ...
+lua box.space[25]:truncate()
+---
+...
+lua box.auto_increment(25, 'a')
+---
+ - 1: {'a'}
+...
+lua box.insert(25, tonumber64(5))
+---
+ - 5: {}
+...
+lua box.auto_increment(25, 'b')
+---
+ - 6: {'b'}
+...
+lua box.auto_increment(25, 'c')
+---
+ - 7: {'c'}
+...
+lua box.space[25]:truncate()
+---
+...
 lua t=box.insert(12, '1', '2', '3', '4', '5', '6', '7')
 ---
 ...
diff --git a/test/big/lua.test b/test/big/lua.test
index 2f90fb5ef1..1dca02e130 100644
--- a/test/big/lua.test
+++ b/test/big/lua.test
@@ -125,6 +125,16 @@ exec admin "lua box.auto_increment(18, 'b')"
 exec admin "lua box.auto_increment(18, 'c')"
 exec admin "lua box.space[18]:truncate()"
 
+#
+# Tests for lua box.auto_increment with NUM64 keys
+#
+exec admin "lua box.space[25]:truncate()"
+exec admin "lua box.auto_increment(25, 'a')"
+exec admin "lua box.insert(25, tonumber64(5))"
+exec admin "lua box.auto_increment(25, 'b')"
+exec admin "lua box.auto_increment(25, 'c')"
+exec admin "lua box.space[25]:truncate()"
+
 #
 # Tests for lua tuple:transform()
 #
diff --git a/test/big/tarantool.cfg b/test/big/tarantool.cfg
index 06c6c576a7..d1d62e138a 100644
--- a/test/big/tarantool.cfg
+++ b/test/big/tarantool.cfg
@@ -252,3 +252,10 @@ 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"
+
+# lua box.auto_increment() with NUM64 keys testing
+space[25].enabled = 1
+space[25].index[0].type = "TREE"
+space[25].index[0].unique = 1
+space[25].index[0].key_field[0].fieldno = 0
+space[25].index[0].key_field[0].type = "NUM64"
-- 
GitLab