mongo 2.1.2 → 2.2.0.rc0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/README.md +10 -3
  5. data/Rakefile +1 -7
  6. data/lib/csasl/csasl.bundle +0 -0
  7. data/lib/mongo/auth/user/view.rb +1 -1
  8. data/lib/mongo/bulk_write.rb +7 -1
  9. data/lib/mongo/client.rb +17 -15
  10. data/lib/mongo/cluster.rb +4 -2
  11. data/lib/mongo/collection.rb +36 -2
  12. data/lib/mongo/collection/view.rb +24 -21
  13. data/lib/mongo/collection/view/aggregation.rb +5 -42
  14. data/lib/mongo/collection/view/builder.rb +20 -0
  15. data/lib/mongo/collection/view/builder/aggregation.rb +98 -0
  16. data/lib/mongo/collection/view/builder/find_command.rb +111 -0
  17. data/lib/mongo/collection/view/builder/flags.rb +62 -0
  18. data/lib/mongo/collection/view/builder/map_reduce.rb +134 -0
  19. data/lib/mongo/collection/view/builder/modifiers.rb +80 -0
  20. data/lib/mongo/collection/view/builder/op_query.rb +83 -0
  21. data/lib/mongo/collection/view/explainable.rb +15 -0
  22. data/lib/mongo/collection/view/immutable.rb +5 -12
  23. data/lib/mongo/collection/view/iterable.rb +24 -2
  24. data/lib/mongo/collection/view/map_reduce.rb +18 -27
  25. data/lib/mongo/collection/view/readable.rb +70 -112
  26. data/lib/mongo/collection/view/writable.rb +23 -7
  27. data/lib/mongo/cursor.rb +76 -25
  28. data/lib/mongo/cursor/builder.rb +18 -0
  29. data/lib/mongo/cursor/builder/get_more_command.rb +71 -0
  30. data/lib/mongo/cursor/builder/kill_cursors_command.rb +62 -0
  31. data/lib/mongo/cursor/builder/op_get_more.rb +61 -0
  32. data/lib/mongo/cursor/builder/op_kill_cursors.rb +56 -0
  33. data/lib/mongo/database.rb +2 -2
  34. data/lib/mongo/database/view.rb +9 -5
  35. data/lib/mongo/dbref.rb +3 -3
  36. data/lib/mongo/error.rb +1 -0
  37. data/lib/mongo/error/invalid_write_concern.rb +35 -0
  38. data/lib/mongo/grid/file/chunk.rb +2 -2
  39. data/lib/mongo/index/view.rb +5 -2
  40. data/lib/mongo/operation.rb +1 -0
  41. data/lib/mongo/operation/commands.rb +2 -0
  42. data/lib/mongo/operation/commands/aggregate.rb +39 -45
  43. data/lib/mongo/operation/commands/aggregate/result.rb +54 -68
  44. data/lib/mongo/operation/commands/collections_info.rb +38 -36
  45. data/lib/mongo/operation/commands/collections_info/result.rb +17 -15
  46. data/lib/mongo/operation/commands/command.rb +24 -22
  47. data/lib/mongo/operation/commands/find.rb +27 -0
  48. data/lib/mongo/operation/commands/find/result.rb +62 -0
  49. data/lib/mongo/operation/commands/get_more.rb +27 -0
  50. data/lib/mongo/operation/commands/get_more/result.rb +62 -0
  51. data/lib/mongo/operation/commands/indexes.rb +41 -39
  52. data/lib/mongo/operation/commands/list_collections.rb +25 -31
  53. data/lib/mongo/operation/commands/list_collections/result.rb +63 -81
  54. data/lib/mongo/operation/commands/list_indexes.rb +27 -35
  55. data/lib/mongo/operation/commands/list_indexes/result.rb +67 -85
  56. data/lib/mongo/operation/commands/map_reduce.rb +29 -37
  57. data/lib/mongo/operation/commands/map_reduce/result.rb +85 -88
  58. data/lib/mongo/operation/commands/parallel_scan.rb +29 -33
  59. data/lib/mongo/operation/commands/parallel_scan/result.rb +34 -42
  60. data/lib/mongo/operation/commands/user_query.rb +40 -38
  61. data/lib/mongo/operation/commands/users_info.rb +24 -29
  62. data/lib/mongo/operation/commands/users_info/result.rb +13 -11
  63. data/lib/mongo/operation/object_id_generator.rb +36 -0
  64. data/lib/mongo/operation/result.rb +30 -0
  65. data/lib/mongo/operation/specifiable.rb +35 -1
  66. data/lib/mongo/operation/write/bulk/bulkable.rb +4 -3
  67. data/lib/mongo/operation/write/bulk/delete/result.rb +18 -25
  68. data/lib/mongo/operation/write/bulk/legacy_mergable.rb +2 -2
  69. data/lib/mongo/operation/write/command/delete.rb +3 -2
  70. data/lib/mongo/operation/write/command/insert.rb +4 -2
  71. data/lib/mongo/operation/write/command/update.rb +6 -3
  72. data/lib/mongo/operation/write/gle.rb +2 -1
  73. data/lib/mongo/operation/write/idable.rb +19 -2
  74. data/lib/mongo/options/mapper.rb +22 -0
  75. data/lib/mongo/protocol/bit_vector.rb +3 -3
  76. data/lib/mongo/protocol/delete.rb +15 -5
  77. data/lib/mongo/protocol/get_more.rb +10 -5
  78. data/lib/mongo/protocol/insert.rb +1 -6
  79. data/lib/mongo/protocol/kill_cursors.rb +14 -1
  80. data/lib/mongo/protocol/message.rb +32 -8
  81. data/lib/mongo/protocol/serializers.rb +15 -16
  82. data/lib/mongo/protocol/update.rb +35 -12
  83. data/lib/mongo/server/connectable.rb +3 -1
  84. data/lib/mongo/server/connection.rb +5 -5
  85. data/lib/mongo/server/description.rb +8 -2
  86. data/lib/mongo/server/description/features.rb +2 -1
  87. data/lib/mongo/server/monitor.rb +1 -12
  88. data/lib/mongo/server/monitor/connection.rb +30 -26
  89. data/lib/mongo/server_selector/selectable.rb +21 -4
  90. data/lib/mongo/uri.rb +2 -0
  91. data/lib/mongo/version.rb +1 -1
  92. data/lib/mongo/write_concern.rb +21 -6
  93. data/mongo.gemspec +1 -2
  94. data/spec/mongo/bulk_write/ordered_combiner_spec.rb +13 -0
  95. data/spec/mongo/bulk_write_spec.rb +58 -0
  96. data/spec/mongo/client_spec.rb +6 -4
  97. data/spec/mongo/collection/view/builder/find_command_spec.rb +167 -0
  98. data/spec/mongo/collection/view/builder/flags_spec.rb +106 -0
  99. data/spec/mongo/collection/view/builder/modifiers_spec.rb +210 -0
  100. data/spec/mongo/collection/view/builder/op_query_spec.rb +154 -0
  101. data/spec/mongo/collection/view/explainable_spec.rb +1 -2
  102. data/spec/mongo/collection/view/immutable_spec.rb +3 -52
  103. data/spec/mongo/collection/view/map_reduce_spec.rb +12 -12
  104. data/spec/mongo/collection/view/readable_spec.rb +86 -80
  105. data/spec/mongo/collection/view_spec.rb +109 -703
  106. data/spec/mongo/collection_spec.rb +594 -11
  107. data/spec/mongo/command_monitoring_spec.rb +40 -27
  108. data/spec/mongo/cursor/builder/get_more_command_spec.rb +160 -0
  109. data/spec/mongo/cursor/builder/op_get_more_spec.rb +52 -0
  110. data/spec/mongo/cursor_spec.rb +10 -60
  111. data/spec/mongo/database_spec.rb +24 -3
  112. data/spec/mongo/dbref_spec.rb +4 -4
  113. data/spec/mongo/grid/file/chunk_spec.rb +1 -1
  114. data/spec/mongo/grid/fs_bucket_spec.rb +3 -3
  115. data/spec/mongo/index/view_spec.rb +41 -0
  116. data/spec/mongo/operation/{aggregate → commands/aggregate}/result_spec.rb +1 -1
  117. data/spec/mongo/operation/commands/aggregate_spec.rb +1 -1
  118. data/spec/mongo/operation/commands/collections_info_spec.rb +1 -1
  119. data/spec/mongo/operation/commands/command_spec.rb +1 -1
  120. data/spec/mongo/operation/commands/indexes_spec.rb +1 -1
  121. data/spec/mongo/operation/commands/map_reduce_spec.rb +1 -1
  122. data/spec/mongo/operation/write/command/delete_spec.rb +25 -0
  123. data/spec/mongo/operation/write/command/insert_spec.rb +25 -0
  124. data/spec/mongo/operation/write/command/update_spec.rb +25 -0
  125. data/spec/mongo/protocol/delete_spec.rb +4 -4
  126. data/spec/mongo/protocol/get_more_spec.rb +4 -4
  127. data/spec/mongo/protocol/insert_spec.rb +3 -3
  128. data/spec/mongo/protocol/kill_cursors_spec.rb +3 -3
  129. data/spec/mongo/protocol/query_spec.rb +7 -7
  130. data/spec/mongo/protocol/update_spec.rb +5 -5
  131. data/spec/mongo/server/description/features_spec.rb +25 -0
  132. data/spec/mongo/write_concern_spec.rb +126 -0
  133. data/spec/spec_helper.rb +9 -19
  134. data/spec/support/command_monitoring.rb +8 -0
  135. data/spec/support/command_monitoring/find.yml +53 -4
  136. data/spec/support/matchers.rb +1 -1
  137. data/spec/support/shared/protocol.rb +5 -5
  138. data/spec/support/travis.rb +1 -1
  139. metadata +43 -10
  140. metadata.gz.sig +0 -0
@@ -0,0 +1,20 @@
1
+ # Copyright (C) 2015 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
+ require 'mongo/collection/view/builder/aggregation'
16
+ require 'mongo/collection/view/builder/map_reduce'
17
+ require 'mongo/collection/view/builder/op_query'
18
+ require 'mongo/collection/view/builder/find_command'
19
+ require 'mongo/collection/view/builder/flags'
20
+ require 'mongo/collection/view/builder/modifiers'
@@ -0,0 +1,98 @@
1
+ # Copyright (C) 2015 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 Mongo
16
+ class Collection
17
+ class View
18
+ module Builder
19
+
20
+ # Builds an aggregation command specification from the view and options.
21
+ #
22
+ # @since 2.2.0
23
+ class Aggregation
24
+ extend Forwardable
25
+
26
+ # The mappings from ruby options to the aggregation options.
27
+ #
28
+ # @since 2.2.0
29
+ MAPPINGS = BSON::Document.new(
30
+ :allow_disk_use => 'allowDiskUse',
31
+ :max_time_ms => 'maxTimeMS',
32
+ :explain => 'explain',
33
+ :bypass_document_validation => 'bypassDocumentValidation'
34
+ ).freeze
35
+
36
+ def_delegators :@view, :collection, :database, :read
37
+
38
+ # @return [ Array<Hash> ] pipeline The pipeline.
39
+ attr_reader :pipeline
40
+
41
+ # @return [ Collection::View ] view The collection view.
42
+ attr_reader :view
43
+
44
+ # @return [ Hash ] options The map/reduce specific options.
45
+ attr_reader :options
46
+
47
+ # Initialize the builder.
48
+ #
49
+ # @example Initialize the builder.
50
+ # Aggregation.new(map, reduce, view, options)
51
+ #
52
+ # @param [ Array<Hash> ] pipeline The aggregation pipeline.
53
+ # @param [ Collection::View ] view The collection view.
54
+ # @param [ Hash ] options The map/reduce options.
55
+ #
56
+ # @since 2.2.0
57
+ def initialize(pipeline, view, options)
58
+ @pipeline = pipeline
59
+ @view = view
60
+ @options = options
61
+ end
62
+
63
+ # Get the specification to pass to the aggregation operation.
64
+ #
65
+ # @example Get the specification.
66
+ # builder.specification
67
+ #
68
+ # @return [ Hash ] The specification.
69
+ #
70
+ # @since 2.2.0
71
+ def specification
72
+ { selector: aggregation_command, db_name: database.name, read: read }
73
+ end
74
+
75
+ private
76
+
77
+ def aggregation_command
78
+ command = BSON::Document.new(:aggregate => collection.name, :pipeline => pipeline)
79
+ command[:cursor] = cursor if cursor
80
+ command[:readConcern] = collection.read_concern if collection.read_concern
81
+ command.merge!(Options::Mapper.transform_documents(options, MAPPINGS))
82
+ command
83
+ end
84
+
85
+ def cursor
86
+ if options[:use_cursor] == true || options[:use_cursor].nil?
87
+ batch_size_doc
88
+ end
89
+ end
90
+
91
+ def batch_size_doc
92
+ (value = options[:batch_size] || view.batch_size) ? { :batchSize => value } : {}
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,111 @@
1
+ # Copyright (C) 2015 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 Mongo
16
+ class Collection
17
+ class View
18
+ module Builder
19
+
20
+ # Builds a find command specification from options.
21
+ #
22
+ # @since 2.2.0
23
+ class FindCommand
24
+ extend Forwardable
25
+
26
+ # The mappings from ruby options to the find command.
27
+ #
28
+ # @since 2.2.0
29
+ MAPPINGS = BSON::Document.new(
30
+ sort: 'sort',
31
+ projection: 'projection',
32
+ hint: 'hint',
33
+ skip: 'skip',
34
+ limit: 'limit',
35
+ batch_size: 'batchSize',
36
+ single_batch: 'singleBatch',
37
+ comment: 'comment',
38
+ max_scan: 'maxScan',
39
+ max_time_ms: 'maxTimeMS',
40
+ max_value: 'max',
41
+ min_value: 'min',
42
+ return_key: 'returnKey',
43
+ show_disk_loc: 'showRecordId',
44
+ snapshot: 'snapshot',
45
+ tailable: 'tailable',
46
+ oplog_replay: 'oplogReplay',
47
+ no_cursor_timeout: 'noCursorTimeout',
48
+ await_data: 'awaitData',
49
+ allow_partial_results: 'allowPartialResults',
50
+ read_concern: 'readConcern'
51
+ ).freeze
52
+
53
+ def_delegators :@view, :collection, :database, :filter, :options, :read
54
+
55
+ # Get the specification for an explain command that wraps the find
56
+ # command.
57
+ #
58
+ # @example Get the explain spec.
59
+ # builder.explain_specification
60
+ #
61
+ # @return [ Hash ] The specification.
62
+ #
63
+ # @since 2.2.0
64
+ def explain_specification
65
+ { selector: { explain: find_command }, db_name: database.name, read: read }
66
+ end
67
+
68
+ # Create the find command builder.
69
+ #
70
+ # @example Create the find command builder.
71
+ # FindCommandBuilder.new(view)
72
+ #
73
+ # @param [ Collection::View ] view The collection view.
74
+ #
75
+ # @since 2.2.2
76
+ def initialize(view)
77
+ @view = view
78
+ end
79
+
80
+ # Get the specification to pass to the find command operation.
81
+ #
82
+ # @example Get the specification.
83
+ # builder.specification
84
+ #
85
+ # @return [ Hash ] The specification.
86
+ #
87
+ # @since 2.2.0
88
+ def specification
89
+ { selector: find_command, db_name: database.name, read: read }
90
+ end
91
+
92
+ private
93
+
94
+ def find_command
95
+ document = BSON::Document.new('find' => collection.name, 'filter' => filter)
96
+ command = Options::Mapper.transform_documents(options, MAPPINGS, document)
97
+ convert_negative_limit(command)
98
+ end
99
+
100
+ def convert_negative_limit(command)
101
+ if command[:limit] && command[:limit] < 0
102
+ command.delete('limit')
103
+ command[:singleBatch] = true
104
+ end
105
+ command
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,62 @@
1
+ # Copyright (C) 2015 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 Mongo
16
+ class Collection
17
+ class View
18
+ module Builder
19
+
20
+ # Provides behaviour for mapping flags.
21
+ #
22
+ # @since 2.2.0
23
+ module Flags
24
+ extend self
25
+
26
+ # Options to cursor flags mapping.
27
+ #
28
+ # @since 2.2.0
29
+ MAPPINGS = {
30
+ :allow_partial_results => [ :partial ],
31
+ :oplog_replay => [ :oplog_replay ],
32
+ :no_cursor_timeout => [ :no_cursor_timeout ],
33
+ :tailable => [ :tailable_cursor ],
34
+ :tailable_await => [ :await_data, :tailable_cursor],
35
+ :await_data => [ :await_data ],
36
+ :exhaust => [ :exhaust ]
37
+ }.freeze
38
+
39
+ # Maps an array of flags from the provided options.
40
+ #
41
+ # @example Map the flags.
42
+ # Flags.map_flags(options)
43
+ #
44
+ # @param [ Hash, BSON::Document ] options The options.
45
+ #
46
+ # @return [ Array<Symbol> ] The flags.
47
+ #
48
+ # @since 2.2.0
49
+ def map_flags(options)
50
+ MAPPINGS.each.reduce(options[:flags] || []) do |flags, (key, value)|
51
+ cursor_type = options[:cursor_type]
52
+ if options[key] || (cursor_type && cursor_type == key)
53
+ flags.push(*value)
54
+ end
55
+ flags
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,134 @@
1
+ # Copyright (C) 2015 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 Mongo
16
+ class Collection
17
+ class View
18
+ module Builder
19
+
20
+ # Builds a map/reduce specification from the view and options.
21
+ #
22
+ # @since 2.2.0
23
+ class MapReduce
24
+ extend Forwardable
25
+
26
+ # The mappings from ruby options to the map/reduce options.
27
+ #
28
+ # @since 2.2.0
29
+ MAPPINGS = BSON::Document.new(
30
+ finalize: 'finalize',
31
+ js_mode: 'jsMode',
32
+ out: 'out',
33
+ scope: 'scope',
34
+ verbose: 'verbose',
35
+ bypass_document_validation: 'bypassDocumentValidation'
36
+ ).freeze
37
+
38
+ def_delegators :@view, :collection, :database, :filter, :read
39
+
40
+ # @return [ String ] map The map function.
41
+ attr_reader :map
42
+
43
+ # @return [ String ] reduce The reduce function.
44
+ attr_reader :reduce
45
+
46
+ # @return [ Collection::View ] view The collection view.
47
+ attr_reader :view
48
+
49
+ # @return [ Hash ] options The map/reduce specific options.
50
+ attr_reader :options
51
+
52
+ # Initialize the builder.
53
+ #
54
+ # @example Initialize the builder.
55
+ # MapReduce.new(map, reduce, view, options)
56
+ #
57
+ # @param [ String ] map The map function.
58
+ # @param [ String ] reduce The reduce function.
59
+ # @param [ Collection::View ] view The collection view.
60
+ # @param [ Hash ] options The map/reduce options.
61
+ #
62
+ # @since 2.2.0
63
+ def initialize(map, reduce, view, options)
64
+ @map = map
65
+ @reduce = reduce
66
+ @view = view
67
+ @options = options
68
+ end
69
+
70
+ # Get the specification for issuing a find command on the map/reduce
71
+ # results.
72
+ #
73
+ # @example Get the command specification.
74
+ # builder.command_specification
75
+ #
76
+ # @return [ Hash ] The specification.
77
+ #
78
+ # @since 2.2.0
79
+ def command_specification
80
+ { selector: find_command, db_name: database.name, read: read }
81
+ end
82
+
83
+ # Get the specification for the document query after a map/reduce.
84
+ #
85
+ # @example Get the query specification.
86
+ # builder.query_specification
87
+ #
88
+ # @return [ Hash ] The specification.
89
+ #
90
+ # @since 2.2.0
91
+ def query_specification
92
+ { selector: {}, options: {}, db_name: database.name, coll_name: query_collection }
93
+ end
94
+
95
+ # Get the specification to pass to the map/reduce operation.
96
+ #
97
+ # @example Get the specification.
98
+ # builder.specification
99
+ #
100
+ # @return [ Hash ] The specification.
101
+ #
102
+ # @since 2.2.0
103
+ def specification
104
+ { selector: map_reduce_command, db_name: database.name, read: read }
105
+ end
106
+
107
+ private
108
+
109
+ def find_command
110
+ BSON::Document.new('find' => query_collection, 'filter' => {})
111
+ end
112
+
113
+ def map_reduce_command
114
+ command = BSON::Document.new(
115
+ :mapreduce => collection.name,
116
+ :map => map,
117
+ :reduce => reduce,
118
+ :query => filter,
119
+ :out => { inline: 1 }
120
+ )
121
+ command[:readConcern] = collection.read_concern if collection.read_concern
122
+ command.merge!(Options::Mapper.transform_documents(options, MAPPINGS))
123
+ command.merge!(view.options)
124
+ command
125
+ end
126
+
127
+ def query_collection
128
+ options[:out].respond_to?(:keys) ? options[:out].values.first : options[:out]
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,80 @@
1
+ # Copyright (C) 2015 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 Mongo
16
+ class Collection
17
+ class View
18
+ module Builder
19
+
20
+ # Provides behaviour for mapping modifiers.
21
+ #
22
+ # @since 2.2.0
23
+ module Modifiers
24
+ extend self
25
+
26
+ # Mappings from driver options to legacy server values.
27
+ #
28
+ # @since 2.2.0
29
+ DRIVER_MAPPINGS = BSON::Document.new(
30
+ sort: '$orderby',
31
+ hint: '$hint',
32
+ comment: '$comment',
33
+ snapshot: '$snapshot',
34
+ max_scan: '$maxScan',
35
+ max_value: '$max',
36
+ min_value: '$min',
37
+ max_time_ms: '$maxTimeMS',
38
+ return_key: '$returnKey',
39
+ show_disk_loc: '$showDiskLoc',
40
+ explain: '$explain'
41
+ ).freeze
42
+
43
+ # Mappings from server values to driver options.
44
+ #
45
+ # @since 2.2.0
46
+ SERVER_MAPPINGS = BSON::Document.new(DRIVER_MAPPINGS.invert).freeze
47
+
48
+ # Transform the provided server modifiers to driver options.
49
+ #
50
+ # @example Transform to driver options.
51
+ # Modifiers.map_driver_options(modifiers)
52
+ #
53
+ # @param [ Hash ] modifiers The modifiers.
54
+ #
55
+ # @return [ BSON::Document ] The driver options.
56
+ #
57
+ # @since 2.2.0
58
+ def self.map_driver_options(modifiers)
59
+ Options::Mapper.transform_documents(modifiers, SERVER_MAPPINGS)
60
+ end
61
+
62
+ # Transform the provided options into a document of only server
63
+ # modifiers.
64
+ #
65
+ # @example Map the server modifiers.
66
+ # Modifiers.map_server_modifiers(options)
67
+ #
68
+ # @param [ Hash, BSON::Document ] options The options.
69
+ #
70
+ # @return [ BSON::Document ] The modifiers.
71
+ #
72
+ # @since 2.2.0
73
+ def self.map_server_modifiers(options)
74
+ Options::Mapper.transform_documents(options, DRIVER_MAPPINGS)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end