Skip to content

Commit 81ef844

Browse files
authored
RUBY-1977 Document and repair edge cases in ByteBuffer (#138)
* Revise ByteBuffer documentation * Reorder the methods * Document rewind in tutorial * Include C extension in documentation * C extension documentation * docs * Split byte buffer tests * Add empty buffer length test * Rewind test for write position * Move rewind test to general byte buffer file * put_bytes with null bytes test * put_bytes documentation * Move put_bytes tests next to put_byte * Document put_byte and prohibit strings of length != 1 * Rename BSON_TYPE_OBJECT to BSON_TYPE_DOCUMENT * Order types as they are listed in the bson spec * Putting partial strings is private helper * Bson string writing is also a private helper * put_string documentation * Move put_string tests * Add put_string with empty string test * Use pvt_put_byte * Number writing documentation * Do not duplicate docstrings * put_cstring docstring * Documentation for positions * Document to_s * put_decimal128 * Documentation and range checks in replace_int32 * More replace_int32 tests * put_symbol and null bytes * Fix signedness warning * Fix the length comparison again * put_hash and put_array docs * JRuby compatibility * JRuby implementation changes for replace_int32 * JRuby implementation changes for put_byte * JRuby 9.1 compatibility
1 parent 78a3d5b commit 81ef844

File tree

11 files changed

+1031
-650
lines changed

11 files changed

+1031
-650
lines changed

.yardopts

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
lib/**/*.rb
2+
ext/bson/*.c
23
-o yard-docs

docs/tutorials/bson-v4.txt

+43-14
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,22 @@ you wish to instantiate and passing it a ``BSON::ByteBuffer`` instance.
6868
String.from_bson(byte_buffer)
6969
BSON::Int32.from_bson(byte_buffer)
7070

71+
7172
Byte Buffers
7273
------------
7374

74-
In 4.0, BSON introduced the use of native byte buffers in both MRI and JRuby
75-
instead of using ``StringIO``. This was done for performance improvements.
75+
BSON library 4.0 introduces the use of native byte buffers in MRI and JRuby
76+
instead of using ``StringIO``, for improved performance.
7677

77-
API
78-
```
78+
Writing
79+
```````
7980

80-
A ``BSON::ByteBuffer`` can be instantiated with nothing (for write mode) or
81-
with a string of raw bytes (for read mode).
81+
To create a ``ByteBuffer`` for writing (i.e. serializing to BSON),
82+
instantiate ``BSON::ByteBuffer`` with no arguments:
8283

8384
.. code-block:: ruby
8485

8586
buffer = BSON::ByteBuffer.new # a write mode buffer.
86-
buffer = BSON::ByteBuffer.new(string) # a read mode buffer.
8787

8888
Writing to the buffer is done via the following API:
8989

@@ -99,6 +99,32 @@ Writing to the buffer is done via the following API:
9999
# writes the string to the buffer.
100100
buffer.put_cstring(value)
101101

102+
To obtain the serialized data as a byte string (for example, to send the data
103+
over a socket), call ``to_s`` on the buffer:
104+
105+
.. code-block:: ruby
106+
107+
buffer = BSON::ByteBuffer.new
108+
buffer.put_string('testing')
109+
socket.write(buffer.to_s)
110+
111+
.. note::
112+
113+
``ByteBuffer`` keeps track of read and write positions separately.
114+
There is no way to rewind the buffer for writing - ``rewind`` only affects
115+
the read position.
116+
117+
118+
Reading
119+
```````
120+
121+
To create a ``ByteBuffer`` for reading (i.e. deserializing from BSON),
122+
instantiate ``BSON::ByteBuffer`` with a byte string as the argument:
123+
124+
.. code-block:: ruby
125+
126+
buffer = BSON::ByteBuffer.new(string) # a read mode buffer.
127+
102128
Reading from the buffer is done via the following API:
103129

104130
.. code-block:: ruby
@@ -111,19 +137,22 @@ Reading from the buffer is done via the following API:
111137
buffer.get_int64 # Pulls a 64-bit integer (8 bytes) from the buffer.
112138
buffer.get_string # Pulls a UTF-8 string from the buffer.
113139

114-
Converting a buffer to its raw bytes (for example, for sending over a socket)
115-
is done by simply calling ``to_s`` on the buffer.
140+
To restart reading from the beginning of a buffer, use ``rewind``:
116141

117142
.. code-block:: ruby
118143

119-
buffer = BSON::ByteBuffer.new
120-
buffer.put_string('testing')
121-
socket.write(buffer.to_s)
144+
buffer.rewind
145+
146+
.. note::
147+
148+
``ByteBuffer`` keeps track of read and write positions separately.
149+
``rewind`` only affects the read position.
150+
122151

123-
Supported Objects
152+
Supported Classes
124153
-----------------
125154

126-
Core Ruby objects that have representations in the BSON specification and
155+
Core Ruby classes that have representations in the BSON specification and
127156
will have a ``to_bson`` method defined for them are: ``Object``, ``Array``,
128157
``FalseClass``, ``Float``, ``Hash``, ``Integer``, ``NilClass``, ``Regexp``,
129158
``String``, ``Symbol`` (deprecated), ``Time``, ``TrueClass``.

ext/bson/bson-native.h

+7-9
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ rb_bson_utf8_validate (const char *utf8, /* IN */
3131
#define HOST_NAME_HASH_MAX 256
3232
#endif
3333

34-
#define BSON_TYPE_DOUBLE 1
35-
#define BSON_TYPE_STRING 2
36-
#define BSON_TYPE_OBJECT 3
37-
#define BSON_TYPE_ARRAY 4
38-
#define BSON_TYPE_INT32 16
39-
#define BSON_TYPE_INT64 18
40-
#define BSON_TYPE_BOOLEAN 8
34+
#define BSON_TYPE_DOUBLE 1
35+
#define BSON_TYPE_STRING 2
36+
#define BSON_TYPE_DOCUMENT 3
37+
#define BSON_TYPE_ARRAY 4
38+
#define BSON_TYPE_BOOLEAN 8
39+
#define BSON_TYPE_INT32 16
40+
#define BSON_TYPE_INT64 18
4141

4242
typedef struct {
4343
size_t size;
@@ -78,13 +78,11 @@ VALUE rb_bson_byte_buffer_get_hash(VALUE self);
7878
VALUE rb_bson_byte_buffer_get_array(VALUE self);
7979
VALUE rb_bson_byte_buffer_put_byte(VALUE self, VALUE byte);
8080
VALUE rb_bson_byte_buffer_put_bytes(VALUE self, VALUE bytes);
81-
VALUE rb_bson_byte_buffer_put_bson_partial_string(VALUE self, const char *str, int32_t length);
8281
VALUE rb_bson_byte_buffer_put_cstring(VALUE self, VALUE string);
8382
VALUE rb_bson_byte_buffer_put_decimal128(VALUE self, VALUE low, VALUE high);
8483
VALUE rb_bson_byte_buffer_put_double(VALUE self, VALUE f);
8584
VALUE rb_bson_byte_buffer_put_int32(VALUE self, VALUE i);
8685
VALUE rb_bson_byte_buffer_put_int64(VALUE self, VALUE i);
87-
VALUE rb_bson_byte_buffer_put_bson_string(VALUE self, const char *str, int32_t length);
8886
VALUE rb_bson_byte_buffer_put_string(VALUE self, VALUE string);
8987
VALUE rb_bson_byte_buffer_put_symbol(VALUE self, VALUE symbol);
9088
VALUE rb_bson_byte_buffer_put_hash(VALUE self, VALUE hash, VALUE validating_keys);

ext/bson/bytebuf.c

+4-12
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,15 @@ VALUE rb_bson_byte_buffer_length(VALUE self)
9090
return UINT2NUM(READ_SIZE(b));
9191
}
9292

93-
/**
94-
* Get the read position.
95-
*/
93+
/* The docstring is in init.c. */
9694
VALUE rb_bson_byte_buffer_read_position(VALUE self)
9795
{
9896
byte_buffer_t *b;
9997
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
10098
return INT2NUM(b->read_position);
10199
}
102100

103-
/**
104-
* Reset the read position to the beginning of the byte buffer.
105-
*/
101+
/* The docstring is in init.c. */
106102
VALUE rb_bson_byte_buffer_rewind(VALUE self)
107103
{
108104
byte_buffer_t *b;
@@ -112,19 +108,15 @@ VALUE rb_bson_byte_buffer_rewind(VALUE self)
112108
return self;
113109
}
114110

115-
/**
116-
* Get the write position.
117-
*/
111+
/* The docstring is in init.c. */
118112
VALUE rb_bson_byte_buffer_write_position(VALUE self)
119113
{
120114
byte_buffer_t *b;
121115
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
122116
return INT2NUM(b->write_position);
123117
}
124118

125-
/**
126-
* Convert the buffer to a string.
127-
*/
119+
/* The docstring is in init.c. */
128120
VALUE rb_bson_byte_buffer_to_s(VALUE self)
129121
{
130122
byte_buffer_t *b;

0 commit comments

Comments
 (0)