Skip to content

Commit fe7dfff

Browse files
author
Rishitha Kalicheti
committed
{181155186} stored procedure's cstring performance
Signed-off-by: Rishitha Kalicheti <[email protected]>
1 parent d156703 commit fe7dfff

File tree

14 files changed

+369
-10
lines changed

14 files changed

+369
-10
lines changed

lua/lapi.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,7 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
347347
if (!ttisstring(o)) {
348348
/* COMDB2 MODIFICATION */
349349
if (luabb_iscstring(L, idx)) {
350-
const char *s = luabb_tocstring(L, idx);
351-
if (len) *len = strlen(s);
350+
const char *s = luabb_tolcstring(L, idx, len);
352351
return s;
353352
}
354353
lua_lock(L); /* `luaV_tostring' may create a new string */

lua/ltypes.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -571,16 +571,16 @@ const char *luabb_tostring_noerr(Lua L, int idx)
571571
static int luabb_toblob_int(Lua lua, int idx, blob_t *ret)
572572
{
573573
const char *c;
574-
size_t s;
574+
size_t s = 0;
575575
int type = luabb_type(lua, idx);
576576
if (type == DBTYPES_LSTRING) {
577-
c = lua_tostring(lua, idx);
578-
str: s = strlen(c);
579-
ret->data = strdup(c);
577+
c = lua_tolstring(lua, idx, &s);
578+
str: ret->data = strdup(c);
580579
ret->length = s;
581580
return 0;
582581
} else if (type == DBTYPES_CSTRING) {
583582
c = ((lua_cstring_t*)lua_touserdata(lua, idx))->val;
583+
s = ((lua_cstring_t*)lua_touserdata(lua, idx))->len;
584584
goto str;
585585
} else if (type == DBTYPES_BLOB) {
586586
const lua_blob_t *blob = lua_topointer(lua, idx);
@@ -765,7 +765,8 @@ static int luabb_todatetime_int(lua_State *lua, int idx, datetime_t *ret)
765765
return 0;
766766
case DBTYPES_CSTRING:
767767
str = ((lua_cstring_t*)lua_topointer(lua, idx))->val;
768-
if (str_to_dttz(str, strlen(str), tzname, &dt, precision) != 0)
768+
int len = ((lua_cstring_t*)lua_topointer(lua, idx))->len;
769+
if (str_to_dttz(str, len, tzname, &dt, precision) != 0)
769770
goto err;
770771
break;
771772
case DBTYPES_REAL:
@@ -1394,6 +1395,7 @@ int l_cstring_new(lua_State *lua) {
13941395
lua_cstring_t *s;
13951396
new_lua_t(s, lua_cstring_t, DBTYPES_CSTRING);
13961397
s->val = NULL;
1398+
s->len = 0;
13971399
return 1;
13981400
}
13991401

@@ -2113,6 +2115,7 @@ void luabb_pushcstring(lua_State *lua, const char* cstrval) {
21132115
l_cstring_new(lua);
21142116
lua_cstring_t *s = (lua_cstring_t *) lua_topointer(lua, -1);
21152117
s->val = strdup(cstrval);
2118+
s->len = strlen(cstrval);
21162119
}
21172120

21182121
void luabb_pushcstringlen(lua_State *lua, const char* cstrval, int len)
@@ -2130,6 +2133,7 @@ void luabb_pushcstring_dl(lua_State *lua, const char* cstrval)
21302133
l_cstring_new(lua);
21312134
lua_cstring_t *s = (lua_cstring_t *) lua_topointer(lua, -1);
21322135
s->val = (char *)cstrval;
2136+
s->len = (int)strlen(cstrval);
21332137
}
21342138

21352139
void luabb_pushblob(lua_State *lua, const blob_t *val)

lua/ltypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ typedef struct {
134134
typedef struct {
135135
DBTYPES_COMMON;
136136
char* val;
137+
int len;
137138
} lua_cstring_t;
138139

139140
typedef struct {

lua/luaglue.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int luabb_dbtype_from_tvalue(struct lua_TValue *);
3939
int luabb_isnumber(struct lua_State *, int idx);
4040
double luabb_tonumber(struct lua_State *, int idx);
4141
int luabb_iscstring(struct lua_State *, int idx);
42-
const char *luabb_tocstring(struct lua_State *, int idx);
42+
const char *luabb_tolcstring(struct lua_State *, int idx, size_t *);
4343
int luabb_isnull(struct lua_State *, int idx);
4444
void luabb_tointeger(struct lua_State *, int idx, long long *val);
4545

lua/luautil.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ HashType luabb_hashinfo(void *udata, double *d, const char **c, size_t *l)
422422
if (c != NULL && l != NULL) {
423423
if (t->dbtype == DBTYPES_CSTRING) {
424424
*c = ((lua_cstring_t *)t)->val;
425-
*l = strlen(*c);
425+
*l = ((lua_cstring_t *)t)->len;
426426
} else {
427427
*c = ((lua_blob_t *)t)->val.data;
428428
*l = ((lua_blob_t *)t)->val.length;
@@ -582,12 +582,13 @@ int luabb_iscstring(Lua l, int idx)
582582
}
583583

584584
/* assumes luabb_iscstring is true */
585-
const char *luabb_tocstring(Lua l, int idx)
585+
const char *luabb_tolcstring(Lua l, int idx, size_t *len)
586586
{
587587
const TValue *o = index2adr(l, idx);
588588
lua_dbtypes_t *bb = luabb_todbpointer(o);
589589
if (bb->dbtype != DBTYPES_CSTRING) abort();
590590
lua_cstring_t *ptr = (lua_cstring_t *)bb;
591+
if (len != NULL) *len = ptr->len;
591592
return ptr->val;
592593
}
593594

lua/sp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4508,6 +4508,7 @@ static int db_setnull(Lua lua)
45084508
c = (lua_cstring_t *)t;
45094509
free(c->val);
45104510
c->val = NULL;
4511+
c->len = 0;
45114512
break;
45124513
case DBTYPES_BLOB:
45134514
b = (lua_blob_t *)t;

tests/sp_perf.test/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ifeq ($(TESTSROOTDIR),)
2+
include ../testcase.mk
3+
else
4+
include $(TESTSROOTDIR)/testcase.mk
5+
endif

tests/sp_perf.test/runit

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/bin/bash
2+
3+
bash -n "$0" | exit 1
4+
source ${TESTSROOTDIR}/tools/runit_common.sh
5+
6+
DBNAME=$1
7+
TESTNAME="sp_perf"
8+
output="$TESTNAME.out"
9+
expected="$TESTNAME.exp"
10+
tier="default"
11+
if [ -f $output ] ; then
12+
rm -f $output
13+
fi
14+
15+
if [[ "x${DBNAME}" == "x" ]] ; then
16+
echo "need a DB name"
17+
exit 1
18+
fi
19+
20+
longoremptystringTest() {
21+
22+
23+
sp=$(cat << 'EOF'
24+
CREATE PROCEDURE foo VERSION 'test' {
25+
local function main(x, input)
26+
db:setmaxinstructions(10000000)
27+
local t = {}
28+
local s = nil
29+
if input ~= nil then
30+
s = input
31+
else
32+
s = string.format("%s%s", string.rep('a,', 999999), 'a')
33+
end
34+
if x == 'cstring' then s = db:cast(s, 'cstring') end
35+
local start = db:now()
36+
for str in string.gmatch(s, "([^,]+)") do table.insert(t, str) end
37+
db:column_name("count", 1) db:column_name("time", 2) db:column_name("type", 3)
38+
db:emit({count = #t, time = db:now() - start, type = type(s)})
39+
end
40+
}
41+
EOF
42+
)
43+
${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "$sp"
44+
time ${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "exec procedure foo()"
45+
time ${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "exec procedure foo('cstring')"
46+
time ${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "exec procedure foo('cstring', 'spookytext')"
47+
time ${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "exec procedure foo('cstring', '')"
48+
}
49+
50+
51+
bindparamTest() {
52+
53+
$CDB2SQL_EXE ${CDB2_OPTIONS} $DBNAME $tier - <<EOF
54+
drop table if exists bindt
55+
create table bindt {
56+
schema {
57+
cstring i[32] null=yes
58+
cstring j[32] null=yes
59+
}}\$\$
60+
EOF
61+
62+
sp_bind_pos=$(cat << 'EOF'
63+
CREATE PROCEDURE bind_sp VERSION 'test' {
64+
local function main()
65+
local t, r
66+
t = db:prepare("INSERT INTO bindt(i, j) values(?,?)")
67+
t:bind(1, "1") -- bind by pos
68+
t:bind(2, "")
69+
t:exec()
70+
end
71+
}
72+
EOF
73+
)
74+
75+
sp_bind_name=$(cat << 'EOF'
76+
CREATE PROCEDURE bind_sp_named VERSION 'test' {
77+
local function main()
78+
local t, r, a, b
79+
t = db:prepare("INSERT INTO bindt(i, j) values(@a, @b)")
80+
t:bind("a", "5") -- bind by name
81+
t:bind("b", "6")
82+
t:exec()
83+
end
84+
}
85+
EOF
86+
)
87+
88+
${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "$sp_bind_pos"
89+
${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "exec procedure bind_sp()"
90+
${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "SELECT i, j FROM bindt"
91+
${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "$sp_bind_name"
92+
${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "exec procedure bind_sp_named()"
93+
${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier "SELECT i, j FROM bindt"
94+
${CDB2SQL_EXE} -r 1 ${CDB2_OPTIONS} $DBNAME $tier - <<EOF
95+
INSERT INTO bindt (i, j) VALUES ('5', NULL)
96+
INSERT INTO bindt (i, j) VALUES ('', '6')
97+
EOF
98+
99+
}
100+
101+
checkdiff() {
102+
if [ -f "$1" ]; then
103+
if ! diff -w $2 $1 > /dev/null 2>&1 ; then
104+
echo "diff:"
105+
diff -w $2 $1
106+
failexit "output $1 diff from expected $2"
107+
else
108+
echo "PASSED!"
109+
fi
110+
else
111+
failexit "ERR: No expected output file ($2) found for runit"
112+
fi
113+
114+
}
115+
116+
longoremptystringTest
117+
118+
bindparamTest >> "$output" 2>&1
119+
checkdiff "$output" "$expected"
120+
121+
${TESTSROOTDIR}/tools/compare_results.sh -s -d $DBNAME
122+
[ $? -eq 0 ] || exit 1
123+
124+
${TESTSBUILDDIR}/sp $DBNAME $tier >> "sp_bind.out" 2>&1
125+
[ $? -eq 0 ] || exit 1
126+
127+
checkdiff "sp_bind.out" "sp_bind.exp"
128+
129+
echo "Testcase passed."
130+
131+
exit 0
132+
133+

tests/sp_perf.test/sp_bind.exp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Procedure created successfully!
2+
Results from procedure:
3+
Row: result[1]="a (1)", result[2]=" (0)", result[3]="hello, world! (13)", result[4]="is "quoted" back there (22)"
4+
Results from procedure:
5+
Row: x="", s=""
6+
Results from procedure:
7+
Row: x="", s=""
8+
Results from procedure:
9+
Row: x="", s=""
10+
Procedure executed successfully!

tests/sp_perf.test/sp_perf.exp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[drop table if exists bindt] rc 0
2+
[create table bindt {
3+
schema {
4+
cstring i[32] null=yes
5+
cstring j[32] null=yes
6+
}}] rc 0
7+
(version='test')
8+
(i='1', j='')
9+
(version='test')
10+
(i='1', j='')
11+
(i='5', j='6')
12+
(rows inserted=1)
13+
[INSERT INTO bindt (i, j) VALUES ('5', NULL)] rc 0
14+
(rows inserted=1)
15+
[INSERT INTO bindt (i, j) VALUES ('', '6')] rc 0

0 commit comments

Comments
 (0)