mongo_ext 0.16 → 0.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/ext/cbson/cbson.c +63 -1
  2. metadata +3 -3
data/ext/cbson/cbson.c CHANGED
@@ -38,8 +38,11 @@
38
38
 
39
39
  #include <assert.h>
40
40
  #include <math.h>
41
+ #include <unistd.h>
42
+ #include <time.h>
41
43
 
42
44
  #define INITIAL_BUFFER_SIZE 256
45
+ #define MAX_HOSTNAME_LENGTH 256
43
46
 
44
47
  static VALUE Binary;
45
48
  static VALUE Time;
@@ -49,6 +52,7 @@ static VALUE Code;
49
52
  static VALUE RegexpOfHolding;
50
53
  static VALUE OrderedHash;
51
54
  static VALUE InvalidName;
55
+ static VALUE DigestMD5;
52
56
 
53
57
  #if HAVE_RUBY_ENCODING_H
54
58
  #include "ruby/encoding.h"
@@ -737,8 +741,58 @@ static VALUE method_deserialize(VALUE self, VALUE bson) {
737
741
  return elements_to_hash(buffer, remaining);
738
742
  }
739
743
 
744
+
745
+ static VALUE fast_pack(VALUE self)
746
+ {
747
+ VALUE res;
748
+ long i;
749
+ char c;
750
+
751
+ res = rb_str_buf_new(0);
752
+
753
+ for (i = 0; i < RARRAY_LEN(self); i++) {
754
+ c = FIX2LONG(RARRAY_PTR(self)[i]);
755
+ rb_str_buf_cat(res, &c, sizeof(char));
756
+ }
757
+
758
+ return res;
759
+ }
760
+
761
+
762
+ static VALUE objectid_generate(VALUE self)
763
+ {
764
+ VALUE oid, digest;
765
+ char hostname[MAX_HOSTNAME_LENGTH];
766
+ unsigned char oid_bytes[12];
767
+ unsigned long t, inc;
768
+ unsigned short pid;
769
+ int i;
770
+
771
+ t = htonl(time(NULL));
772
+ MEMCPY(&oid_bytes, &t, unsigned char, 4);
773
+
774
+ if (gethostname(&hostname, MAX_HOSTNAME_LENGTH) != 0) {
775
+ rb_raise(rb_eRuntimeError, "failed to get hostname");
776
+ }
777
+ digest = rb_funcall(DigestMD5, rb_intern("digest"), 1, rb_str_new2(hostname));
778
+ MEMCPY(&oid_bytes[4], RSTRING_PTR(digest), unsigned char, 3);
779
+
780
+ pid = htons(getpid());
781
+ MEMCPY(&oid_bytes[7], &pid, unsigned char, 2);
782
+
783
+ inc = htonl(FIX2ULONG(rb_funcall(self, rb_intern("get_inc"), 0)));
784
+ MEMCPY(&oid_bytes[9], ((unsigned char*)&inc + 1), unsigned char, 3);
785
+
786
+ oid = rb_ary_new2(12);
787
+ for(i = 0; i < 12; i++) {
788
+ rb_ary_store(oid, i, INT2FIX((unsigned int)oid_bytes[i]));
789
+ }
790
+ return oid;
791
+ }
792
+
793
+
740
794
  void Init_cbson() {
741
- VALUE mongo, CBson;
795
+ VALUE mongo, CBson, Digest;
742
796
  Time = rb_const_get(rb_cObject, rb_intern("Time"));
743
797
 
744
798
  mongo = rb_const_get(rb_cObject, rb_intern("Mongo"));
@@ -760,4 +814,12 @@ void Init_cbson() {
760
814
  CBson = rb_define_module("CBson");
761
815
  rb_define_module_function(CBson, "serialize", method_serialize, 2);
762
816
  rb_define_module_function(CBson, "deserialize", method_deserialize, 1);
817
+
818
+ rb_require("digest/md5");
819
+ Digest = rb_const_get(rb_cObject, rb_intern("Digest"));
820
+ DigestMD5 = rb_const_get(Digest, rb_intern("MD5"));
821
+
822
+ rb_define_method(ObjectID, "generate", objectid_generate, 0);
823
+
824
+ rb_define_method(rb_cArray, "fast_pack", fast_pack, 0);
763
825
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.16"
4
+ version: "0.17"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Dirolf
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-26 00:00:00 -04:00
12
+ date: 2009-11-16 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -26,7 +26,7 @@ files:
26
26
  - mongo-extensions.gemspec
27
27
  - ext/cbson/extconf.rb
28
28
  - ext/cbson/cbson.c
29
- has_rdoc: true
29
+ has_rdoc: false
30
30
  homepage: http://www.mongodb.org
31
31
  licenses: []
32
32