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
bigbes committed Dec 2, 2018
1 parent d62eb1a commit 0b2db7f
Show file tree
Hide file tree
Showing 5 changed files with 93 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 @@ -630,21 +630,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 @@ -659,10 +668,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 @@ -678,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 == 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 @@ -695,6 +717,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 @@ -721,7 +744,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 @@ -776,7 +800,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
34 changes: 34 additions & 0 deletions src/tarantool_schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,21 @@ schema_add_space(
return -1;
}

int
tarantool_schema_has_space_no(
struct tarantool_schema *schema_obj,
uint32_t space_no) {
/* Prepare key for space search */
struct schema_key space_key;
space_key.number = space_no;
space_key.id = (void *)&(space_key.number);
space_key.id_len = sizeof(uint32_t);

struct mh_schema_space_t *schema = schema_obj->space_hash;
mh_int_t space_slot = mh_schema_space_find(schema, &space_key, NULL);
return (space_slot != mh_end(schema));
}

int
tarantool_schema_add_spaces(
struct tarantool_schema *schema_obj,
Expand Down Expand Up @@ -557,6 +572,25 @@ tarantool_schema_get_sid_by_string(
return space->space_number;
}

/* ALIAS for verify existens */
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
5 changes: 5 additions & 0 deletions src/tarantool_schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ struct tarantool_schema {
struct mh_schema_space_t *space_hash;
};

int
tarantool_schema_has_space_no(struct tarantool_schema *, uint32_t);

int
tarantool_schema_add_spaces(struct tarantool_schema *, const char *, uint32_t);
int
Expand All @@ -56,6 +59,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
8 changes: 8 additions & 0 deletions test/DMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,12 @@ public function test_18_02_delete_loop() {
gc_collect_cycles();
gc_collect_cycles();
}

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

0 comments on commit 0b2db7f

Please sign in to comment.