@@ -31,9 +31,6 @@ local function bootstrap()
3131 box .schema .user .grant (' jepsen' , ' write' , ' space' , ' _schema' )
3232 box .schema .user .grant (' jepsen' , ' write' , ' space' , ' _space' )
3333
34- box .schema .space .create (' JEPSEN' , {engine = ' %TARANTOOL_DATA_ENGINE%' })
35- box .space [' JEPSEN' ]:create_index (' primary' , {parts = {1 , ' unsigned' }})
36-
3734--[[ Function implements a CAS (Compare And Set) operation, which takes a key,
3835old value, and new value and sets the key to the new value if and only if the
3936old value matches what's currently there, and returns a detailed response
@@ -44,61 +41,39 @@ box.schema.func.create('_CAS',
4441 {language = ' LUA' ,
4542 returns = ' boolean' ,
4643 body = [[ function(id, old_value, new_value, table)
47- local rc = false
48- box.begin()
49- local tuple = box.space[table]:get{id}
50- if tuple then
51- if tuple[2] == old_value then
52- box.space[table]:update({id}, {{'=', 2, new_value}})
53- rc = true
54- end
55- end
56- box.commit()
44+ local rc = false
45+ box.begin()
46+ local tuple = box.space[table]:get{id}
47+ if tuple then
48+ if tuple[2] == old_value then
49+ box.space[table]:update({id}, {{'=', 2, new_value}})
50+ rc = true
51+ end
52+ end
53+ box.commit()
5754
58- return rc
59- end]] ,
55+ return rc
56+ end]] ,
6057 is_sandboxed = false ,
6158 param_list = {' integer' , ' integer' , ' integer' , ' string' },
6259 exports = {' LUA' , ' SQL' },
6360 is_deterministic = true })
6461
65- --[[ Function implements an WRITE operation, which takes a key and value
66- and sets the key to the value if and only if the key is already exists, and
67- insert value if it is absent.
68- Example: SELECT _WRITE(1, 3, 'JEPSEN')
62+ --[[ Function implements a UPSERT operation, which takes a key and value
63+ and sets the key to the value if key exists or insert new key with that value.
64+ Example: SELECT _UPSERT(1, 3, 4, 'JEPSEN')
6965]]
70- box .schema .func .create (' _WRITE ' ,
66+ box .schema .func .create (' _UPSERT ' ,
7167 {language = ' LUA' ,
72- returns = ' integer ' ,
73- body = [[ function (id, value, table)
74- box.space[table]:upsert({id, value}, {{'=', 1, 1}, {'=', 2, value}})
75- return value
68+ returns = ' boolean ' ,
69+ body = [[ function(id, value, table)
70+ box.space[table]:upsert({id, value}, {{'=', 2, value}})
71+ return true
7672 end]] ,
7773 is_sandboxed = false ,
7874 param_list = {' integer' , ' integer' , ' string' },
7975 exports = {' LUA' , ' SQL' },
8076 is_deterministic = true })
81-
82- --[[ Function implements an READ operation, which takes a key and returns a
83- value.
84- Example: SELECT _READ(1, 'JEPSEN')
85- ]]
86- box .schema .func .create (' _READ' ,
87- {language = ' LUA' ,
88- returns = ' integer' ,
89- body = [[ function (id, table)
90- box.begin()
91- local tuple = box.space[table]:get{id}
92- if tuple then
93- return tuple[2]
94- end
95- box.commit()
96- return nil
97- end]] ,
98- is_sandboxed = false ,
99- param_list = {' integer' , " string" },
100- exports = {' LUA' , ' SQL' },
101- is_deterministic = true })
10277end
10378
10479box .once (' jepsen' , bootstrap )
0 commit comments