Esempio n. 1
0
/**
 * Initialize the bson c extension.
 *
 * @since 2.0.0
 */
void Init_native()
{
  // Get all the constants to be used in the extensions.
  VALUE bson = rb_const_get(rb_cObject, rb_intern("BSON"));
  VALUE integer = rb_const_get(bson, rb_intern("Integer"));
  VALUE floats = rb_const_get(bson, rb_intern("Float"));
  VALUE float_class = rb_const_get(floats, rb_intern("ClassMethods"));
  VALUE time = rb_const_get(bson, rb_intern("Time"));
  VALUE time_class = rb_singleton_class(time);
  VALUE int32 = rb_const_get(bson, rb_intern("Int32"));
  VALUE int32_class = rb_singleton_class(int32);
  VALUE int64 = rb_const_get(bson, rb_intern("Int64"));
  VALUE int64_class = rb_singleton_class(int64);
  VALUE object_id = rb_const_get(bson, rb_intern("ObjectId"));
  VALUE generator = rb_const_get(object_id, rb_intern("Generator"));
  VALUE string = rb_const_get(bson, rb_intern("String"));
  VALUE true_class = rb_const_get(bson, rb_intern("TrueClass"));
  VALUE false_class = rb_const_get(bson, rb_intern("FalseClass"));
  rb_bson_utf8_string = rb_const_get(bson, rb_intern("UTF8"));
  rb_utc_method = rb_intern("utc");

  // Get the object id machine id.
  gethostname(rb_bson_machine_id, sizeof rb_bson_machine_id);
  rb_bson_machine_id[HOST_NAME_MAX - 1] = '\0';

  // Integer optimizations.
  rb_undef_method(integer, "to_bson_int32");
  rb_define_method(integer, "to_bson_int32", rb_integer_to_bson_int32, 1);
  rb_undef_method(integer, "to_bson_int64");
  rb_define_method(integer, "to_bson_int64", rb_integer_to_bson_int64, 1);
  rb_undef_method(integer, "bson_int32?");
  rb_define_method(integer, "bson_int32?", rb_integer_is_bson_int32, 0);
  rb_bson_init_integer_bson_array_indexes();
  rb_undef_method(integer, "to_bson_key");
  rb_define_method(integer, "to_bson_key", rb_integer_to_bson_key, -1);
  rb_undef_method(int32_class, "from_bson_int32");
  rb_define_private_method(int32_class, "from_bson_int32", rb_integer_from_bson_int32, 1);
  rb_undef_method(int64_class, "from_bson_int64");
  rb_define_private_method(int64_class, "from_bson_int64", rb_integer_from_bson_int64, 1);

  // Float optimizations.
  rb_undef_method(floats, "to_bson");
  rb_define_method(floats, "to_bson", rb_float_to_bson, -1);
  rb_undef_method(float_class, "from_bson_double");
  rb_define_private_method(float_class, "from_bson_double", rb_float_from_bson_double, 1);

  // Boolean optimizations - deserialization has no benefit so we provide
  // no extensions there.
  rb_undef_method(true_class, "to_bson");
  rb_define_method(true_class, "to_bson", rb_true_class_to_bson, -1);
  rb_undef_method(false_class, "to_bson");
  rb_define_method(false_class, "to_bson", rb_false_class_to_bson, -1);

  // Optimizations around time serialization and deserialization.
  rb_undef_method(time, "to_bson");
  rb_define_method(time, "to_bson", rb_time_to_bson, -1);
  rb_undef_method(time_class, "from_bson");
  rb_define_method(time_class, "from_bson", rb_time_from_bson, 1);

  // String optimizations.
  rb_undef_method(string, "set_int32");
  rb_define_method(string, "set_int32", rb_string_set_int32, 2);
  rb_undef_method(string, "to_utf8_binary");
  rb_define_private_method(string, "to_utf8_binary", rb_string_to_bson_string, 1);
  rb_undef_method(string, "from_bson_string");
  rb_define_method(string, "from_bson_string", rb_bson_from_bson_string, 0);
  rb_undef_method(string, "check_for_illegal_characters!");
  rb_define_private_method(string, "check_for_illegal_characters!", rb_string_check_for_illegal_characters, 0);

  // Redefine the next method on the object id generator.
  rb_undef_method(generator, "next");
  rb_define_method(generator, "next", rb_object_id_generator_next, -1);
}
Esempio n. 2
0
/**
 * Initialize the bson c extension.
 *
 * @since 2.0.0
 */
void Init_native()
{
  // Get all the constants to be used in the extensions.
  VALUE bson = rb_const_get(rb_cObject, rb_intern("BSON"));
  VALUE integer = rb_const_get(bson, rb_intern("Integer"));
  VALUE floats = rb_const_get(bson, rb_intern("Float"));
  VALUE float_class = rb_const_get(floats, rb_intern("ClassMethods"));
  VALUE time = rb_const_get(bson, rb_intern("Time"));
  VALUE time_class = rb_singleton_class(time);
  VALUE int32 = rb_const_get(bson, rb_intern("Int32"));
  VALUE int32_class = rb_singleton_class(int32);
  VALUE int64 = rb_const_get(bson, rb_intern("Int64"));
  VALUE int64_class = rb_singleton_class(int64);
  VALUE object_id = rb_const_get(bson, rb_intern("ObjectId"));
  VALUE generator = rb_const_get(object_id, rb_intern("Generator"));
  VALUE string = rb_const_get(bson, rb_intern("String"));
  VALUE true_class = rb_const_get(bson, rb_intern("TrueClass"));
  VALUE false_class = rb_const_get(bson, rb_intern("FalseClass"));
  // needed to hash the machine id
  rb_require("digest/md5");
  VALUE digest_class = rb_const_get(rb_cObject, rb_intern("Digest"));
  VALUE md5_class = rb_const_get(digest_class, rb_intern("MD5"));
  rb_bson_utf8_string = rb_const_get(bson, rb_intern("UTF8"));
  rb_utc_method = rb_intern("utc");

  // Get the object id machine id and hash it.
  char rb_bson_machine_id[256];
  gethostname(rb_bson_machine_id, sizeof rb_bson_machine_id);
  rb_bson_machine_id[255] = '\0';
  VALUE digest = rb_funcall(md5_class, rb_intern("digest"), 1, rb_str_new2(rb_bson_machine_id));
  memcpy(rb_bson_machine_id_hash, RSTRING_PTR(digest), RSTRING_LEN(digest));

  // Integer optimizations.
  rb_undef_method(integer, "to_bson_int32");
  rb_define_method(integer, "to_bson_int32", rb_integer_to_bson_int32, 1);
  rb_undef_method(integer, "to_bson_int64");
  rb_define_method(integer, "to_bson_int64", rb_integer_to_bson_int64, 1);
  rb_undef_method(integer, "bson_int32?");
  rb_define_method(integer, "bson_int32?", rb_integer_is_bson_int32, 0);
  rb_bson_init_integer_bson_array_indexes();
  rb_undef_method(integer, "to_bson_key");
  rb_define_method(integer, "to_bson_key", rb_integer_to_bson_key, -1);
  rb_undef_method(int32_class, "from_bson_int32");
  rb_define_private_method(int32_class, "from_bson_int32", rb_integer_from_bson_int32, 1);
  rb_undef_method(int64_class, "from_bson_int64");
  rb_define_private_method(int64_class, "from_bson_int64", rb_integer_from_bson_int64, 1);

  // Float optimizations.
  rb_undef_method(floats, "to_bson");
  rb_define_method(floats, "to_bson", rb_float_to_bson, -1);
  rb_undef_method(float_class, "from_bson_double");
  rb_define_private_method(float_class, "from_bson_double", rb_float_from_bson_double, 1);

  // Boolean optimizations - deserialization has no benefit so we provide
  // no extensions there.
  rb_undef_method(true_class, "to_bson");
  rb_define_method(true_class, "to_bson", rb_true_class_to_bson, -1);
  rb_undef_method(false_class, "to_bson");
  rb_define_method(false_class, "to_bson", rb_false_class_to_bson, -1);

  // Optimizations around time serialization and deserialization.
  rb_undef_method(time, "to_bson");
  rb_define_method(time, "to_bson", rb_time_to_bson, -1);
  rb_undef_method(time_class, "from_bson");
  rb_define_method(time_class, "from_bson", rb_time_from_bson, 1);

  // String optimizations.
  rb_undef_method(string, "set_int32");
  rb_define_method(string, "set_int32", rb_string_set_int32, 2);
  rb_undef_method(string, "from_bson_string");
  rb_define_method(string, "from_bson_string", rb_bson_from_bson_string, 0);
  rb_undef_method(string, "check_for_illegal_characters!");
  rb_define_private_method(string, "check_for_illegal_characters!", rb_string_check_for_illegal_characters, 0);

  // Redefine the next method on the object id generator.
  rb_undef_method(generator, "next_object_id");
  rb_define_method(generator, "next_object_id", rb_object_id_generator_next, -1);
}