@@ -31,9 +31,6 @@ local function bootstrap()
31
31
box .schema .user .grant (' jepsen' , ' write' , ' space' , ' _schema' )
32
32
box .schema .user .grant (' jepsen' , ' write' , ' space' , ' _space' )
33
33
34
- box .schema .space .create (' JEPSEN' , {engine = ' %TARANTOOL_DATA_ENGINE%' })
35
- box .space [' JEPSEN' ]:create_index (' primary' , {parts = {1 , ' unsigned' }})
36
-
37
34
--[[ Function implements a CAS (Compare And Set) operation, which takes a key,
38
35
old value, and new value and sets the key to the new value if and only if the
39
36
old value matches what's currently there, and returns a detailed response
@@ -44,61 +41,39 @@ box.schema.func.create('_CAS',
44
41
{language = ' LUA' ,
45
42
returns = ' boolean' ,
46
43
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()
57
54
58
- return rc
59
- end]] ,
55
+ return rc
56
+ end]] ,
60
57
is_sandboxed = false ,
61
58
param_list = {' integer' , ' integer' , ' integer' , ' string' },
62
59
exports = {' LUA' , ' SQL' },
63
60
is_deterministic = true })
64
61
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')
69
65
]]
70
- box .schema .func .create (' _WRITE ' ,
66
+ box .schema .func .create (' _UPSERT ' ,
71
67
{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
76
72
end]] ,
77
73
is_sandboxed = false ,
78
74
param_list = {' integer' , ' integer' , ' string' },
79
75
exports = {' LUA' , ' SQL' },
80
76
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 })
102
77
end
103
78
104
79
box .once (' jepsen' , bootstrap )
0 commit comments