bson 2.0.0.alpha → 2.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bson might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e50654bfd852cf0daad41abc4d7249bf6dc293fc
4
- data.tar.gz: 13709ff7d3d55d1990df6ac2f9c92acf193ae242
3
+ metadata.gz: cfc4e9e218bc9b71f1881192cfe7d4a647a085c2
4
+ data.tar.gz: 3b4f3a00ffb362383bb153471683ccb117d7bed6
5
5
  SHA512:
6
- metadata.gz: 7caafece5f3d7fd32f470e928c9ef94297cefc953ba4a8b656b68258db73ecc397f1ff3f27d79d735e713e5cb586ec7882b17fba114172f1b67a739108984f06
7
- data.tar.gz: aad6c2fea3db480b2e8b763e45c08cf9c663e28713f8322020921bce35a9aede9c32cbfcd4e324df9798b4fa6986ac146ebd0299d6be7eb29c6f650ed0e5a810
6
+ metadata.gz: 2fe2667809ba737140cc19a02c75cee9e2a96c107ab99d3cd7b6b87f060b15cdfca75eba4ae43eec222a3da8f059a4f8fd5935baec018ea43d76473ac563378e
7
+ data.tar.gz: 88d6cd83e9f4160c12a433ff624932e4868d6605a3c8202587a5a89a1f8353fb86aecbc4e455e0b4b7eaa1be5cf63dc897774c2a56219d7a65504bd9e48e30d6
data/Rakefile CHANGED
@@ -14,16 +14,16 @@ end
14
14
  if jruby?
15
15
  require "rake/javaextensiontask"
16
16
  Rake::JavaExtensionTask.new do |ext|
17
- ext.name = "NativeService"
17
+ ext.name = "bson-ruby"
18
18
  ext.ext_dir = "src"
19
- ext.lib_dir = "lib/bson"
19
+ ext.lib_dir = "lib"
20
20
  end
21
21
  else
22
22
  require "rake/extensiontask"
23
23
  Rake::ExtensionTask.new do |ext|
24
24
  ext.name = "native"
25
25
  ext.ext_dir = "ext/bson"
26
- ext.lib_dir = "lib/bson"
26
+ ext.lib_dir = "lib"
27
27
  end
28
28
  end
29
29
 
@@ -51,7 +51,7 @@ task :clean_all => :clean do
51
51
  Dir.chdir(Pathname(__FILE__).dirname + "lib/bson") do
52
52
  `rm native.#{extension}`
53
53
  `rm native.o`
54
- `rm NativeService.jar`
54
+ `rm native.jar`
55
55
  end
56
56
  rescue Exception => e
57
57
  puts e.message
@@ -0,0 +1,763 @@
1
+ #include <arpa/inet.h>
2
+ #include <ruby.h>
3
+ #include <stdint.h>
4
+ #include <time.h>
5
+ #include <unistd.h>
6
+
7
+ /**
8
+ * For 64 byte systems we convert to longs, for 32 byte systems we convert
9
+ * to a long long.
10
+ *
11
+ * @since 2.0.0
12
+ */
13
+ #if SIZEOF_LONG == 8
14
+ #define NUM2INT64(v) NUM2LONG(v)
15
+ #define INT642NUM(v) LONG2NUM(v)
16
+ #else
17
+ #define NUM2INT64(v) NUM2LL(v)
18
+ #define INT642NUM(v) LL2NUM(v)
19
+ #endif
20
+
21
+ /**
22
+ * Ruby 1.8.7 does not define DBL2NUM, so we define it if it's not there.
23
+ *
24
+ * @since 2.0.0
25
+ */
26
+ #ifndef DBL2NUM
27
+ #define DBL2NUM(dbl) rb_float_new(dbl)
28
+ #endif
29
+
30
+ /**
31
+ * Define the max hostname length constant if nonexistant.
32
+ *
33
+ * @since 2.0.0
34
+ */
35
+ #ifndef HOST_NAME_MAX
36
+ #define HOST_NAME_MAX 256
37
+ #endif
38
+
39
+ /**
40
+ * Define index sizes for array serialization.
41
+ *
42
+ * @since 2.0.0
43
+ */
44
+ #define BSON_INDEX_SIZE 1024
45
+ #define BSON_INDEX_CHAR_SIZE 5
46
+ #define INTEGER_CHAR_SIZE 22
47
+
48
+ /**
49
+ * Constant for the intetger array indexes.
50
+ *
51
+ * @since 2.0.0
52
+ */
53
+ static char rb_bson_array_indexes[BSON_INDEX_SIZE][BSON_INDEX_CHAR_SIZE];
54
+
55
+ /**
56
+ * BSON::BINARY
57
+ *
58
+ * @since 2.0.0
59
+ */
60
+ static VALUE rb_bson_binary;
61
+
62
+ /**
63
+ * BSON::UTF8
64
+ *
65
+ * @since 2.0.0
66
+ */
67
+ static VALUE rb_bson_utf8_string;
68
+
69
+ static VALUE rb_utc_method;
70
+
71
+ /**
72
+ * Define encoding macros to be able to support 1.8.
73
+ *
74
+ * @since 2.0.0
75
+ */
76
+ #ifdef HAVE_RUBY_ENCODING_H
77
+ #include <ruby/encoding.h>
78
+
79
+ /**
80
+ * Convert a ruby string into a utf-8 compatible binary string.
81
+ *
82
+ * @example Convert the string to utf-8 binary.
83
+ * rb_bson_to_utf8_binary("test");
84
+ *
85
+ * @param [ String ] string The ruby string.
86
+ *
87
+ * @return [ String ] The encoded string.
88
+ *
89
+ * @since 2.0.0
90
+ */
91
+ static VALUE rb_bson_to_utf8_binary(VALUE string)
92
+ {
93
+ VALUE utf8 = rb_str_encode(string, rb_bson_utf8_string, 0, Qnil);
94
+ return rb_enc_associate(utf8, rb_ascii8bit_encoding());
95
+ }
96
+
97
+ /**
98
+ * Convert the binary string to a ruby utf8 string.
99
+ *
100
+ * @example Convert the string to binary.
101
+ * rb_bson_from_bson_string("test");
102
+ *
103
+ * @param [ String ] string The ruby string.
104
+ *
105
+ * @return [ String ] The encoded string.
106
+ *
107
+ * @since 2.0.0
108
+ */
109
+ static VALUE rb_bson_from_bson_string(VALUE string)
110
+ {
111
+ return rb_enc_associate(string, rb_utf8_encoding());
112
+ }
113
+
114
+ /**
115
+ * Provide default new string with binary encoding.
116
+ *
117
+ * @example Check encoded and provide default new binary encoded string.
118
+ * if (NIL_P(encoded)) encoded = rb_str_new_encoded_binary();
119
+ *
120
+ * @return [ String ] The new string with binary encoding.
121
+ *
122
+ * @since 2.0.0
123
+ */
124
+ static VALUE rb_str_new_encoded_binary(void)
125
+ {
126
+ return rb_enc_str_new("", 0, rb_ascii8bit_encoding());
127
+ }
128
+ #else
129
+
130
+ /**
131
+ * convert a ruby string into a utf-8 compatible binary string.
132
+ *
133
+ * @example convert the string to utf-8 binary.
134
+ * rb_bson_to_utf8_binary("test");
135
+ *
136
+ * @param [ string ] string the ruby string.
137
+ *
138
+ * @return [ string ] the encoded string.
139
+ *
140
+ * @since 2.0.0
141
+ */
142
+ static VALUE rb_bson_to_utf8_binary(VALUE string)
143
+ {
144
+ return string;
145
+ }
146
+
147
+ /**
148
+ * Convert the binary string to a ruby utf8 string.
149
+ *
150
+ * @example Convert the string to binary.
151
+ * rb_bson_from_bson_string("test");
152
+ *
153
+ * @param [ String ] string The ruby string.
154
+ *
155
+ * @return [ String ] The encoded string.
156
+ *
157
+ * @since 2.0.0
158
+ */
159
+ static VALUE rb_bson_from_bson_string(VALUE string)
160
+ {
161
+ return string;
162
+ }
163
+
164
+ /**
165
+ * Provide default new string with binary encoding.
166
+ *
167
+ * @example Check encoded and provide default new binary encoded string.
168
+ * if (NIL_P(encoded)) encoded = rb_str_new_encoded_binary();
169
+ *
170
+ * @return [ String ] The new string with binary encoding.
171
+ *
172
+ * @since 2.0.0
173
+ */
174
+ static VALUE rb_str_new_encoded_binary(void)
175
+ {
176
+ return rb_str_new("", 0);
177
+ }
178
+ #endif
179
+
180
+ /**
181
+ * Constant for a null byte.
182
+ *
183
+ * @since 2.0.0
184
+ */
185
+ static const char rb_bson_null_byte = 0;
186
+
187
+ /**
188
+ * Constant for a true byte.
189
+ *
190
+ * @since 2.0.0
191
+ */
192
+ static const char rb_bson_true_byte = 1;
193
+
194
+ /**
195
+ * Holds the machine id for object id generation.
196
+ *
197
+ * @since 2.0.0
198
+ *
199
+ * @todo: Need to set this value properly.
200
+ */
201
+ static char rb_bson_machine_id[HOST_NAME_MAX];
202
+
203
+ /**
204
+ * The counter for incrementing object ids.
205
+ *
206
+ * @since 2.0.0
207
+ */
208
+ static unsigned int rb_bson_object_id_counter = 0;
209
+
210
+ /**
211
+ * Get the current time in milliseconds, used in object id generation.
212
+ *
213
+ * @example Get the current time in milliseconds.
214
+ * rb_current_time_milliseconds();
215
+ *
216
+ * @return [ int ] The current time in millis.
217
+ *
218
+ * @since 2.0.0
219
+ */
220
+ static unsigned long rb_current_time_milliseconds()
221
+ {
222
+ struct timeval time;
223
+ gettimeofday(&time, NULL);
224
+ return (time.tv_sec) * 1000 + (time.tv_usec) / 1000;
225
+ }
226
+
227
+ /**
228
+ * Take the provided params and return the encoded bytes or a default one.
229
+ *
230
+ * @example Get the default encoded bytes.
231
+ * rb_get_default_encoded(1, bytes);
232
+ *
233
+ * @param [ int ] argc The number of arguments.
234
+ * @param [ Object ] argv The arguments.
235
+ *
236
+ * @return [ String ] The encoded string.
237
+ *
238
+ * @since 2.0.0
239
+ */
240
+ static VALUE rb_get_default_encoded(int argc, VALUE *argv)
241
+ {
242
+ VALUE encoded;
243
+ rb_scan_args(argc, argv, "01", &encoded);
244
+ if (NIL_P(encoded)) encoded = rb_str_new_encoded_binary();
245
+ return encoded;
246
+ }
247
+
248
+ /**
249
+ * Append the ruby float as 8-byte double value to buffer.
250
+ *
251
+ * @example Convert float to double and append.
252
+ * rb_float_to_bson(..., 1.2311);
253
+ *
254
+ * @param [ String] encoded Optional string buffer, default provided by rb_str_encoded_binary
255
+ * @param [ Float ] self The ruby float value.
256
+ *
257
+ * @return [ String ] The encoded bytes with double value appended.
258
+ *
259
+ * @since 2.0.0
260
+ */
261
+ static VALUE rb_float_to_bson(int argc, VALUE *argv, VALUE self)
262
+ {
263
+ const double v = NUM2DBL(self);
264
+ VALUE encoded = rb_get_default_encoded(argc, argv);
265
+ rb_str_cat(encoded, (char*) &v, 8);
266
+ return encoded;
267
+ }
268
+
269
+ /**
270
+ * Convert the bytes for the double into a Ruby float.
271
+ *
272
+ * @example Convert the bytes to a float.
273
+ * rb_float_from_bson_double(class, bytes);
274
+ *
275
+ * @param [ Class ] The float class.
276
+ * @param [ String ] The double bytes.
277
+ *
278
+ * @return [ Float ] The ruby float value.
279
+ *
280
+ * @since 2.0.0
281
+ */
282
+ static VALUE rb_float_from_bson_double(VALUE self, VALUE value)
283
+ {
284
+ const char * bytes;
285
+ double v;
286
+ bytes = RSTRING_PTR(value);
287
+ memcpy(&v, bytes, RSTRING_LEN(value));
288
+ return DBL2NUM(v);
289
+ }
290
+
291
+ /**
292
+ * Generate the data for the next object id.
293
+ *
294
+ * @example Generate the data for the next object id.
295
+ * rb_object_id_generator_next(0, NULL, object_id);
296
+ *
297
+ * @param [ int ] argc The argument count.
298
+ * @param [ Time ] time The optional Ruby time.
299
+ * @param [ BSON::ObjectId ] self The object id.
300
+ *
301
+ * @return [ String ] The raw bytes for the id.
302
+ *
303
+ * @since 2.0.0
304
+ */
305
+ static VALUE rb_object_id_generator_next(int argc, VALUE* time, VALUE self)
306
+ {
307
+ char bytes[12];
308
+ unsigned long t;
309
+ unsigned short pid = htons(getpid());
310
+
311
+ if (argc == 0 || (argc == 1 && *time == Qnil)) {
312
+ t = rb_current_time_milliseconds();
313
+ }
314
+ else {
315
+ t = htonl(NUM2UINT(rb_funcall(*time, rb_intern("to_i"), 0)));
316
+ }
317
+
318
+ memcpy(&bytes, &t, 4);
319
+ memcpy(&bytes[4], rb_bson_machine_id, 3);
320
+ memcpy(&bytes[7], &pid, 2);
321
+ memcpy(&bytes[9], (unsigned char*) &rb_bson_object_id_counter, 3);
322
+ rb_bson_object_id_counter++;
323
+ return rb_str_new(bytes, 12);
324
+ }
325
+
326
+ /**
327
+ * Check if the integer is a 32 bit integer.
328
+ *
329
+ * @example Check if the integer is 32 bit.
330
+ * rb_integer_is_bson_int32(integer);
331
+ *
332
+ * @param [ Integer ] self The ruby integer.
333
+ *
334
+ * @return [ true, false ] If the integer is 32 bit.
335
+ *
336
+ * @since 2.0.0
337
+ */
338
+ static VALUE rb_integer_is_bson_int32(VALUE self)
339
+ {
340
+ const int64_t v = NUM2INT64(self);
341
+ if (INT_MIN <= v && v <= INT_MAX) {
342
+ return Qtrue;
343
+ }
344
+ else {
345
+ return Qfalse;
346
+ }
347
+ }
348
+
349
+ /**
350
+ * Convert the Ruby integer into a BSON as per the 32 bit specification,
351
+ * which is 4 bytes.
352
+ *
353
+ * @example Convert the integer to 32bit BSON.
354
+ * rb_integer_to_bson_int32(128, encoded);
355
+ *
356
+ * @param [ Integer ] self The Ruby integer.
357
+ * @param [ String ] encoded The Ruby binary string to append to.
358
+ *
359
+ * @return [ String ] encoded Ruby binary string with BSON raw bytes appended.
360
+ *
361
+ * @since 2.0.0
362
+ */
363
+ static VALUE rb_integer_to_bson_int32(VALUE self, VALUE encoded)
364
+ {
365
+ const int32_t v = NUM2INT(self);
366
+ const char bytes[4] = {
367
+ v & 255,
368
+ (v >> 8) & 255,
369
+ (v >> 16) & 255,
370
+ (v >> 24) & 255
371
+ };
372
+ return rb_str_cat(encoded, bytes, 4);
373
+ }
374
+
375
+ /**
376
+ * Initialize the bson array index for integers.
377
+ *
378
+ * @example Initialize the array.
379
+ * rb_bson_init_integer_bson_array_indexes();
380
+ *
381
+ * @since 2.0.0
382
+ */
383
+ static void rb_bson_init_integer_bson_array_indexes(void)
384
+ {
385
+ int i;
386
+ for (i = 0; i < BSON_INDEX_SIZE; i++) {
387
+ snprintf(rb_bson_array_indexes[i], BSON_INDEX_CHAR_SIZE, "%d", i);
388
+ }
389
+ }
390
+
391
+ /**
392
+ * Convert the Ruby integer into a character string and append with nullchar to encoded BSON.
393
+ *
394
+ * @example Convert the integer to string and append with nullchar.
395
+ * rb_integer_to_bson_key(128, encoded);
396
+ *
397
+ * @param [ Integer ] self The Ruby integer.
398
+ * @param [ String ] encoded The Ruby binary string to append to.
399
+ *
400
+ * @return [ String ] encoded Ruby binary string with BSON raw bytes appended.
401
+ *
402
+ * @since 2.0.0
403
+ */
404
+ static VALUE rb_integer_to_bson_key(int argc, VALUE *argv, VALUE self)
405
+ {
406
+ char bytes[INTEGER_CHAR_SIZE];
407
+ const int64_t v = NUM2INT64(self);
408
+ VALUE encoded = rb_get_default_encoded(argc, argv);
409
+ int length;
410
+ if (v < BSON_INDEX_SIZE)
411
+ return rb_str_cat(encoded, rb_bson_array_indexes[v], strlen(rb_bson_array_indexes[v]) + 1);
412
+ length = snprintf(bytes, INTEGER_CHAR_SIZE, "%ld", (long)v);
413
+ return rb_str_cat(encoded, bytes, length + 1);
414
+ }
415
+
416
+ /**
417
+ * Convert the provided raw bytes into a 32bit Ruby integer.
418
+ *
419
+ * @example Convert the bytes to an Integer.
420
+ * rb_integer_from_bson_int32(Int32, bytes);
421
+ *
422
+ * @param [ BSON::Int32 ] self The Int32 eigenclass.
423
+ * @param [ String ] bytes The raw bytes.
424
+ *
425
+ * @return [ Integer ] The Ruby integer.
426
+ *
427
+ * @since 2.0.0
428
+ */
429
+ static VALUE rb_integer_from_bson_int32(VALUE self, VALUE bson)
430
+ {
431
+ const uint8_t *v = (const uint8_t*) RSTRING_PTR(bson);
432
+ const uint32_t integer = v[0] + (v[1] << 8) + (v[2] << 16) + (v[3] << 24);
433
+ return INT2NUM(integer);
434
+ }
435
+
436
+ /**
437
+ * Convert the raw BSON bytes into an int64_t type.
438
+ *
439
+ * @example Convert the bytes into an int64_t.
440
+ * rb_bson_to_int64_t(bson);
441
+ *
442
+ * @param [ String ] bson The raw bytes.
443
+ *
444
+ * @return [ int64_t ] The int64_t.
445
+ *
446
+ * @since 2.0.0
447
+ */
448
+ static int64_t rb_bson_to_int64_t(VALUE bson)
449
+ {
450
+ uint8_t *v;
451
+ uint32_t byte_0, byte_1, byte_2, byte_3;
452
+ int64_t lower, upper;
453
+ v = (uint8_t*) RSTRING_PTR(bson);
454
+ byte_0 = v[0];
455
+ byte_1 = v[1];
456
+ byte_2 = v[2];
457
+ byte_3 = v[3];
458
+ lower = byte_0 + (byte_1 << 8) + (byte_2 << 16) + (byte_3 << 24);
459
+ byte_0 = v[4];
460
+ byte_1 = v[5];
461
+ byte_2 = v[6];
462
+ byte_3 = v[7];
463
+ upper = byte_0 + (byte_1 << 8) + (byte_2 << 16) + (byte_3 << 24);
464
+ return lower + (upper << 32);
465
+ }
466
+
467
+ /**
468
+ * Convert the provided raw bytes into a 64bit Ruby integer.
469
+ *
470
+ * @example Convert the bytes to an Integer.
471
+ * rb_integer_from_bson_int64(Int64, bytes);
472
+ *
473
+ * @param [ BSON::Int64 ] self The Int64 eigenclass.
474
+ * @param [ String ] bytes The raw bytes.
475
+ *
476
+ * @return [ Integer ] The Ruby integer.
477
+ *
478
+ * @since 2.0.0
479
+ */
480
+ static VALUE rb_integer_from_bson_int64(VALUE self, VALUE bson)
481
+ {
482
+ return INT642NUM(rb_bson_to_int64_t(bson));
483
+ }
484
+
485
+ /**
486
+ * Append the 64-bit integer to encoded BSON Ruby binary string.
487
+ *
488
+ * @example Append the 64-bit integer to encoded BSON.
489
+ * int64_t_to_bson(128, encoded);
490
+ *
491
+ * @param [ int64_t ] self The 64-bit integer.
492
+ * @param [ String ] encoded The BSON Ruby binary string to append to.
493
+ *
494
+ * @return [ String ] encoded Ruby binary string with BSON raw bytes appended.
495
+ *
496
+ * @since 2.0.0
497
+ */
498
+ static VALUE int64_t_to_bson(int64_t v, VALUE encoded)
499
+ {
500
+ const char bytes[8] = {
501
+ v & 255,
502
+ (v >> 8) & 255,
503
+ (v >> 16) & 255,
504
+ (v >> 24) & 255,
505
+ (v >> 32) & 255,
506
+ (v >> 40) & 255,
507
+ (v >> 48) & 255,
508
+ (v >> 56) & 255
509
+ };
510
+ return rb_str_cat(encoded, bytes, 8);
511
+ }
512
+
513
+ /**
514
+ * Convert the Ruby integer into a BSON as per the 64 bit specification,
515
+ * which is 8 bytes.
516
+ *
517
+ * @example Convert the integer to 64bit BSON.
518
+ * rb_integer_to_bson_int64(128, encoded);
519
+ *
520
+ * @param [ Integer ] self The Ruby integer.
521
+ * @param [ String ] encoded The Ruby binary string to append to.
522
+ *
523
+ * @return [ String ] encoded Ruby binary string with BSON raw bytes appended.
524
+ *
525
+ * @since 2.0.0
526
+ */
527
+ static VALUE rb_integer_to_bson_int64(VALUE self, VALUE encoded)
528
+ {
529
+ return int64_t_to_bson(NUM2INT64(self), encoded);
530
+ }
531
+
532
+ /**
533
+ * Converts the milliseconds time to the raw BSON bytes. We need to
534
+ * explicitly convert using 64 bit here.
535
+ *
536
+ * @example Convert the milliseconds value to BSON bytes.
537
+ * rb_time_to_bson(time, 2124132340000, encoded);
538
+ *
539
+ * @param [ Time ] self The Ruby Time object.
540
+ * @param [ Integer ] milliseconds The milliseconds pre/post epoch.
541
+ * @param [ String ] encoded The Ruby binary string to append to.
542
+ *
543
+ * @return [ String ] encoded Ruby binary string with time BSON raw bytes appended.
544
+ *
545
+ * @since 2.0.0
546
+ */
547
+ static VALUE rb_time_to_bson(int argc, VALUE *argv, VALUE self)
548
+ {
549
+ double t = NUM2DBL(rb_funcall(self, rb_intern("to_f"), 0));
550
+ int64_t milliseconds = (int64_t)(t * 1000);
551
+ VALUE encoded = rb_get_default_encoded(argc, argv);
552
+ return int64_t_to_bson(milliseconds, encoded);
553
+ }
554
+
555
+ /**
556
+ * Converts the raw BSON bytes into a UTC Ruby time.
557
+ *
558
+ * @example Convert the bytes to a Ruby time.
559
+ * rb_time_from_bson(time, bytes);
560
+ *
561
+ * @param [ Class ] self The Ruby Time class.
562
+ * @param [ String ] bytes The raw BSON bytes.
563
+ *
564
+ * @return [ Time ] The UTC time.
565
+ *
566
+ * @since 2.0.0
567
+ */
568
+ static VALUE rb_time_from_bson(VALUE self, VALUE bytes)
569
+ {
570
+ const int64_t millis = rb_bson_to_int64_t(bytes);
571
+ const VALUE time = rb_time_new(millis / 1000, (millis % 1000) * 1000);
572
+ return rb_funcall(time, rb_utc_method, 0);
573
+ }
574
+
575
+ /**
576
+ * Set four bytes for int32 in a binary string and return it.
577
+ *
578
+ * @example Set int32 in a BSON string.
579
+ * rb_string_set_int32(self, pos, int32)
580
+ *
581
+ * @param [ String ] self The Ruby binary string.
582
+ * @param [ Fixnum ] The position to set.
583
+ * @param [ Fixnum ] The int32 value.
584
+ *
585
+ * @return [ String ] The binary string.
586
+ *
587
+ * @since 2.0.0
588
+ */
589
+ static VALUE rb_string_set_int32(VALUE str, VALUE pos, VALUE an_int32)
590
+ {
591
+ const int32_t offset = NUM2INT(pos);
592
+ const int32_t v = NUM2INT(an_int32);
593
+ const char bytes[4] = {
594
+ v & 255,
595
+ (v >> 8) & 255,
596
+ (v >> 16) & 255,
597
+ (v >> 24) & 255
598
+ };
599
+ if (offset < 0 || offset + 4 > RSTRING_LEN(str)) {
600
+ rb_raise(rb_eArgError, "invalid position");
601
+ }
602
+ memcpy(RSTRING_PTR(str) + offset, bytes, 4);
603
+ return str;
604
+ }
605
+
606
+ /**
607
+ * Convert the ruby string to a BSON string.
608
+ *
609
+ * @example Convert the Ruby string to a BSON string.
610
+ * rb_string_to_bson_string(0, Qnil, "test");
611
+ *
612
+ * @param [ String ] self The string value.
613
+ * @param [ String ] encoded The encoded string to append to.
614
+ *
615
+ * @return [ String ] The encoded string.
616
+ *
617
+ * @since 2.0.0
618
+ */
619
+ static VALUE rb_string_to_bson_string(VALUE self, VALUE encoded)
620
+ {
621
+ const VALUE binary = rb_bson_to_utf8_binary(self);
622
+ rb_str_cat(encoded, RSTRING_PTR(binary), RSTRING_LEN(binary));
623
+ return encoded;
624
+ }
625
+
626
+ /**
627
+ * Check for illegal characters in string.
628
+ *
629
+ * @example Check for illegal characters.
630
+ * rb_string_check_for_illegal_characters("test");
631
+ *
632
+ * @param [ String ] self The string value.
633
+ *
634
+ * @since 2.0.0
635
+ */
636
+ static VALUE rb_string_check_for_illegal_characters(VALUE self)
637
+ {
638
+ if (strlen(RSTRING_PTR(self)) != (size_t) RSTRING_LEN(self))
639
+ rb_raise(rb_eRuntimeError, "Illegal C-String contains a null byte.");
640
+ return self;
641
+ }
642
+
643
+ /**
644
+ * Encode a false value to bson.
645
+ *
646
+ * @example Encode the false value.
647
+ * rb_false_class_to_bson(0, false);
648
+ *
649
+ * @param [ int ] argc The number or arguments.
650
+ * @param [ Array<Object> ] argv The arguments.
651
+ * @param [ TrueClass ] self The true value.
652
+ *
653
+ * @return [ String ] The encoded string.
654
+ *
655
+ * @since 2.0.0
656
+ */
657
+ static VALUE rb_false_class_to_bson(int argc, VALUE *argv, VALUE self)
658
+ {
659
+ VALUE encoded = rb_get_default_encoded(argc, argv);
660
+ rb_str_cat(encoded, &rb_bson_null_byte, 1);
661
+ return encoded;
662
+ }
663
+
664
+ /**
665
+ * Encode a true value to bson.
666
+ *
667
+ * @example Encode the true value.
668
+ * rb_true_class_to_bson(0, true);
669
+ *
670
+ * @param [ int ] argc The number or arguments.
671
+ * @param [ Array<Object> ] argv The arguments.
672
+ * @param [ TrueClass ] self The true value.
673
+ *
674
+ * @return [ String ] The encoded string.
675
+ *
676
+ * @since 2.0.0
677
+ */
678
+ static VALUE rb_true_class_to_bson(int argc, VALUE *argv, VALUE self)
679
+ {
680
+ VALUE encoded = rb_get_default_encoded(argc, argv);
681
+ rb_str_cat(encoded, &rb_bson_true_byte, 1);
682
+ return encoded;
683
+ }
684
+
685
+ /**
686
+ * Initialize the bson c extension.
687
+ *
688
+ * @since 2.0.0
689
+ */
690
+ void Init_native()
691
+ {
692
+ // Get all the constants to be used in the extensions.
693
+ VALUE bson = rb_const_get(rb_cObject, rb_intern("BSON"));
694
+ VALUE integer = rb_const_get(bson, rb_intern("Integer"));
695
+ VALUE floats = rb_const_get(bson, rb_intern("Float"));
696
+ VALUE float_class = rb_const_get(floats, rb_intern("ClassMethods"));
697
+ VALUE time = rb_const_get(bson, rb_intern("Time"));
698
+ VALUE time_class = rb_singleton_class(time);
699
+ VALUE int32 = rb_const_get(bson, rb_intern("Int32"));
700
+ VALUE int32_class = rb_singleton_class(int32);
701
+ VALUE int64 = rb_const_get(bson, rb_intern("Int64"));
702
+ VALUE int64_class = rb_singleton_class(int64);
703
+ VALUE object_id = rb_const_get(bson, rb_intern("ObjectId"));
704
+ VALUE generator = rb_const_get(object_id, rb_intern("Generator"));
705
+ VALUE string = rb_const_get(bson, rb_intern("String"));
706
+ VALUE true_class = rb_const_get(bson, rb_intern("TrueClass"));
707
+ VALUE false_class = rb_const_get(bson, rb_intern("FalseClass"));
708
+ rb_bson_binary = rb_const_get(bson, rb_intern("BINARY"));
709
+ rb_bson_utf8_string = rb_const_get(bson, rb_intern("UTF8"));
710
+ rb_utc_method = rb_intern("utc");
711
+
712
+ // Get the object id machine id.
713
+ gethostname(rb_bson_machine_id, sizeof rb_bson_machine_id);
714
+ rb_bson_machine_id[HOST_NAME_MAX - 1] = '\0';
715
+
716
+ // Integer optimizations.
717
+ rb_undef_method(integer, "to_bson_int32");
718
+ rb_define_method(integer, "to_bson_int32", rb_integer_to_bson_int32, 1);
719
+ rb_undef_method(integer, "to_bson_int64");
720
+ rb_define_method(integer, "to_bson_int64", rb_integer_to_bson_int64, 1);
721
+ rb_undef_method(integer, "bson_int32?");
722
+ rb_define_method(integer, "bson_int32?", rb_integer_is_bson_int32, 0);
723
+ rb_bson_init_integer_bson_array_indexes();
724
+ rb_undef_method(integer, "to_bson_key");
725
+ rb_define_method(integer, "to_bson_key", rb_integer_to_bson_key, -1);
726
+ rb_undef_method(int32_class, "from_bson_int32");
727
+ rb_define_private_method(int32_class, "from_bson_int32", rb_integer_from_bson_int32, 1);
728
+ rb_undef_method(int64_class, "from_bson_int64");
729
+ rb_define_private_method(int64_class, "from_bson_int64", rb_integer_from_bson_int64, 1);
730
+
731
+ // Float optimizations.
732
+ rb_undef_method(floats, "to_bson");
733
+ rb_define_method(floats, "to_bson", rb_float_to_bson, -1);
734
+ rb_undef_method(float_class, "from_bson_double");
735
+ rb_define_private_method(float_class, "from_bson_double", rb_float_from_bson_double, 1);
736
+
737
+ // Boolean optimizations - deserialization has no benefit so we provide
738
+ // no extensions there.
739
+ rb_undef_method(true_class, "to_bson");
740
+ rb_define_method(true_class, "to_bson", rb_true_class_to_bson, -1);
741
+ rb_undef_method(false_class, "to_bson");
742
+ rb_define_method(false_class, "to_bson", rb_false_class_to_bson, -1);
743
+
744
+ // Optimizations around time serialization and deserialization.
745
+ rb_undef_method(time, "to_bson");
746
+ rb_define_method(time, "to_bson", rb_time_to_bson, -1);
747
+ rb_undef_method(time_class, "from_bson");
748
+ rb_define_method(time_class, "from_bson", rb_time_from_bson, 1);
749
+
750
+ // String optimizations.
751
+ rb_undef_method(string, "set_int32");
752
+ rb_define_method(string, "set_int32", rb_string_set_int32, 2);
753
+ rb_undef_method(string, "to_utf8_binary");
754
+ rb_define_private_method(string, "to_utf8_binary", rb_string_to_bson_string, 1);
755
+ rb_undef_method(string, "from_bson_string");
756
+ rb_define_method(string, "from_bson_string", rb_bson_from_bson_string, 0);
757
+ rb_undef_method(string, "check_for_illegal_characters!");
758
+ rb_define_private_method(string, "check_for_illegal_characters!", rb_string_check_for_illegal_characters, 0);
759
+
760
+ // Redefine the next method on the object id generator.
761
+ rb_undef_method(generator, "next");
762
+ rb_define_method(generator, "next", rb_object_id_generator_next, -1);
763
+ }
Binary file
@@ -114,10 +114,10 @@ require "bson/version"
114
114
  # @since 2.0.0
115
115
  begin
116
116
  if jruby?
117
- require "bson/NativeService.jar"
117
+ require "bson-ruby.jar"
118
118
  org.bson.NativeService.new.basicLoad(JRuby.runtime)
119
119
  else
120
- require "bson/native"
120
+ require "native"
121
121
  end
122
122
  rescue LoadError
123
123
  $stderr.puts("BSON is using the pure Ruby implementation.")
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module BSON
3
- VERSION = "2.0.0.alpha"
3
+ VERSION = "2.0.0.beta"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bson
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.alpha
4
+ version: 2.0.0.beta
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Brock
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2013-05-08 00:00:00.000000000 Z
15
+ date: 2013-05-09 00:00:00.000000000 Z
16
16
  dependencies: []
17
17
  description: A full featured BSON specification implementation, in Ruby
18
18
  email:
@@ -55,7 +55,9 @@ files:
55
55
  - lib/bson/true_class.rb
56
56
  - lib/bson/undefined.rb
57
57
  - lib/bson/version.rb
58
+ - lib/bson-ruby.jar
58
59
  - lib/bson.rb
60
+ - ext/bson/native.c
59
61
  - ext/bson/extconf.rb
60
62
  homepage: http://bsonspec.org
61
63
  licenses: