Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix select() by space_no and index_name #142

Merged
merged 1 commit into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions src/tarantool.c
Original file line number Diff line number Diff line change
Expand Up @@ -631,21 +631,30 @@ int convert_iterator(zval *iter, int all) {
}

int get_spaceno_by_name(tarantool_connection *obj, zval *name) {
if (Z_TYPE_P(name) == IS_LONG)
return Z_LVAL_P(name);
if (Z_TYPE_P(name) != IS_STRING) {
if (Z_TYPE_P(name) != IS_STRING && Z_TYPE_P(name) != IS_LONG) {
Totktonada marked this conversation as resolved.
Show resolved Hide resolved
tarantool_throw_exception("Space ID must be String or Long");
return FAILURE;
}
int32_t space_no = tarantool_schema_get_sid_by_string(obj->schema,
Z_STRVAL_P(name), Z_STRLEN_P(name));
if (space_no != FAILURE)
return space_no;

int32_t space_no;
tarantool_tp_update(obj->tps);
tp_select(obj->tps, SPACE_SPACE, INDEX_SPACE_NAME, 0, 4096);
tp_key(obj->tps, 1);
tp_encode_str(obj->tps, Z_STRVAL_P(name), Z_STRLEN_P(name));
if (Z_TYPE_P(name) == IS_LONG) {
space_no = tarantool_schema_get_sid_by_number(obj->schema,
Z_LVAL_P(name));
if (space_no != -1)
return space_no;
tp_select(obj->tps, SPACE_SPACE, INDEX_SPACE_NO, 0, 4096);
tp_key(obj->tps, 1);
tp_encode_uint(obj->tps, Z_LVAL_P(name));
} else {
space_no = tarantool_schema_get_sid_by_string(obj->schema,
Z_STRVAL_P(name),
Z_STRLEN_P(name));
if (space_no != -1)
return space_no;
tp_select(obj->tps, SPACE_SPACE, INDEX_SPACE_NAME, 0, 4096);
tp_key(obj->tps, 1);
tp_encode_str(obj->tps, Z_STRVAL_P(name), Z_STRLEN_P(name));
}
tp_reqid(obj->tps, TARANTOOL_G(sync_counter)++);

obj->value->len = tp_used(obj->tps);
Expand Down Expand Up @@ -679,10 +688,22 @@ int get_spaceno_by_name(tarantool_connection *obj, zval *name) {
tarantool_throw_parsingexception("schema (space)");
return FAILURE;
}
space_no = tarantool_schema_get_sid_by_string(obj->schema,
Z_STRVAL_P(name), Z_STRLEN_P(name));
if (space_no == FAILURE)
THROW_EXC("No space '%s' defined", Z_STRVAL_P(name));

if (Z_TYPE_P(name) == IS_LONG) {
space_no = tarantool_schema_get_sid_by_number(obj->schema,
Z_LVAL_P(name));
if (space_no == -1) {
THROW_EXC("No space %d defined", Z_LVAL_P(name));
}
} else {
space_no = tarantool_schema_get_sid_by_string(obj->schema,
Z_STRVAL_P(name),
Z_STRLEN_P(name));
if (space_no == -1) {
THROW_EXC("No space '%s' defined", Z_STRVAL_P(name));
}
}

return space_no;
}

Expand Down
2 changes: 2 additions & 0 deletions src/tarantool_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#define SPACE_SPACE 281
#define SPACE_INDEX 289

#define INDEX_SPACE_NO 0
#define INDEX_INDEX_NO 0
#define INDEX_SPACE_NAME 2
#define INDEX_INDEX_NAME 2

Expand Down
19 changes: 19 additions & 0 deletions src/tarantool_schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,25 @@ tarantool_schema_get_sid_by_string(
return space->space_number;
}

int32_t
tarantool_schema_get_sid_by_number(
struct tarantool_schema *schema_obj,
uint32_t sid
) {
struct mh_schema_space_t *schema = schema_obj->space_hash;
struct schema_key space_key = {
(void *)&sid,
sizeof(uint32_t),
sid, /* ignored */
};
mh_int_t space_slot = mh_schema_space_find(schema, &space_key, NULL);
if (space_slot == mh_end(schema))
return -1;
const struct schema_space_value *space =
*mh_schema_space_node(schema, space_slot);
return space->space_number;
}

int32_t
tarantool_schema_get_iid_by_string(
struct tarantool_schema *schema_obj, uint32_t sid,
Expand Down
2 changes: 2 additions & 0 deletions src/tarantool_schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ int32_t
tarantool_schema_get_sid_by_string(struct tarantool_schema *, const char *,
uint32_t);
int32_t
tarantool_schema_get_sid_by_number(struct tarantool_schema *, uint32_t);
int32_t
tarantool_schema_get_iid_by_string(struct tarantool_schema *, uint32_t,
const char *, uint32_t);
int32_t
Expand Down
37 changes: 37 additions & 0 deletions test/DMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,41 @@ public function test_18_02_delete_loop() {
gc_collect_cycles();
gc_collect_cycles();
}

public function test_19_select_space_no_index_name_known() {
/*
* gh-42: 'Failed to parse schema (index)' at select()
* error when a space is passed as a number, but an index
* is passed as a name.
*
* The problem appears when a schema is not fetched yet,
* so let's test it on a new client instance.
*/
$port = TestHelpers::getTarantoolPort();
$tarantool = new Tarantool('localhost', $port, 'test');

$vindex_id = 289;
$res = $tarantool->select($vindex_id, [$vindex_id], 'primary', 1);
$this->assertEquals($res[0][0], $vindex_id);
}

public function test_20_select_space_no_index_name_unknown() {
/*
* Related to gh-42, see above. This case is to verify
* that the client gives an error in the case when unknown
* space id is passed (together with some index name).
*
* One of transient versions of gh-42 patch had a mistake
* on the error path that cause a segfault. This case
* triggers execution of the corresponding code.
*/
$port = TestHelpers::getTarantoolPort();
$tarantool = new Tarantool('localhost', $port, 'test');

$this->expectException(TarantoolException::class);
$this->expectExceptionMessage('No space 1024 defined');

$unknown_space_id = 1024;
$tarantool->select($unknown_space_id, [], 'primary');
}
}