checkpoint - beginnings of c encoder
This commit is contained in:
parent
f907edcde5
commit
c69fbb7f67
3
.gitignore
vendored
3
.gitignore
vendored
@ -4,3 +4,6 @@ pkg
|
||||
doc
|
||||
mongo-ruby-driver-*.gem
|
||||
nbproject
|
||||
*.bundle
|
||||
*.o
|
||||
ext/cbson/Makefile
|
||||
|
11
Rakefile
11
Rakefile
@ -4,7 +4,10 @@ require 'fileutils'
|
||||
require 'rake'
|
||||
require 'rake/testtask'
|
||||
require 'rake/gempackagetask'
|
||||
begin
|
||||
require 'rake/contrib/rubyforgepublisher'
|
||||
rescue LoadError
|
||||
end
|
||||
|
||||
# NOTE: some of the tests assume Mongo is running
|
||||
Rake::TestTask.new do |t|
|
||||
@ -23,6 +26,14 @@ task :publish => [:rdoc] do
|
||||
Rake::RubyForgePublisher.new(GEM, RUBYFORGE_USER).upload
|
||||
end
|
||||
|
||||
desc "Compile the extension"
|
||||
task :compile do
|
||||
cd 'ext/cbson'
|
||||
ruby 'extconf.rb'
|
||||
sh 'make'
|
||||
cp 'cbson.bundle', '../../lib/mongo/ext/cbson.bundle'
|
||||
end
|
||||
|
||||
namespace :gem do
|
||||
|
||||
desc "Install the gem locally"
|
||||
|
76
ext/cbson/cbson.c
Normal file
76
ext/cbson/cbson.c
Normal file
@ -0,0 +1,76 @@
|
||||
#include "ruby.h"
|
||||
#include <assert.h>
|
||||
|
||||
#define INITIAL_BUFFER_SIZE 256
|
||||
|
||||
typedef struct {
|
||||
char* buffer;
|
||||
int size;
|
||||
int position;
|
||||
} bson_buffer;
|
||||
|
||||
static bson_buffer* buffer_new(void) {
|
||||
bson_buffer* buffer;
|
||||
buffer = (bson_buffer*)malloc(sizeof(bson_buffer));
|
||||
assert(buffer);
|
||||
|
||||
buffer->size = INITIAL_BUFFER_SIZE;
|
||||
buffer->position = 0;
|
||||
buffer->buffer = (char*)malloc(INITIAL_BUFFER_SIZE);
|
||||
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;
|
||||
}
|
||||
buffer->buffer = (char*)realloc(buffer->buffer, size);
|
||||
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) {
|
||||
buffer_assure_space(buffer, size);
|
||||
int position = buffer->position;
|
||||
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;
|
||||
}
|
||||
|
||||
static VALUE method_serialize(VALUE self, VALUE doc) {
|
||||
char data[5] = {0x05, 0x00, 0x00, 0x00, 0x00};
|
||||
return rb_str_new(data, 5);
|
||||
}
|
||||
|
||||
void Init_cbson() {
|
||||
VALUE CBson = rb_define_module("CBson");
|
||||
rb_define_module_function(CBson, "serialize", method_serialize, 1);
|
||||
}
|
7
ext/cbson/extconf.rb
Normal file
7
ext/cbson/extconf.rb
Normal file
@ -0,0 +1,7 @@
|
||||
require 'mkmf'
|
||||
|
||||
extension_name = 'cbson'
|
||||
|
||||
dir_config(extension_name)
|
||||
|
||||
create_makefile(extension_name)
|
@ -72,6 +72,12 @@ class BSON
|
||||
@buf.to_a
|
||||
end
|
||||
|
||||
begin
|
||||
require 'mongo/ext/cbson'
|
||||
def serialize(obj)
|
||||
@buf = ByteBuffer.new(CBson.serialize(obj))
|
||||
end
|
||||
rescue LoadError
|
||||
def serialize(obj)
|
||||
raise "Document is null" unless obj
|
||||
|
||||
@ -88,6 +94,7 @@ class BSON
|
||||
@buf.put_int(@buf.size, 0)
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
def serialize_key_value(k, v)
|
||||
type = bson_type(v, k)
|
||||
|
Loading…
Reference in New Issue
Block a user