From dba5d7e632f5f901735d2641c33de49271fcc78d Mon Sep 17 00:00:00 2001
From: Alexandr Lyapunov <a.lyapunov@corp.mail.ru>
Date: Tue, 9 Feb 2016 15:34:45 +0300
Subject: [PATCH] fixed gh-1323 : Index:count works too long

---
 src/box/memtx_index.cc |  2 +-
 test/box/misc.result   | 43 ++++++++++++++++++++++++++++++++++++++++++
 test/box/misc.test.lua | 15 +++++++++++++++
 3 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/src/box/memtx_index.cc b/src/box/memtx_index.cc
index 8c0da8c48d..a6170392a7 100644
--- a/src/box/memtx_index.cc
+++ b/src/box/memtx_index.cc
@@ -75,7 +75,7 @@ size_t
 MemtxIndex::count(enum iterator_type type, const char *key,
 		  uint32_t part_count) const
 {
-	if (type == ITER_ALL && key == NULL)
+	if (type == ITER_ALL)
 		return size(); /* optimization */
 	struct iterator *it = position();
 	initIterator(it, type, key, part_count);
diff --git a/test/box/misc.result b/test/box/misc.result
index b360a5403a..2947862f88 100644
--- a/test/box/misc.result
+++ b/test/box/misc.result
@@ -673,3 +673,46 @@ box.space.test:select{}
 box.space.test:drop()
 ---
 ...
+-- https://github.com/tarantool/tarantool/issues/1323
+-- index:count() work too long
+fiber = require('fiber')
+---
+...
+s = box.schema.create_space('test')
+---
+...
+i1 = s:create_index('test', {parts = {1, 'num'}})
+---
+...
+for i = 1,10000 do s:insert{i} end
+---
+...
+count = 0
+---
+...
+done = false
+---
+...
+function test1() for i = 1,100 do count = count + i1:count() end end
+---
+...
+function test2() for j = 1,100 do test1() fiber.sleep(0) end done = true  end
+---
+...
+fib = fiber.create(test2)
+---
+...
+for i = 1,10 do if done then break end fiber.sleep(0.1) end
+---
+...
+done and "count calculated fast enough" or "count is calculating too long"
+---
+- count calculated fast enough
+...
+count
+---
+- 100000000
+...
+box.space.test:drop()
+---
+...
diff --git a/test/box/misc.test.lua b/test/box/misc.test.lua
index f1ca900b8e..59313b6095 100644
--- a/test/box/misc.test.lua
+++ b/test/box/misc.test.lua
@@ -195,3 +195,18 @@ test_run:cmd("restart server default")
 box.space.test:select{}
 box.space.test:drop()
 
+-- https://github.com/tarantool/tarantool/issues/1323
+-- index:count() works too long
+fiber = require('fiber')
+s = box.schema.create_space('test')
+i1 = s:create_index('test', {parts = {1, 'num'}})
+for i = 1,10000 do s:insert{i} end
+count = 0
+done = false
+function test1() for i = 1,100 do count = count + i1:count() end end
+function test2() for j = 1,100 do test1() fiber.sleep(0) end done = true  end
+fib = fiber.create(test2)
+for i = 1,10 do if done then break end fiber.sleep(0.1) end
+done and "count calculated fast enough" or "count is calculating too long"
+count
+box.space.test:drop()
-- 
GitLab