Skip to content

Commit d4d1fa3

Browse files
committed
PHPC-314: Record whether we're dealing with a document or an array for each level
1 parent f5e4976 commit d4d1fa3

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

php_bson.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ 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+
4349
typedef struct {
4450
php_phongo_bson_typemap_types document_type;
4551
zend_class_entry* document;
@@ -50,10 +56,11 @@ typedef struct {
5056
} php_phongo_bson_typemap;
5157

5258
typedef struct {
53-
const char** elements;
54-
size_t allocated_levels;
55-
size_t current_level;
56-
size_t ref_count;
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;
5764
} php_phongo_field_path;
5865

5966
typedef struct {
@@ -91,7 +98,8 @@ void php_phongo_bson_state_copy_ctor(php_phongo_bson_state* dst, php_phongo_bson
9198
php_phongo_field_path* php_phongo_field_path_alloc(void);
9299
void php_phongo_field_path_free(php_phongo_field_path* field_path);
93100
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);
101+
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);
102+
bool php_phongo_field_path_push(php_phongo_field_path* field_path, const char* element, php_phongo_bson_field_path_item_types element_type);
95103
bool php_phongo_field_path_pop(php_phongo_field_path* field_path);
96104

97105
char* php_phongo_field_path_as_string(php_phongo_field_path* field_path);

src/bson-encode.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ 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+
php_phongo_field_path_write_type_at_current_level(field_path, PHONGO_FIELD_PATH_ITEM_ARRAY);
368369
field_path->current_level++;
369370
php_phongo_zval_to_bson_internal(entry, field_path, flags, &child, NULL TSRMLS_CC);
370371
field_path->current_level--;
@@ -391,6 +392,7 @@ 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

395+
php_phongo_field_path_write_type_at_current_level(field_path, PHONGO_FIELD_PATH_ITEM_DOCUMENT);
394396
field_path->current_level++;
395397
php_phongo_bson_append_object(bson, field_path, flags, key, key_len, entry TSRMLS_CC);
396398
field_path->current_level--;

src/bson.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ void php_phongo_field_path_free(php_phongo_field_path* field_path)
9999
if (field_path->elements) {
100100
efree(field_path->elements);
101101
}
102+
if (field_path->element_types) {
103+
efree(field_path->element_types);
104+
}
102105
efree(field_path);
103106
}
104107

@@ -109,9 +112,11 @@ static void php_phongo_field_path_ensure_allocation(php_phongo_field_path* field
109112

110113
field_path->allocated_levels = field_path->current_level + PHONGO_FIELD_PATH_EXPANSION;
111114
field_path->elements = erealloc(field_path->elements, sizeof(char**) * field_path->allocated_levels);
115+
field_path->element_types = erealloc(field_path->element_types, sizeof(php_phongo_bson_field_path_item_types*) * field_path->allocated_levels);
112116

113117
for (i = level; i < field_path->allocated_levels; i++) {
114-
field_path->elements[i] = NULL;
118+
field_path->elements[i] = NULL;
119+
field_path->element_types[i] = PHONGO_FIELD_PATH_ITEM_NONE;
115120
}
116121
}
117122
}
@@ -123,9 +128,17 @@ void php_phongo_field_path_write_item_at_current_level(php_phongo_field_path* fi
123128
field_path->elements[field_path->current_level] = element;
124129
}
125130

126-
bool php_phongo_field_path_push(php_phongo_field_path* field_path, const char* element)
131+
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)
132+
{
133+
php_phongo_field_path_ensure_allocation(field_path, field_path->current_level);
134+
135+
field_path->element_types[field_path->current_level] = element_type;
136+
}
137+
138+
bool php_phongo_field_path_push(php_phongo_field_path* field_path, const char* element, php_phongo_bson_field_path_item_types element_type)
127139
{
128140
php_phongo_field_path_write_item_at_current_level(field_path, element);
141+
php_phongo_field_path_write_type_at_current_level(field_path, element_type);
129142

130143
field_path->current_level++;
131144

@@ -134,11 +147,13 @@ bool php_phongo_field_path_push(php_phongo_field_path* field_path, const char* e
134147

135148
bool php_phongo_field_path_pop(php_phongo_field_path* field_path)
136149
{
137-
field_path->elements[field_path->current_level] = NULL;
150+
field_path->elements[field_path->current_level] = NULL;
151+
field_path->element_types[field_path->current_level] = PHONGO_FIELD_PATH_ITEM_NONE;
138152

139153
field_path->current_level--;
140154

141-
field_path->elements[field_path->current_level] = NULL;
155+
field_path->elements[field_path->current_level] = NULL;
156+
field_path->element_types[field_path->current_level] = PHONGO_FIELD_PATH_ITEM_NONE;
142157

143158
return true;
144159
}
@@ -774,7 +789,7 @@ static bool php_phongo_bson_visit_document(const bson_iter_t* iter ARG_UNUSED, c
774789
php_phongo_bson_state* parent_state = (php_phongo_bson_state*) data;
775790
TSRMLS_FETCH();
776791

777-
php_phongo_field_path_push(parent_state->field_path, key);
792+
php_phongo_field_path_push(parent_state->field_path, key, PHONGO_FIELD_PATH_ITEM_DOCUMENT);
778793

779794
if (bson_iter_init(&child, v_document)) {
780795
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
@@ -881,7 +896,7 @@ static bool php_phongo_bson_visit_array(const bson_iter_t* iter ARG_UNUSED, cons
881896
php_phongo_bson_state* parent_state = (php_phongo_bson_state*) data;
882897
TSRMLS_FETCH();
883898

884-
php_phongo_field_path_push(parent_state->field_path, key);
899+
php_phongo_field_path_push(parent_state->field_path, key, PHONGO_FIELD_PATH_ITEM_ARRAY);
885900

886901
if (bson_iter_init(&child, v_array)) {
887902
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;

0 commit comments

Comments
 (0)