module BSON::Grow

Public Instance Methods

array(key) click to toggle source
# File lib/bson/grow.rb, line 145
def array(key) # Append array element finished
  b_do(key, BSON::BSON_RUBY::ARRAY)
end
array!(key) click to toggle source
# File lib/bson/grow.rb, line 141
def array!(key) # Append array element unfinished
  b_do!(key, BSON::BSON_RUBY::ARRAY)
end
b_do(key, type = BSON::BSON_RUBY::OBJECT) click to toggle source
# File lib/bson/grow.rb, line 126
def b_do(key, type = BSON::BSON_RUBY::OBJECT) # Append object/array element finished
  @b_pos ||= [0]
  @cursor = @str.size - @b_pos.size
  b_do!(key, type)
  finish!
end
b_do!(key, type = BSON::BSON_RUBY::OBJECT) click to toggle source
# File lib/bson/grow.rb, line 115
def b_do!(key, type = BSON::BSON_RUBY::OBJECT) # Append object/array element unfinished
  put(type)
  BSON::BSON_RUBY.serialize_cstr(self, key)
  @b_pos ||= [0]
  @a_index ||= [0]
  @b_pos << @cursor # mark position of size
  @a_index << 0
  put_int(0)
  self
end
b_end() click to toggle source
# File lib/bson/grow.rb, line 158
def b_end # End object/array finished - next operation will be up one level
  @b_pos ||= [0]
  @b_pos.pop
  @a_index ||= [0]
  @a_index.pop
  self
end
b_end!() click to toggle source
# File lib/bson/grow.rb, line 149
def b_end! # End object/array unfinished - next operation will be up one level
  @b_pos ||= [0]
  finish_one!(@b_pos[-1])
  @b_pos.pop
  @a_index ||= [0]
  @a_index.pop
  self
end
clear!() click to toggle source
# File lib/bson/grow.rb, line 166
def clear! # Clear internal state for reuse
  @b_pos = @a_index = nil
  self
end
doc(key) click to toggle source
# File lib/bson/grow.rb, line 137
def doc(key) # Append object element finished
  b_do(key, BSON::BSON_RUBY::OBJECT)
end
doc!(key) click to toggle source
# File lib/bson/grow.rb, line 133
def doc!(key) # Append object element unfinished
  b_do!(key, BSON::BSON_RUBY::OBJECT)
end
finish!() click to toggle source
# File lib/bson/grow.rb, line 54
def finish! # Append all terminating null bytes and set all sizes
  @b_pos ||= [0]
  (@b_pos.size-1).downto(0){|i| finish_one!(@b_pos[i])}
  self
end
finish_one!(offset = 0) click to toggle source
# File lib/bson/grow.rb, line 41
def finish_one!(offset = 0) # Appends terminating null byte and sets size
  put(0)
  put_int(@str.size - offset, offset)
  @cursor = @str.size
  self
end
grow(bson) click to toggle source
# File lib/bson/grow.rb, line 65
def grow(bson) # Appends BSON elements finished
  @b_pos ||= [0]
  put_binary(bson.to_e, @str.size - @b_pos.size)
  finish!
end
grow!(bson) click to toggle source
# File lib/bson/grow.rb, line 60
def grow!(bson) # Appends BSON elements unfinished
  put_binary(bson.to_e)
  self
end
push(bson) click to toggle source
# File lib/bson/grow.rb, line 82
def push(bson) # Appends BSON element value with correct key finished
  @a_index ||= [0]
  @b_pos ||= [0]
  put_binary(bson.to_t, @str.size - @b_pos.size)
  put_binary(@a_index[-1].to_s)
  put_binary(NULL_BYTE)
  @a_index[-1] += 1
  put_binary(bson.to_v)
  finish!
end
push!(bson) click to toggle source
# File lib/bson/grow.rb, line 71
def push!(bson) # Appends BSON element value with correct key unfinished
  @a_index ||= [0]
  @b_pos ||= [0]
  put_binary(bson.to_t)
  put_binary(@a_index[-1].to_s)
  put_binary(NULL_BYTE)
  @a_index[-1] += 1
  put_binary(bson.to_v)
  self
end
push_doc(bson) click to toggle source
# File lib/bson/grow.rb, line 104
def push_doc(bson) # Appends BSON doc with correct key finished
  @a_index ||= [0]
  @b_pos ||= [0]
  put(BSON::BSON_RUBY::OBJECT, @str.size - @b_pos.size)
  put_binary(@a_index[-1].to_s)
  put(0)
  @a_index[-1] += 1
  put_binary(bson.to_s)
  finish!
end
push_doc!(bson) click to toggle source
# File lib/bson/grow.rb, line 93
def push_doc!(bson) # Appends BSON doc with correct key unfinished
  @a_index ||= [0]
  @b_pos ||= [0]
  put(BSON::BSON_RUBY::OBJECT)
  put_binary(@a_index[-1].to_s)
  put(0)
  @a_index[-1] += 1
  put_binary(bson.to_s)
  self
end
to_e() click to toggle source

module with methods to grow BSON docs/objects/arrays this module is intended for internal use and is subject to change proper usage is essential as minimal overhead is preferred over usage checks unfinish! returns unfinished BSON for faster growing with bang! methods bang! methods work on unfinished BSON with neither terminating nulls nor proper sizes finish! must be called to finish BSON after using bang! methods corresponding non-bang methods work on finished BSON object/array methods should be paired, ex., array!/b_end! and array/b_end push!/push and #push_doc!/push_doc append to arrays with correct keys #b_end needs a better name

# File lib/bson/grow.rb, line 29
def to_e # Extract bytes for elements from BSON
  @str[4...-1]
end
to_t() click to toggle source
# File lib/bson/grow.rb, line 33
def to_t # Extract type from (single-element) BSON
  @str[4,1]
end
to_v() click to toggle source
# File lib/bson/grow.rb, line 37
def to_v # Extract value from (single-element) BSON
  @str[(@str.index(NULL_BYTE,5)+1)...-1]
end
unfinish!() click to toggle source
# File lib/bson/grow.rb, line 48
def unfinish! # Backup past terminating null bytes
  @b_pos ||= [0]
  @cursor = @str.size - @b_pos.size # BSON::BSON_CODER.serialize may not restore @cursor
  self
end