towards a working windows build for the C extension
This commit is contained in:
parent
0d8ba577ba
commit
706d06aee0
@ -48,6 +48,19 @@ static VALUE OrderedHash;
|
|||||||
#define EXTENDED RE_OPTION_EXTENDED
|
#define EXTENDED RE_OPTION_EXTENDED
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* TODO we ought to check that the malloc or asprintf was successful
|
||||||
|
* and raise an exception if not. */
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define INT2STRING(buffer, i) \
|
||||||
|
{ \
|
||||||
|
int vslength = _scprintf("%d", i) + 1; \
|
||||||
|
*buffer = malloc(vslength); \
|
||||||
|
_snprintf(*buffer, vslength, "%d", i); \
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#define INT2STRING(buffer, i) asprintf(buffer, "%d", i);
|
||||||
|
#endif
|
||||||
|
|
||||||
// this sucks too.
|
// this sucks too.
|
||||||
#ifndef RREGEXP_SRC_PTR
|
#ifndef RREGEXP_SRC_PTR
|
||||||
#define RREGEXP_SRC_PTR(r) RREGEXP(r)->str
|
#define RREGEXP_SRC_PTR(r) RREGEXP(r)->str
|
||||||
@ -114,8 +127,8 @@ static void buffer_assure_space(bson_buffer* buffer, int size) {
|
|||||||
|
|
||||||
/* returns offset for writing */
|
/* returns offset for writing */
|
||||||
static int buffer_save_bytes(bson_buffer* buffer, int size) {
|
static int buffer_save_bytes(bson_buffer* buffer, int size) {
|
||||||
buffer_assure_space(buffer, size);
|
|
||||||
int position = buffer->position;
|
int position = buffer->position;
|
||||||
|
buffer_assure_space(buffer, size);
|
||||||
buffer->position += size;
|
buffer->position += size;
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
@ -155,10 +168,10 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (check_keys == Qtrue) {
|
if (check_keys == Qtrue) {
|
||||||
|
int i;
|
||||||
if (RSTRING_LEN(key) > 0 && RSTRING_PTR(key)[0] == '$') {
|
if (RSTRING_LEN(key) > 0 && RSTRING_PTR(key)[0] == '$') {
|
||||||
rb_raise(rb_eRuntimeError, "key must not start with '$'");
|
rb_raise(rb_eRuntimeError, "key must not start with '$'");
|
||||||
}
|
}
|
||||||
int i;
|
|
||||||
for (i = 0; i < RSTRING_LEN(key); i++) {
|
for (i = 0; i < RSTRING_LEN(key); i++) {
|
||||||
if (RSTRING_PTR(key)[i] == '.') {
|
if (RSTRING_PTR(key)[i] == '.') {
|
||||||
rb_raise(rb_eRuntimeError, "key must not contain '.'");
|
rb_raise(rb_eRuntimeError, "key must not contain '.'");
|
||||||
@ -169,20 +182,23 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
|
|||||||
switch(TYPE(value)) {
|
switch(TYPE(value)) {
|
||||||
case T_BIGNUM:
|
case T_BIGNUM:
|
||||||
{
|
{
|
||||||
|
VALUE as_f;
|
||||||
|
int int_value;
|
||||||
if (rb_funcall(value, rb_intern(">"), 1, INT2NUM(2147483647)) == Qtrue ||
|
if (rb_funcall(value, rb_intern(">"), 1, INT2NUM(2147483647)) == Qtrue ||
|
||||||
rb_funcall(value, rb_intern("<"), 1, INT2NUM(-2147483648)) == Qtrue) {
|
rb_funcall(value, rb_intern("<"), 1, INT2NUM(-2147483648)) == Qtrue) {
|
||||||
rb_raise(rb_eRangeError, "MongoDB can only handle 4-byte ints - try converting to a double before saving");
|
rb_raise(rb_eRangeError, "MongoDB can only handle 4-byte ints"
|
||||||
|
" - try converting to a double before saving");
|
||||||
}
|
}
|
||||||
write_name_and_type(buffer, key, 0x10);
|
write_name_and_type(buffer, key, 0x10);
|
||||||
VALUE as_f = rb_funcall(value, rb_intern("to_f"), 0);
|
as_f = rb_funcall(value, rb_intern("to_f"), 0);
|
||||||
int int_value = NUM2LL(as_f);
|
int_value = NUM2LL(as_f);
|
||||||
buffer_write_bytes(buffer, (char*)&int_value, 4);
|
buffer_write_bytes(buffer, (char*)&int_value, 4);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case T_FIXNUM:
|
case T_FIXNUM:
|
||||||
{
|
{
|
||||||
write_name_and_type(buffer, key, 0x10);
|
|
||||||
int int_value = FIX2INT(value);
|
int int_value = FIX2INT(value);
|
||||||
|
write_name_and_type(buffer, key, 0x10);
|
||||||
buffer_write_bytes(buffer, (char*)&int_value, 4);
|
buffer_write_bytes(buffer, (char*)&int_value, 4);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -200,8 +216,8 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
|
|||||||
}
|
}
|
||||||
case T_FLOAT:
|
case T_FLOAT:
|
||||||
{
|
{
|
||||||
write_name_and_type(buffer, key, 0x01);
|
|
||||||
double d = NUM2DBL(value);
|
double d = NUM2DBL(value);
|
||||||
|
write_name_and_type(buffer, key, 0x01);
|
||||||
buffer_write_bytes(buffer, (char*)&d, 8);
|
buffer_write_bytes(buffer, (char*)&d, 8);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -218,26 +234,29 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
|
|||||||
}
|
}
|
||||||
case T_ARRAY:
|
case T_ARRAY:
|
||||||
{
|
{
|
||||||
|
int start_position, length_location, items, i, obj_length;
|
||||||
|
VALUE* values;
|
||||||
|
|
||||||
write_name_and_type(buffer, key, 0x04);
|
write_name_and_type(buffer, key, 0x04);
|
||||||
int start_position = buffer->position;
|
start_position = buffer->position;
|
||||||
|
|
||||||
// save space for length
|
// save space for length
|
||||||
int length_location = buffer_save_bytes(buffer, 4);
|
length_location = buffer_save_bytes(buffer, 4);
|
||||||
|
|
||||||
int items = RARRAY_LEN(value);
|
items = RARRAY_LEN(value);
|
||||||
VALUE* values = RARRAY_PTR(value);
|
values = RARRAY_PTR(value);
|
||||||
int i;
|
|
||||||
for(i = 0; i < items; i++) {
|
for(i = 0; i < items; i++) {
|
||||||
char* name;
|
char* name;
|
||||||
asprintf(&name, "%d", i);
|
VALUE key;
|
||||||
VALUE key = rb_str_new2(name);
|
INT2STRING(&name, i);
|
||||||
|
key = rb_str_new2(name);
|
||||||
write_element(key, values[i], pack_extra(buffer, check_keys));
|
write_element(key, values[i], pack_extra(buffer, check_keys));
|
||||||
free(name);
|
free(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// write null byte and fill in length
|
// write null byte and fill in length
|
||||||
buffer_write_bytes(buffer, &zero, 1);
|
buffer_write_bytes(buffer, &zero, 1);
|
||||||
int obj_length = buffer->position - start_position;
|
obj_length = buffer->position - start_position;
|
||||||
memcpy(buffer->buffer + length_location, &obj_length, 4);
|
memcpy(buffer->buffer + length_location, &obj_length, 4);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -245,24 +264,25 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
|
|||||||
{
|
{
|
||||||
if (strcmp(rb_class2name(RBASIC(value)->klass),
|
if (strcmp(rb_class2name(RBASIC(value)->klass),
|
||||||
"XGen::Mongo::Driver::Code") == 0) {
|
"XGen::Mongo::Driver::Code") == 0) {
|
||||||
|
int start_position, length_location, length, total_length;
|
||||||
write_name_and_type(buffer, key, 0x0F);
|
write_name_and_type(buffer, key, 0x0F);
|
||||||
|
|
||||||
int start_position = buffer->position;
|
start_position = buffer->position;
|
||||||
int length_location = buffer_save_bytes(buffer, 4);
|
length_location = buffer_save_bytes(buffer, 4);
|
||||||
|
|
||||||
int length = RSTRING_LEN(value) + 1;
|
length = RSTRING_LEN(value) + 1;
|
||||||
buffer_write_bytes(buffer, (char*)&length, 4);
|
buffer_write_bytes(buffer, (char*)&length, 4);
|
||||||
buffer_write_bytes(buffer, RSTRING_PTR(value), length - 1);
|
buffer_write_bytes(buffer, RSTRING_PTR(value), length - 1);
|
||||||
buffer_write_bytes(buffer, &zero, 1);
|
buffer_write_bytes(buffer, &zero, 1);
|
||||||
write_doc(buffer, rb_funcall(value, rb_intern("scope"), 0), Qfalse);
|
write_doc(buffer, rb_funcall(value, rb_intern("scope"), 0), Qfalse);
|
||||||
|
|
||||||
int total_length = buffer->position - start_position;
|
total_length = buffer->position - start_position;
|
||||||
memcpy(buffer->buffer + length_location, &total_length, 4);
|
memcpy(buffer->buffer + length_location, &total_length, 4);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
write_name_and_type(buffer, key, 0x02);
|
|
||||||
int length = RSTRING_LEN(value) + 1;
|
int length = RSTRING_LEN(value) + 1;
|
||||||
|
write_name_and_type(buffer, key, 0x02);
|
||||||
buffer_write_bytes(buffer, (char*)&length, 4);
|
buffer_write_bytes(buffer, (char*)&length, 4);
|
||||||
buffer_write_bytes(buffer, RSTRING_PTR(value), length - 1);
|
buffer_write_bytes(buffer, RSTRING_PTR(value), length - 1);
|
||||||
buffer_write_bytes(buffer, &zero, 1);
|
buffer_write_bytes(buffer, &zero, 1);
|
||||||
@ -271,9 +291,9 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
|
|||||||
}
|
}
|
||||||
case T_SYMBOL:
|
case T_SYMBOL:
|
||||||
{
|
{
|
||||||
write_name_and_type(buffer, key, 0x0E);
|
|
||||||
const char* str_value = rb_id2name(SYM2ID(value));
|
const char* str_value = rb_id2name(SYM2ID(value));
|
||||||
int length = strlen(str_value) + 1;
|
int length = strlen(str_value) + 1;
|
||||||
|
write_name_and_type(buffer, key, 0x0E);
|
||||||
buffer_write_bytes(buffer, (char*)&length, 4);
|
buffer_write_bytes(buffer, (char*)&length, 4);
|
||||||
buffer_write_bytes(buffer, str_value, length);
|
buffer_write_bytes(buffer, str_value, length);
|
||||||
break;
|
break;
|
||||||
@ -284,11 +304,11 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
|
|||||||
const char* cls = rb_class2name(RBASIC(value)->klass);
|
const char* cls = rb_class2name(RBASIC(value)->klass);
|
||||||
if (strcmp(cls, "XGen::Mongo::Driver::Binary") == 0 ||
|
if (strcmp(cls, "XGen::Mongo::Driver::Binary") == 0 ||
|
||||||
strcmp(cls, "ByteBuffer") == 0) {
|
strcmp(cls, "ByteBuffer") == 0) {
|
||||||
write_name_and_type(buffer, key, 0x05);
|
|
||||||
const char subtype = strcmp(cls, "ByteBuffer") ?
|
const char subtype = strcmp(cls, "ByteBuffer") ?
|
||||||
(const char)FIX2INT(rb_funcall(value, rb_intern("subtype"), 0)) : 2;
|
(const char)FIX2INT(rb_funcall(value, rb_intern("subtype"), 0)) : 2;
|
||||||
VALUE string_data = rb_funcall(value, rb_intern("to_s"), 0);
|
VALUE string_data = rb_funcall(value, rb_intern("to_s"), 0);
|
||||||
int length = RSTRING_LEN(string_data);
|
int length = RSTRING_LEN(string_data);
|
||||||
|
write_name_and_type(buffer, key, 0x05);
|
||||||
if (subtype == 2) {
|
if (subtype == 2) {
|
||||||
const int other_length = length + 4;
|
const int other_length = length + 4;
|
||||||
buffer_write_bytes(buffer, (const char*)&other_length, 4);
|
buffer_write_bytes(buffer, (const char*)&other_length, 4);
|
||||||
@ -302,9 +322,9 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (strcmp(cls, "XGen::Mongo::Driver::ObjectID") == 0) {
|
if (strcmp(cls, "XGen::Mongo::Driver::ObjectID") == 0) {
|
||||||
write_name_and_type(buffer, key, 0x07);
|
|
||||||
VALUE as_array = rb_funcall(value, rb_intern("to_a"), 0);
|
VALUE as_array = rb_funcall(value, rb_intern("to_a"), 0);
|
||||||
int i;
|
int i;
|
||||||
|
write_name_and_type(buffer, key, 0x07);
|
||||||
for (i = 0; i < 12; i++) {
|
for (i = 0; i < 12; i++) {
|
||||||
char byte = (char)FIX2INT(RARRAY_PTR(as_array)[i]);
|
char byte = (char)FIX2INT(RARRAY_PTR(as_array)[i]);
|
||||||
buffer_write_bytes(buffer, &byte, 1);
|
buffer_write_bytes(buffer, &byte, 1);
|
||||||
@ -312,21 +332,23 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (strcmp(cls, "XGen::Mongo::Driver::DBRef") == 0) {
|
if (strcmp(cls, "XGen::Mongo::Driver::DBRef") == 0) {
|
||||||
|
int start_position, length_location, obj_length;
|
||||||
|
VALUE ns, oid;
|
||||||
write_name_and_type(buffer, key, 0x03);
|
write_name_and_type(buffer, key, 0x03);
|
||||||
|
|
||||||
int start_position = buffer->position;
|
start_position = buffer->position;
|
||||||
|
|
||||||
// save space for length
|
// save space for length
|
||||||
int length_location = buffer_save_bytes(buffer, 4);
|
length_location = buffer_save_bytes(buffer, 4);
|
||||||
|
|
||||||
VALUE ns = rb_funcall(value, rb_intern("namespace"), 0);
|
ns = rb_funcall(value, rb_intern("namespace"), 0);
|
||||||
write_element(rb_str_new2("$ref"), ns, pack_extra(buffer, Qfalse));
|
write_element(rb_str_new2("$ref"), ns, pack_extra(buffer, Qfalse));
|
||||||
VALUE oid = rb_funcall(value, rb_intern("object_id"), 0);
|
oid = rb_funcall(value, rb_intern("object_id"), 0);
|
||||||
write_element(rb_str_new2("$id"), oid, pack_extra(buffer, Qfalse));
|
write_element(rb_str_new2("$id"), oid, pack_extra(buffer, Qfalse));
|
||||||
|
|
||||||
// write null byte and fill in length
|
// write null byte and fill in length
|
||||||
buffer_write_bytes(buffer, &zero, 1);
|
buffer_write_bytes(buffer, &zero, 1);
|
||||||
int obj_length = buffer->position - start_position;
|
obj_length = buffer->position - start_position;
|
||||||
memcpy(buffer->buffer + length_location, &obj_length, 4);
|
memcpy(buffer->buffer + length_location, &obj_length, 4);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -340,23 +362,25 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
|
|||||||
// TODO again, is this really the only way to do this?
|
// TODO again, is this really the only way to do this?
|
||||||
const char* cls = rb_class2name(RBASIC(value)->klass);
|
const char* cls = rb_class2name(RBASIC(value)->klass);
|
||||||
if (strcmp(cls, "Time") == 0) {
|
if (strcmp(cls, "Time") == 0) {
|
||||||
write_name_and_type(buffer, key, 0x09);
|
|
||||||
double t = NUM2DBL(rb_funcall(value, rb_intern("to_f"), 0));
|
double t = NUM2DBL(rb_funcall(value, rb_intern("to_f"), 0));
|
||||||
long long time_since_epoch = (long long)round(t * 1000);
|
long long time_since_epoch = (long long)round(t * 1000);
|
||||||
|
write_name_and_type(buffer, key, 0x09);
|
||||||
buffer_write_bytes(buffer, (const char*)&time_since_epoch, 8);
|
buffer_write_bytes(buffer, (const char*)&time_since_epoch, 8);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case T_REGEXP:
|
case T_REGEXP:
|
||||||
{
|
{
|
||||||
write_name_and_type(buffer, key, 0x0B);
|
|
||||||
|
|
||||||
int length = RREGEXP_SRC_LEN(value);
|
int length = RREGEXP_SRC_LEN(value);
|
||||||
char* pattern = (char*)RREGEXP_SRC_PTR(value);
|
char* pattern = (char*)RREGEXP_SRC_PTR(value);
|
||||||
|
long flags = RREGEXP(value)->ptr->options;
|
||||||
|
VALUE has_extra;
|
||||||
|
|
||||||
|
write_name_and_type(buffer, key, 0x0B);
|
||||||
|
|
||||||
buffer_write_bytes(buffer, pattern, length);
|
buffer_write_bytes(buffer, pattern, length);
|
||||||
buffer_write_bytes(buffer, &zero, 1);
|
buffer_write_bytes(buffer, &zero, 1);
|
||||||
|
|
||||||
long flags = RREGEXP(value)->ptr->options;
|
|
||||||
if (flags & IGNORECASE) {
|
if (flags & IGNORECASE) {
|
||||||
char ignorecase = 'i';
|
char ignorecase = 'i';
|
||||||
buffer_write_bytes(buffer, &ignorecase, 1);
|
buffer_write_bytes(buffer, &ignorecase, 1);
|
||||||
@ -370,7 +394,7 @@ static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow
|
|||||||
buffer_write_bytes(buffer, &extended, 1);
|
buffer_write_bytes(buffer, &extended, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
VALUE has_extra = rb_funcall(value, rb_intern("respond_to?"), 1, rb_str_new2("extra_options_str"));
|
has_extra = rb_funcall(value, rb_intern("respond_to?"), 1, rb_str_new2("extra_options_str"));
|
||||||
if (TYPE(has_extra) == T_TRUE) {
|
if (TYPE(has_extra) == T_TRUE) {
|
||||||
VALUE extra = rb_funcall(value, rb_intern("extra_options_str"), 0);
|
VALUE extra = rb_funcall(value, rb_intern("extra_options_str"), 0);
|
||||||
int old_position = buffer->position;
|
int old_position = buffer->position;
|
||||||
@ -397,6 +421,7 @@ static int write_element(VALUE key, VALUE value, VALUE extra) {
|
|||||||
static void write_doc(bson_buffer* buffer, VALUE hash, VALUE check_keys) {
|
static void write_doc(bson_buffer* buffer, VALUE hash, VALUE check_keys) {
|
||||||
int start_position = buffer->position;
|
int start_position = buffer->position;
|
||||||
int length_location = buffer_save_bytes(buffer, 4);
|
int length_location = buffer_save_bytes(buffer, 4);
|
||||||
|
int length;
|
||||||
|
|
||||||
VALUE key = rb_str_new2("_id");
|
VALUE key = rb_str_new2("_id");
|
||||||
if (rb_funcall(hash, rb_intern("has_key?"), 1, key) == Qtrue) {
|
if (rb_funcall(hash, rb_intern("has_key?"), 1, key) == Qtrue) {
|
||||||
@ -425,17 +450,18 @@ static void write_doc(bson_buffer* buffer, VALUE hash, VALUE check_keys) {
|
|||||||
|
|
||||||
// write null byte and fill in length
|
// write null byte and fill in length
|
||||||
buffer_write_bytes(buffer, &zero, 1);
|
buffer_write_bytes(buffer, &zero, 1);
|
||||||
int length = buffer->position - start_position;
|
length = buffer->position - start_position;
|
||||||
memcpy(buffer->buffer + length_location, &length, 4);
|
memcpy(buffer->buffer + length_location, &length, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE method_serialize(VALUE self, VALUE doc, VALUE check_keys) {
|
static VALUE method_serialize(VALUE self, VALUE doc, VALUE check_keys) {
|
||||||
|
VALUE result;
|
||||||
bson_buffer* buffer = buffer_new();
|
bson_buffer* buffer = buffer_new();
|
||||||
assert(buffer);
|
assert(buffer);
|
||||||
|
|
||||||
write_doc(buffer, doc, check_keys);
|
write_doc(buffer, doc, check_keys);
|
||||||
|
|
||||||
VALUE result = rb_str_new(buffer->buffer, buffer->position);
|
result = rb_str_new(buffer->buffer, buffer->position);
|
||||||
buffer_free(buffer);
|
buffer_free(buffer);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -454,8 +480,9 @@ static VALUE get_value(const char* buffer, int* position, int type) {
|
|||||||
case 2:
|
case 2:
|
||||||
case 13:
|
case 13:
|
||||||
{
|
{
|
||||||
|
int value_length;
|
||||||
*position += 4;
|
*position += 4;
|
||||||
int value_length = strlen(buffer + *position);
|
value_length = strlen(buffer + *position);
|
||||||
value = rb_str_new(buffer+ *position, value_length);
|
value = rb_str_new(buffer+ *position, value_length);
|
||||||
*position += value_length + 1;
|
*position += value_length + 1;
|
||||||
break;
|
break;
|
||||||
@ -468,9 +495,11 @@ static VALUE get_value(const char* buffer, int* position, int type) {
|
|||||||
int offset = *position + 14;
|
int offset = *position + 14;
|
||||||
VALUE argv[2];
|
VALUE argv[2];
|
||||||
int collection_length = strlen(buffer + offset);
|
int collection_length = strlen(buffer + offset);
|
||||||
|
char id_type;
|
||||||
|
|
||||||
argv[0] = rb_str_new(buffer + offset, collection_length);
|
argv[0] = rb_str_new(buffer + offset, collection_length);
|
||||||
offset += collection_length + 1;
|
offset += collection_length + 1;
|
||||||
char id_type = buffer[offset];
|
id_type = buffer[offset];
|
||||||
offset += 5;
|
offset += 5;
|
||||||
argv[1] = get_value(buffer, &offset, (int)id_type);
|
argv[1] = get_value(buffer, &offset, (int)id_type);
|
||||||
value = rb_class_new_instance(2, argv, DBRef);
|
value = rb_class_new_instance(2, argv, DBRef);
|
||||||
@ -482,17 +511,19 @@ static VALUE get_value(const char* buffer, int* position, int type) {
|
|||||||
}
|
}
|
||||||
case 4:
|
case 4:
|
||||||
{
|
{
|
||||||
int size;
|
int size, end;
|
||||||
memcpy(&size, buffer + *position, 4);
|
memcpy(&size, buffer + *position, 4);
|
||||||
int end = *position + size - 1;
|
end = *position + size - 1;
|
||||||
*position += 4;
|
*position += 4;
|
||||||
|
|
||||||
value = rb_ary_new();
|
value = rb_ary_new();
|
||||||
while (*position < end) {
|
while (*position < end) {
|
||||||
int type = (int)buffer[(*position)++];
|
int type = (int)buffer[(*position)++];
|
||||||
int key_size = strlen(buffer + *position);
|
int key_size = strlen(buffer + *position);
|
||||||
|
VALUE to_append;
|
||||||
|
|
||||||
*position += key_size + 1; // just skip the key, they're in order.
|
*position += key_size + 1; // just skip the key, they're in order.
|
||||||
VALUE to_append = get_value(buffer, position, type);
|
to_append = get_value(buffer, position, type);
|
||||||
rb_ary_push(value, to_append);
|
rb_ary_push(value, to_append);
|
||||||
}
|
}
|
||||||
(*position)++;
|
(*position)++;
|
||||||
@ -500,17 +531,20 @@ static VALUE get_value(const char* buffer, int* position, int type) {
|
|||||||
}
|
}
|
||||||
case 5:
|
case 5:
|
||||||
{
|
{
|
||||||
int length;
|
int length, subtype;
|
||||||
|
VALUE data, st;
|
||||||
|
VALUE argv[2];
|
||||||
memcpy(&length, buffer + *position, 4);
|
memcpy(&length, buffer + *position, 4);
|
||||||
int subtype = (unsigned char)buffer[*position + 4];
|
subtype = (unsigned char)buffer[*position + 4];
|
||||||
VALUE data;
|
data;
|
||||||
if (subtype == 2) {
|
if (subtype == 2) {
|
||||||
data = rb_str_new(buffer + *position + 9, length - 4);
|
data = rb_str_new(buffer + *position + 9, length - 4);
|
||||||
} else {
|
} else {
|
||||||
data = rb_str_new(buffer + *position + 5, length);
|
data = rb_str_new(buffer + *position + 5, length);
|
||||||
}
|
}
|
||||||
VALUE st = INT2FIX(subtype);
|
st = INT2FIX(subtype);
|
||||||
VALUE argv[2] = {data, st};
|
argv[0] = data;
|
||||||
|
argv[1] = st;
|
||||||
value = rb_class_new_instance(2, argv, Binary);
|
value = rb_class_new_instance(2, argv, Binary);
|
||||||
*position += length + 5;
|
*position += length + 5;
|
||||||
break;
|
break;
|
||||||
@ -536,9 +570,10 @@ static VALUE get_value(const char* buffer, int* position, int type) {
|
|||||||
case 9:
|
case 9:
|
||||||
{
|
{
|
||||||
long long millis;
|
long long millis;
|
||||||
|
VALUE seconds, microseconds;
|
||||||
memcpy(&millis, buffer + *position, 8);
|
memcpy(&millis, buffer + *position, 8);
|
||||||
VALUE seconds = INT2NUM(millis / 1000);
|
seconds = INT2NUM(millis / 1000);
|
||||||
VALUE microseconds = INT2NUM((millis % 1000) * 1000);
|
microseconds = INT2NUM((millis % 1000) * 1000);
|
||||||
|
|
||||||
value = rb_funcall(Time, rb_intern("at"), 2, seconds, microseconds);
|
value = rb_funcall(Time, rb_intern("at"), 2, seconds, microseconds);
|
||||||
value = rb_funcall(value, rb_intern("utc"), 0);
|
value = rb_funcall(value, rb_intern("utc"), 0);
|
||||||
@ -554,13 +589,12 @@ static VALUE get_value(const char* buffer, int* position, int type) {
|
|||||||
{
|
{
|
||||||
int pattern_length = strlen(buffer + *position);
|
int pattern_length = strlen(buffer + *position);
|
||||||
VALUE pattern = rb_str_new(buffer + *position, pattern_length);
|
VALUE pattern = rb_str_new(buffer + *position, pattern_length);
|
||||||
|
int flags_length, flags = 0, i = 0;
|
||||||
|
char extra[10];
|
||||||
|
VALUE argv[3];
|
||||||
*position += pattern_length + 1;
|
*position += pattern_length + 1;
|
||||||
|
|
||||||
int flags_length = strlen(buffer + *position);
|
flags_length = strlen(buffer + *position);
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
int flags = 0;
|
|
||||||
char extra[10];
|
|
||||||
extra[0] = 0;
|
extra[0] = 0;
|
||||||
for (i = 0; i < flags_length; i++) {
|
for (i = 0; i < flags_length; i++) {
|
||||||
char flag = buffer[*position + i];
|
char flag = buffer[*position + i];
|
||||||
@ -577,28 +611,29 @@ static VALUE get_value(const char* buffer, int* position, int type) {
|
|||||||
strncat(extra, &flag, 1);
|
strncat(extra, &flag, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
VALUE argv[3] = {
|
argv[0] = pattern;
|
||||||
pattern,
|
argv[1] = INT2FIX(flags);
|
||||||
INT2FIX(flags),
|
argv[2] = rb_str_new2(extra);
|
||||||
rb_str_new2(extra)
|
|
||||||
};
|
|
||||||
value = rb_class_new_instance(3, argv, RegexpOfHolding);
|
value = rb_class_new_instance(3, argv, RegexpOfHolding);
|
||||||
*position += flags_length + 1;
|
*position += flags_length + 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 12:
|
case 12:
|
||||||
{
|
{
|
||||||
|
int collection_length;
|
||||||
|
VALUE collection, str, oid, id, argv[2];
|
||||||
*position += 4;
|
*position += 4;
|
||||||
int collection_length = strlen(buffer + *position);
|
collection_length = strlen(buffer + *position);
|
||||||
VALUE collection = rb_str_new(buffer + *position, collection_length);
|
collection = rb_str_new(buffer + *position, collection_length);
|
||||||
*position += collection_length + 1;
|
*position += collection_length + 1;
|
||||||
|
|
||||||
VALUE str = rb_str_new(buffer + *position, 12);
|
str = rb_str_new(buffer + *position, 12);
|
||||||
VALUE oid = rb_funcall(str, rb_intern("unpack"), 1, rb_str_new2("C*"));
|
oid = rb_funcall(str, rb_intern("unpack"), 1, rb_str_new2("C*"));
|
||||||
VALUE id = rb_class_new_instance(1, &oid, ObjectID);
|
id = rb_class_new_instance(1, &oid, ObjectID);
|
||||||
*position += 12;
|
*position += 12;
|
||||||
|
|
||||||
VALUE argv[2] = {collection, id};
|
argv[0] = collection;
|
||||||
|
argv[1] = id;
|
||||||
value = rb_class_new_instance(2, argv, DBRef);
|
value = rb_class_new_instance(2, argv, DBRef);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -612,17 +647,19 @@ static VALUE get_value(const char* buffer, int* position, int type) {
|
|||||||
}
|
}
|
||||||
case 15:
|
case 15:
|
||||||
{
|
{
|
||||||
|
int code_length, scope_size;
|
||||||
|
VALUE code, scope, argv[2];
|
||||||
*position += 8;
|
*position += 8;
|
||||||
int code_length = strlen(buffer + *position);
|
code_length = strlen(buffer + *position);
|
||||||
VALUE code = rb_str_new(buffer + *position, code_length);
|
code = rb_str_new(buffer + *position, code_length);
|
||||||
*position += code_length + 1;
|
*position += code_length + 1;
|
||||||
|
|
||||||
int scope_size;
|
|
||||||
memcpy(&scope_size, buffer + *position, 4);
|
memcpy(&scope_size, buffer + *position, 4);
|
||||||
VALUE scope = elements_to_hash(buffer + *position + 4, scope_size - 5);
|
scope = elements_to_hash(buffer + *position + 4, scope_size - 5);
|
||||||
*position += scope_size;
|
*position += scope_size;
|
||||||
|
|
||||||
VALUE argv[2] = {code, scope};
|
argv[0] = code;
|
||||||
|
argv[1] = scope;
|
||||||
value = rb_class_new_instance(2, argv, Code);
|
value = rb_class_new_instance(2, argv, Code);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -660,8 +697,9 @@ static VALUE elements_to_hash(const char* buffer, int max) {
|
|||||||
int type = (int)buffer[position++];
|
int type = (int)buffer[position++];
|
||||||
int name_length = strlen(buffer + position);
|
int name_length = strlen(buffer + position);
|
||||||
VALUE name = rb_str_new(buffer + position, name_length);
|
VALUE name = rb_str_new(buffer + position, name_length);
|
||||||
|
VALUE value;
|
||||||
position += name_length + 1;
|
position += name_length + 1;
|
||||||
VALUE value = get_value(buffer, &position, type);
|
value = get_value(buffer, &position, type);
|
||||||
rb_funcall(hash, rb_intern("[]="), 2, name, value);
|
rb_funcall(hash, rb_intern("[]="), 2, name, value);
|
||||||
}
|
}
|
||||||
return hash;
|
return hash;
|
||||||
@ -679,9 +717,10 @@ static VALUE method_deserialize(VALUE self, VALUE bson) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Init_cbson() {
|
void Init_cbson() {
|
||||||
|
VALUE driver, CBson;
|
||||||
Time = rb_const_get(rb_cObject, rb_intern("Time"));
|
Time = rb_const_get(rb_cObject, rb_intern("Time"));
|
||||||
|
|
||||||
VALUE driver = rb_const_get(rb_const_get(rb_const_get(rb_cObject,
|
driver = rb_const_get(rb_const_get(rb_const_get(rb_cObject,
|
||||||
rb_intern("XGen")),
|
rb_intern("XGen")),
|
||||||
rb_intern("Mongo")),
|
rb_intern("Mongo")),
|
||||||
rb_intern("Driver"));
|
rb_intern("Driver"));
|
||||||
@ -700,7 +739,7 @@ void Init_cbson() {
|
|||||||
rb_require("mongo/util/ordered_hash");
|
rb_require("mongo/util/ordered_hash");
|
||||||
OrderedHash = rb_const_get(rb_cObject, rb_intern("OrderedHash"));
|
OrderedHash = rb_const_get(rb_cObject, rb_intern("OrderedHash"));
|
||||||
|
|
||||||
VALUE CBson = rb_define_module("CBson");
|
CBson = rb_define_module("CBson");
|
||||||
rb_define_module_function(CBson, "serialize", method_serialize, 2);
|
rb_define_module_function(CBson, "serialize", method_serialize, 2);
|
||||||
rb_define_module_function(CBson, "deserialize", method_deserialize, 1);
|
rb_define_module_function(CBson, "deserialize", method_deserialize, 1);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user