RUBY-131 read bson io object; b2json executable (neomantra)

This commit is contained in:
Kyle Banker 2010-05-28 14:01:51 -04:00
parent b7dd6053c1
commit 99c2d1463e
5 changed files with 91 additions and 3 deletions

63
bin/b2json Executable file
View File

@ -0,0 +1,63 @@
#!/usr/bin/ruby
# encoding: UTF-8
# --
# Copyright (C) 2008-2010 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.
# ++
require 'rubygems'
require 'bson'
begin
require 'yajl'
rescue LoadError
puts "This script requires yajl. Please install as follows:"
puts " gem install yajl-ruby"
Process.exit
end
# Convert all documents in an IO into JSON.
def print_b2json(io)
while not io.eof? do
bsonobj = BSON.read_bson_document(io)
Yajl::Encoder.encode(bsonobj, STDOUT)
STDOUT << "\n"
end
end
# print usage
def usage()
STDERR << <<END_OF_USAGE
usage: b2json.rb [-h] [file1 [file2]]
Converts a BSON file to JSON on STDOUT.
You can pass multiple filename.
If no filenames are passed, then STDIN is consumed.
END_OF_USAGE
exit
end
# no arg, use STDIN
# -h, print usage and exit
# otherwise loop of filenames
if ARGV.empty? then
print_b2json(STDIN)
exit
elsif ARGV[0] == "-h" then
usage()
else
ARGV.each { |fname| print_b2json(File.new(fname)) }
end

View File

@ -13,8 +13,11 @@ Gem::Specification.new do |s|
s.files = ['Rakefile', 'bson.gemspec', 'LICENSE.txt']
s.files += ['lib/bson.rb'] + Dir['lib/bson/**/*.rb']
s.files += ['bin/b2json']
s.test_files = Dir['test/mongo_bson/*.rb']
s.executables = ['b2json']
s.has_rdoc = true
s.authors = ['Jim Menard', 'Mike Dirolf', 'Kyle Banker']

View File

@ -14,6 +14,22 @@ module BSON
def self.deserialize(buf=nil)
BSON_CODER.deserialize(buf)
end
# Reads a single BSON document from an IO object.
# This method is used in the binary b2json, bundled with
# the bson gem, for reading a file full of bson documents.
#
# @param [IO] io an io object containing a bson object.
#
# @return [ByteBuffer]
def self.read_bson_document(io)
bytebuf = BSON::ByteBuffer.new
sz = io.read(4).unpack("V")[0]
bytebuf.put_int(sz)
bytebuf.put_array(io.read(sz-4).unpack("C*"))
bytebuf.rewind
return BSON.deserialize(bytebuf)
end
end
begin

View File

@ -20,9 +20,6 @@ module BSON
# A BSON seralizer/deserializer in pure Ruby.
class BSON_RUBY
# why was this necessary?
#include Mongo
MINKEY = -1
EOO = 0
NUMBER = 1

View File

@ -22,6 +22,15 @@ class BSONTest < Test::Unit::TestCase
include BSON
def test_read_bson_io_document
doc = {'doc' => 'hello, world'}
bson = BSON.serialize(doc)
io = StringIO.new
io.write(bson.to_s)
io.rewind
assert_equal BSON.deserialize(bson), BSON.read_bson_document(io)
end
def test_serialize_returns_byte_buffer
doc = {'doc' => 'hello, world'}
bson = BSON.serialize(doc)