Skip to content

Commit eab4a21

Browse files
committed
Merged pull request #793
2 parents 3fad0c5 + da8853a commit eab4a21

14 files changed

+1195
-42
lines changed

php_bson.h

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,41 @@ typedef enum {
4040
PHONGO_TYPEMAP_CLASS
4141
} php_phongo_bson_typemap_types;
4242

43+
typedef enum {
44+
PHONGO_FIELD_PATH_ITEM_NONE,
45+
PHONGO_FIELD_PATH_ITEM_ARRAY,
46+
PHONGO_FIELD_PATH_ITEM_DOCUMENT
47+
} php_phongo_bson_field_path_item_types;
48+
49+
typedef struct {
50+
char** elements;
51+
php_phongo_bson_field_path_item_types* element_types;
52+
size_t allocated_size;
53+
size_t size;
54+
size_t ref_count;
55+
bool owns_elements;
56+
} php_phongo_field_path;
57+
58+
typedef struct _php_phongo_field_path_map_element {
59+
php_phongo_field_path* entry;
60+
php_phongo_bson_typemap_types node_type;
61+
zend_class_entry* node_ce;
62+
} php_phongo_field_path_map_element;
63+
4364
typedef struct {
4465
php_phongo_bson_typemap_types document_type;
4566
zend_class_entry* document;
4667
php_phongo_bson_typemap_types array_type;
4768
zend_class_entry* array;
4869
php_phongo_bson_typemap_types root_type;
4970
zend_class_entry* root;
71+
struct {
72+
php_phongo_field_path_map_element** map;
73+
size_t allocated_size;
74+
size_t size;
75+
} field_paths;
5076
} php_phongo_bson_typemap;
5177

52-
typedef struct {
53-
const char** elements;
54-
size_t allocated_levels;
55-
size_t current_level;
56-
size_t ref_count;
57-
} php_phongo_field_path;
58-
5978
typedef struct {
6079
ZVAL_RETVAL_TYPE zchild;
6180
php_phongo_bson_typemap map;
@@ -87,11 +106,13 @@ bool php_phongo_bson_typemap_to_state(zval* typemap, php_phongo_bson_typemap* ma
87106
void php_phongo_bson_state_ctor(php_phongo_bson_state* state);
88107
void php_phongo_bson_state_dtor(php_phongo_bson_state* state);
89108
void php_phongo_bson_state_copy_ctor(php_phongo_bson_state* dst, php_phongo_bson_state* src);
109+
void php_phongo_bson_typemap_dtor(php_phongo_bson_typemap* map);
90110

91-
php_phongo_field_path* php_phongo_field_path_alloc(void);
111+
php_phongo_field_path* php_phongo_field_path_alloc(bool owns_elements);
92112
void php_phongo_field_path_free(php_phongo_field_path* field_path);
93113
void php_phongo_field_path_write_item_at_current_level(php_phongo_field_path* field_path, const char* element);
94-
bool php_phongo_field_path_push(php_phongo_field_path* field_path, const char* element);
114+
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);
115+
bool php_phongo_field_path_push(php_phongo_field_path* field_path, const char* element, php_phongo_bson_field_path_item_types element_type);
95116
bool php_phongo_field_path_pop(php_phongo_field_path* field_path);
96117

97118
char* php_phongo_field_path_as_string(php_phongo_field_path* field_path);

src/BSON/functions.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ PHP_FUNCTION(MongoDB_BSON_toPHP)
7474

7575
if (!php_phongo_bson_to_zval_ex((const unsigned char*) data, data_len, &state)) {
7676
zval_ptr_dtor(&state.zchild);
77+
php_phongo_bson_typemap_dtor(&state.map);
7778
RETURN_NULL();
7879
}
7980

81+
php_phongo_bson_typemap_dtor(&state.map);
82+
8083
#if PHP_VERSION_ID >= 70000
8184
RETURN_ZVAL(&state.zchild, 0, 1);
8285
#else

src/MongoDB/Cursor.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ static PHP_METHOD(Cursor, setTypeMap)
225225
restore_current_element = true;
226226
}
227227

228+
php_phongo_bson_typemap_dtor(&intern->visitor_data.map);
229+
228230
intern->visitor_data = state;
229231

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

403+
php_phongo_bson_typemap_dtor(&intern->visitor_data.map);
404+
401405
php_phongo_cursor_free_current(intern);
402406

403407
#if PHP_VERSION_ID < 70000

src/bson-encode.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,10 @@ static void php_phongo_bson_append(bson_t* bson, php_phongo_field_path* field_pa
365365
}
366366

367367
bson_append_array_begin(bson, key, key_len, &child);
368-
field_path->current_level++;
368+
php_phongo_field_path_write_type_at_current_level(field_path, PHONGO_FIELD_PATH_ITEM_ARRAY);
369+
field_path->size++;
369370
php_phongo_zval_to_bson_internal(entry, field_path, flags, &child, NULL TSRMLS_CC);
370-
field_path->current_level--;
371+
field_path->size--;
371372
bson_append_array_end(bson, &child);
372373

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

394-
field_path->current_level++;
395+
php_phongo_field_path_write_type_at_current_level(field_path, PHONGO_FIELD_PATH_ITEM_DOCUMENT);
396+
field_path->size++;
395397
php_phongo_bson_append_object(bson, field_path, flags, key, key_len, entry TSRMLS_CC);
396-
field_path->current_level--;
398+
field_path->size--;
397399

398400
if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) {
399401
ZEND_HASH_DEC_APPLY_COUNT(tmp_ht);
@@ -648,7 +650,7 @@ static void php_phongo_zval_to_bson_internal(zval* data, php_phongo_field_path*
648650
* will be used. */
649651
void php_phongo_zval_to_bson(zval* data, php_phongo_bson_flags_t flags, bson_t* bson, bson_t** bson_out TSRMLS_DC) /* {{{ */
650652
{
651-
php_phongo_field_path* field_path = php_phongo_field_path_alloc();
653+
php_phongo_field_path* field_path = php_phongo_field_path_alloc(false);
652654

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

0 commit comments

Comments
 (0)