Skip to content

Commit 726240f

Browse files
committed
Consolidate BinaryVector and Binary classes, create VectorType enum
1 parent e2aa7e8 commit 726240f

35 files changed

+278
-745
lines changed

config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ if test "$PHP_MONGODB" != "no"; then
168168
src/BSON/Unserializable.c \
169169
src/BSON/UTCDateTime.c \
170170
src/BSON/UTCDateTimeInterface.c \
171+
src/BSON/VectorType.c \
171172
src/MongoDB/BulkWrite.c \
172173
src/MongoDB/BulkWriteCommand.c \
173174
src/MongoDB/BulkWriteCommandResult.c \

config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ if (PHP_MONGODB != "no") {
116116

117117
EXTENSION("mongodb", "php_phongo.c", null, PHP_MONGODB_CFLAGS);
118118
MONGODB_ADD_SOURCES("/src", "phongo_apm.c phongo_atomic.c phongo_bson.c phongo_bson_encode.c phongo_client.c phongo_compat.c phongo_error.c phongo_execute.c phongo_ini.c phongo_log.c phongo_util.c");
119-
MONGODB_ADD_SOURCES("/src/BSON", "Binary.c BinaryInterface.c Document.c Iterator.c DBPointer.c Decimal128.c Decimal128Interface.c Int64.c Javascript.c JavascriptInterface.c MaxKey.c MaxKeyInterface.c MinKey.c MinKeyInterface.c ObjectId.c ObjectIdInterface.c PackedArray.c Persistable.c Regex.c RegexInterface.c Serializable.c Symbol.c Timestamp.c TimestampInterface.c Type.c Undefined.c Unserializable.c UTCDateTime.c UTCDateTimeInterface.c");
119+
MONGODB_ADD_SOURCES("/src/BSON", "Binary.c BinaryInterface.c Document.c Iterator.c DBPointer.c Decimal128.c Decimal128Interface.c Int64.c Javascript.c JavascriptInterface.c MaxKey.c MaxKeyInterface.c MinKey.c MinKeyInterface.c ObjectId.c ObjectIdInterface.c PackedArray.c Persistable.c Regex.c RegexInterface.c Serializable.c Symbol.c Timestamp.c TimestampInterface.c Type.c Undefined.c Unserializable.c UTCDateTime.c UTCDateTimeInterface.c VectorType.c");
120120
MONGODB_ADD_SOURCES("/src/MongoDB", "BulkWrite.c BulkWriteCommand.c BulkWriteCommandResult.c ClientEncryption.c Command.c Cursor.c CursorInterface.c Manager.c Query.c ReadConcern.c ReadPreference.c Server.c ServerApi.c ServerDescription.c Session.c TopologyDescription.c WriteConcern.c WriteConcernError.c WriteError.c WriteResult.c");
121121
MONGODB_ADD_SOURCES("/src/MongoDB/Exception", "AuthenticationException.c BulkWriteException.c BulkWriteCommandException.c CommandException.c ConnectionException.c ConnectionTimeoutException.c EncryptionException.c Exception.c ExecutionTimeoutException.c InvalidArgumentException.c LogicException.c RuntimeException.c ServerException.c UnexpectedValueException.c");
122122
MONGODB_ADD_SOURCES("/src/MongoDB/Monitoring", "CommandFailedEvent.c CommandStartedEvent.c CommandSubscriber.c CommandSucceededEvent.c LogSubscriber.c SDAMSubscriber.c Subscriber.c ServerChangedEvent.c ServerClosedEvent.c ServerHeartbeatFailedEvent.c ServerHeartbeatStartedEvent.c ServerHeartbeatSucceededEvent.c ServerOpeningEvent.c TopologyChangedEvent.c TopologyClosedEvent.c TopologyOpeningEvent.c functions.c");

php_phongo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ PHP_MINIT_FUNCTION(mongodb) /* {{{ */
235235
php_phongo_document_init_ce(INIT_FUNC_ARGS_PASSTHRU);
236236

237237
php_phongo_binary_init_ce(INIT_FUNC_ARGS_PASSTHRU);
238-
php_phongo_binaryvector_init_ce(INIT_FUNC_ARGS_PASSTHRU);
239238
php_phongo_dbpointer_init_ce(INIT_FUNC_ARGS_PASSTHRU);
240239
php_phongo_decimal128_init_ce(INIT_FUNC_ARGS_PASSTHRU);
241240
php_phongo_int64_init_ce(INIT_FUNC_ARGS_PASSTHRU);
@@ -249,6 +248,7 @@ PHP_MINIT_FUNCTION(mongodb) /* {{{ */
249248
php_phongo_timestamp_init_ce(INIT_FUNC_ARGS_PASSTHRU);
250249
php_phongo_undefined_init_ce(INIT_FUNC_ARGS_PASSTHRU);
251250
php_phongo_utcdatetime_init_ce(INIT_FUNC_ARGS_PASSTHRU);
251+
php_phongo_vectortype_init_ce(INIT_FUNC_ARGS_PASSTHRU);
252252

253253
php_phongo_cursor_interface_init_ce(INIT_FUNC_ARGS_PASSTHRU);
254254

src/BSON/Binary.c

Lines changed: 68 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616

1717
#include <php.h>
1818
#include <ext/standard/base64.h>
19+
#include <Zend/zend_enum.h>
1920
#include <Zend/zend_interfaces.h>
2021

2122
#include "php_phongo.h"
2223
#include "phongo_bson_encode.h"
2324
#include "phongo_error.h"
2425
#include "Binary.h"
2526
#include "Binary_arginfo.h"
26-
#include "BinaryVector_arginfo.h"
2727

2828
zend_class_entry* php_phongo_binary_ce;
2929

30-
static phongo_bson_vector_type_t phongo_binaryvector_get_vector_type_from_data(const uint8_t* data, uint32_t data_len);
31-
static phongo_bson_vector_type_t phongo_binaryvector_get_vector_type(const php_phongo_binary_t* intern);
32-
static void phongo_binaryvector_get_vector_as_array(const php_phongo_binary_t* intern, zval* return_value);
30+
static phongo_bson_vector_type_t phongo_binary_get_vector_type_from_data(const uint8_t* data, uint32_t data_len);
31+
static phongo_bson_vector_type_t phongo_binary_get_vector_type(const php_phongo_binary_t* intern);
32+
static void phongo_binary_get_vector_as_array(const php_phongo_binary_t* intern, zval* return_value);
3333

3434
/* Initialize the object and return whether it was successful. An exception will
3535
* be thrown on error. */
@@ -45,7 +45,7 @@ static bool php_phongo_binary_init(php_phongo_binary_t* intern, const char* data
4545
return false;
4646
}
4747

48-
if ((type == BSON_SUBTYPE_VECTOR) && phongo_binaryvector_get_vector_type_from_data((const uint8_t*) data, data_len) == PHONGO_BSON_VECTOR_TYPE_INVALID) {
48+
if ((type == BSON_SUBTYPE_VECTOR) && phongo_binary_get_vector_type_from_data((const uint8_t*) data, data_len) == PHONGO_BSON_VECTOR_TYPE_INVALID) {
4949
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Binary vector data is invalid");
5050
return false;
5151
}
@@ -290,7 +290,7 @@ static HashTable* php_phongo_binary_get_debug_info(zend_object* object, int* is_
290290
if (intern->type == BSON_SUBTYPE_VECTOR) {
291291
zval vector;
292292

293-
phongo_binaryvector_get_vector_as_array(intern, &vector);
293+
phongo_binary_get_vector_as_array(intern, &vector);
294294

295295
if (EG(exception)) {
296296
return props;
@@ -300,7 +300,7 @@ static HashTable* php_phongo_binary_get_debug_info(zend_object* object, int* is_
300300

301301
zval vector_type;
302302

303-
ZVAL_LONG(&vector_type, phongo_binaryvector_get_vector_type(intern));
303+
ZVAL_LONG(&vector_type, phongo_binary_get_vector_type(intern));
304304
zend_hash_str_update(props, "vectorType", sizeof("vectorType") - 1, &vector_type);
305305
}
306306

@@ -328,22 +328,12 @@ void php_phongo_binary_init_ce(INIT_FUNC_ARGS)
328328

329329
bool phongo_binary_new(zval* object, const char* data, size_t data_len, bson_subtype_t type)
330330
{
331-
switch (type) {
332-
case BSON_SUBTYPE_VECTOR:
333-
object_init_ex(object, php_phongo_binaryvector_ce);
334-
break;
335-
336-
default:
337-
object_init_ex(object, php_phongo_binary_ce);
338-
}
331+
object_init_ex(object, php_phongo_binary_ce);
339332

340333
return php_phongo_binary_init(Z_BINARY_OBJ_P(object), data, data_len, type);
341334
}
342335

343-
/* MongoDB\BSON\BinaryVector implementation */
344-
zend_class_entry* php_phongo_binaryvector_ce;
345-
346-
static inline void phongo_binaryvector_init_from_bson_key(php_phongo_binary_t* intern, const bson_t* doc, const char* key)
336+
static inline void phongo_binary_init_vector_from_bson_key(php_phongo_binary_t* intern, const bson_t* doc, const char* key)
347337
{
348338
bson_iter_t iter;
349339

@@ -359,7 +349,7 @@ static inline void phongo_binaryvector_init_from_bson_key(php_phongo_binary_t* i
359349
php_phongo_binary_init(intern, (const char*) data, data_len, BSON_SUBTYPE_VECTOR);
360350
}
361351

362-
static void phongo_binaryvector_init_from_float32_array(php_phongo_binary_t* intern, HashTable* vector)
352+
static void phongo_binary_init_vector_from_float32_array(php_phongo_binary_t* intern, HashTable* vector)
363353
{
364354
if (!zend_array_is_list(vector)) {
365355
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected vector to be a list");
@@ -397,10 +387,10 @@ static void phongo_binaryvector_init_from_float32_array(php_phongo_binary_t* int
397387
}
398388
ZEND_HASH_FOREACH_END();
399389

400-
phongo_binaryvector_init_from_bson_key(intern, &doc, "vector");
390+
phongo_binary_init_vector_from_bson_key(intern, &doc, "vector");
401391
}
402392

403-
static void phongo_binaryvector_init_from_int8_array(php_phongo_binary_t* intern, HashTable* vector)
393+
static void phongo_binary_init_vector_from_int8_array(php_phongo_binary_t* intern, HashTable* vector)
404394
{
405395
if (!zend_array_is_list(vector)) {
406396
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected vector to be a list");
@@ -443,10 +433,10 @@ static void phongo_binaryvector_init_from_int8_array(php_phongo_binary_t* intern
443433
}
444434
ZEND_HASH_FOREACH_END();
445435

446-
phongo_binaryvector_init_from_bson_key(intern, &doc, "vector");
436+
phongo_binary_init_vector_from_bson_key(intern, &doc, "vector");
447437
}
448438

449-
static void phongo_binaryvector_init_from_packed_bit_array(php_phongo_binary_t* intern, HashTable* vector)
439+
static void phongo_binary_init_vector_from_packed_bit_array(php_phongo_binary_t* intern, HashTable* vector)
450440
{
451441
if (!zend_array_is_list(vector)) {
452442
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected vector to be a list");
@@ -489,84 +479,44 @@ static void phongo_binaryvector_init_from_packed_bit_array(php_phongo_binary_t*
489479
}
490480
ZEND_HASH_FOREACH_END();
491481

492-
phongo_binaryvector_init_from_bson_key(intern, &doc, "vector");
482+
phongo_binary_init_vector_from_bson_key(intern, &doc, "vector");
493483
}
494484

495-
static PHP_METHOD(MongoDB_BSON_BinaryVector, __construct)
485+
static PHP_METHOD(MongoDB_BSON_Binary, fromVector)
496486
{
497-
php_phongo_binary_t* intern;
498487
HashTable* vector;
499-
zend_long type = BSON_SUBTYPE_BINARY;
488+
zend_object* type;
500489

501-
intern = Z_BINARY_OBJ_P(getThis());
490+
object_init_ex(return_value, php_phongo_binary_ce);
491+
php_phongo_binary_t* intern = Z_BINARY_OBJ_P(return_value);
502492

503493
PHONGO_PARSE_PARAMETERS_START(2, 2)
504494
Z_PARAM_ARRAY_HT(vector)
505-
Z_PARAM_LONG(type)
495+
Z_PARAM_OBJ_OF_CLASS(type, php_phongo_vectortype_ce)
506496
PHONGO_PARSE_PARAMETERS_END();
507497

508-
switch (type) {
509-
case PHONGO_BSON_VECTOR_TYPE_FLOAT32:
510-
phongo_binaryvector_init_from_float32_array(intern, vector);
511-
break;
512-
513-
case PHONGO_BSON_VECTOR_TYPE_INT8:
514-
phongo_binaryvector_init_from_int8_array(intern, vector);
515-
break;
516-
517-
case PHONGO_BSON_VECTOR_TYPE_PACKED_BIT:
518-
phongo_binaryvector_init_from_packed_bit_array(intern, vector);
519-
break;
498+
zval *type_name = zend_enum_fetch_case_name(type);
520499

521-
default:
522-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Unsupported binary vector type: %" PHONGO_LONG_FORMAT, type);
523-
RETURN_THROWS();
500+
if (zend_string_equals_literal(Z_STR_P(type_name), "Float32")) {
501+
phongo_binary_init_vector_from_float32_array(intern, vector);
502+
return;
524503
}
525-
}
526504

527-
static PHP_METHOD(MongoDB_BSON_BinaryVector, fromFloat32Array)
528-
{
529-
HashTable* vector;
530-
531-
PHONGO_PARSE_PARAMETERS_START(1, 1)
532-
Z_PARAM_ARRAY_HT(vector)
533-
PHONGO_PARSE_PARAMETERS_END();
534-
535-
object_init_ex(return_value, php_phongo_binaryvector_ce);
536-
php_phongo_binary_t* intern = Z_BINARY_OBJ_P(return_value);
537-
538-
phongo_binaryvector_init_from_float32_array(intern, vector);
539-
}
540-
541-
static PHP_METHOD(MongoDB_BSON_BinaryVector, fromInt8Array)
542-
{
543-
HashTable* vector;
544-
545-
PHONGO_PARSE_PARAMETERS_START(1, 1)
546-
Z_PARAM_ARRAY_HT(vector)
547-
PHONGO_PARSE_PARAMETERS_END();
548-
549-
object_init_ex(return_value, php_phongo_binaryvector_ce);
550-
php_phongo_binary_t* intern = Z_BINARY_OBJ_P(return_value);
551-
552-
phongo_binaryvector_init_from_int8_array(intern, vector);
553-
}
554-
555-
static PHP_METHOD(MongoDB_BSON_BinaryVector, fromPackedBitArray)
556-
{
557-
HashTable* vector;
558-
559-
PHONGO_PARSE_PARAMETERS_START(1, 1)
560-
Z_PARAM_ARRAY_HT(vector)
561-
PHONGO_PARSE_PARAMETERS_END();
505+
if (zend_string_equals_literal(Z_STR_P(type_name), "Int8")) {
506+
phongo_binary_init_vector_from_int8_array(intern, vector);
507+
return;
508+
}
562509

563-
object_init_ex(return_value, php_phongo_binaryvector_ce);
564-
php_phongo_binary_t* intern = Z_BINARY_OBJ_P(return_value);
510+
if (zend_string_equals_literal(Z_STR_P(type_name), "PackedBit")) {
511+
phongo_binary_init_vector_from_packed_bit_array(intern, vector);
512+
return;
513+
}
565514

566-
phongo_binaryvector_init_from_packed_bit_array(intern, vector);
515+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Unsupported binary vector type: %s", Z_STR_P(type_name));
516+
RETURN_THROWS();
567517
}
568518

569-
static phongo_bson_vector_type_t phongo_binaryvector_get_vector_type_from_data(const uint8_t* data, uint32_t data_len)
519+
static phongo_bson_vector_type_t phongo_binary_get_vector_type_from_data(const uint8_t* data, uint32_t data_len)
570520
{
571521
if (bson_vector_int8_const_view_init(NULL, data, data_len)) {
572522
return PHONGO_BSON_VECTOR_TYPE_INT8;
@@ -583,29 +533,47 @@ static phongo_bson_vector_type_t phongo_binaryvector_get_vector_type_from_data(c
583533
return PHONGO_BSON_VECTOR_TYPE_INVALID;
584534
}
585535

586-
static phongo_bson_vector_type_t phongo_binaryvector_get_vector_type(const php_phongo_binary_t* intern)
536+
static phongo_bson_vector_type_t phongo_binary_get_vector_type(const php_phongo_binary_t* intern)
587537
{
588-
return phongo_binaryvector_get_vector_type_from_data((const uint8_t*) intern->data, intern->data_len);
538+
return phongo_binary_get_vector_type_from_data((const uint8_t*) intern->data, intern->data_len);
589539
}
590540

591-
static PHP_METHOD(MongoDB_BSON_BinaryVector, getVectorType)
541+
static PHP_METHOD(MongoDB_BSON_Binary, getVectorType)
592542
{
593543
PHONGO_PARSE_PARAMETERS_NONE();
594544

595-
phongo_bson_vector_type_t type = phongo_binaryvector_get_vector_type(Z_BINARY_OBJ_P(getThis()));
545+
php_phongo_binary_t* intern = Z_BINARY_OBJ_P(getThis());
596546

597-
// The vector should always be valid by this point, but check for an error
598-
if (type == PHONGO_BSON_VECTOR_TYPE_INVALID) {
599-
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Binary vector data is invalid");
547+
if (intern->type != BSON_SUBTYPE_VECTOR) {
548+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected Binary of type vector (%" PRId8 ") but it is %" PHONGO_LONG_FORMAT, BSON_SUBTYPE_VECTOR, intern->type);
600549
RETURN_THROWS();
601550
}
602551

603-
RETURN_LONG(type);
552+
phongo_bson_vector_type_t type = phongo_binary_get_vector_type(Z_BINARY_OBJ_P(getThis()));
553+
const char *type_case;
554+
555+
switch (type) {
556+
case PHONGO_BSON_VECTOR_TYPE_FLOAT32:
557+
type_case = "Float32";
558+
break;
559+
case PHONGO_BSON_VECTOR_TYPE_INT8:
560+
type_case = "Int8";
561+
break;
562+
case PHONGO_BSON_VECTOR_TYPE_PACKED_BIT:
563+
type_case = "PackedBit";
564+
break;
565+
default:
566+
// The vector should always be valid by this point, but check for an error
567+
phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "Binary vector data is invalid");
568+
RETURN_THROWS();
569+
}
570+
571+
RETVAL_OBJ_COPY(zend_enum_get_case_cstr(php_phongo_vectortype_ce, type_case));
604572
}
605573

606-
static void phongo_binaryvector_get_vector_as_array(const php_phongo_binary_t* intern, zval* return_value)
574+
static void phongo_binary_get_vector_as_array(const php_phongo_binary_t* intern, zval* return_value)
607575
{
608-
phongo_bson_vector_type_t type = phongo_binaryvector_get_vector_type(intern);
576+
phongo_bson_vector_type_t type = phongo_binary_get_vector_type(intern);
609577

610578
// The vector should always be valid by this point, but check for an error
611579
if (type == PHONGO_BSON_VECTOR_TYPE_INVALID) {
@@ -683,57 +651,16 @@ static void phongo_binaryvector_get_vector_as_array(const php_phongo_binary_t* i
683651
RETURN_ZVAL(&state.zchild, 0, 1);
684652
}
685653

686-
static PHP_METHOD(MongoDB_BSON_BinaryVector, toArray)
654+
static PHP_METHOD(MongoDB_BSON_Binary, toArray)
687655
{
688656
PHONGO_PARSE_PARAMETERS_NONE();
689657

690-
phongo_binaryvector_get_vector_as_array(Z_BINARY_OBJ_P(getThis()), return_value);
691-
}
692-
693-
static PHP_METHOD(MongoDB_BSON_BinaryVector, __set_state)
694-
{
695-
php_phongo_binary_t* intern;
696-
HashTable* props;
697-
zval* array;
698-
699-
PHONGO_PARSE_PARAMETERS_START(1, 1)
700-
Z_PARAM_ARRAY(array)
701-
PHONGO_PARSE_PARAMETERS_END();
702-
703-
/* This implementation is similar to Binary::__set_state(), except that we
704-
* initialize a BinaryVector object and also validate its data. */
705-
object_init_ex(return_value, php_phongo_binaryvector_ce);
658+
php_phongo_binary_t* intern = Z_BINARY_OBJ_P(getThis());
706659

707-
intern = Z_BINARY_OBJ_P(return_value);
708-
props = Z_ARRVAL_P(array);
709-
710-
php_phongo_binary_init_from_hash(intern, props);
711-
712-
if (intern->type != BSON_SUBTYPE_VECTOR || phongo_binaryvector_get_vector_type(intern) == PHONGO_BSON_VECTOR_TYPE_INVALID) {
713-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Binary vector data is invalid");
660+
if (intern->type != BSON_SUBTYPE_VECTOR) {
661+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected Binary of type vector (%" PRId8 ") but it is %" PHONGO_LONG_FORMAT, BSON_SUBTYPE_VECTOR, intern->type);
714662
RETURN_THROWS();
715663
}
716-
}
717664

718-
/* MongoDB\BSON\BinaryVector object handlers */
719-
static zend_object_handlers php_phongo_handler_binaryvector;
720-
721-
static zend_object* php_phongo_binaryvector_create_object(zend_class_entry* class_type)
722-
{
723-
php_phongo_binary_t* intern = zend_object_alloc(sizeof(php_phongo_binary_t), class_type);
724-
725-
zend_object_std_init(&intern->std, class_type);
726-
object_properties_init(&intern->std, class_type);
727-
728-
intern->std.handlers = &php_phongo_handler_binaryvector;
729-
730-
return &intern->std;
665+
phongo_binary_get_vector_as_array(Z_BINARY_OBJ_P(getThis()), return_value);
731666
}
732-
733-
void php_phongo_binaryvector_init_ce(INIT_FUNC_ARGS)
734-
{
735-
php_phongo_binaryvector_ce = register_class_MongoDB_BSON_BinaryVector(php_phongo_binary_ce);
736-
php_phongo_binaryvector_ce->create_object = php_phongo_binaryvector_create_object;
737-
738-
memcpy(&php_phongo_handler_binaryvector, &php_phongo_handler_binary, sizeof(zend_object_handlers));
739-
}

0 commit comments

Comments
 (0)