Skip to content

Commit ebe746f

Browse files
committed
Push to fix
1 parent be0257a commit ebe746f

File tree

4 files changed

+949
-884
lines changed

4 files changed

+949
-884
lines changed

external-libs/bson/bson.cc

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -602,22 +602,26 @@ uint32_t BSON::serialize(char *serialized_object, uint32_t index, Handle<Value>
602602
// Adjust index for double
603603
index = index + 8;
604604
} else if(l_number <= BSON_INT32_MAX && l_number >= BSON_INT32_MIN) {
605+
printf("--------------------------------------------------------------- 2\n");
605606
// Smaller than 32 bit, write as 32 bit value
606607
BSON::write_int32(serialized_object + index, value->ToInt32()->Value());
607608
// Adjust the size of the index
608609
index = index + 4;
609610
} else if(l_number <= (2^53) && l_number >= (-2^53)) {
611+
printf("--------------------------------------------------------------- 3\n");
610612
// Write the double to the char array
611613
BSON::write_double((serialized_object + index), d_number);
612614
// Adjust type to be double
613615
*(serialized_object + first_pointer) = BSON_DATA_NUMBER;
614616
// Adjust index for double
615617
index = index + 8;
616618
} else {
617-
BSON::write_int64((serialized_object + index), d_number);
619+
printf("--------------------------------------------------------------- 4\n");
620+
BSON::write_double((serialized_object + index), d_number);
621+
// BSON::write_int64((serialized_object + index), d_number);
618622
// BSON::write_int64((serialized_object + index), l_number);
619623
// Adjust type to be double
620-
*(serialized_object + first_pointer) = BSON_DATA_LONG;
624+
*(serialized_object + first_pointer) = BSON_DATA_NUMBER;
621625
// Adjust the size of the index
622626
index = index + 8;
623627
}
@@ -1192,6 +1196,8 @@ Handle<Value> BSON::deserialize(char *data, bool is_array_item) {
11921196
// Free up the memory
11931197
free(string_name);
11941198
} else if(type == BSON_DATA_LONG) {
1199+
printf("=================================================== 1\n");
1200+
11951201
// Read the null terminated index String
11961202
char *string_name = BSON::extract_string(data, index);
11971203
if(string_name == NULL) return VException("Invalid C String found.");
@@ -1725,11 +1731,19 @@ Handle<Value> BSON::decodeLong(char *data, uint32_t index) {
17251731
memcpy(&highBits, (data + index + 4), 4);
17261732

17271733
// Decode 64bit value
1728-
// double value = 0;
1729-
// memcpy(&value, (data + index), 8);
1734+
int64_t value = 0;
1735+
memcpy(&value, (data + index), 8);
1736+
1737+
printf("==================================== %llu\n", value);
1738+
1739+
if(value >= (-2^53) && value <= (2^53)) {
1740+
printf("----------------------------------------------- 2\n");
1741+
1742+
}
17301743

17311744
// If value is < 2^53 and >-2^53
17321745
if((highBits < 0x200000 || (highBits == 0x200000 && lowBits == 0)) && highBits >= -0x200000) {
1746+
printf("----------------------------------------------- 1\n");
17331747
int64_t finalValue = 0;
17341748
memcpy(&finalValue, (data + index), 8);
17351749
return scope.Close(Number::New(finalValue));

lib/mongodb/bson/bson.js

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -269,36 +269,50 @@ BSON.serializeWithBufferAndIndex = function serializeWithBufferAndIndex(object,
269269
// Write zero
270270
buffer[index++] = 0;
271271
} else if((typeof value == 'number' || toString.call(value) === '[object Number]') &&
272-
value === parseInt(value, 10) &&
273-
value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
272+
value == parseInt(value, 10) &&
273+
value.toString().match(/\./) == null) {
274274
// Write the type
275-
var int64 = value >= BSON.BSON_INT32_MAX || value < BSON.BSON_INT32_MIN;
275+
var int64 = value >= BSON.BSON_INT32_MAX || value < BSON.BSON_INT32_MIN;
276+
var startIndex = index;
277+
// Set type
276278
buffer[index++] = int64 ? BSON.BSON_DATA_LONG : BSON.BSON_DATA_INT;
277279
// Write the name
278280
if(name != null) {
279281
index = index + buffer.write(name, index, 'utf8') + 1;
280282
buffer[index - 1] = 0;
281283
}
282-
283-
if(int64) {
284+
285+
if(int64) {
284286
// Write the number
285287
var long = Long.fromNumber(value);
286288
var lowBits = long.getLowBits();
287289
var highBits = long.getHighBits();
288290

289-
buffer[index + 3] = (lowBits >> 24) & 0xff;
290-
buffer[index + 2] = (lowBits >> 16) & 0xff;
291-
buffer[index + 1] = (lowBits >> 8) & 0xff;
292-
buffer[index] = lowBits & 0xff;
293-
294-
index += 4;
295-
296-
buffer[index + 3] = (highBits >> 24) & 0xff;
297-
buffer[index + 2] = (highBits >> 16) & 0xff;
298-
buffer[index + 1] = (highBits >> 8) & 0xff;
299-
buffer[index] = highBits & 0xff;
300-
301-
index += 4;
291+
// If the number is the same after long conversion force double
292+
if(value.toString() == long.toNumber().toString()) {
293+
buffer[startIndex] = BSON.BSON_DATA_NUMBER;
294+
// Write float
295+
ieee754.writeIEEE754(buffer, value, index, 'little', 52, 8);
296+
// Ajust index
297+
index = index + 8;
298+
} else {
299+
debug(value.toString())
300+
debug(long.toNumber().toString())
301+
302+
buffer[index + 3] = (lowBits >> 24) & 0xff;
303+
buffer[index + 2] = (lowBits >> 16) & 0xff;
304+
buffer[index + 1] = (lowBits >> 8) & 0xff;
305+
buffer[index] = lowBits & 0xff;
306+
307+
index += 4;
308+
309+
buffer[index + 3] = (highBits >> 24) & 0xff;
310+
buffer[index + 2] = (highBits >> 16) & 0xff;
311+
buffer[index + 1] = (highBits >> 8) & 0xff;
312+
buffer[index] = highBits & 0xff;
313+
314+
index += 4;
315+
}
302316
} else {
303317
// Write the int value to the buffer
304318
buffer[index + 3] = (value >> 24) & 0xff;
@@ -309,6 +323,21 @@ BSON.serializeWithBufferAndIndex = function serializeWithBufferAndIndex(object,
309323
}
310324
} else if(typeof value == 'number' || toString.call(value) === '[object Number]' ||
311325
value instanceof Double) {
326+
327+
// } else if((typeof value == 'number' || toString.call(value) === '[object Number]') &&
328+
// value == parseInt(value, 10) &&
329+
// value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
330+
//
331+
// debug("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
332+
// debug("====================== " + (typeof value == 'number'))
333+
// debug("====================== " + (toString.call(value) === '[object Number]'))
334+
// debug("====================== " + (value == parseInt(value, 10)))
335+
// debug("====================== " + (value >= parseInt(BSON.JS_INT_MIN, 10)))
336+
// debug("" + value + " = " + BSON.JS_INT_MIN)
337+
// debug("" + value + " = " + BSON.JS_INT_MAX)
338+
// debug(value.toString())
339+
// debug(value.toString().match(/\./))
340+
312341
// Write the type
313342
buffer[index++] = BSON.BSON_DATA_NUMBER;
314343
// Write the name
@@ -1100,13 +1129,23 @@ BSON.deserialize = function(data, options) {
11001129
var value;
11011130

11021131
if (type === BSON.BSON_DATA_LONG) {
1132+
// Convert to long
11031133
value = new Long(low_bits, high_bits);
1104-
// Tricky: if this value is in [-2^53, 2^53], then it's perfectly
1105-
// representable as a Number.
1106-
if ((high_bits < 0x200000 || (high_bits === 0x200000 && low_bits === 0)) &&
1107-
high_bits >= -0x200000) {
1108-
value = value.toNumber();
1109-
}
1134+
// convert back to number and compare the converted value with the original
1135+
// var value2 = value.toNumber();
1136+
//
1137+
// // if(value2 > )
1138+
//
1139+
// // If Long string representiation is the same return the normal value
1140+
// debug("========================== :: " + Long.MAX_VALUE)
1141+
// debug("========================== :: " + BSON.JS_INT_MAX)
1142+
// debug("========================== :: " + Math.pow(2, 53))
1143+
// debug("========================== :: " + value.toString())
1144+
// debug("========================== :: " + value2.toString())
1145+
1146+
// if(value.toString() === value2.toString()) {
1147+
// value = value2;
1148+
// }
11101149
} else {
11111150
value = new Timestamp(low_bits, high_bits);
11121151
}

lib/mongodb/goog/math/long.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,22 @@ exports.Long.fromInt = function(value) {
9797
* @return {exports.Long} The corresponding exports.Long value.
9898
*/
9999
exports.Long.fromNumber = function(value) {
100+
// var debug = require('sys').debug;
101+
100102
if (isNaN(value) || !isFinite(value)) {
103+
// debug("----------------------------------- 0")
101104
return exports.Long.ZERO;
102105
} else if (value <= -exports.Long.TWO_PWR_63_DBL_) {
106+
// debug("----------------------------------- 1")
103107
return exports.Long.MIN_VALUE;
104108
} else if (value + 1 >= exports.Long.TWO_PWR_63_DBL_) {
109+
// debug("----------------------------------- 2")
105110
return exports.Long.MAX_VALUE;
106111
} else if (value < 0) {
112+
// debug("----------------------------------- 3")
107113
return exports.Long.fromNumber(-value).negate();
108114
} else {
115+
// debug("----------------------------------- 1")
109116
return new exports.Long(
110117
(value % exports.Long.TWO_PWR_32_DBL_) | 0,
111118
(value / exports.Long.TWO_PWR_32_DBL_) | 0);

0 commit comments

Comments
 (0)