1919#include " code.h"
2020#include " dbref.h"
2121#include " symbol.h"
22+ #include " minkey.h"
23+ #include " maxkey.h"
2224
2325using namespace v8 ;
2426using namespace node ;
@@ -40,7 +42,8 @@ const uint32_t BSON_DATA_CODE_W_SCOPE = 15;
4042const uint32_t BSON_DATA_INT = 16 ;
4143const uint32_t BSON_DATA_TIMESTAMP = 17 ;
4244const uint32_t BSON_DATA_LONG = 18 ;
43-
45+ const uint32_t BSON_DATA_MIN_KEY = 0xff ;
46+ const uint32_t BSON_DATA_MAX_KEY = 0x7f ;
4447
4548const int32_t BSON_INT32_MAX = (int32_t )2147483647L ;
4649const int32_t BSON_INT32_MIN = (int32_t )(-1 ) * 2147483648L ;
@@ -559,7 +562,31 @@ uint32_t BSON::serialize(char *serialized_object, uint32_t index, Handle<Value>
559562 *(serialized_object + index + str->Length ()) = ' \0 ' ;
560563 // Adjust the index
561564 index = index + str->Length () + 1 ;
562- }
565+ }
566+ } else if (MinKey::HasInstance (value)) {
567+ // Save the string at the offset provided
568+ *(serialized_object + index) = BSON_DATA_MIN_KEY;
569+ // Adjust writing position for the first byte
570+ index = index + 1 ;
571+ // Convert name to char*
572+ ssize_t len = DecodeBytes (name, UTF8);
573+ ssize_t written = DecodeWrite ((serialized_object + index), len, name, UTF8);
574+ // Add null termiation for the string
575+ *(serialized_object + index + len) = ' \0 ' ;
576+ // Adjust the index
577+ index = index + len + 1 ;
578+ } else if (MaxKey::HasInstance (value)) {
579+ // Save the string at the offset provided
580+ *(serialized_object + index) = BSON_DATA_MAX_KEY;
581+ // Adjust writing position for the first byte
582+ index = index + 1 ;
583+ // Convert name to char*
584+ ssize_t len = DecodeBytes (name, UTF8);
585+ ssize_t written = DecodeWrite ((serialized_object + index), len, name, UTF8);
586+ // Add null termiation for the string
587+ *(serialized_object + index + len) = ' \0 ' ;
588+ // Adjust the index
589+ index = index + len + 1 ;
563590 } else if (value->IsNull () || value->IsUndefined ()) {
564591 // Save the string at the offset provided
565592 *(serialized_object + index) = BSON_DATA_NULL;
@@ -787,6 +814,9 @@ uint32_t BSON::serialize(char *serialized_object, uint32_t index, Handle<Value>
787814 || constructorString->Equals (String::New (" Binary" ))
788815 || constructorString->Equals (String::New (" exports.DBRef" ))
789816 || constructorString->Equals (String::New (" exports.Code" ))
817+ || constructorString->Equals (String::New (" exports.Double" ))
818+ || constructorString->Equals (String::New (" exports.MinKey" ))
819+ || constructorString->Equals (String::New (" exports.MaxKey" ))
790820 || constructorString->Equals (String::New (" exports.Symbol" )))) {
791821
792822 // Throw an error due to wrong class
@@ -906,6 +936,7 @@ uint32_t BSON::calculate_object_size(Handle<Value> value) {
906936 if (db_ref_obj->db != NULL ) obj->Set (String::New (" $db" ), dbref->Get (String::New (" db" )));
907937 // Calculate size
908938 object_size += BSON::calculate_object_size (obj);
939+ } else if (MinKey::HasInstance (value) || MaxKey::HasInstance (value)) {
909940 } else if (Symbol::HasInstance (value)) {
910941 // Unpack the dbref
911942 Local<Object> dbref = value->ToObject ();
@@ -998,6 +1029,9 @@ uint32_t BSON::calculate_object_size(Handle<Value> value) {
9981029 || constructorString->Equals (String::New (" Binary" ))
9991030 || constructorString->Equals (String::New (" exports.DBRef" ))
10001031 || constructorString->Equals (String::New (" exports.Code" ))
1032+ || constructorString->Equals (String::New (" exports.Double" ))
1033+ || constructorString->Equals (String::New (" exports.MinKey" ))
1034+ || constructorString->Equals (String::New (" exports.MaxKey" ))
10011035 || constructorString->Equals (String::New (" exports.Symbol" )))) {
10021036
10031037 // Throw an error due to wrong class
@@ -1104,7 +1138,7 @@ Handle<Value> BSON::deserialize(char *data, bool is_array_item) {
11041138 // While we have data left let's decode
11051139 while (index < size) {
11061140 // Read the first to bytes to indicate the type of object we are decoding
1107- uint16_t type = BSON::deserialize_int8 (data, index);
1141+ uint8_t type = BSON::deserialize_int8 (data, index);
11081142 // Handles the internal size of the object
11091143 uint32_t insert_index = 0 ;
11101144 // Adjust index to skip type byte
@@ -1246,6 +1280,50 @@ Handle<Value> BSON::deserialize(char *data, bool is_array_item) {
12461280 }
12471281 // Free up the memory
12481282 free (string_name);
1283+ } else if (type == BSON_DATA_MIN_KEY) {
1284+ // Read the null terminated index String
1285+ char *string_name = BSON::extract_string (data, index);
1286+ if (string_name == NULL ) return VException (" Invalid C String found." );
1287+ // Let's create a new string
1288+ index = index + strlen (string_name) + 1 ;
1289+ // Handle array value if applicable
1290+ uint32_t insert_index = 0 ;
1291+ if (is_array_item) {
1292+ insert_index = atoi (string_name);
1293+ }
1294+
1295+ // Create new MinKey
1296+ MinKey *minKey = MinKey::New ();
1297+ // Add the element to the object
1298+ if (is_array_item) {
1299+ return_array->Set (Number::New (insert_index), minKey->handle_ );
1300+ } else {
1301+ return_data->Set (String::New (string_name), minKey->handle_ );
1302+ }
1303+ // Free up the memory
1304+ free (string_name);
1305+ } else if (type == BSON_DATA_MAX_KEY) {
1306+ // Read the null terminated index String
1307+ char *string_name = BSON::extract_string (data, index);
1308+ if (string_name == NULL ) return VException (" Invalid C String found." );
1309+ // Let's create a new string
1310+ index = index + strlen (string_name) + 1 ;
1311+ // Handle array value if applicable
1312+ uint32_t insert_index = 0 ;
1313+ if (is_array_item) {
1314+ insert_index = atoi (string_name);
1315+ }
1316+
1317+ // Create new MinKey
1318+ MaxKey *maxKey = MaxKey::New ();
1319+ // Add the element to the object
1320+ if (is_array_item) {
1321+ return_array->Set (Number::New (insert_index), maxKey->handle_ );
1322+ } else {
1323+ return_data->Set (String::New (string_name), maxKey->handle_ );
1324+ }
1325+ // Free up the memory
1326+ free (string_name);
12491327 } else if (type == BSON_DATA_NULL) {
12501328 // Read the null terminated index String
12511329 char *string_name = BSON::extract_string (data, index);
@@ -1870,6 +1948,8 @@ extern "C" void init(Handle<Object> target) {
18701948 DBRef::Initialize (target);
18711949 Timestamp::Initialize (target);
18721950 Symbol::Initialize (target);
1951+ MinKey::Initialize (target);
1952+ MaxKey::Initialize (target);
18731953}
18741954
18751955// NODE_MODULE(bson, BSON::Initialize);
0 commit comments