encoder for binary
This commit is contained in:
parent
79b3df3c9e
commit
24e695b40d
|
@ -1,3 +1,25 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
#include "ruby.h"
|
#include "ruby.h"
|
||||||
#include "st.h"
|
#include "st.h"
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
@ -136,6 +158,28 @@ static int write_element(VALUE key, VALUE value, VALUE extra) {
|
||||||
buffer_write_bytes(buffer, RSTRING(value)->ptr, length - 1);
|
buffer_write_bytes(buffer, RSTRING(value)->ptr, length - 1);
|
||||||
buffer_write_bytes(buffer, &zero, 1);
|
buffer_write_bytes(buffer, &zero, 1);
|
||||||
break;
|
break;
|
||||||
|
case T_OBJECT:
|
||||||
|
{
|
||||||
|
// TODO there has to be a better way to do these checks...
|
||||||
|
const char* cls = rb_class2name(RBASIC(value)->klass);
|
||||||
|
if (strcmp(cls, "XGen::Mongo::Driver::Binary") == 0) {
|
||||||
|
write_name_and_type(buffer, key, 0x05);
|
||||||
|
const char subtype = (const char)FIX2INT(rb_funcall(value, rb_intern("subtype"), 0));
|
||||||
|
VALUE string_data = rb_funcall(value, rb_intern("to_s"), 0);
|
||||||
|
int length = RSTRING(string_data)->len;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
buffer_write_bytes(buffer, RSTRING(string_data)->ptr, length);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
rb_raise(rb_eTypeError, "no c encoder for this type yet");
|
rb_raise(rb_eTypeError, "no c encoder for this type yet");
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -135,11 +135,19 @@ class ByteBuffer
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_a
|
def to_a
|
||||||
|
if @buf.respond_to? "unpack"
|
||||||
|
@buf.unpack("C*")
|
||||||
|
else
|
||||||
@buf
|
@buf
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
if @buf.respond_to? "pack"
|
||||||
@buf.pack("C*")
|
@buf.pack("C*")
|
||||||
|
else
|
||||||
|
@buf
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def dump
|
def dump
|
||||||
|
|
|
@ -132,7 +132,8 @@ class BSONTest < Test::Unit::TestCase
|
||||||
doc2 = @b.deserialize
|
doc2 = @b.deserialize
|
||||||
bin2 = doc2['bin']
|
bin2 = doc2['bin']
|
||||||
assert_kind_of Binary, bin2
|
assert_kind_of Binary, bin2
|
||||||
assert_equal [1, 2, 3, 4, 5], bin2.to_a
|
b = bin2.to_a
|
||||||
|
assert_equal [1, 2, 3, 4, 5], b
|
||||||
assert_equal Binary::SUBTYPE_USER_DEFINED, bin2.subtype
|
assert_equal Binary::SUBTYPE_USER_DEFINED, bin2.subtype
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue