Skip to content

Commit bdda892

Browse files
committed
PHPC-314: Parse fieldPaths element of the typemap into a tree
1 parent d4d1fa3 commit bdda892

File tree

8 files changed

+506
-42
lines changed

8 files changed

+506
-42
lines changed

php_bson.h

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,35 @@ typedef enum {
4646
PHONGO_FIELD_PATH_ITEM_DOCUMENT
4747
} php_phongo_bson_field_path_item_types;
4848

49+
typedef struct {
50+
char** elements;
51+
php_phongo_bson_field_path_item_types* element_types;
52+
size_t allocated;
53+
size_t size;
54+
size_t ref_count;
55+
bool free_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+
4964
typedef struct {
5065
php_phongo_bson_typemap_types document_type;
5166
zend_class_entry* document;
5267
php_phongo_bson_typemap_types array_type;
5368
zend_class_entry* array;
5469
php_phongo_bson_typemap_types root_type;
5570
zend_class_entry* root;
71+
struct {
72+
php_phongo_field_path_map_element** map;
73+
size_t allocated;
74+
size_t size;
75+
} field_paths;
5676
} php_phongo_bson_typemap;
5777

58-
typedef struct {
59-
const char** elements;
60-
php_phongo_bson_field_path_item_types* element_types;
61-
size_t allocated_levels;
62-
size_t current_level;
63-
size_t ref_count;
64-
} php_phongo_field_path;
65-
6678
typedef struct {
6779
ZVAL_RETVAL_TYPE zchild;
6880
php_phongo_bson_typemap map;
@@ -94,8 +106,9 @@ bool php_phongo_bson_typemap_to_state(zval* typemap, php_phongo_bson_typemap* ma
94106
void php_phongo_bson_state_ctor(php_phongo_bson_state* state);
95107
void php_phongo_bson_state_dtor(php_phongo_bson_state* state);
96108
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);
97110

98-
php_phongo_field_path* php_phongo_field_path_alloc(void);
111+
php_phongo_field_path* php_phongo_field_path_alloc(bool free_elements);
99112
void php_phongo_field_path_free(php_phongo_field_path* field_path);
100113
void php_phongo_field_path_write_item_at_current_level(php_phongo_field_path* field_path, const char* element);
101114
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);

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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,9 @@ static void php_phongo_bson_append(bson_t* bson, php_phongo_field_path* field_pa
366366

367367
bson_append_array_begin(bson, key, key_len, &child);
368368
php_phongo_field_path_write_type_at_current_level(field_path, PHONGO_FIELD_PATH_ITEM_ARRAY);
369-
field_path->current_level++;
369+
field_path->size++;
370370
php_phongo_zval_to_bson_internal(entry, field_path, flags, &child, NULL TSRMLS_CC);
371-
field_path->current_level--;
371+
field_path->size--;
372372
bson_append_array_end(bson, &child);
373373

374374
if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) {
@@ -393,9 +393,9 @@ static void php_phongo_bson_append(bson_t* bson, php_phongo_field_path* field_pa
393393
}
394394

395395
php_phongo_field_path_write_type_at_current_level(field_path, PHONGO_FIELD_PATH_ITEM_DOCUMENT);
396-
field_path->current_level++;
396+
field_path->size++;
397397
php_phongo_bson_append_object(bson, field_path, flags, key, key_len, entry TSRMLS_CC);
398-
field_path->current_level--;
398+
field_path->size--;
399399

400400
if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) {
401401
ZEND_HASH_DEC_APPLY_COUNT(tmp_ht);
@@ -650,7 +650,7 @@ static void php_phongo_zval_to_bson_internal(zval* data, php_phongo_field_path*
650650
* will be used. */
651651
void php_phongo_zval_to_bson(zval* data, php_phongo_bson_flags_t flags, bson_t* bson, bson_t** bson_out TSRMLS_DC) /* {{{ */
652652
{
653-
php_phongo_field_path* field_path = php_phongo_field_path_alloc();
653+
php_phongo_field_path* field_path = php_phongo_field_path_alloc(false);
654654

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

0 commit comments

Comments
 (0)