Module: BSON::Grow
- Included in:
- ByteBuffer
- Defined in:
- lib/bson/grow.rb
Instance Method Summary collapse
-
#array(key) ⇒ Object
Append array element finished.
-
#array!(key) ⇒ Object
Append array element unfinished.
-
#b_do(key, type = BSON::BSON_RUBY::OBJECT) ⇒ Object
Append object/array element finished.
-
#b_do!(key, type = BSON::BSON_RUBY::OBJECT) ⇒ Object
Append object/array element unfinished.
-
#b_end ⇒ Object
End object/array finished - next operation will be up one level.
-
#b_end! ⇒ Object
End object/array unfinished - next operation will be up one level.
-
#clear! ⇒ Object
Clear internal state for reuse.
-
#doc(key) ⇒ Object
Append object element finished.
-
#doc!(key) ⇒ Object
Append object element unfinished.
-
#finish! ⇒ Object
Append all terminating null bytes and set all sizes.
-
#finish_one!(offset = 0) ⇒ Object
Appends terminating null byte and sets size.
-
#grow(bson) ⇒ Object
Appends BSON elements finished.
-
#grow!(bson) ⇒ Object
Appends BSON elements unfinished.
-
#push(bson) ⇒ Object
Appends BSON element value with correct key finished.
-
#push!(bson) ⇒ Object
Appends BSON element value with correct key unfinished.
-
#push_doc(bson) ⇒ Object
Appends BSON doc with correct key finished.
-
#push_doc!(bson) ⇒ Object
Appends BSON doc with correct key unfinished.
-
#to_e ⇒ Object
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.
-
#to_t ⇒ Object
Extract type from (single-element) BSON.
-
#to_v ⇒ Object
Extract value from (single-element) BSON.
-
#unfinish! ⇒ Object
Backup past terminating null bytes.
Instance Method Details
#array(key) ⇒ Object
Append array element finished
146 147 148 |
# File 'lib/bson/grow.rb', line 146 def array(key) # Append array element finished b_do(key, BSON::BSON_RUBY::ARRAY) end |
#array!(key) ⇒ Object
Append array element unfinished
142 143 144 |
# File 'lib/bson/grow.rb', line 142 def array!(key) # Append array element unfinished b_do!(key, BSON::BSON_RUBY::ARRAY) end |
#b_do(key, type = BSON::BSON_RUBY::OBJECT) ⇒ Object
Append object/array element finished
127 128 129 130 131 132 |
# File 'lib/bson/grow.rb', line 127 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) ⇒ Object
Append object/array element unfinished
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/bson/grow.rb', line 116 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 ⇒ Object
End object/array finished - next operation will be up one level
159 160 161 162 163 164 165 |
# File 'lib/bson/grow.rb', line 159 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! ⇒ Object
End object/array unfinished - next operation will be up one level
150 151 152 153 154 155 156 157 |
# File 'lib/bson/grow.rb', line 150 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! ⇒ Object
Clear internal state for reuse
167 168 169 170 |
# File 'lib/bson/grow.rb', line 167 def clear! # Clear internal state for reuse @b_pos = @a_index = nil self end |
#doc(key) ⇒ Object
Append object element finished
138 139 140 |
# File 'lib/bson/grow.rb', line 138 def doc(key) # Append object element finished b_do(key, BSON::BSON_RUBY::OBJECT) end |
#doc!(key) ⇒ Object
Append object element unfinished
134 135 136 |
# File 'lib/bson/grow.rb', line 134 def doc!(key) # Append object element unfinished b_do!(key, BSON::BSON_RUBY::OBJECT) end |
#finish! ⇒ Object
Append all terminating null bytes and set all sizes
55 56 57 58 59 |
# File 'lib/bson/grow.rb', line 55 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) ⇒ Object
Appends terminating null byte and sets size
42 43 44 45 46 47 |
# File 'lib/bson/grow.rb', line 42 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) ⇒ Object
Appends BSON elements finished
66 67 68 69 70 |
# File 'lib/bson/grow.rb', line 66 def grow(bson) # Appends BSON elements finished @b_pos ||= [0] put_binary(bson.to_e, @str.size - @b_pos.size) finish! end |
#grow!(bson) ⇒ Object
Appends BSON elements unfinished
61 62 63 64 |
# File 'lib/bson/grow.rb', line 61 def grow!(bson) # Appends BSON elements unfinished put_binary(bson.to_e) self end |
#push(bson) ⇒ Object
Appends BSON element value with correct key finished
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/bson/grow.rb', line 83 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) ⇒ Object
Appends BSON element value with correct key unfinished
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/bson/grow.rb', line 72 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) ⇒ Object
Appends BSON doc with correct key finished
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/bson/grow.rb', line 105 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) ⇒ Object
Appends BSON doc with correct key unfinished
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/bson/grow.rb', line 94 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 ⇒ Object
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
30 31 32 |
# File 'lib/bson/grow.rb', line 30 def to_e # Extract bytes for elements from BSON @str[4...-1] end |
#to_t ⇒ Object
Extract type from (single-element) BSON
34 35 36 |
# File 'lib/bson/grow.rb', line 34 def to_t # Extract type from (single-element) BSON @str[4,1] end |
#to_v ⇒ Object
Extract value from (single-element) BSON
38 39 40 |
# File 'lib/bson/grow.rb', line 38 def to_v # Extract value from (single-element) BSON @str[(@str.index(NULL_BYTE,5)+1)...-1] end |
#unfinish! ⇒ Object
Backup past terminating null bytes
49 50 51 52 53 |
# File 'lib/bson/grow.rb', line 49 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 |