2009-03-04 22:38:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2009 10gen, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file contains C implementations of some of the functions needed by the
|
|
|
|
* bson module. If possible, these implementations should be used to speed up
|
|
|
|
* BSON encoding and decoding.
|
|
|
|
*/
|
|
|
|
|
2009-03-03 22:07:22 +00:00
|
|
|
#include "ruby.h"
|
2009-08-03 16:06:51 +00:00
|
|
|
|
|
|
|
#if HAVE_RUBY_ST_H
|
2009-08-03 15:18:13 +00:00
|
|
|
#include "ruby/st.h"
|
2009-08-03 16:06:51 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_ST_H
|
|
|
|
#include "st.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE_RUBY_REGEX_H
|
2009-08-03 15:18:13 +00:00
|
|
|
#include "ruby/regex.h"
|
2009-08-03 16:06:51 +00:00
|
|
|
#endif
|
|
|
|
#if HAVE_REGEX_H
|
|
|
|
#include "regex.h"
|
|
|
|
#endif
|
|
|
|
|
2009-03-03 22:07:22 +00:00
|
|
|
#include <assert.h>
|
2009-06-05 18:52:01 +00:00
|
|
|
#include <math.h>
|
2009-03-03 22:07:22 +00:00
|
|
|
|
|
|
|
#define INITIAL_BUFFER_SIZE 256
|
|
|
|
|
2009-03-10 15:48:06 +00:00
|
|
|
static VALUE Binary;
|
2009-03-10 17:19:18 +00:00
|
|
|
static VALUE Time;
|
|
|
|
static VALUE ObjectID;
|
2009-03-10 18:58:20 +00:00
|
|
|
static VALUE DBRef;
|
2009-03-12 20:12:52 +00:00
|
|
|
static VALUE Code;
|
2009-03-10 19:33:03 +00:00
|
|
|
static VALUE RegexpOfHolding;
|
2009-03-10 19:41:28 +00:00
|
|
|
static VALUE OrderedHash;
|
2009-08-13 19:52:11 +00:00
|
|
|
static VALUE InvalidName;
|
2009-03-10 15:48:06 +00:00
|
|
|
|
2009-09-04 15:02:04 +00:00
|
|
|
#if HAVE_RUBY_ENCODING_H
|
|
|
|
#include "ruby/encoding.h"
|
|
|
|
#define STR_NEW(p,n) rb_enc_str_new((p), (n), rb_utf8_encoding())
|
|
|
|
#else
|
|
|
|
#define STR_NEW(p,n) rb_str_new((p), (n))
|
|
|
|
#endif
|
|
|
|
|
2009-03-23 16:14:38 +00:00
|
|
|
// this sucks. but for some reason these moved around between 1.8 and 1.9
|
|
|
|
#ifdef ONIGURUMA_H
|
|
|
|
#define IGNORECASE ONIG_OPTION_IGNORECASE
|
|
|
|
#define MULTILINE ONIG_OPTION_MULTILINE
|
|
|
|
#define EXTENDED ONIG_OPTION_EXTEND
|
|
|
|
#else
|
|
|
|
#define IGNORECASE RE_OPTION_IGNORECASE
|
|
|
|
#define MULTILINE RE_OPTION_MULTILINE
|
|
|
|
#define EXTENDED RE_OPTION_EXTENDED
|
|
|
|
#endif
|
|
|
|
|
2009-07-17 16:16:33 +00:00
|
|
|
/* 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
|
|
|
|
|
2009-03-23 16:14:38 +00:00
|
|
|
// this sucks too.
|
|
|
|
#ifndef RREGEXP_SRC_PTR
|
|
|
|
#define RREGEXP_SRC_PTR(r) RREGEXP(r)->str
|
|
|
|
#define RREGEXP_SRC_LEN(r) RREGEXP(r)->len
|
|
|
|
#endif
|
|
|
|
|
2009-03-03 22:07:22 +00:00
|
|
|
typedef struct {
|
|
|
|
char* buffer;
|
|
|
|
int size;
|
|
|
|
int position;
|
|
|
|
} bson_buffer;
|
|
|
|
|
2009-03-04 15:05:50 +00:00
|
|
|
static char zero = 0;
|
2009-03-04 16:07:22 +00:00
|
|
|
static char one = 1;
|
|
|
|
|
2009-03-05 18:16:34 +00:00
|
|
|
static int cmp_char(const void* a, const void* b) {
|
|
|
|
return *(char*)a - *(char*)b;
|
|
|
|
}
|
|
|
|
|
2009-06-02 13:38:31 +00:00
|
|
|
static void write_doc(bson_buffer* buffer, VALUE hash, VALUE check_keys);
|
2009-03-05 20:48:40 +00:00
|
|
|
static int write_element(VALUE key, VALUE value, VALUE extra);
|
2009-03-10 14:45:56 +00:00
|
|
|
static VALUE elements_to_hash(const char* buffer, int max);
|
2009-03-04 15:05:50 +00:00
|
|
|
|
2009-03-03 22:07:22 +00:00
|
|
|
static bson_buffer* buffer_new(void) {
|
|
|
|
bson_buffer* buffer;
|
2009-03-05 14:48:12 +00:00
|
|
|
buffer = ALLOC(bson_buffer);
|
2009-03-03 22:07:22 +00:00
|
|
|
assert(buffer);
|
|
|
|
|
|
|
|
buffer->size = INITIAL_BUFFER_SIZE;
|
|
|
|
buffer->position = 0;
|
2009-03-05 14:48:12 +00:00
|
|
|
buffer->buffer = ALLOC_N(char, INITIAL_BUFFER_SIZE);
|
2009-03-03 22:07:22 +00:00
|
|
|
assert(buffer->buffer);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void buffer_free(bson_buffer* buffer) {
|
|
|
|
assert(buffer);
|
|
|
|
assert(buffer->buffer);
|
|
|
|
|
|
|
|
free(buffer->buffer);
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void buffer_resize(bson_buffer* buffer, int min_length) {
|
|
|
|
int size = buffer->size;
|
|
|
|
if (size >= min_length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (size < min_length) {
|
|
|
|
size *= 2;
|
|
|
|
}
|
2009-03-05 14:48:12 +00:00
|
|
|
buffer->buffer = REALLOC_N(buffer->buffer, char, size);
|
2009-03-03 22:07:22 +00:00
|
|
|
assert(buffer->buffer);
|
|
|
|
buffer->size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void buffer_assure_space(bson_buffer* buffer, int size) {
|
|
|
|
if (buffer->position + size <= buffer->size) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buffer_resize(buffer, buffer->position + size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns offset for writing */
|
|
|
|
static int buffer_save_bytes(bson_buffer* buffer, int size) {
|
|
|
|
int position = buffer->position;
|
2009-07-17 16:16:33 +00:00
|
|
|
buffer_assure_space(buffer, size);
|
2009-03-03 22:07:22 +00:00
|
|
|
buffer->position += size;
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void buffer_write_bytes(bson_buffer* buffer, const char* bytes, int size) {
|
|
|
|
buffer_assure_space(buffer, size);
|
|
|
|
|
|
|
|
memcpy(buffer->buffer + buffer->position, bytes, size);
|
|
|
|
buffer->position += size;
|
|
|
|
}
|
|
|
|
|
2009-06-02 13:38:31 +00:00
|
|
|
static VALUE pack_extra(bson_buffer* buffer, VALUE check_keys) {
|
2009-09-04 14:22:28 +00:00
|
|
|
return rb_ary_new3(2, LL2NUM((long long)buffer), check_keys);
|
2009-06-01 21:38:11 +00:00
|
|
|
}
|
|
|
|
|
2009-03-04 15:05:50 +00:00
|
|
|
static void write_name_and_type(bson_buffer* buffer, VALUE name, char type) {
|
|
|
|
buffer_write_bytes(buffer, &type, 1);
|
2009-03-23 16:14:38 +00:00
|
|
|
buffer_write_bytes(buffer, RSTRING_PTR(name), RSTRING_LEN(name));
|
2009-03-04 15:05:50 +00:00
|
|
|
buffer_write_bytes(buffer, &zero, 1);
|
|
|
|
}
|
|
|
|
|
2009-03-05 20:48:40 +00:00
|
|
|
static int write_element_allow_id(VALUE key, VALUE value, VALUE extra, int allow_id) {
|
2009-09-04 14:22:28 +00:00
|
|
|
bson_buffer* buffer = (bson_buffer*)NUM2LL(rb_ary_entry(extra, 0));
|
2009-06-02 13:38:31 +00:00
|
|
|
VALUE check_keys = rb_ary_entry(extra, 1);
|
2009-03-04 15:05:50 +00:00
|
|
|
|
2009-03-05 15:27:19 +00:00
|
|
|
if (TYPE(key) == T_SYMBOL) {
|
|
|
|
// TODO better way to do this... ?
|
|
|
|
key = rb_str_new2(rb_id2name(SYM2ID(key)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TYPE(key) != T_STRING) {
|
|
|
|
rb_raise(rb_eTypeError, "keys must be strings or symbols");
|
|
|
|
}
|
|
|
|
|
2009-03-23 16:14:38 +00:00
|
|
|
if (!allow_id && strcmp("_id", RSTRING_PTR(key)) == 0) {
|
2009-03-05 20:48:40 +00:00
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2009-06-02 13:38:31 +00:00
|
|
|
if (check_keys == Qtrue) {
|
2009-07-17 16:16:33 +00:00
|
|
|
int i;
|
2009-06-02 13:38:31 +00:00
|
|
|
if (RSTRING_LEN(key) > 0 && RSTRING_PTR(key)[0] == '$') {
|
2009-08-13 19:52:11 +00:00
|
|
|
rb_raise(InvalidName, "key must not start with '$'");
|
2009-06-02 13:38:31 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < RSTRING_LEN(key); i++) {
|
|
|
|
if (RSTRING_PTR(key)[i] == '.') {
|
2009-08-13 19:52:11 +00:00
|
|
|
rb_raise(InvalidName, "key must not contain '.'");
|
2009-06-02 13:38:31 +00:00
|
|
|
}
|
2009-06-01 21:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-04 15:05:50 +00:00
|
|
|
switch(TYPE(value)) {
|
2009-03-05 22:03:49 +00:00
|
|
|
case T_BIGNUM:
|
2009-03-04 16:07:22 +00:00
|
|
|
case T_FIXNUM:
|
2009-03-10 17:19:18 +00:00
|
|
|
{
|
2009-08-04 14:39:25 +00:00
|
|
|
if (rb_funcall(value, rb_intern(">"), 1, LL2NUM(9223372036854775807LL)) == Qtrue ||
|
|
|
|
rb_funcall(value, rb_intern("<"), 1, LL2NUM(-9223372036854775808LL)) == Qtrue) {
|
|
|
|
rb_raise(rb_eRangeError, "MongoDB can only handle 8-byte ints");
|
|
|
|
}
|
|
|
|
if (rb_funcall(value, rb_intern(">"), 1, INT2NUM(2147483647L)) == Qtrue ||
|
|
|
|
rb_funcall(value, rb_intern("<"), 1, INT2NUM(-2147483648L)) == Qtrue) {
|
|
|
|
long long ll_value;
|
|
|
|
write_name_and_type(buffer, key, 0x12);
|
|
|
|
ll_value = NUM2LL(value);
|
|
|
|
buffer_write_bytes(buffer, (char*)&ll_value, 8);
|
|
|
|
} else {
|
|
|
|
int int_value;
|
|
|
|
write_name_and_type(buffer, key, 0x10);
|
|
|
|
int_value = NUM2LL(value);
|
|
|
|
buffer_write_bytes(buffer, (char*)&int_value, 4);
|
2009-08-03 15:08:10 +00:00
|
|
|
}
|
2009-03-10 17:19:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-03-04 16:07:22 +00:00
|
|
|
case T_TRUE:
|
2009-03-10 17:19:18 +00:00
|
|
|
{
|
|
|
|
write_name_and_type(buffer, key, 0x08);
|
|
|
|
buffer_write_bytes(buffer, &one, 1);
|
|
|
|
break;
|
|
|
|
}
|
2009-03-04 16:07:22 +00:00
|
|
|
case T_FALSE:
|
2009-03-10 17:19:18 +00:00
|
|
|
{
|
|
|
|
write_name_and_type(buffer, key, 0x08);
|
|
|
|
buffer_write_bytes(buffer, &zero, 1);
|
|
|
|
break;
|
|
|
|
}
|
2009-03-04 16:07:22 +00:00
|
|
|
case T_FLOAT:
|
2009-03-10 17:19:18 +00:00
|
|
|
{
|
|
|
|
double d = NUM2DBL(value);
|
2009-07-17 16:16:33 +00:00
|
|
|
write_name_and_type(buffer, key, 0x01);
|
2009-03-10 17:19:18 +00:00
|
|
|
buffer_write_bytes(buffer, (char*)&d, 8);
|
|
|
|
break;
|
|
|
|
}
|
2009-03-04 16:07:22 +00:00
|
|
|
case T_NIL:
|
2009-03-10 17:19:18 +00:00
|
|
|
{
|
|
|
|
write_name_and_type(buffer, key, 0x0A);
|
|
|
|
break;
|
|
|
|
}
|
2009-03-04 16:07:22 +00:00
|
|
|
case T_HASH:
|
2009-03-10 17:19:18 +00:00
|
|
|
{
|
|
|
|
write_name_and_type(buffer, key, 0x03);
|
2009-06-02 13:38:31 +00:00
|
|
|
write_doc(buffer, value, check_keys);
|
2009-03-10 17:19:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-03-04 16:07:22 +00:00
|
|
|
case T_ARRAY:
|
2009-03-10 17:19:18 +00:00
|
|
|
{
|
2009-07-17 16:16:33 +00:00
|
|
|
int start_position, length_location, items, i, obj_length;
|
|
|
|
VALUE* values;
|
|
|
|
|
2009-03-10 17:19:18 +00:00
|
|
|
write_name_and_type(buffer, key, 0x04);
|
2009-07-17 16:16:33 +00:00
|
|
|
start_position = buffer->position;
|
2009-03-04 16:07:22 +00:00
|
|
|
|
2009-03-10 17:19:18 +00:00
|
|
|
// save space for length
|
2009-07-17 16:16:33 +00:00
|
|
|
length_location = buffer_save_bytes(buffer, 4);
|
2009-03-04 16:07:22 +00:00
|
|
|
|
2009-07-17 16:16:33 +00:00
|
|
|
items = RARRAY_LEN(value);
|
|
|
|
values = RARRAY_PTR(value);
|
2009-03-10 17:19:18 +00:00
|
|
|
for(i = 0; i < items; i++) {
|
|
|
|
char* name;
|
2009-07-17 16:16:33 +00:00
|
|
|
VALUE key;
|
|
|
|
INT2STRING(&name, i);
|
|
|
|
key = rb_str_new2(name);
|
2009-06-02 13:38:31 +00:00
|
|
|
write_element(key, values[i], pack_extra(buffer, check_keys));
|
2009-03-10 17:19:18 +00:00
|
|
|
free(name);
|
|
|
|
}
|
2009-03-04 16:07:22 +00:00
|
|
|
|
2009-03-10 17:19:18 +00:00
|
|
|
// write null byte and fill in length
|
|
|
|
buffer_write_bytes(buffer, &zero, 1);
|
2009-07-17 16:16:33 +00:00
|
|
|
obj_length = buffer->position - start_position;
|
2009-03-10 17:19:18 +00:00
|
|
|
memcpy(buffer->buffer + length_location, &obj_length, 4);
|
|
|
|
break;
|
|
|
|
}
|
2009-03-04 15:05:50 +00:00
|
|
|
case T_STRING:
|
2009-03-10 17:19:18 +00:00
|
|
|
{
|
2009-03-12 20:12:52 +00:00
|
|
|
if (strcmp(rb_class2name(RBASIC(value)->klass),
|
2009-08-20 14:50:48 +00:00
|
|
|
"Mongo::Code") == 0) {
|
2009-07-17 16:16:33 +00:00
|
|
|
int start_position, length_location, length, total_length;
|
2009-03-12 20:12:52 +00:00
|
|
|
write_name_and_type(buffer, key, 0x0F);
|
|
|
|
|
2009-07-17 16:16:33 +00:00
|
|
|
start_position = buffer->position;
|
|
|
|
length_location = buffer_save_bytes(buffer, 4);
|
2009-03-12 20:12:52 +00:00
|
|
|
|
2009-07-17 16:16:33 +00:00
|
|
|
length = RSTRING_LEN(value) + 1;
|
2009-03-12 20:12:52 +00:00
|
|
|
buffer_write_bytes(buffer, (char*)&length, 4);
|
2009-03-23 16:14:38 +00:00
|
|
|
buffer_write_bytes(buffer, RSTRING_PTR(value), length - 1);
|
2009-03-12 20:12:52 +00:00
|
|
|
buffer_write_bytes(buffer, &zero, 1);
|
2009-06-01 21:38:11 +00:00
|
|
|
write_doc(buffer, rb_funcall(value, rb_intern("scope"), 0), Qfalse);
|
2009-03-12 20:12:52 +00:00
|
|
|
|
2009-07-17 16:16:33 +00:00
|
|
|
total_length = buffer->position - start_position;
|
2009-03-12 20:12:52 +00:00
|
|
|
memcpy(buffer->buffer + length_location, &total_length, 4);
|
|
|
|
|
|
|
|
break;
|
2009-03-10 17:19:18 +00:00
|
|
|
} else {
|
2009-03-23 16:14:38 +00:00
|
|
|
int length = RSTRING_LEN(value) + 1;
|
2009-07-17 16:16:33 +00:00
|
|
|
write_name_and_type(buffer, key, 0x02);
|
2009-03-12 20:12:52 +00:00
|
|
|
buffer_write_bytes(buffer, (char*)&length, 4);
|
2009-03-23 16:14:38 +00:00
|
|
|
buffer_write_bytes(buffer, RSTRING_PTR(value), length - 1);
|
2009-03-12 20:12:52 +00:00
|
|
|
buffer_write_bytes(buffer, &zero, 1);
|
|
|
|
break;
|
2009-03-10 17:19:18 +00:00
|
|
|
}
|
2009-03-05 21:08:10 +00:00
|
|
|
}
|
2009-03-05 16:09:52 +00:00
|
|
|
case T_SYMBOL:
|
2009-03-10 17:19:18 +00:00
|
|
|
{
|
|
|
|
const char* str_value = rb_id2name(SYM2ID(value));
|
|
|
|
int length = strlen(str_value) + 1;
|
2009-07-17 16:16:33 +00:00
|
|
|
write_name_and_type(buffer, key, 0x0E);
|
2009-03-10 17:19:18 +00:00
|
|
|
buffer_write_bytes(buffer, (char*)&length, 4);
|
|
|
|
buffer_write_bytes(buffer, str_value, length);
|
|
|
|
break;
|
|
|
|
}
|
2009-03-04 22:38:06 +00:00
|
|
|
case T_OBJECT:
|
|
|
|
{
|
|
|
|
// TODO there has to be a better way to do these checks...
|
|
|
|
const char* cls = rb_class2name(RBASIC(value)->klass);
|
2009-08-20 14:50:48 +00:00
|
|
|
if (strcmp(cls, "Mongo::Binary") == 0 ||
|
2009-03-05 14:22:55 +00:00
|
|
|
strcmp(cls, "ByteBuffer") == 0) {
|
|
|
|
const char subtype = strcmp(cls, "ByteBuffer") ?
|
|
|
|
(const char)FIX2INT(rb_funcall(value, rb_intern("subtype"), 0)) : 2;
|
2009-03-04 22:38:06 +00:00
|
|
|
VALUE string_data = rb_funcall(value, rb_intern("to_s"), 0);
|
2009-03-23 16:14:38 +00:00
|
|
|
int length = RSTRING_LEN(string_data);
|
2009-07-17 16:16:33 +00:00
|
|
|
write_name_and_type(buffer, key, 0x05);
|
2009-03-04 22:38:06 +00:00
|
|
|
if (subtype == 2) {
|
|
|
|
const int other_length = length + 4;
|
|
|
|
buffer_write_bytes(buffer, (const char*)&other_length, 4);
|
|
|
|
buffer_write_bytes(buffer, &subtype, 1);
|
|
|
|
}
|
|
|
|
buffer_write_bytes(buffer, (const char*)&length, 4);
|
|
|
|
if (subtype != 2) {
|
|
|
|
buffer_write_bytes(buffer, &subtype, 1);
|
|
|
|
}
|
2009-03-23 16:14:38 +00:00
|
|
|
buffer_write_bytes(buffer, RSTRING_PTR(string_data), length);
|
2009-03-04 22:38:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-08-20 14:50:48 +00:00
|
|
|
if (strcmp(cls, "Mongo::ObjectID") == 0) {
|
2009-03-05 16:38:08 +00:00
|
|
|
VALUE as_array = rb_funcall(value, rb_intern("to_a"), 0);
|
|
|
|
int i;
|
2009-07-17 16:16:33 +00:00
|
|
|
write_name_and_type(buffer, key, 0x07);
|
2009-03-05 16:38:08 +00:00
|
|
|
for (i = 0; i < 12; i++) {
|
2009-03-23 16:14:38 +00:00
|
|
|
char byte = (char)FIX2INT(RARRAY_PTR(as_array)[i]);
|
2009-03-05 16:38:08 +00:00
|
|
|
buffer_write_bytes(buffer, &byte, 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-08-20 14:50:48 +00:00
|
|
|
if (strcmp(cls, "Mongo::DBRef") == 0) {
|
2009-07-17 16:16:33 +00:00
|
|
|
int start_position, length_location, obj_length;
|
|
|
|
VALUE ns, oid;
|
2009-03-18 17:54:33 +00:00
|
|
|
write_name_and_type(buffer, key, 0x03);
|
|
|
|
|
2009-07-17 16:16:33 +00:00
|
|
|
start_position = buffer->position;
|
2009-03-18 17:54:33 +00:00
|
|
|
|
|
|
|
// save space for length
|
2009-07-17 16:16:33 +00:00
|
|
|
length_location = buffer_save_bytes(buffer, 4);
|
2009-03-05 16:45:04 +00:00
|
|
|
|
2009-07-17 16:16:33 +00:00
|
|
|
ns = rb_funcall(value, rb_intern("namespace"), 0);
|
2009-06-01 21:38:11 +00:00
|
|
|
write_element(rb_str_new2("$ref"), ns, pack_extra(buffer, Qfalse));
|
2009-07-17 16:16:33 +00:00
|
|
|
oid = rb_funcall(value, rb_intern("object_id"), 0);
|
2009-06-01 21:38:11 +00:00
|
|
|
write_element(rb_str_new2("$id"), oid, pack_extra(buffer, Qfalse));
|
2009-03-05 16:45:04 +00:00
|
|
|
|
2009-03-18 17:54:33 +00:00
|
|
|
// write null byte and fill in length
|
|
|
|
buffer_write_bytes(buffer, &zero, 1);
|
2009-07-17 16:16:33 +00:00
|
|
|
obj_length = buffer->position - start_position;
|
2009-03-18 17:54:33 +00:00
|
|
|
memcpy(buffer->buffer + length_location, &obj_length, 4);
|
2009-03-05 16:45:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-03-04 22:38:06 +00:00
|
|
|
}
|
2009-03-05 15:58:56 +00:00
|
|
|
case T_DATA:
|
|
|
|
{
|
|
|
|
// TODO again, is this really the only way to do this?
|
|
|
|
const char* cls = rb_class2name(RBASIC(value)->klass);
|
|
|
|
if (strcmp(cls, "Time") == 0) {
|
|
|
|
double t = NUM2DBL(rb_funcall(value, rb_intern("to_f"), 0));
|
2009-06-05 18:52:01 +00:00
|
|
|
long long time_since_epoch = (long long)round(t * 1000);
|
2009-07-17 16:16:33 +00:00
|
|
|
write_name_and_type(buffer, key, 0x09);
|
2009-03-05 15:58:56 +00:00
|
|
|
buffer_write_bytes(buffer, (const char*)&time_since_epoch, 8);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-03-05 18:16:34 +00:00
|
|
|
case T_REGEXP:
|
2009-03-10 17:19:18 +00:00
|
|
|
{
|
2009-03-23 16:14:38 +00:00
|
|
|
int length = RREGEXP_SRC_LEN(value);
|
|
|
|
char* pattern = (char*)RREGEXP_SRC_PTR(value);
|
2009-07-17 16:16:33 +00:00
|
|
|
long flags = RREGEXP(value)->ptr->options;
|
|
|
|
VALUE has_extra;
|
|
|
|
|
|
|
|
write_name_and_type(buffer, key, 0x0B);
|
|
|
|
|
2009-03-10 17:19:18 +00:00
|
|
|
buffer_write_bytes(buffer, pattern, length);
|
|
|
|
buffer_write_bytes(buffer, &zero, 1);
|
2009-03-05 18:16:34 +00:00
|
|
|
|
2009-03-23 16:14:38 +00:00
|
|
|
if (flags & IGNORECASE) {
|
2009-03-10 17:19:18 +00:00
|
|
|
char ignorecase = 'i';
|
|
|
|
buffer_write_bytes(buffer, &ignorecase, 1);
|
|
|
|
}
|
2009-03-23 16:14:38 +00:00
|
|
|
if (flags & MULTILINE) {
|
2009-03-10 17:19:18 +00:00
|
|
|
char multiline = 'm';
|
|
|
|
buffer_write_bytes(buffer, &multiline, 1);
|
|
|
|
}
|
2009-03-23 16:14:38 +00:00
|
|
|
if (flags & EXTENDED) {
|
2009-03-10 17:19:18 +00:00
|
|
|
char extended = 'x';
|
|
|
|
buffer_write_bytes(buffer, &extended, 1);
|
|
|
|
}
|
2009-03-05 18:16:34 +00:00
|
|
|
|
2009-07-17 16:16:33 +00:00
|
|
|
has_extra = rb_funcall(value, rb_intern("respond_to?"), 1, rb_str_new2("extra_options_str"));
|
2009-03-10 17:19:18 +00:00
|
|
|
if (TYPE(has_extra) == T_TRUE) {
|
|
|
|
VALUE extra = rb_funcall(value, rb_intern("extra_options_str"), 0);
|
|
|
|
int old_position = buffer->position;
|
2009-03-23 16:14:38 +00:00
|
|
|
buffer_write_bytes(buffer, RSTRING_PTR(extra), RSTRING_LEN(extra));
|
|
|
|
qsort(buffer->buffer + old_position, RSTRING_LEN(extra), sizeof(char), cmp_char);
|
2009-03-10 17:19:18 +00:00
|
|
|
}
|
|
|
|
buffer_write_bytes(buffer, &zero, 1);
|
2009-03-05 18:16:34 +00:00
|
|
|
|
2009-03-10 17:19:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-03-04 15:05:50 +00:00
|
|
|
default:
|
2009-03-10 17:19:18 +00:00
|
|
|
{
|
|
|
|
rb_raise(rb_eTypeError, "no c encoder for this type yet (%d)", TYPE(value));
|
|
|
|
break;
|
|
|
|
}
|
2009-03-04 15:05:50 +00:00
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2009-03-05 20:48:40 +00:00
|
|
|
static int write_element(VALUE key, VALUE value, VALUE extra) {
|
|
|
|
return write_element_allow_id(key, value, extra, 0);
|
|
|
|
}
|
|
|
|
|
2009-06-02 13:38:31 +00:00
|
|
|
static void write_doc(bson_buffer* buffer, VALUE hash, VALUE check_keys) {
|
2009-03-04 15:05:50 +00:00
|
|
|
int start_position = buffer->position;
|
|
|
|
int length_location = buffer_save_bytes(buffer, 4);
|
2009-07-17 16:16:33 +00:00
|
|
|
int length;
|
2009-03-04 15:05:50 +00:00
|
|
|
|
2009-03-05 20:48:40 +00:00
|
|
|
VALUE key = rb_str_new2("_id");
|
2009-04-24 14:03:40 +00:00
|
|
|
if (rb_funcall(hash, rb_intern("has_key?"), 1, key) == Qtrue) {
|
|
|
|
VALUE id = rb_hash_aref(hash, key);
|
2009-06-02 13:38:31 +00:00
|
|
|
write_element_allow_id(key, id, pack_extra(buffer, check_keys), 1);
|
2009-03-05 20:48:40 +00:00
|
|
|
}
|
|
|
|
key = ID2SYM(rb_intern("_id"));
|
2009-04-24 14:03:40 +00:00
|
|
|
if (rb_funcall(hash, rb_intern("has_key?"), 1, key) == Qtrue) {
|
|
|
|
VALUE id = rb_hash_aref(hash, key);
|
2009-06-02 13:38:31 +00:00
|
|
|
write_element_allow_id(key, id, pack_extra(buffer, check_keys), 1);
|
2009-03-05 20:48:40 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 19:58:34 +00:00
|
|
|
// we have to check for an OrderedHash and handle that specially
|
|
|
|
if (strcmp(rb_class2name(RBASIC(hash)->klass), "OrderedHash") == 0) {
|
|
|
|
VALUE keys = rb_funcall(hash, rb_intern("keys"), 0);
|
|
|
|
int i;
|
2009-03-23 16:14:38 +00:00
|
|
|
for(i = 0; i < RARRAY_LEN(keys); i++) {
|
|
|
|
VALUE key = RARRAY_PTR(keys)[i];
|
2009-03-05 19:58:34 +00:00
|
|
|
VALUE value = rb_hash_aref(hash, key);
|
2009-06-01 21:38:11 +00:00
|
|
|
|
2009-06-02 13:38:31 +00:00
|
|
|
write_element(key, value, pack_extra(buffer, check_keys));
|
2009-03-05 19:58:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-06-02 13:38:31 +00:00
|
|
|
rb_hash_foreach(hash, write_element, pack_extra(buffer, check_keys));
|
2009-03-05 19:58:34 +00:00
|
|
|
}
|
2009-03-04 15:05:50 +00:00
|
|
|
|
|
|
|
// write null byte and fill in length
|
|
|
|
buffer_write_bytes(buffer, &zero, 1);
|
2009-07-17 16:16:33 +00:00
|
|
|
length = buffer->position - start_position;
|
2009-03-04 15:05:50 +00:00
|
|
|
memcpy(buffer->buffer + length_location, &length, 4);
|
|
|
|
}
|
|
|
|
|
2009-06-02 13:38:31 +00:00
|
|
|
static VALUE method_serialize(VALUE self, VALUE doc, VALUE check_keys) {
|
2009-07-17 16:16:33 +00:00
|
|
|
VALUE result;
|
2009-03-04 15:05:50 +00:00
|
|
|
bson_buffer* buffer = buffer_new();
|
|
|
|
assert(buffer);
|
|
|
|
|
2009-06-02 13:38:31 +00:00
|
|
|
write_doc(buffer, doc, check_keys);
|
2009-03-04 15:05:50 +00:00
|
|
|
|
2009-07-17 16:16:33 +00:00
|
|
|
result = rb_str_new(buffer->buffer, buffer->position);
|
2009-03-04 15:05:50 +00:00
|
|
|
buffer_free(buffer);
|
|
|
|
return result;
|
2009-03-03 22:07:22 +00:00
|
|
|
}
|
|
|
|
|
2009-03-10 14:23:42 +00:00
|
|
|
static VALUE get_value(const char* buffer, int* position, int type) {
|
|
|
|
VALUE value;
|
|
|
|
switch (type) {
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
double d;
|
|
|
|
memcpy(&d, buffer + *position, 8);
|
2009-03-10 14:45:56 +00:00
|
|
|
value = rb_float_new(d);
|
2009-03-10 14:23:42 +00:00
|
|
|
*position += 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2:
|
2009-03-10 15:48:06 +00:00
|
|
|
case 13:
|
2009-03-10 14:23:42 +00:00
|
|
|
{
|
2009-07-17 16:16:33 +00:00
|
|
|
int value_length;
|
2009-03-10 14:23:42 +00:00
|
|
|
*position += 4;
|
2009-07-17 16:16:33 +00:00
|
|
|
value_length = strlen(buffer + *position);
|
2009-09-04 15:02:04 +00:00
|
|
|
value = STR_NEW(buffer + *position, value_length);
|
2009-03-10 14:23:42 +00:00
|
|
|
*position += value_length + 1;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-10 14:45:56 +00:00
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
memcpy(&size, buffer + *position, 4);
|
2009-03-18 17:54:33 +00:00
|
|
|
if (strcmp(buffer + *position + 5, "$ref") == 0) { // DBRef
|
|
|
|
int offset = *position + 14;
|
|
|
|
VALUE argv[2];
|
|
|
|
int collection_length = strlen(buffer + offset);
|
2009-07-17 16:16:33 +00:00
|
|
|
char id_type;
|
|
|
|
|
2009-09-04 15:02:04 +00:00
|
|
|
argv[0] = STR_NEW(buffer + offset, collection_length);
|
2009-03-18 17:54:33 +00:00
|
|
|
offset += collection_length + 1;
|
2009-07-17 16:16:33 +00:00
|
|
|
id_type = buffer[offset];
|
2009-03-18 17:54:33 +00:00
|
|
|
offset += 5;
|
|
|
|
argv[1] = get_value(buffer, &offset, (int)id_type);
|
|
|
|
value = rb_class_new_instance(2, argv, DBRef);
|
|
|
|
} else {
|
|
|
|
value = elements_to_hash(buffer + *position + 4, size - 5);
|
|
|
|
}
|
2009-03-10 14:45:56 +00:00
|
|
|
*position += size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 4:
|
|
|
|
{
|
2009-07-17 16:16:33 +00:00
|
|
|
int size, end;
|
2009-03-10 14:45:56 +00:00
|
|
|
memcpy(&size, buffer + *position, 4);
|
2009-07-17 16:16:33 +00:00
|
|
|
end = *position + size - 1;
|
2009-03-10 14:45:56 +00:00
|
|
|
*position += 4;
|
|
|
|
|
|
|
|
value = rb_ary_new();
|
|
|
|
while (*position < end) {
|
|
|
|
int type = (int)buffer[(*position)++];
|
|
|
|
int key_size = strlen(buffer + *position);
|
2009-07-17 16:16:33 +00:00
|
|
|
VALUE to_append;
|
|
|
|
|
2009-03-10 14:45:56 +00:00
|
|
|
*position += key_size + 1; // just skip the key, they're in order.
|
2009-07-17 16:16:33 +00:00
|
|
|
to_append = get_value(buffer, position, type);
|
2009-03-10 14:45:56 +00:00
|
|
|
rb_ary_push(value, to_append);
|
|
|
|
}
|
|
|
|
(*position)++;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-10 15:48:06 +00:00
|
|
|
case 5:
|
|
|
|
{
|
2009-07-17 16:16:33 +00:00
|
|
|
int length, subtype;
|
|
|
|
VALUE data, st;
|
|
|
|
VALUE argv[2];
|
2009-03-10 15:48:06 +00:00
|
|
|
memcpy(&length, buffer + *position, 4);
|
2009-07-17 16:16:33 +00:00
|
|
|
subtype = (unsigned char)buffer[*position + 4];
|
2009-03-10 15:48:06 +00:00
|
|
|
if (subtype == 2) {
|
|
|
|
data = rb_str_new(buffer + *position + 9, length - 4);
|
|
|
|
} else {
|
|
|
|
data = rb_str_new(buffer + *position + 5, length);
|
|
|
|
}
|
2009-07-17 16:16:33 +00:00
|
|
|
st = INT2FIX(subtype);
|
|
|
|
argv[0] = data;
|
|
|
|
argv[1] = st;
|
2009-03-10 15:48:06 +00:00
|
|
|
value = rb_class_new_instance(2, argv, Binary);
|
|
|
|
*position += length + 5;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-10 17:19:18 +00:00
|
|
|
case 6:
|
|
|
|
{
|
2009-08-19 19:18:02 +00:00
|
|
|
value = Qnil;
|
2009-03-10 17:19:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 7:
|
|
|
|
{
|
|
|
|
VALUE str = rb_str_new(buffer + *position, 12);
|
|
|
|
VALUE oid = rb_funcall(str, rb_intern("unpack"), 1, rb_str_new2("C*"));
|
2009-03-10 18:58:20 +00:00
|
|
|
value = rb_class_new_instance(1, &oid, ObjectID);
|
2009-03-10 17:19:18 +00:00
|
|
|
*position += 12;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-10 14:23:42 +00:00
|
|
|
case 8:
|
|
|
|
{
|
|
|
|
value = buffer[(*position)++] ? Qtrue : Qfalse;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-10 17:19:18 +00:00
|
|
|
case 9:
|
|
|
|
{
|
|
|
|
long long millis;
|
2009-07-17 16:16:33 +00:00
|
|
|
VALUE seconds, microseconds;
|
2009-03-10 17:19:18 +00:00
|
|
|
memcpy(&millis, buffer + *position, 8);
|
2009-07-17 16:16:33 +00:00
|
|
|
seconds = INT2NUM(millis / 1000);
|
|
|
|
microseconds = INT2NUM((millis % 1000) * 1000);
|
2009-03-10 17:19:18 +00:00
|
|
|
|
|
|
|
value = rb_funcall(Time, rb_intern("at"), 2, seconds, microseconds);
|
2009-07-16 20:36:07 +00:00
|
|
|
value = rb_funcall(value, rb_intern("utc"), 0);
|
2009-03-10 17:19:18 +00:00
|
|
|
*position += 8;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-10 14:45:56 +00:00
|
|
|
case 10:
|
|
|
|
{
|
|
|
|
value = Qnil;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-10 19:33:03 +00:00
|
|
|
case 11:
|
|
|
|
{
|
|
|
|
int pattern_length = strlen(buffer + *position);
|
2009-09-04 15:02:04 +00:00
|
|
|
VALUE pattern = STR_NEW(buffer + *position, pattern_length);
|
2009-07-17 16:16:33 +00:00
|
|
|
int flags_length, flags = 0, i = 0;
|
|
|
|
char extra[10];
|
|
|
|
VALUE argv[3];
|
2009-03-10 19:33:03 +00:00
|
|
|
*position += pattern_length + 1;
|
|
|
|
|
2009-07-17 16:16:33 +00:00
|
|
|
flags_length = strlen(buffer + *position);
|
2009-03-10 19:33:03 +00:00
|
|
|
extra[0] = 0;
|
|
|
|
for (i = 0; i < flags_length; i++) {
|
|
|
|
char flag = buffer[*position + i];
|
2009-03-11 14:49:30 +00:00
|
|
|
if (flag == 'i') {
|
2009-03-23 16:14:38 +00:00
|
|
|
flags |= IGNORECASE;
|
2009-03-11 14:49:30 +00:00
|
|
|
}
|
|
|
|
else if (flag == 'm') {
|
2009-03-23 16:14:38 +00:00
|
|
|
flags |= MULTILINE;
|
2009-03-11 14:49:30 +00:00
|
|
|
}
|
|
|
|
else if (flag == 'x') {
|
2009-03-23 16:14:38 +00:00
|
|
|
flags |= EXTENDED;
|
2009-03-10 19:33:03 +00:00
|
|
|
}
|
|
|
|
else if (strlen(extra) < 9) {
|
|
|
|
strncat(extra, &flag, 1);
|
|
|
|
}
|
|
|
|
}
|
2009-07-17 16:16:33 +00:00
|
|
|
argv[0] = pattern;
|
|
|
|
argv[1] = INT2FIX(flags);
|
|
|
|
argv[2] = rb_str_new2(extra);
|
2009-03-10 19:33:03 +00:00
|
|
|
value = rb_class_new_instance(3, argv, RegexpOfHolding);
|
|
|
|
*position += flags_length + 1;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-10 18:58:20 +00:00
|
|
|
case 12:
|
|
|
|
{
|
2009-07-17 16:16:33 +00:00
|
|
|
int collection_length;
|
|
|
|
VALUE collection, str, oid, id, argv[2];
|
2009-03-10 18:58:20 +00:00
|
|
|
*position += 4;
|
2009-07-17 16:16:33 +00:00
|
|
|
collection_length = strlen(buffer + *position);
|
2009-09-04 15:02:04 +00:00
|
|
|
collection = STR_NEW(buffer + *position, collection_length);
|
2009-03-10 18:58:20 +00:00
|
|
|
*position += collection_length + 1;
|
|
|
|
|
2009-07-17 16:16:33 +00:00
|
|
|
str = rb_str_new(buffer + *position, 12);
|
|
|
|
oid = rb_funcall(str, rb_intern("unpack"), 1, rb_str_new2("C*"));
|
|
|
|
id = rb_class_new_instance(1, &oid, ObjectID);
|
2009-03-10 18:58:20 +00:00
|
|
|
*position += 12;
|
|
|
|
|
2009-07-17 16:16:33 +00:00
|
|
|
argv[0] = collection;
|
|
|
|
argv[1] = id;
|
2009-03-10 18:58:20 +00:00
|
|
|
value = rb_class_new_instance(2, argv, DBRef);
|
|
|
|
break;
|
|
|
|
}
|
2009-03-10 15:48:06 +00:00
|
|
|
case 14:
|
|
|
|
{
|
|
|
|
int value_length;
|
|
|
|
memcpy(&value_length, buffer + *position, 4);
|
|
|
|
value = ID2SYM(rb_intern(buffer + *position + 4));
|
2009-03-11 14:49:30 +00:00
|
|
|
*position += value_length + 4;
|
2009-03-10 15:48:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-03-12 20:12:52 +00:00
|
|
|
case 15:
|
|
|
|
{
|
2009-07-17 16:16:33 +00:00
|
|
|
int code_length, scope_size;
|
|
|
|
VALUE code, scope, argv[2];
|
2009-03-12 20:12:52 +00:00
|
|
|
*position += 8;
|
2009-07-17 16:16:33 +00:00
|
|
|
code_length = strlen(buffer + *position);
|
2009-09-04 15:02:04 +00:00
|
|
|
code = STR_NEW(buffer + *position, code_length);
|
2009-03-12 20:12:52 +00:00
|
|
|
*position += code_length + 1;
|
|
|
|
|
|
|
|
memcpy(&scope_size, buffer + *position, 4);
|
2009-07-17 16:16:33 +00:00
|
|
|
scope = elements_to_hash(buffer + *position + 4, scope_size - 5);
|
2009-03-12 20:12:52 +00:00
|
|
|
*position += scope_size;
|
|
|
|
|
2009-07-17 16:16:33 +00:00
|
|
|
argv[0] = code;
|
|
|
|
argv[1] = scope;
|
2009-03-12 20:12:52 +00:00
|
|
|
value = rb_class_new_instance(2, argv, Code);
|
|
|
|
break;
|
|
|
|
}
|
2009-03-10 14:23:42 +00:00
|
|
|
case 16:
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
memcpy(&i, buffer + *position, 4);
|
2009-03-11 14:49:30 +00:00
|
|
|
value = LL2NUM(i);
|
2009-03-10 14:23:42 +00:00
|
|
|
*position += 4;
|
|
|
|
break;
|
|
|
|
}
|
2009-04-22 17:49:56 +00:00
|
|
|
case 17:
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
memcpy(&i, buffer + *position, 4);
|
|
|
|
memcpy(&j, buffer + *position + 4, 4);
|
|
|
|
value = rb_ary_new3(2, LL2NUM(i), LL2NUM(j));
|
|
|
|
*position += 8;
|
|
|
|
break;
|
|
|
|
}
|
2009-08-04 14:39:25 +00:00
|
|
|
case 18:
|
|
|
|
{
|
|
|
|
long long ll;
|
|
|
|
memcpy(&ll, buffer + *position, 8);
|
|
|
|
value = LL2NUM(ll);
|
|
|
|
*position += 8;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-10 14:23:42 +00:00
|
|
|
default:
|
2009-03-10 17:19:18 +00:00
|
|
|
{
|
|
|
|
rb_raise(rb_eTypeError, "no c decoder for this type yet (%d)", type);
|
|
|
|
break;
|
|
|
|
}
|
2009-03-10 14:23:42 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE elements_to_hash(const char* buffer, int max) {
|
2009-03-10 19:41:28 +00:00
|
|
|
VALUE hash = rb_class_new_instance(0, NULL, OrderedHash);
|
2009-03-10 14:23:42 +00:00
|
|
|
int position = 0;
|
|
|
|
while (position < max) {
|
|
|
|
int type = (int)buffer[position++];
|
|
|
|
int name_length = strlen(buffer + position);
|
2009-09-04 15:02:04 +00:00
|
|
|
VALUE name = STR_NEW(buffer + position, name_length);
|
2009-07-17 16:16:33 +00:00
|
|
|
VALUE value;
|
2009-03-10 14:23:42 +00:00
|
|
|
position += name_length + 1;
|
2009-07-17 16:16:33 +00:00
|
|
|
value = get_value(buffer, &position, type);
|
2009-03-10 19:41:28 +00:00
|
|
|
rb_funcall(hash, rb_intern("[]="), 2, name, value);
|
2009-03-10 14:23:42 +00:00
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE method_deserialize(VALUE self, VALUE bson) {
|
2009-03-23 16:14:38 +00:00
|
|
|
const char* buffer = RSTRING_PTR(bson);
|
|
|
|
int remaining = RSTRING_LEN(bson);
|
2009-03-10 14:23:42 +00:00
|
|
|
|
|
|
|
// NOTE we just swallow the size and end byte here
|
|
|
|
buffer += 4;
|
|
|
|
remaining -= 5;
|
|
|
|
|
|
|
|
return elements_to_hash(buffer, remaining);
|
|
|
|
}
|
|
|
|
|
2009-03-03 22:07:22 +00:00
|
|
|
void Init_cbson() {
|
2009-08-20 14:50:48 +00:00
|
|
|
VALUE mongo, CBson;
|
2009-03-10 17:19:18 +00:00
|
|
|
Time = rb_const_get(rb_cObject, rb_intern("Time"));
|
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
mongo = rb_const_get(rb_cObject, rb_intern("Mongo"));
|
2009-03-10 17:19:18 +00:00
|
|
|
rb_require("mongo/types/binary");
|
2009-08-20 14:50:48 +00:00
|
|
|
Binary = rb_const_get(mongo, rb_intern("Binary"));
|
2009-03-10 17:19:18 +00:00
|
|
|
rb_require("mongo/types/objectid");
|
2009-08-20 14:50:48 +00:00
|
|
|
ObjectID = rb_const_get(mongo, rb_intern("ObjectID"));
|
2009-03-10 18:58:20 +00:00
|
|
|
rb_require("mongo/types/dbref");
|
2009-08-20 14:50:48 +00:00
|
|
|
DBRef = rb_const_get(mongo, rb_intern("DBRef"));
|
2009-03-12 20:12:52 +00:00
|
|
|
rb_require("mongo/types/code");
|
2009-08-20 14:50:48 +00:00
|
|
|
Code = rb_const_get(mongo, rb_intern("Code"));
|
2009-03-10 19:33:03 +00:00
|
|
|
rb_require("mongo/types/regexp_of_holding");
|
2009-08-20 14:50:48 +00:00
|
|
|
RegexpOfHolding = rb_const_get(mongo, rb_intern("RegexpOfHolding"));
|
2009-08-13 19:52:11 +00:00
|
|
|
rb_require("mongo/errors");
|
2009-08-20 14:50:48 +00:00
|
|
|
InvalidName = rb_const_get(mongo, rb_intern("InvalidName"));
|
2009-03-10 19:41:28 +00:00
|
|
|
rb_require("mongo/util/ordered_hash");
|
|
|
|
OrderedHash = rb_const_get(rb_cObject, rb_intern("OrderedHash"));
|
2009-03-10 17:19:18 +00:00
|
|
|
|
2009-07-17 16:16:33 +00:00
|
|
|
CBson = rb_define_module("CBson");
|
2009-06-01 21:38:11 +00:00
|
|
|
rb_define_module_function(CBson, "serialize", method_serialize, 2);
|
2009-03-10 14:23:42 +00:00
|
|
|
rb_define_module_function(CBson, "deserialize", method_deserialize, 1);
|
2009-03-03 22:07:22 +00:00
|
|
|
}
|