Skip to content

PHPC-314: Implement type map syntax for documents within field paths #793

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

Merged
merged 12 commits into from
Apr 18, 2018
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
39 changes: 30 additions & 9 deletions php_bson.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,41 @@ typedef enum {
PHONGO_TYPEMAP_CLASS
} php_phongo_bson_typemap_types;

typedef enum {
PHONGO_FIELD_PATH_ITEM_NONE,
PHONGO_FIELD_PATH_ITEM_ARRAY,
PHONGO_FIELD_PATH_ITEM_DOCUMENT
} php_phongo_bson_field_path_item_types;

typedef struct {
char** elements;
php_phongo_bson_field_path_item_types* element_types;
size_t allocated_size;
size_t size;
size_t ref_count;
bool owns_elements;
} php_phongo_field_path;

typedef struct _php_phongo_field_path_map_element {
php_phongo_field_path* entry;
php_phongo_bson_typemap_types node_type;
zend_class_entry* node_ce;
} php_phongo_field_path_map_element;

typedef struct {
php_phongo_bson_typemap_types document_type;
zend_class_entry* document;
php_phongo_bson_typemap_types array_type;
zend_class_entry* array;
php_phongo_bson_typemap_types root_type;
zend_class_entry* root;
struct {
php_phongo_field_path_map_element** map;
size_t allocated_size;
size_t size;
} field_paths;
} php_phongo_bson_typemap;

typedef struct {
const char** elements;
size_t allocated_levels;
size_t current_level;
size_t ref_count;
} php_phongo_field_path;

typedef struct {
ZVAL_RETVAL_TYPE zchild;
php_phongo_bson_typemap map;
Expand Down Expand Up @@ -87,11 +106,13 @@ bool php_phongo_bson_typemap_to_state(zval* typemap, php_phongo_bson_typemap* ma
void php_phongo_bson_state_ctor(php_phongo_bson_state* state);
void php_phongo_bson_state_dtor(php_phongo_bson_state* state);
void php_phongo_bson_state_copy_ctor(php_phongo_bson_state* dst, php_phongo_bson_state* src);
void php_phongo_bson_typemap_dtor(php_phongo_bson_typemap* map);

php_phongo_field_path* php_phongo_field_path_alloc(void);
php_phongo_field_path* php_phongo_field_path_alloc(bool owns_elements);
void php_phongo_field_path_free(php_phongo_field_path* field_path);
void php_phongo_field_path_write_item_at_current_level(php_phongo_field_path* field_path, const char* element);
bool php_phongo_field_path_push(php_phongo_field_path* field_path, const char* element);
void php_phongo_field_path_write_type_at_current_level(php_phongo_field_path* field_path, php_phongo_bson_field_path_item_types element_type);
bool php_phongo_field_path_push(php_phongo_field_path* field_path, const char* element, php_phongo_bson_field_path_item_types element_type);
bool php_phongo_field_path_pop(php_phongo_field_path* field_path);

char* php_phongo_field_path_as_string(php_phongo_field_path* field_path);
Expand Down
3 changes: 3 additions & 0 deletions src/BSON/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ PHP_FUNCTION(MongoDB_BSON_toPHP)

if (!php_phongo_bson_to_zval_ex((const unsigned char*) data, data_len, &state)) {
zval_ptr_dtor(&state.zchild);
php_phongo_bson_typemap_dtor(&state.map);
RETURN_NULL();
}

php_phongo_bson_typemap_dtor(&state.map);

#if PHP_VERSION_ID >= 70000
RETURN_ZVAL(&state.zchild, 0, 1);
#else
Expand Down
4 changes: 4 additions & 0 deletions src/MongoDB/Cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ static PHP_METHOD(Cursor, setTypeMap)
restore_current_element = true;
}

php_phongo_bson_typemap_dtor(&intern->visitor_data.map);

intern->visitor_data = state;

/* If the cursor has a current element, we just freed it and should restore
Expand Down Expand Up @@ -398,6 +400,8 @@ static void php_phongo_cursor_free_object(phongo_free_object_arg* object TSRMLS_
zval_ptr_dtor(&intern->session);
}

php_phongo_bson_typemap_dtor(&intern->visitor_data.map);

php_phongo_cursor_free_current(intern);

#if PHP_VERSION_ID < 70000
Expand Down
12 changes: 7 additions & 5 deletions src/bson-encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,10 @@ static void php_phongo_bson_append(bson_t* bson, php_phongo_field_path* field_pa
}

bson_append_array_begin(bson, key, key_len, &child);
field_path->current_level++;
php_phongo_field_path_write_type_at_current_level(field_path, PHONGO_FIELD_PATH_ITEM_ARRAY);
field_path->size++;
php_phongo_zval_to_bson_internal(entry, field_path, flags, &child, NULL TSRMLS_CC);
field_path->current_level--;
field_path->size--;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to have macros or inline functions for incrementing and decrementing size?

Does php_phongo_field_path_write_type_at_current_level() ensure that there is always sufficient buffer space to increment size?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it does. I can make it a macro, but as we're only doing this twice, I didn't see much use of it. Happy to do so if you would prefer it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK with keeping this as-is if it's only two uses.

bson_append_array_end(bson, &child);

if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) {
Expand All @@ -391,9 +392,10 @@ static void php_phongo_bson_append(bson_t* bson, php_phongo_field_path* field_pa
ZEND_HASH_INC_APPLY_COUNT(tmp_ht);
}

field_path->current_level++;
php_phongo_field_path_write_type_at_current_level(field_path, PHONGO_FIELD_PATH_ITEM_DOCUMENT);
field_path->size++;
php_phongo_bson_append_object(bson, field_path, flags, key, key_len, entry TSRMLS_CC);
field_path->current_level--;
field_path->size--;

if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) {
ZEND_HASH_DEC_APPLY_COUNT(tmp_ht);
Expand Down Expand Up @@ -648,7 +650,7 @@ static void php_phongo_zval_to_bson_internal(zval* data, php_phongo_field_path*
* will be used. */
void php_phongo_zval_to_bson(zval* data, php_phongo_bson_flags_t flags, bson_t* bson, bson_t** bson_out TSRMLS_DC) /* {{{ */
{
php_phongo_field_path* field_path = php_phongo_field_path_alloc();
php_phongo_field_path* field_path = php_phongo_field_path_alloc(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is php_phongo_field_path* field_path something that was introduced when you improved BSON exception messages to report the entire field path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.


php_phongo_zval_to_bson_internal(data, field_path, flags, bson, bson_out TSRMLS_CC);

Expand Down
Loading