bson 3.2.7-java → 4.0.0.beta-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +1 -3
  4. data/Rakefile +2 -10
  5. data/lib/bson-ruby.jar +0 -0
  6. data/lib/bson.rb +0 -1
  7. data/lib/bson/array.rb +15 -14
  8. data/lib/bson/binary.rb +13 -13
  9. data/lib/bson/boolean.rb +3 -3
  10. data/lib/bson/code.rb +5 -8
  11. data/lib/bson/code_with_scope.rb +10 -13
  12. data/lib/bson/date.rb +2 -2
  13. data/lib/bson/date_time.rb +2 -2
  14. data/lib/bson/document.rb +33 -0
  15. data/lib/bson/false_class.rb +2 -2
  16. data/lib/bson/float.rb +5 -11
  17. data/lib/bson/hash.rb +15 -14
  18. data/lib/bson/int32.rb +8 -9
  19. data/lib/bson/int64.rb +3 -9
  20. data/lib/bson/integer.rb +6 -20
  21. data/lib/bson/nil_class.rb +4 -16
  22. data/lib/bson/object.rb +1 -1
  23. data/lib/bson/object_id.rb +14 -16
  24. data/lib/bson/regexp.rb +7 -7
  25. data/lib/bson/specialized.rb +6 -6
  26. data/lib/bson/string.rb +7 -91
  27. data/lib/bson/symbol.rb +8 -7
  28. data/lib/bson/time.rb +5 -5
  29. data/lib/bson/timestamp.rb +8 -6
  30. data/lib/bson/true_class.rb +2 -2
  31. data/lib/bson/undefined.rb +1 -26
  32. data/lib/bson/version.rb +1 -1
  33. data/spec/bson/array_spec.rb +1 -1
  34. data/spec/bson/byte_buffer_spec.rb +445 -0
  35. data/spec/bson/code_with_scope_spec.rb +3 -7
  36. data/spec/bson/document_spec.rb +66 -10
  37. data/spec/bson/hash_spec.rb +5 -5
  38. data/spec/bson/int32_spec.rb +7 -5
  39. data/spec/bson/integer_spec.rb +1 -6
  40. data/spec/bson/object_id_spec.rb +2 -39
  41. data/spec/bson/regexp_spec.rb +1 -1
  42. data/spec/bson/string_spec.rb +2 -204
  43. data/spec/bson/symbol_spec.rb +2 -17
  44. data/spec/support/shared_examples.rb +3 -26
  45. metadata +14 -13
  46. metadata.gz.sig +0 -0
  47. data/lib/bson/encodable.rb +0 -86
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2a5293be2b750f0c2995673bdb3bafcf4134b1e3
4
- data.tar.gz: 81b1291f4fd4b50983b523defd408eb84fbeba77
3
+ metadata.gz: 6111b7a5d3785b63d89444ddd5cf6e686a8a17da
4
+ data.tar.gz: ecbe32290134736b498ba719d60aaa23059039ae
5
5
  SHA512:
6
- metadata.gz: 1a59e4379133878b8cee99a68486663f0bbebef22d20dd92b05466a01e8541f40fc5428cf755febd7b771bcefd5b432f2744c8c35864037c2b636dddd18a2555
7
- data.tar.gz: 2be69d69bbbeba1174d65b88d5c069e58f564258cb4bb488f46363f666dfe7cc426b7d0887dca72e5ef0afa15e3386ed083819bc0d67665dfe101d155cb02d7b
6
+ metadata.gz: e4c1f290674d55904de628351da900aa2ea94e1da17152409a7fa37fef0b9b8d79144397a19475bb9cc5f274253e707932fec49e033d731ca7aa5e6fa50d4ee9
7
+ data.tar.gz: d0a6549648f3c5b376dd68826c8d87ebabc1001b9ab49bb1498f20cf616299eb7270f067ce4fcf6540dd5be7542c08ed479bc552d6b2734e8091ef1f3e0b3624
Binary file
data.tar.gz.sig CHANGED
@@ -1,3 +1 @@
1
- ����W)���d="{x��yRB��w� �;���8Ȫ
2
- �h��Cf�(eVR�x:e���R/pr,g<����:+y��r��Yv�O�����B�����7�-���d�����Y0=��+���sSy��;�O$�|�Jq�l�ݶ��
3
- ���T���<?b8�ճз��K��^%�[/�S����N�›�]������^�DǼ�ɣ�$����`���(�/>qc��}#�}�t���q:%b�������*)y� ��}���
1
+ i=�_�Q)y��X@�0X$B��̵9H���>(�U&��r��A���b�#M�)[s^m�C#苨l��"�>.o�������X�P�D�vB7�L6(�3�;U�T��&�ƵqV�����4�æ�A�P،��� r����8zޅ|����Ԁ2=�)�p�0���;~#h+��_�[
data/Rakefile CHANGED
@@ -49,7 +49,6 @@ end
49
49
 
50
50
  require_relative "perf/bench"
51
51
 
52
- RSpec::Core::RakeTask.new(:spec)
53
52
  RSpec::Core::RakeTask.new(:rspec)
54
53
 
55
54
  if jruby?
@@ -74,8 +73,7 @@ task :clean_all => :clean do
74
73
  end
75
74
  end
76
75
 
77
- task :ext_spec => :compile do
78
- ENV["WITH_EXT"] = "C"
76
+ task :spec => :compile do
79
77
  Rake::Task["rspec"].invoke
80
78
  end
81
79
 
@@ -110,12 +108,6 @@ namespace :benchmark do
110
108
  require "bson"
111
109
  benchmark!
112
110
  end
113
-
114
- task :profile => :compile do
115
- puts "Profiling with native extensions..."
116
- require "bson"
117
- profile!
118
- end
119
111
  end
120
112
 
121
- task :default => [ :clean_all, :spec, :ext_spec ]
113
+ task :default => [ :clean_all, :spec ]
Binary file
@@ -62,7 +62,6 @@ require "bson/json"
62
62
  require "bson/int32"
63
63
  require "bson/int64"
64
64
  require "bson/integer"
65
- require "bson/encodable"
66
65
  require "bson/array"
67
66
  require "bson/binary"
68
67
  require "bson/boolean"
@@ -21,7 +21,6 @@ module BSON
21
21
  #
22
22
  # @since 2.0.0
23
23
  module Array
24
- include Encodable
25
24
 
26
25
  # An array is type 0x04 in the BSON spec.
27
26
  #
@@ -41,14 +40,16 @@ module BSON
41
40
  # @see http://bsonspec.org/#/specification
42
41
  #
43
42
  # @since 2.0.0
44
- def to_bson(encoded = ''.force_encoding(BINARY))
45
- encode_with_placeholder_and_null(BSON_ADJUST, encoded) do |encoded|
46
- each_with_index do |value, index|
47
- encoded << value.bson_type
48
- index.to_bson_key(encoded)
49
- value.to_bson(encoded)
50
- end
43
+ def to_bson(buffer = ByteBuffer.new)
44
+ position = buffer.length
45
+ buffer.put_int32(0)
46
+ each_with_index do |value, index|
47
+ buffer.put_byte(value.bson_type)
48
+ buffer.put_cstring(index.to_s)
49
+ value.to_bson(buffer)
51
50
  end
51
+ buffer.put_byte(NULL_BYTE)
52
+ buffer.replace_int32(position, buffer.length - position)
52
53
  end
53
54
 
54
55
  # Convert the array to an object id. This will only work for arrays of size
@@ -84,19 +85,19 @@ module BSON
84
85
 
85
86
  # Deserialize the array from BSON.
86
87
  #
87
- # @param [ BSON ] bson The bson representing an array.
88
+ # @param [ ByteBuffer ] buffer The byte buffer.
88
89
  #
89
90
  # @return [ Array ] The decoded array.
90
91
  #
91
92
  # @see http://bsonspec.org/#/specification
92
93
  #
93
94
  # @since 2.0.0
94
- def from_bson(bson)
95
+ def from_bson(buffer)
95
96
  array = new
96
- bson.read(4) # throw away the length
97
- while (type = bson.readbyte.chr) != NULL_BYTE
98
- bson.gets(NULL_BYTE)
99
- array << BSON::Registry.get(type).from_bson(bson)
97
+ buffer.get_int32 # throw away the length
98
+ while (type = buffer.get_byte) != NULL_BYTE
99
+ buffer.get_cstring
100
+ array << BSON::Registry.get(type).from_bson(buffer)
100
101
  end
101
102
  array
102
103
  end
@@ -21,7 +21,6 @@ module BSON
21
21
  # @since 2.0.0
22
22
  class Binary
23
23
  include JSON
24
- include Encodable
25
24
 
26
25
  # A binary is type 0x05 in the BSON spec.
27
26
  #
@@ -130,28 +129,29 @@ module BSON
130
129
  # @see http://bsonspec.org/#/specification
131
130
  #
132
131
  # @since 2.0.0
133
- def to_bson(encoded = ''.force_encoding(BINARY))
134
- encode_binary_data_with_placeholder(encoded) do |encoded|
135
- encoded << SUBTYPES.fetch(type)
136
- encoded << data.bytesize.to_bson if type == :old
137
- encoded << data.force_encoding(BINARY)
138
- end
132
+ def to_bson(buffer = ByteBuffer.new)
133
+ position = buffer.length
134
+ buffer.put_int32(0)
135
+ buffer.put_byte(SUBTYPES.fetch(type))
136
+ buffer.put_int32(data.bytesize) if type == :old
137
+ buffer.put_bytes(data.force_encoding(BINARY))
138
+ buffer.replace_int32(position, buffer.length - position - 5)
139
139
  end
140
140
 
141
141
  # Deserialize the binary data from BSON.
142
142
  #
143
- # @param [ BSON ] bson The bson representing binary data.
143
+ # @param [ ByteBuffer ] buffer The byte buffer.
144
144
  #
145
145
  # @return [ Binary ] The decoded binary data.
146
146
  #
147
147
  # @see http://bsonspec.org/#/specification
148
148
  #
149
149
  # @since 2.0.0
150
- def self.from_bson(bson)
151
- length = Int32.from_bson(bson)
152
- type = TYPES[bson.read(1)]
153
- length = Int32.from_bson(bson) if type == :old
154
- data = bson.read(length)
150
+ def self.from_bson(buffer)
151
+ length = buffer.get_int32
152
+ type = TYPES[buffer.get_byte]
153
+ length = buffer.get_int32 if type == :old
154
+ data = buffer.get_bytes(length)
155
155
  new(data, type)
156
156
  end
157
157
 
@@ -29,15 +29,15 @@ module BSON
29
29
 
30
30
  # Deserialize a boolean from BSON.
31
31
  #
32
- # @param [ BSON ] bson The encoded boolean.
32
+ # @param [ ByteBuffer ] buffer The byte buffer.
33
33
  #
34
34
  # @return [ TrueClass, FalseClass ] The decoded boolean.
35
35
  #
36
36
  # @see http://bsonspec.org/#/specification
37
37
  #
38
38
  # @since 2.0.0
39
- def self.from_bson(bson)
40
- bson.readbyte.chr == TrueClass::TRUE_BYTE
39
+ def self.from_bson(buffer)
40
+ buffer.get_byte == TrueClass::TRUE_BYTE
41
41
  end
42
42
 
43
43
  # Register this type when the module is loaded.
@@ -21,7 +21,6 @@ module BSON
21
21
  # @since 2.0.0
22
22
  class Code
23
23
  include JSON
24
- include Encodable
25
24
 
26
25
  # A code is type 0x0D in the BSON spec.
27
26
  #
@@ -82,23 +81,21 @@ module BSON
82
81
  # @see http://bsonspec.org/#/specification
83
82
  #
84
83
  # @since 2.0.0
85
- def to_bson(encoded = ''.force_encoding(BINARY))
86
- encode_with_placeholder_and_null(STRING_ADJUST, encoded) do |encoded|
87
- javascript.to_bson_string(encoded)
88
- end
84
+ def to_bson(buffer = ByteBuffer.new)
85
+ buffer.put_string(javascript) # @todo: was formerly to_bson_string
89
86
  end
90
87
 
91
88
  # Deserialize code from BSON.
92
89
  #
93
- # @param [ BSON ] bson The encoded code.
90
+ # @param [ ByteBuffer ] buffer The byte buffer.
94
91
  #
95
92
  # @return [ TrueClass, FalseClass ] The decoded code.
96
93
  #
97
94
  # @see http://bsonspec.org/#/specification
98
95
  #
99
96
  # @since 2.0.0
100
- def self.from_bson(bson)
101
- new(bson.read(Int32.from_bson(bson)).from_bson_string.chop!)
97
+ def self.from_bson(buffer)
98
+ new(buffer.get_string)
102
99
  end
103
100
 
104
101
  # Register this type when the module is loaded.
@@ -21,7 +21,6 @@ module BSON
21
21
  #
22
22
  # @since 2.0.0
23
23
  class CodeWithScope
24
- include Encodable
25
24
  include JSON
26
25
 
27
26
  # A code with scope is type 0x0F in the BSON spec.
@@ -88,28 +87,26 @@ module BSON
88
87
  # @see http://bsonspec.org/#/specification
89
88
  #
90
89
  # @since 2.0.0
91
- def to_bson(encoded = ''.force_encoding(BINARY))
92
- # -1 because we are removing an extra byte
93
- out = encode_with_placeholder_and_null(BSON_ADJUST - 1, encoded) do |encoded|
94
- javascript.to_bson(encoded)
95
- scope.to_bson(encoded)
96
- end
97
- # an extra null byte has been added; we must remove it
98
- out.chop!
90
+ def to_bson(buffer = ByteBuffer.new)
91
+ position = buffer.length
92
+ buffer.put_int32(0)
93
+ buffer.put_string(javascript)
94
+ scope.to_bson(buffer)
95
+ buffer.replace_int32(position, buffer.length - position)
99
96
  end
100
97
 
101
98
  # Deserialize a code with scope from BSON.
102
99
  #
103
- # @param [ BSON ] bson The encoded code with scope.
100
+ # @param [ ByteBuffer ] buffer The byte buffer.
104
101
  #
105
102
  # @return [ TrueClass, FalseClass ] The decoded code with scope.
106
103
  #
107
104
  # @see http://bsonspec.org/#/specification
108
105
  #
109
106
  # @since 2.0.0
110
- def self.from_bson(bson)
111
- bson.read(4) # Throw away the total length.
112
- new(bson.read(Int32.from_bson(bson)).from_bson_string.chop!, ::Hash.from_bson(bson))
107
+ def self.from_bson(buffer)
108
+ buffer.get_int32 # Throw away the total length.
109
+ new(buffer.get_string, ::Hash.from_bson(buffer))
113
110
  end
114
111
 
115
112
  # Register this type when the module is loaded.
@@ -34,8 +34,8 @@ module BSON
34
34
  # @see http://bsonspec.org/#/specification
35
35
  #
36
36
  # @since 2.1.0
37
- def to_bson(encoded = ''.force_encoding(BINARY))
38
- ::Time.utc(year, month, day).to_bson(encoded)
37
+ def to_bson(buffer = ByteBuffer.new)
38
+ ::Time.utc(year, month, day).to_bson(buffer)
39
39
  end
40
40
 
41
41
  # Get the BSON type for the date.
@@ -34,8 +34,8 @@ module BSON
34
34
  # @see http://bsonspec.org/#/specification
35
35
  #
36
36
  # @since 2.1.0
37
- def to_bson(encoded = ''.force_encoding(BINARY))
38
- to_time.to_bson(encoded)
37
+ def to_bson(buffer = ByteBuffer.new)
38
+ to_time.to_bson(buffer)
39
39
  end
40
40
  end
41
41
 
@@ -68,6 +68,39 @@ module BSON
68
68
  super(convert_key(key), convert_value(value))
69
69
  end
70
70
 
71
+ # Returns true if the given key is present in the document. Will normalize
72
+ # symbol keys into strings.
73
+ #
74
+ # @example Test if a key exists using a symbol
75
+ # document.has_key?(:test)
76
+ #
77
+ # @return [ true, false]
78
+ #
79
+ # @since 3.2.7
80
+ def has_key?(key)
81
+ super(convert_key(key))
82
+ end
83
+
84
+ alias :include? :has_key?
85
+ alias :key? :has_key?
86
+ alias :member? :has_key?
87
+
88
+
89
+ # Returns true if the given value is present in the document. Will normalize
90
+ # symbols into strings.
91
+ #
92
+ # @example Test if a key exists using a symbol
93
+ # document.has_value?(:test)
94
+ #
95
+ # @return [ true, false]
96
+ #
97
+ # @since 3.2.7
98
+ def has_value?(value)
99
+ super(convert_value(value))
100
+ end
101
+
102
+ alias :value :has_value?
103
+
71
104
  # Instantiate a new Document. Valid parameters for instantiation is a hash
72
105
  # only or nothing.
73
106
  #
@@ -49,8 +49,8 @@ module BSON
49
49
  # @see http://bsonspec.org/#/specification
50
50
  #
51
51
  # @since 2.0.0
52
- def to_bson(encoded = ''.force_encoding(BINARY))
53
- encoded << FALSE_BYTE
52
+ def to_bson(buffer = ByteBuffer.new)
53
+ buffer.put_byte(FALSE_BYTE)
54
54
  end
55
55
  end
56
56
 
@@ -42,29 +42,23 @@ module BSON
42
42
  # @see http://bsonspec.org/#/specification
43
43
  #
44
44
  # @since 2.0.0
45
- def to_bson(encoded = ''.force_encoding(BINARY))
46
- encoded << [ self ].pack(PACK)
45
+ def to_bson(buffer = ByteBuffer.new)
46
+ buffer.put_double(self)
47
47
  end
48
48
 
49
49
  module ClassMethods
50
50
 
51
51
  # Deserialize an instance of a Float from a BSON double.
52
52
  #
53
- # @param [ BSON ] bson The encoded double.
53
+ # @param [ ByteBuffer ] buffer The byte buffer.
54
54
  #
55
55
  # @return [ Float ] The decoded Float.
56
56
  #
57
57
  # @see http://bsonspec.org/#/specification
58
58
  #
59
59
  # @since 2.0.0
60
- def from_bson(bson)
61
- from_bson_double(bson.read(8))
62
- end
63
-
64
- private
65
-
66
- def from_bson_double(double)
67
- double.unpack(PACK).first
60
+ def from_bson(buffer)
61
+ buffer.get_double
68
62
  end
69
63
  end
70
64
 
@@ -21,7 +21,6 @@ module BSON
21
21
  #
22
22
  # @since 2.0.0
23
23
  module Hash
24
- include Encodable
25
24
 
26
25
  # An hash (embedded document) is type 0x03 in the BSON spec.
27
26
  #
@@ -38,14 +37,16 @@ module BSON
38
37
  # @see http://bsonspec.org/#/specification
39
38
  #
40
39
  # @since 2.0.0
41
- def to_bson(encoded = ''.force_encoding(BINARY))
42
- encode_with_placeholder_and_null(BSON_ADJUST, encoded) do |encoded|
43
- each do |field, value|
44
- encoded << value.bson_type
45
- field.to_bson_key(encoded)
46
- value.to_bson(encoded)
47
- end
40
+ def to_bson(buffer = ByteBuffer.new)
41
+ position = buffer.length
42
+ buffer.put_int32(0)
43
+ each do |field, value|
44
+ buffer.put_byte(value.bson_type)
45
+ buffer.put_cstring(field.to_bson_key)
46
+ value.to_bson(buffer)
48
47
  end
48
+ buffer.put_byte(NULL_BYTE)
49
+ buffer.replace_int32(position, buffer.length - position)
49
50
  end
50
51
 
51
52
  # Converts the hash to a normalized value in a BSON document.
@@ -64,19 +65,19 @@ module BSON
64
65
 
65
66
  # Deserialize the hash from BSON.
66
67
  #
67
- # @param [ IO ] bson The bson representing a hash.
68
+ # @param [ ByteBuffer ] buffer The byte buffer.
68
69
  #
69
70
  # @return [ Array ] The decoded hash.
70
71
  #
71
72
  # @see http://bsonspec.org/#/specification
72
73
  #
73
74
  # @since 2.0.0
74
- def from_bson(bson)
75
+ def from_bson(buffer)
75
76
  hash = Document.allocate
76
- bson.read(4) # Swallow the first four bytes.
77
- while (type = bson.readbyte.chr) != NULL_BYTE
78
- field = bson.gets(NULL_BYTE).from_bson_string.chop!
79
- hash.store(field, BSON::Registry.get(type).from_bson(bson))
77
+ buffer.get_int32 # Throw away the size - todo: just move read position?
78
+ while (type = buffer.get_byte) != NULL_BYTE
79
+ field = buffer.get_cstring
80
+ hash.store(field, BSON::Registry.get(type).from_bson(buffer))
80
81
  end
81
82
  hash
82
83
  end
@@ -27,6 +27,11 @@ module BSON
27
27
  # @since 2.0.0
28
28
  BSON_TYPE = 16.chr.force_encoding(BINARY).freeze
29
29
 
30
+ # The number of bytes constant.
31
+ #
32
+ # @since 4.0.0
33
+ BYTES_LENGTH = 4
34
+
30
35
  # Constant for the int 32 pack directive.
31
36
  #
32
37
  # @since 2.0.0
@@ -34,21 +39,15 @@ module BSON
34
39
 
35
40
  # Deserialize an Integer from BSON.
36
41
  #
37
- # @param [ BSON ] bson The encoded int32.
42
+ # @param [ ByteBuffer ] buffer The byte buffer.
38
43
  #
39
44
  # @return [ Integer ] The decoded Integer.
40
45
  #
41
46
  # @see http://bsonspec.org/#/specification
42
47
  #
43
48
  # @since 2.0.0
44
- def self.from_bson(bson)
45
- from_bson_int32(bson.read(4))
46
- end
47
-
48
- private
49
-
50
- def self.from_bson_int32(bytes)
51
- bytes.unpack(PACK).first
49
+ def self.from_bson(buffer)
50
+ buffer.get_int32
52
51
  end
53
52
 
54
53
  # Register this type when the module is loaded.