bson 1.12.5-java → 2.0.0-java

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.

Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/CHANGELOG.md +80 -0
  5. data/CONTRIBUTING.md +42 -0
  6. data/NOTICE +2 -0
  7. data/README.md +190 -0
  8. data/Rakefile +109 -0
  9. data/lib/bson-ruby.jar +0 -0
  10. data/lib/bson.rb +60 -87
  11. data/lib/bson/array.rb +104 -0
  12. data/lib/bson/binary.rb +193 -0
  13. data/lib/bson/boolean.rb +48 -0
  14. data/lib/bson/code.rb +109 -0
  15. data/lib/bson/code_with_scope.rb +120 -0
  16. data/lib/bson/document.rb +549 -0
  17. data/lib/bson/encodable.rb +86 -0
  18. data/lib/bson/environment.rb +98 -0
  19. data/lib/bson/false_class.rb +61 -0
  20. data/lib/bson/float.rb +82 -0
  21. data/lib/bson/hash.rb +84 -0
  22. data/lib/bson/int32.rb +59 -0
  23. data/lib/bson/int64.rb +59 -0
  24. data/lib/bson/integer.rb +185 -0
  25. data/lib/bson/json.rb +37 -0
  26. data/lib/bson/max_key.rb +70 -0
  27. data/lib/bson/min_key.rb +70 -0
  28. data/lib/bson/nil_class.rb +70 -0
  29. data/lib/bson/object_id.rb +395 -0
  30. data/lib/bson/regexp.rb +124 -0
  31. data/lib/bson/registry.rb +70 -0
  32. data/lib/bson/specialized.rb +74 -0
  33. data/lib/bson/string.rb +203 -0
  34. data/lib/bson/symbol.rb +87 -0
  35. data/lib/bson/time.rb +72 -0
  36. data/lib/bson/timestamp.rb +113 -0
  37. data/lib/bson/true_class.rb +61 -0
  38. data/lib/bson/undefined.rb +74 -0
  39. data/lib/bson/version.rb +17 -0
  40. data/spec/bson/array_spec.rb +58 -0
  41. data/spec/bson/binary_spec.rb +115 -0
  42. data/spec/bson/boolean_spec.rb +48 -0
  43. data/spec/bson/code_spec.rb +42 -0
  44. data/spec/bson/code_with_scope_spec.rb +74 -0
  45. data/spec/bson/document_spec.rb +778 -0
  46. data/spec/bson/false_class_spec.rb +28 -0
  47. data/spec/bson/float_spec.rb +29 -0
  48. data/spec/bson/hash_spec.rb +56 -0
  49. data/spec/bson/int32_spec.rb +28 -0
  50. data/spec/bson/int64_spec.rb +28 -0
  51. data/spec/bson/integer_spec.rb +76 -0
  52. data/spec/bson/json_spec.rb +53 -0
  53. data/spec/bson/max_key_spec.rb +75 -0
  54. data/spec/bson/min_key_spec.rb +75 -0
  55. data/spec/bson/nil_class_spec.rb +29 -0
  56. data/spec/bson/object_id_spec.rb +527 -0
  57. data/spec/bson/regexp_spec.rb +89 -0
  58. data/spec/bson/registry_spec.rb +55 -0
  59. data/spec/bson/string_spec.rb +298 -0
  60. data/spec/bson/symbol_spec.rb +55 -0
  61. data/spec/bson/time_spec.rb +43 -0
  62. data/spec/bson/timestamp_spec.rb +74 -0
  63. data/spec/bson/true_class_spec.rb +28 -0
  64. data/spec/bson/undefined_spec.rb +29 -0
  65. data/{lib/bson/types/dbref.rb → spec/bson_spec.rb} +22 -16
  66. data/spec/spec_helper.rb +32 -0
  67. data/spec/support/shared_examples.rb +95 -0
  68. metadata +116 -48
  69. metadata.gz.sig +1 -1
  70. data/VERSION +0 -1
  71. data/bin/b2json +0 -63
  72. data/bin/j2bson +0 -64
  73. data/bson.gemspec +0 -34
  74. data/ext/jbson/lib/java-bson.jar +0 -0
  75. data/ext/jbson/target/jbson.jar +0 -0
  76. data/lib/bson/bson_c.rb +0 -37
  77. data/lib/bson/bson_java.rb +0 -49
  78. data/lib/bson/bson_ruby.rb +0 -645
  79. data/lib/bson/byte_buffer.rb +0 -241
  80. data/lib/bson/exceptions.rb +0 -37
  81. data/lib/bson/grow.rb +0 -173
  82. data/lib/bson/ordered_hash.rb +0 -197
  83. data/lib/bson/support/hash_with_indifferent_access.rb +0 -174
  84. data/lib/bson/types/binary.rb +0 -52
  85. data/lib/bson/types/code.rb +0 -55
  86. data/lib/bson/types/min_max_keys.rb +0 -56
  87. data/lib/bson/types/object_id.rb +0 -226
  88. data/lib/bson/types/regex.rb +0 -116
  89. data/lib/bson/types/timestamp.rb +0 -72
@@ -0,0 +1,70 @@
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module BSON
16
+
17
+ # Provides constant values for each to the BSON types and mappings from raw
18
+ # bytes back to these types.
19
+ #
20
+ # @see http://bsonspec.org/#/specification
21
+ #
22
+ # @since 2.0.0
23
+ module Registry
24
+ extend self
25
+
26
+ # A Mapping of all the BSON types to their corresponding Ruby classes.
27
+ #
28
+ # @since 2.0.0
29
+ MAPPINGS = {}
30
+
31
+ # Get the class for the single byte identifier for the type in the BSON
32
+ # specification.
33
+ #
34
+ # @example Get the type for the byte.
35
+ # BSON::Registry.get("\x01")
36
+ #
37
+ # @return [ Class ] The corresponding Ruby class for the type.
38
+ #
39
+ # @see http://bsonspec.org/#/specification
40
+ #
41
+ # @since 2.0.0
42
+ def get(byte)
43
+ MAPPINGS.fetch(byte)
44
+ end
45
+
46
+ # Register the Ruby type for the corresponding single byte.
47
+ #
48
+ # @example Register the type.
49
+ # BSON::Registry.register("\x01", Float)
50
+ #
51
+ # @param [ String ] byte The single byte.
52
+ # @param [ Class ] The class the byte maps to.
53
+ #
54
+ # @return [ Class ] The class.
55
+ #
56
+ # @since 2.0.0
57
+ def register(byte, type)
58
+ MAPPINGS.store(byte, type)
59
+ define_type_reader(type)
60
+ end
61
+
62
+ private
63
+
64
+ def define_type_reader(type)
65
+ type.module_eval <<-MOD
66
+ def bson_type; BSON_TYPE; end
67
+ MOD
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,74 @@
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module BSON
16
+
17
+ # Provides behaviour to special values that exist in the BSON spec that don't
18
+ # have a native type, like $minKey and $maxKey.
19
+ #
20
+ # @see http://bsonspec.org/#/specification
21
+ #
22
+ # @since 2.0.0
23
+ module Specialized
24
+
25
+ # Determine if the min key is equal to another object.
26
+ #
27
+ # @example Check min key equality.
28
+ # BSON::MinKey.new == object
29
+ #
30
+ # @param [ Object ] other The object to check against.
31
+ #
32
+ # @return [ true, false ] If the objects are equal.
33
+ #
34
+ # @since 2.0.0
35
+ def ==(other)
36
+ self.class == other.class
37
+ end
38
+
39
+ # Encode the min key - has no value since it only needs the type and field
40
+ # name when being encoded.
41
+ #
42
+ # @example Encode the min key value.
43
+ # min_key.to_bson
44
+ #
45
+ # @return [ String ] An empty string.
46
+ #
47
+ # @since 2.0.0
48
+ def to_bson(encoded = ''.force_encoding(BINARY))
49
+ encoded
50
+ end
51
+
52
+ private
53
+
54
+ def self.included(klass)
55
+ klass.extend(ClassMethods)
56
+ end
57
+
58
+ module ClassMethods
59
+
60
+ # Deserialize MinKey from BSON.
61
+ #
62
+ # @param [ BSON ] bson The encoded MinKey.
63
+ #
64
+ # @return [ MinKey ] The decoded MinKey.
65
+ #
66
+ # @see http://bsonspec.org/#/specification
67
+ #
68
+ # @since 2.0.0
69
+ def from_bson(bson)
70
+ new
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,203 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright (C) 2009-2013 MongoDB Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ module BSON
17
+
18
+ # Injects behaviour for encoding and decoding string values to and from
19
+ # raw bytes as specified by the BSON spec.
20
+ #
21
+ # @see http://bsonspec.org/#/specification
22
+ #
23
+ # @since 2.0.0
24
+ module String
25
+ include Encodable
26
+
27
+ # A string is type 0x02 in the BSON spec.
28
+ #
29
+ # @since 2.0.0
30
+ BSON_TYPE = 2.chr.force_encoding(BINARY).freeze
31
+
32
+ # Get the string as encoded BSON.
33
+ #
34
+ # @example Get the string as encoded BSON.
35
+ # "test".to_bson
36
+ #
37
+ # @raise [ EncodingError ] If the string is not UTF-8.
38
+ #
39
+ # @return [ String ] The encoded string.
40
+ #
41
+ # @see http://bsonspec.org/#/specification
42
+ #
43
+ # @since 2.0.0
44
+ def to_bson(encoded = ''.force_encoding(BINARY))
45
+ encode_with_placeholder_and_null(STRING_ADJUST, encoded) do |encoded|
46
+ to_bson_string(encoded)
47
+ end
48
+ end
49
+
50
+ # Get the string as a BSON key name encoded C string with checking for special characters.
51
+ #
52
+ # @example Get the string as key name.
53
+ # "test".to_bson_key
54
+ #
55
+ # @raise [ EncodingError ] If the string is not UTF-8.
56
+ #
57
+ # @return [ String ] The encoded string.
58
+ #
59
+ # @see http://bsonspec.org/#/specification
60
+ #
61
+ # @since 2.0.0
62
+ def to_bson_key(encoded = ''.force_encoding(BINARY))
63
+ to_bson_cstring(encoded)
64
+ end
65
+
66
+ # Get the string as an encoded C string.
67
+ #
68
+ # @example Get the string as an encoded C string.
69
+ # "test".to_bson_cstring
70
+ #
71
+ # @raise [ EncodingError ] If the string is not UTF-8.
72
+ #
73
+ # @return [ String ] The encoded string.
74
+ #
75
+ # @see http://bsonspec.org/#/specification
76
+ #
77
+ # @since 2.0.0
78
+ def to_bson_cstring(encoded = ''.force_encoding(BINARY))
79
+ check_for_illegal_characters!
80
+ to_bson_string(encoded) << NULL_BYTE
81
+ end
82
+
83
+ # Convert the string to an object id. This will only work for strings of size
84
+ # 12.
85
+ #
86
+ # @example Convert the string to an object id.
87
+ # string.to_bson_object_id
88
+ #
89
+ # @note This is used for repairing legacy bson data.
90
+ #
91
+ # @raise [ InvalidObjectId ] If the string is not 12 elements.
92
+ #
93
+ # @return [ String ] The raw object id bytes.
94
+ #
95
+ # @since 2.0.0
96
+ def to_bson_object_id
97
+ ObjectId.repair(self)
98
+ end
99
+
100
+ # Convert the string to a UTF-8 string then force to binary. This is so
101
+ # we get errors for strings that are not UTF-8 encoded.
102
+ #
103
+ # @example Convert to valid BSON string.
104
+ # "Straße".to_bson_string
105
+ #
106
+ # @raise [ EncodingError ] If the string is not UTF-8.
107
+ #
108
+ # @return [ String ] The binary string.
109
+ #
110
+ # @since 2.0.0
111
+ def to_bson_string(encoded = ''.force_encoding(BINARY))
112
+ begin
113
+ to_utf8_binary(encoded)
114
+ rescue EncodingError
115
+ data = dup.force_encoding(UTF8)
116
+ raise unless data.valid_encoding?
117
+ encoded << data.force_encoding(BINARY)
118
+ end
119
+ end
120
+
121
+ # Convert the string to a hexidecimal representation.
122
+ #
123
+ # @example Convert the string to hex.
124
+ # "\x01".to_hex_string
125
+ #
126
+ # @return [ String ] The string as hex.
127
+ #
128
+ # @since 2.0.0
129
+ def to_hex_string
130
+ unpack("H*")[0]
131
+ end
132
+
133
+ # Take the binary string and return a UTF-8 encoded string.
134
+ #
135
+ # @example Convert from a BSON string.
136
+ # "\x00".from_bson_string
137
+ #
138
+ # @raise [ EncodingError ] If the string is not UTF-8.
139
+ #
140
+ # @return [ String ] The UTF-8 string.
141
+ #
142
+ # @since 2.0.0
143
+ def from_bson_string
144
+ force_encoding(UTF8)
145
+ end
146
+
147
+ # Set four bytes for int32 in a binary string and return it.
148
+ #
149
+ # @example Set int32 in a BSON string.
150
+ # "".set_int32(pos, int32)
151
+ #
152
+ # @param [ Fixnum ] The position to set.
153
+ # @param [ Fixnum ] The int32 value.
154
+ #
155
+ # @return [ String ] The binary string.
156
+ #
157
+ # @since 2.0.0
158
+ def set_int32(pos, int32)
159
+ self[pos, 4] = [ int32 ].pack(Int32::PACK)
160
+ end
161
+
162
+ private
163
+
164
+ def to_utf8_binary(encoded)
165
+ encoded << encode(UTF8).force_encoding(BINARY)
166
+ end
167
+
168
+ module ClassMethods
169
+
170
+ # Deserialize a string from BSON.
171
+ #
172
+ # @param [ BSON ] bson The bson representing a string.
173
+ #
174
+ # @return [ Regexp ] The decoded string.
175
+ #
176
+ # @see http://bsonspec.org/#/specification
177
+ #
178
+ # @since 2.0.0
179
+ def from_bson(bson)
180
+ bson.read(Int32.from_bson(bson)).from_bson_string.chop!
181
+ end
182
+ end
183
+
184
+ private
185
+
186
+ def check_for_illegal_characters!
187
+ if include?(NULL_BYTE)
188
+ raise(ArgumentError, "Illegal C-String '#{self}' contains a null byte.")
189
+ end
190
+ end
191
+
192
+ # Register this type when the module is loaded.
193
+ #
194
+ # @since 2.0.0
195
+ Registry.register(BSON_TYPE, ::String)
196
+ end
197
+
198
+ # Enrich the core String class with this module.
199
+ #
200
+ # @since 2.0.0
201
+ ::String.send(:include, String)
202
+ ::String.send(:extend, String::ClassMethods)
203
+ end
@@ -0,0 +1,87 @@
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module BSON
16
+
17
+ # Injects behaviour for encoding and decoding symbol values to and from
18
+ # raw bytes as specified by the BSON spec.
19
+ #
20
+ # @note Symbols are deprecated in the BSON spec, but they are still
21
+ # currently supported here for backwards compatibility.
22
+ #
23
+ # @see http://bsonspec.org/#/specification
24
+ #
25
+ # @since 2.0.0
26
+ module Symbol
27
+
28
+ # A symbol is type 0x0E in the BSON spec.
29
+ #
30
+ # @since 2.0.0
31
+ BSON_TYPE = 14.chr.force_encoding(BINARY).freeze
32
+
33
+ # Get the symbol as encoded BSON.
34
+ #
35
+ # @example Get the symbol as encoded BSON.
36
+ # :test.to_bson
37
+ #
38
+ # @return [ Symbol ] The encoded symbol.
39
+ #
40
+ # @see http://bsonspec.org/#/specification
41
+ #
42
+ # @since 2.0.0
43
+ def to_bson(encoded = ''.force_encoding(BINARY))
44
+ to_s.to_bson(encoded)
45
+ end
46
+
47
+ # Get the symbol as a BSON key name encoded C symbol.
48
+ #
49
+ # @example Get the symbol as a key name.
50
+ # :test.to_bson_key
51
+ #
52
+ # @return [ String ] The encoded symbol as a BSON key.
53
+ #
54
+ # @see http://bsonspec.org/#/specification
55
+ #
56
+ # @since 2.0.0
57
+ def to_bson_key(encoded = ''.force_encoding(BINARY))
58
+ to_s.to_bson_key(encoded)
59
+ end
60
+
61
+ module ClassMethods
62
+ # Deserialize a symbol from BSON.
63
+ #
64
+ # @param [ BSON ] bson The bson representing a symbol.
65
+ #
66
+ # @return [ Regexp ] The decoded symbol.
67
+ #
68
+ # @see http://bsonspec.org/#/specification
69
+ #
70
+ # @since 2.0.0
71
+ def from_bson(bson)
72
+ bson.read(Int32.from_bson(bson)).from_bson_string.chop!.intern
73
+ end
74
+ end
75
+
76
+ # Register this type when the module is loaded.
77
+ #
78
+ # @since 2.0.0
79
+ Registry.register(BSON_TYPE, ::Symbol)
80
+ end
81
+
82
+ # Enrich the core Symbol class with this module.
83
+ #
84
+ # @since 2.0.0
85
+ ::Symbol.send(:include, Symbol)
86
+ ::Symbol.send(:extend, Symbol::ClassMethods)
87
+ end
@@ -0,0 +1,72 @@
1
+ # Copyright (C) 2009-2013 MongoDB Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module BSON
16
+
17
+ # Injects behaviour for encoding and decoding time values to
18
+ # and from raw bytes as specified by the BSON spec.
19
+ #
20
+ # @see http://bsonspec.org/#/specification
21
+ #
22
+ # @since 2.0.0
23
+ module Time
24
+
25
+ # A time is type 0x09 in the BSON spec.
26
+ #
27
+ # @since 2.0.0
28
+ BSON_TYPE = 9.chr.force_encoding(BINARY).freeze
29
+
30
+ # Get the time as encoded BSON.
31
+ #
32
+ # @example Get the time as encoded BSON.
33
+ # Time.new(2012, 1, 1, 0, 0, 0).to_bson
34
+ #
35
+ # @return [ String ] The encoded string.
36
+ #
37
+ # @see http://bsonspec.org/#/specification
38
+ #
39
+ # @since 2.0.0
40
+ def to_bson(encoded = ''.force_encoding(BINARY))
41
+ encoded << [ (to_f * 1000.0).to_i ].pack(Int64::PACK)
42
+ end
43
+
44
+ module ClassMethods
45
+
46
+ # Deserialize UTC datetime from BSON.
47
+ #
48
+ # @param [ BSON ] bson The bson representing UTC datetime.
49
+ #
50
+ # @return [ Time ] The decoded UTC datetime.
51
+ #
52
+ # @see http://bsonspec.org/#/specification
53
+ #
54
+ # @since 2.0.0
55
+ def from_bson(bson)
56
+ seconds, fragment = Int64.from_bson(bson).divmod(1000)
57
+ at(seconds, fragment * 1000).utc
58
+ end
59
+ end
60
+
61
+ # Register this type when the module is loaded.
62
+ #
63
+ # @since 2.0.0
64
+ Registry.register(BSON_TYPE, ::Time)
65
+ end
66
+
67
+ # Enrich the core Time class with this module.
68
+ #
69
+ # @since 2.0.0
70
+ ::Time.send(:include, Time)
71
+ ::Time.send(:extend, Time::ClassMethods)
72
+ end