Skip to content

Commit

Permalink
Selecting by space_no/index_name shouldn't throw error now
Browse files Browse the repository at this point in the history
  • Loading branch information
MariaHajdic committed Jun 19, 2020
1 parent 21f638b commit f3ba38f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 19 deletions.
63 changes: 44 additions & 19 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) {
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 = -1;
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 != FAILURE)
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 != FAILURE)
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 All @@ -660,10 +669,11 @@ int get_spaceno_by_name(tarantool_connection *obj, zval *name) {
size_t body_size = php_mp_unpack_package_size(pack_len);
smart_string_ensure(obj->value, body_size);
if (tarantool_stream_read(obj, obj->value->c,
body_size) == FAILURE)
body_size) == FAILURE)
return FAILURE;

struct tnt_response resp; memset(&resp, 0, sizeof(struct tnt_response));
struct tnt_response resp;
memset(&resp, 0, sizeof(struct tnt_response));
if (php_tp_response(&resp, obj->value->c, body_size) == -1) {
tarantool_throw_parsingexception("query");
return FAILURE;
Expand All @@ -679,10 +689,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 == FAILURE) {
THROW_EXC("No space '%s' defined", Z_STRVAL_P(name));
}
} else {
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 '%ul' defined", Z_LVAL_P(name));
}
}

return space_no;
}

Expand All @@ -696,6 +718,7 @@ int get_indexno_by_name(tarantool_connection *obj, int space_no,
THROW_EXC("Index ID must be String or Long");
return FAILURE;
}

int32_t index_no = tarantool_schema_get_iid_by_string(obj->schema,
space_no, Z_STRVAL_P(name), Z_STRLEN_P(name));
if (index_no != FAILURE)
Expand All @@ -722,7 +745,8 @@ int get_indexno_by_name(tarantool_connection *obj, int space_no,
if (tarantool_stream_read(obj, obj->value->c, body_size) == FAILURE)
return FAILURE;

struct tnt_response resp; memset(&resp, 0, sizeof(struct tnt_response));
struct tnt_response resp;
memset(&resp, 0, sizeof(struct tnt_response));
if (php_tp_response(&resp, obj->value->c, body_size) == -1) {
tarantool_throw_parsingexception("query");
return FAILURE;
Expand Down Expand Up @@ -777,7 +801,8 @@ int get_fieldno_by_name(tarantool_connection *obj, uint32_t space_no,
if (tarantool_stream_read(obj, obj->value->c, body_size) == FAILURE)
return FAILURE;

struct tnt_response resp; memset(&resp, 0, sizeof(struct tnt_response));
struct tnt_response resp;
memset(&resp, 0, sizeof(struct tnt_response));
if (php_tp_response(&resp, obj->value->c, body_size) == -1) {
tarantool_throw_parsingexception("query");
return FAILURE;
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
18 changes: 18 additions & 0 deletions src/tarantool_schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,24 @@ 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), 0
};
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
9 changes: 9 additions & 0 deletions test/DMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,13 @@ public function test_18_02_delete_loop() {
gc_collect_cycles();
gc_collect_cycles();
}

/**
* @doesNotPerformAssertions
*/
public function test_19_schema_obtain_failed() {
$port = TestHelpers::getTarantoolPort();
$empty_tarantool = new Tarantool('localhost', $port, 'test');
$empty_tarantool->select(289, [], 'name');
}
}

0 comments on commit f3ba38f

Please sign in to comment.