riak-client-noenc 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/.gitignore +42 -0
  4. data/.rspec +1 -0
  5. data/Gemfile +17 -0
  6. data/Guardfile +20 -0
  7. data/LICENSE.md +16 -0
  8. data/README.markdown +640 -0
  9. data/RELEASE_NOTES.md +392 -0
  10. data/Rakefile +119 -0
  11. data/lib/riak.rb +22 -0
  12. data/lib/riak/bucket.rb +297 -0
  13. data/lib/riak/bucket_properties.rb +74 -0
  14. data/lib/riak/bucket_type.rb +77 -0
  15. data/lib/riak/bucket_typed/bucket.rb +121 -0
  16. data/lib/riak/client.rb +433 -0
  17. data/lib/riak/client/beefcake/bucket_properties_operator.rb +178 -0
  18. data/lib/riak/client/beefcake/crdt/counter_loader.rb +18 -0
  19. data/lib/riak/client/beefcake/crdt/map_loader.rb +64 -0
  20. data/lib/riak/client/beefcake/crdt/set_loader.rb +18 -0
  21. data/lib/riak/client/beefcake/crdt_loader.rb +84 -0
  22. data/lib/riak/client/beefcake/crdt_operator.rb +223 -0
  23. data/lib/riak/client/beefcake/footer +4 -0
  24. data/lib/riak/client/beefcake/header +6 -0
  25. data/lib/riak/client/beefcake/message_codes.rb +89 -0
  26. data/lib/riak/client/beefcake/message_overlay.rb +87 -0
  27. data/lib/riak/client/beefcake/messages.rb +772 -0
  28. data/lib/riak/client/beefcake/object_methods.rb +112 -0
  29. data/lib/riak/client/beefcake/protocol.rb +105 -0
  30. data/lib/riak/client/beefcake/socket.rb +260 -0
  31. data/lib/riak/client/beefcake_protobuffs_backend.rb +538 -0
  32. data/lib/riak/client/decaying.rb +36 -0
  33. data/lib/riak/client/feature_detection.rb +120 -0
  34. data/lib/riak/client/instrumentation.rb +19 -0
  35. data/lib/riak/client/node.rb +49 -0
  36. data/lib/riak/client/protobuffs_backend.rb +143 -0
  37. data/lib/riak/client/search.rb +27 -0
  38. data/lib/riak/client/yokozuna.rb +52 -0
  39. data/lib/riak/conflict.rb +13 -0
  40. data/lib/riak/core_ext.rb +7 -0
  41. data/lib/riak/core_ext/blank.rb +53 -0
  42. data/lib/riak/core_ext/deep_dup.rb +13 -0
  43. data/lib/riak/core_ext/extract_options.rb +7 -0
  44. data/lib/riak/core_ext/json.rb +15 -0
  45. data/lib/riak/core_ext/slice.rb +18 -0
  46. data/lib/riak/core_ext/stringify_keys.rb +10 -0
  47. data/lib/riak/core_ext/symbolize_keys.rb +10 -0
  48. data/lib/riak/core_ext/to_param.rb +31 -0
  49. data/lib/riak/counter.rb +101 -0
  50. data/lib/riak/crdt.rb +21 -0
  51. data/lib/riak/crdt/base.rb +183 -0
  52. data/lib/riak/crdt/batch_counter.rb +19 -0
  53. data/lib/riak/crdt/batch_map.rb +41 -0
  54. data/lib/riak/crdt/counter.rb +82 -0
  55. data/lib/riak/crdt/inner_counter.rb +81 -0
  56. data/lib/riak/crdt/inner_flag.rb +42 -0
  57. data/lib/riak/crdt/inner_map.rb +75 -0
  58. data/lib/riak/crdt/inner_register.rb +26 -0
  59. data/lib/riak/crdt/inner_set.rb +102 -0
  60. data/lib/riak/crdt/map.rb +121 -0
  61. data/lib/riak/crdt/operation.rb +19 -0
  62. data/lib/riak/crdt/set.rb +166 -0
  63. data/lib/riak/crdt/typed_collection.rb +181 -0
  64. data/lib/riak/encoding.rb +6 -0
  65. data/lib/riak/errors/backend_creation.rb +9 -0
  66. data/lib/riak/errors/base.rb +9 -0
  67. data/lib/riak/errors/connection_error.rb +50 -0
  68. data/lib/riak/errors/crdt_error.rb +38 -0
  69. data/lib/riak/errors/failed_request.rb +58 -0
  70. data/lib/riak/errors/protobuffs_error.rb +11 -0
  71. data/lib/riak/errors/search_error.rb +35 -0
  72. data/lib/riak/i18n.rb +7 -0
  73. data/lib/riak/index_collection.rb +71 -0
  74. data/lib/riak/instrumentation.rb +6 -0
  75. data/lib/riak/json.rb +52 -0
  76. data/lib/riak/link.rb +96 -0
  77. data/lib/riak/list_buckets.rb +28 -0
  78. data/lib/riak/locale/en.yml +107 -0
  79. data/lib/riak/locale/fr.yml +51 -0
  80. data/lib/riak/map_reduce.rb +295 -0
  81. data/lib/riak/map_reduce/filter_builder.rb +103 -0
  82. data/lib/riak/map_reduce/phase.rb +98 -0
  83. data/lib/riak/map_reduce/results.rb +49 -0
  84. data/lib/riak/map_reduce_error.rb +7 -0
  85. data/lib/riak/multiget.rb +122 -0
  86. data/lib/riak/preflist_item.rb +7 -0
  87. data/lib/riak/rcontent.rb +173 -0
  88. data/lib/riak/robject.rb +222 -0
  89. data/lib/riak/search.rb +11 -0
  90. data/lib/riak/search/index.rb +87 -0
  91. data/lib/riak/search/query.rb +141 -0
  92. data/lib/riak/search/result_collection.rb +144 -0
  93. data/lib/riak/search/result_document.rb +129 -0
  94. data/lib/riak/search/schema.rb +65 -0
  95. data/lib/riak/secondary_index.rb +81 -0
  96. data/lib/riak/serializers.rb +73 -0
  97. data/lib/riak/stamp.rb +77 -0
  98. data/lib/riak/util/escape.rb +80 -0
  99. data/lib/riak/util/tcp_socket_extensions.rb +58 -0
  100. data/lib/riak/util/translation.rb +18 -0
  101. data/lib/riak/version.rb +3 -0
  102. data/lib/riak/walk_spec.rb +145 -0
  103. data/spec/failover/failover.rb +59 -0
  104. data/spec/fixtures/bitcask.txt +25 -0
  105. data/spec/fixtures/cat.jpg +0 -0
  106. data/spec/fixtures/multipart-basic-conflict.txt +15 -0
  107. data/spec/fixtures/multipart-blank.txt +7 -0
  108. data/spec/fixtures/multipart-mapreduce.txt +10 -0
  109. data/spec/fixtures/multipart-with-body.txt +16 -0
  110. data/spec/fixtures/multipart-with-marked-tombstones.txt +17 -0
  111. data/spec/fixtures/multipart-with-unmarked-tombstone.txt +16 -0
  112. data/spec/fixtures/server.cert.crt +15 -0
  113. data/spec/fixtures/server.cert.key +15 -0
  114. data/spec/fixtures/test.pem +1 -0
  115. data/spec/fixtures/yz_schema_template.xml +18 -0
  116. data/spec/integration/riak/bucket_types_spec.rb +270 -0
  117. data/spec/integration/riak/conflict_resolution_spec.rb +96 -0
  118. data/spec/integration/riak/counters_spec.rb +36 -0
  119. data/spec/integration/riak/crdt/configuration_spec.rb +37 -0
  120. data/spec/integration/riak/crdt_search_spec.rb +176 -0
  121. data/spec/integration/riak/crdt_spec.rb +250 -0
  122. data/spec/integration/riak/crdt_validation/map_spec.rb +63 -0
  123. data/spec/integration/riak/crdt_validation/set_spec.rb +122 -0
  124. data/spec/integration/riak/preflist_spec.rb +31 -0
  125. data/spec/integration/riak/properties_spec.rb +69 -0
  126. data/spec/integration/riak/protobuffs/interrupted_request_spec.rb +33 -0
  127. data/spec/integration/riak/protobuffs_backends_spec.rb +40 -0
  128. data/spec/integration/riak/search_spec.rb +104 -0
  129. data/spec/integration/riak/secondary_index_spec.rb +72 -0
  130. data/spec/integration/riak/security_spec.rb +100 -0
  131. data/spec/integration/riak/threading_spec.rb +150 -0
  132. data/spec/integration/yokozuna/index_spec.rb +61 -0
  133. data/spec/integration/yokozuna/queries_spec.rb +115 -0
  134. data/spec/integration/yokozuna/schema_spec.rb +49 -0
  135. data/spec/riak/beefcake_protobuffs_backend/bucket_properties_operator_spec.rb +247 -0
  136. data/spec/riak/beefcake_protobuffs_backend/crdt_operator_spec.rb +222 -0
  137. data/spec/riak/beefcake_protobuffs_backend/object_methods_spec.rb +23 -0
  138. data/spec/riak/beefcake_protobuffs_backend/protocol_spec.rb +189 -0
  139. data/spec/riak/beefcake_protobuffs_backend_spec.rb +162 -0
  140. data/spec/riak/bucket_properties_spec.rb +135 -0
  141. data/spec/riak/bucket_spec.rb +275 -0
  142. data/spec/riak/bucket_type_spec.rb +50 -0
  143. data/spec/riak/bucket_typed/bucket_spec.rb +62 -0
  144. data/spec/riak/client_spec.rb +246 -0
  145. data/spec/riak/core_ext/to_param_spec.rb +15 -0
  146. data/spec/riak/counter_spec.rb +122 -0
  147. data/spec/riak/crdt/counter_spec.rb +55 -0
  148. data/spec/riak/crdt/inner_counter_spec.rb +21 -0
  149. data/spec/riak/crdt/inner_flag_spec.rb +39 -0
  150. data/spec/riak/crdt/inner_map_spec.rb +47 -0
  151. data/spec/riak/crdt/inner_register_spec.rb +40 -0
  152. data/spec/riak/crdt/inner_set_spec.rb +33 -0
  153. data/spec/riak/crdt/map_spec.rb +78 -0
  154. data/spec/riak/crdt/set_spec.rb +61 -0
  155. data/spec/riak/crdt/shared_examples.rb +74 -0
  156. data/spec/riak/crdt/typed_collection_spec.rb +225 -0
  157. data/spec/riak/escape_spec.rb +72 -0
  158. data/spec/riak/feature_detection_spec.rb +77 -0
  159. data/spec/riak/index_collection_spec.rb +53 -0
  160. data/spec/riak/instrumentation_spec.rb +124 -0
  161. data/spec/riak/link_spec.rb +85 -0
  162. data/spec/riak/list_buckets_spec.rb +41 -0
  163. data/spec/riak/map_reduce/filter_builder_spec.rb +32 -0
  164. data/spec/riak/map_reduce/phase_spec.rb +142 -0
  165. data/spec/riak/map_reduce_spec.rb +434 -0
  166. data/spec/riak/multiget_spec.rb +81 -0
  167. data/spec/riak/node_spec.rb +26 -0
  168. data/spec/riak/robject_spec.rb +496 -0
  169. data/spec/riak/search/index_spec.rb +72 -0
  170. data/spec/riak/search/query_spec.rb +88 -0
  171. data/spec/riak/search/result_collection_spec.rb +89 -0
  172. data/spec/riak/search/result_document_spec.rb +106 -0
  173. data/spec/riak/search/schema_spec.rb +63 -0
  174. data/spec/riak/search_spec.rb +107 -0
  175. data/spec/riak/secondary_index_spec.rb +225 -0
  176. data/spec/riak/serializers_spec.rb +121 -0
  177. data/spec/riak/stamp_spec.rb +54 -0
  178. data/spec/riak/walk_spec_spec.rb +203 -0
  179. data/spec/spec_helper.rb +66 -0
  180. data/spec/support/certs/README.md +13 -0
  181. data/spec/support/certs/ca.crt +21 -0
  182. data/spec/support/certs/client.crl +13 -0
  183. data/spec/support/certs/client.crt +94 -0
  184. data/spec/support/certs/client.csr +18 -0
  185. data/spec/support/certs/client.key +27 -0
  186. data/spec/support/certs/empty_ca.crt +21 -0
  187. data/spec/support/certs/server.crl +13 -0
  188. data/spec/support/certs/server.crt +94 -0
  189. data/spec/support/certs/server.key +27 -0
  190. data/spec/support/crdt_search_config.rb +112 -0
  191. data/spec/support/crdt_search_fixtures.rb +42 -0
  192. data/spec/support/integration_setup.rb +10 -0
  193. data/spec/support/search_config.rb +83 -0
  194. data/spec/support/search_corpus_setup.rb +39 -0
  195. data/spec/support/test_client.rb +46 -0
  196. data/spec/support/test_client.yml.example +10 -0
  197. data/spec/support/unified_backend_examples.rb +380 -0
  198. data/spec/support/version_filter.rb +12 -0
  199. data/spec/support/wait_until.rb +20 -0
  200. metadata +511 -0
@@ -0,0 +1,103 @@
1
+
2
+ require 'riak/util/translation'
3
+
4
+ module Riak
5
+ class MapReduce
6
+ # Builds key-filter lists for MapReduce inputs in a DSL-like fashion.
7
+ class FilterBuilder
8
+ include Util::Translation
9
+
10
+ # Known filters available in riak_kv_mapred_filters, mapped to
11
+ # their arities. These are turned into instance methods.
12
+ # Example:
13
+ #
14
+ # FilterBuilder.new do
15
+ # string_to_int
16
+ # less_than 50
17
+ # end
18
+ FILTERS = {
19
+ :int_to_string => 0,
20
+ :string_to_int => 0,
21
+ :float_to_string => 0,
22
+ :string_to_float => 0,
23
+ :to_upper => 0,
24
+ :to_lower => 0,
25
+ :tokenize => 2,
26
+ :urldecode => 0,
27
+ :greater_than => 1,
28
+ :less_than => 1,
29
+ :greater_than_eq => 1,
30
+ :less_than_eq => 1,
31
+ :between => [2, 3],
32
+ :matches => 1,
33
+ :neq => 1,
34
+ :eq => 1,
35
+ :set_member => -1,
36
+ :similar_to => 2,
37
+ :starts_with => 1,
38
+ :ends_with => 1
39
+ }
40
+
41
+ # Available logical operations for joining filter chains. These
42
+ # are turned into instance methods with leading underscores,
43
+ # with aliases to uppercase versions.
44
+ # Example:
45
+ #
46
+ # FilterBuilder.new do
47
+ # string_to_int
48
+ # AND do
49
+ # greater_than_eq 50
50
+ # neq 100
51
+ # end
52
+ # end
53
+ LOGICAL_OPERATIONS = %w{and or not}
54
+
55
+ FILTERS.each do |f, arity|
56
+ arities = [arity].flatten
57
+
58
+ define_method(f) { |*args|
59
+ unless arities.include?(-1) or arities.include?(args.size)
60
+ raise ArgumentError.new t("filter_arity_mismatch",
61
+ :filter => f,
62
+ :expected => arities,
63
+ :received => args.size
64
+ )
65
+ end
66
+
67
+ @filters << [f, *args]
68
+ }
69
+ end
70
+
71
+ LOGICAL_OPERATIONS.each do |op|
72
+ # NB: string eval is needed here because in ruby 1.8, blocks can't yield to
73
+ # other blocks
74
+ class_eval <<-CODE
75
+ def _#{op}(&block)
76
+ raise ArgumentError.new(t('filter_needs_block', :filter => '#{op}')) unless block_given?
77
+ @filters << [:#{op}, self.class.new(&block).to_a]
78
+ end
79
+ alias :#{op.to_s.upcase} :_#{op}
80
+ CODE
81
+ end
82
+
83
+ # Creates a new FilterBuilder. Pass a block that will be
84
+ # instance_eval'ed to construct the sequence of filters.
85
+ def initialize(&block)
86
+ @filters = []
87
+ instance_eval(&block) if block_given?
88
+ end
89
+
90
+ # Wraps multi-step filters for use inside logical
91
+ # operations. Does not correspond to an actual filter.
92
+ def sequence(&block)
93
+ @filters << self.class.new(&block).to_a
94
+ end
95
+ alias :seq :sequence
96
+
97
+ # @return A list of filters for handing to the MapReduce inputs.
98
+ def to_a
99
+ @filters
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,98 @@
1
+
2
+ require 'riak/json'
3
+ require 'riak/util/translation'
4
+ require 'riak/walk_spec'
5
+
6
+ module Riak
7
+ class MapReduce
8
+ # Represents an individual phase in a map-reduce pipeline. Generally you'll want to call
9
+ # methods of MapReduce instead of using this directly.
10
+ class Phase
11
+ include Util::Translation
12
+ # @return [Symbol] the type of phase - :map, :reduce, or :link
13
+ attr_accessor :type
14
+
15
+ # @return [String, Array<String, String>, Hash, WalkSpec] For :map and :reduce types, the Javascript function to run (as a string or hash with bucket/key), or the module + function in Erlang to run. For a :link type, a {Riak::WalkSpec} or an equivalent hash.
16
+ attr_accessor :function
17
+
18
+ # @return [String] the language of the phase's function - "javascript" or "erlang". Meaningless for :link type phases.
19
+ attr_accessor :language
20
+
21
+ # @return [Boolean] whether results of this phase will be returned
22
+ attr_accessor :keep
23
+
24
+ # @return [Array] any extra static arguments to pass to the phase
25
+ attr_accessor :arg
26
+
27
+ # Creates a phase in the map-reduce pipeline
28
+ # @param [Hash] options options for the phase
29
+ # @option options [Symbol] :type one of :map, :reduce, :link
30
+ # @option options [String] :language ("javascript") "erlang" or "javascript"
31
+ # @option options [String, Array, Hash] :function In the case of Javascript, a literal function in a string, or a hash with :bucket and :key. In the case of Erlang, an Array of [module, function]. For a :link phase, a hash including any of :bucket, :tag or a WalkSpec.
32
+ # @option options [Boolean] :keep (false) whether to return the results of this phase
33
+ # @option options [Array] :arg (nil) any extra static arguments to pass to the phase
34
+ def initialize(options = {})
35
+ self.type = options[:type]
36
+ self.language = options[:language] || "javascript"
37
+ self.function = options[:function]
38
+ self.keep = options[:keep] || false
39
+ self.arg = options[:arg]
40
+ end
41
+
42
+ def type=(value)
43
+ raise ArgumentError, t("invalid_phase_type") unless value.to_s =~ /^(map|reduce|link)$/i
44
+ @type = value.to_s.downcase.to_sym
45
+ end
46
+
47
+ def function=(value)
48
+ case value
49
+ when Array
50
+ raise ArgumentError, t("module_function_pair_required") unless value.size == 2
51
+ @language = "erlang"
52
+ when Hash
53
+ raise ArgumentError, t("stored_function_invalid") unless type == :link || value.has_key?(:bucket) && value.has_key?(:key)
54
+ @language = "javascript"
55
+ when String
56
+ @language = (value =~ /\s*fun\s*\(.*\)\s*->/) ? "erlang" : "javascript"
57
+ when WalkSpec
58
+ raise ArgumentError, t("walk_spec_invalid_unless_link") unless type == :link
59
+ else
60
+ raise ArgumentError, t("invalid_function_value", :value => value.inspect)
61
+ end
62
+ @function = value
63
+ end
64
+
65
+ # Converts the phase to JSON for use while invoking a job.
66
+ # @return [String] a JSON representation of the phase
67
+ def to_json(*a)
68
+ as_json.to_json(*a)
69
+ end
70
+
71
+ # Converts the phase to its JSON-compatible representation for job invocation.
72
+ # @return [Hash] a Hash-equivalent of the phase
73
+ def as_json(options = nil)
74
+ obj = case type
75
+ when :map, :reduce
76
+ defaults = {"language" => language, "keep" => keep}
77
+ case function
78
+ when Hash
79
+ defaults.merge(function)
80
+ when String
81
+ if function =~ /\s*function\s*\(/ || function =~ /\s*fun\s*\(.*\)\s*->/
82
+ defaults.merge("source" => function)
83
+ else
84
+ defaults.merge("name" => function)
85
+ end
86
+ when Array
87
+ defaults.merge("module" => function[0], "function" => function[1])
88
+ end
89
+ when :link
90
+ spec = WalkSpec.normalize(function).first
91
+ {"bucket" => spec.bucket, "tag" => spec.tag, "keep" => spec.keep || keep}
92
+ end
93
+ obj["arg"] = arg if arg
94
+ { type => obj }
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,49 @@
1
+ module Riak
2
+ class MapReduce
3
+ # @api private
4
+ # Collects and normalizes results from MapReduce requests
5
+ class Results
6
+ # Creates a new result collector
7
+ # @param [MapReduce] mr the MapReduce query
8
+ def initialize(mr)
9
+ @keep_count = mr.query.select {|p| p.keep }.size
10
+ @hash = create_results_hash(mr.query)
11
+ end
12
+
13
+ # Adds a new result to the collector
14
+ # @param [Fixnum] phase the phase index
15
+ # @param [Array] result the phase result
16
+ def add(phase, result)
17
+ @hash[phase] += result
18
+ end
19
+
20
+ # Coalesces the query results
21
+ # @return [Array] the query results, coalesced according to the
22
+ # phase configuration
23
+ def report
24
+ if @keep_count > 1
25
+ @hash.to_a.sort.map {|(num, results)| results }
26
+ else
27
+ @hash[@hash.keys.first]
28
+ end
29
+ end
30
+
31
+ private
32
+ def create_results_hash(query)
33
+ # When the query is empty, only bucket/key pairs are returned,
34
+ # but implicitly in phase 0.
35
+ return { 0 => [] } if query.empty?
36
+
37
+ # Pre-populate the hash with empty results for kept phases.
38
+ # Additionally, the last phase is always implictly kept, even
39
+ # when keep is false.
40
+ query.inject({}) do |hash, phase|
41
+ if phase.keep || query[-1] == phase
42
+ hash[query.index(phase)] = []
43
+ end
44
+ hash
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,7 @@
1
+ require 'riak/util/translation'
2
+
3
+ module Riak
4
+ # Raised when an error occurred in the Javascript map-reduce chain.
5
+ # The message will be the body of the JSON error response.
6
+ class MapReduceError < StandardError; end
7
+ end
@@ -0,0 +1,122 @@
1
+ require 'riak/client'
2
+ require 'riak/bucket'
3
+
4
+ module Riak
5
+ # Coordinates a parallel fetch operation for multiple values.
6
+ class Multiget
7
+ include Util::Translation
8
+
9
+ # @return [Riak::Client] the associated client
10
+ attr_reader :client
11
+
12
+ # @return [Array<Bucket, String>] fetch_list an {Array} of {Bucket} and {String} keys to fetch
13
+ attr_reader :fetch_list
14
+
15
+ # @return [Hash<fetch_list_entry, RObject] result_hash a {Hash} of {Bucket} and {String} key pairs to {RObject} instances
16
+ attr_accessor :result_hash
17
+
18
+ # @return [Boolean] finished if the fetch operation has completed
19
+ attr_reader :finished
20
+
21
+ # @return [Integer] The number of threads to use
22
+ attr_accessor :thread_count
23
+
24
+ # Perform a Riak Multiget operation.
25
+ # @param [Client] client the {Riak::Client} that will perform the multiget
26
+ # @param [Array<Bucket, String>] fetch_list an {Array} of {Bucket} and {String} keys to fetch
27
+ # @return [Hash<fetch_list_entry, RObject] result_hash a {Hash} of {Bucket} and {String} key pairs to {RObject} instances
28
+ def self.get_all(client, fetch_list)
29
+ multi = new client, fetch_list
30
+ multi.fetch
31
+ multi.results
32
+ end
33
+
34
+ # Create a Riak Multiget operation.
35
+ # @param [Client] client the {Riak::Client} that will perform the multiget
36
+ # @param [Array<Bucket, String>] fetch_list an {Array} of {Bucket} and {String} keys to fetch
37
+ def initialize(client, fetch_list)
38
+ raise ArgumentError, t('client_type', :client => client.inspect) unless client.is_a? Riak::Client
39
+ raise ArgumentError, t('array_type', :array => fetch_list.inspect) unless fetch_list.is_a? Array
40
+
41
+ validate_fetch_list fetch_list
42
+ @client, @fetch_list = client, fetch_list.uniq
43
+ self.result_hash = Hash.new
44
+ @finished = false
45
+ self.thread_count = client.multiget_threads
46
+ end
47
+
48
+ # Starts the parallelized fetch operation
49
+ # @raise [ArgumentError] when a non-positive-Integer count is given
50
+ def fetch
51
+ queue = fetch_list.dup
52
+ queue_mutex = Mutex.new
53
+ result_mutex = Mutex.new
54
+
55
+ unless thread_count.is_a?(Integer) && thread_count > 0
56
+ raise ArgumentError, t("invalid_multiget_thread_count")
57
+ end
58
+
59
+ @threads = 1.upto(thread_count).map do |_node|
60
+ Thread.new do
61
+ loop do
62
+ pair = queue_mutex.synchronize do
63
+ queue.shift
64
+ end
65
+
66
+ break if pair.nil?
67
+
68
+ found = attempt_fetch(*pair)
69
+ result_mutex.synchronize do
70
+ result_hash[pair] = found
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ def results
78
+ wait_for_finish
79
+ result_hash
80
+ end
81
+
82
+ def finished?
83
+ set_finished_for_thread_liveness
84
+ finished
85
+ end
86
+
87
+ def wait_for_finish
88
+ return if finished?
89
+ @threads.each {|t| t.join }
90
+ @finished = true
91
+ end
92
+
93
+ private
94
+
95
+ def attempt_fetch(bucket, key)
96
+ bucket[key]
97
+ rescue Riak::FailedRequest => e
98
+ raise e unless e.not_found?
99
+ nil
100
+ end
101
+
102
+ def set_finished_for_thread_liveness
103
+ return if @finished # already done
104
+
105
+ all_dead = @threads.none? {|t| t.alive? }
106
+ return unless all_dead # still working
107
+
108
+ @finished = true
109
+ return
110
+ end
111
+
112
+ def validate_fetch_list(fetch_list)
113
+ return unless erroneous = fetch_list.detect do |e|
114
+ bucket, key = e
115
+ next true unless bucket.is_a? Bucket
116
+ next true unless key.is_a? String
117
+ end
118
+
119
+ raise ArgumentError, t('fetch_list_type', :problem => erroneous)
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,7 @@
1
+ require 'riak'
2
+
3
+ module Riak
4
+ # @abstract Parent module for preflist items
5
+ module PreflistItem
6
+ end
7
+ end
@@ -0,0 +1,173 @@
1
+ require 'set'
2
+ require 'time'
3
+ require 'yaml'
4
+ require 'forwardable'
5
+ require 'riak/util/translation'
6
+ require 'riak/serializers'
7
+
8
+ module Riak
9
+ # Represents single (potentially-conflicted) value stored against a
10
+ # key in the Riak database. This includes the raw value as well as
11
+ # metadata.
12
+ # @since 1.1.0
13
+ class RContent
14
+ extend Forwardable
15
+ include Util::Translation
16
+
17
+ # @return [String] the MIME content type of the value
18
+ attr_accessor :content_type
19
+
20
+ # @return [Set<Link>] a Set of {Riak::Link} objects for relationships between this object and other resources
21
+ attr_accessor :links
22
+
23
+ # @return [String] the ETag header from the most recent HTTP response, useful for caching and reloading
24
+ attr_accessor :etag
25
+
26
+ # @return [Time] the Last-Modified header from the most recent HTTP response, useful for caching and reloading
27
+ attr_accessor :last_modified
28
+
29
+ # @return [Hash] a hash of any X-Riak-Meta-* headers that were in the HTTP response, keyed on the trailing portion
30
+ attr_accessor :meta
31
+
32
+ # @return [Hash<Set>] a hash of secondary indexes, where the
33
+ # key is the index name and the value is a Set of index
34
+ # entries for that index
35
+ attr_accessor :indexes
36
+
37
+ # @return [Riak::RObject] the RObject to which this sibling belongs
38
+ attr_accessor :robject
39
+
40
+ def_delegators :robject, :bucket, :key, :vclock
41
+
42
+ # Creates a new object value. This should not normally need to be
43
+ # called by users of the client. Normal, single-value use can rely
44
+ # on the delegating accessors on {Riak::RObject}.
45
+ # @param [RObject] robject the object that this value belongs to
46
+ # @yield self the new RContent
47
+ def initialize(robject)
48
+ @robject = robject
49
+ @links, @meta = Set.new, {}
50
+ @indexes = new_index_hash
51
+ yield self if block_given?
52
+ end
53
+
54
+ def indexes=(hash)
55
+ @indexes = hash.inject(new_index_hash) do |h, (k, v)|
56
+ h[k].merge([*v])
57
+ h
58
+ end
59
+ end
60
+
61
+ # @return [Object] the unmarshaled form of {#raw_data} stored in riak at this object's key
62
+ def data
63
+ if @raw_data && !@data
64
+ raw = @raw_data.respond_to?(:read) ? @raw_data.read : @raw_data
65
+ @data = deserialize(raw)
66
+ @raw_data = nil
67
+ end
68
+ @data
69
+ end
70
+
71
+ # @param [Object] new_data unmarshaled form of the data to be stored in
72
+ # Riak. Object will be serialized using {#serialize} if a known
73
+ # content_type is used. Setting this overrides values stored with
74
+ # {#raw_data=}
75
+ # @return [Object] the object stored
76
+ def data=(new_data)
77
+ if new_data.respond_to?(:read)
78
+ raise ArgumentError.new(t("invalid_io_object"))
79
+ end
80
+
81
+ @raw_data = nil
82
+ @data = new_data
83
+ end
84
+
85
+
86
+ # @return [String] raw data stored in riak for this object's key
87
+ def raw_data
88
+ if @data && !@raw_data
89
+ @raw_data = serialize(@data)
90
+ @data = nil
91
+ end
92
+ @raw_data
93
+ end
94
+
95
+ # @param [String, IO-like] new_raw_data the raw data to be stored in Riak
96
+ # at this key, will not be marshaled or manipulated prior to storage.
97
+ # Overrides any data stored by {#data=}
98
+ # @return [String] the data stored
99
+ def raw_data=(new_raw_data)
100
+ @data = nil
101
+ @raw_data = new_raw_data
102
+ end
103
+
104
+ # Serializes the internal object data for sending to Riak. Differs based on the content-type.
105
+ # This method is called internally when storing the object.
106
+ # Automatically serialized formats:
107
+ # * JSON (application/json)
108
+ # * YAML (text/yaml)
109
+ # * Marshal (application/x-ruby-marshal)
110
+ # When given an IO-like object (e.g. File), no serialization will
111
+ # be done.
112
+ # @param [Object] payload the data to serialize
113
+ def serialize(payload)
114
+ Serializers.serialize(@content_type, payload)
115
+ end
116
+
117
+ # Deserializes the internal object data from a Riak response. Differs based on the content-type.
118
+ # This method is called internally when loading the object.
119
+ # Automatically deserialized formats:
120
+ # * JSON (application/json)
121
+ # * YAML (text/yaml)
122
+ # * Marshal (application/x-ruby-marshal)
123
+ # @param [String] body the serialized response body
124
+ def deserialize(body)
125
+ Serializers.deserialize(@content_type, body)
126
+ end
127
+
128
+ # @return [String] A representation suitable for IRB and debugging output
129
+ def inspect
130
+ body = if @data || Serializers[content_type]
131
+ data.inspect
132
+ else
133
+ @raw_data && "(#{@raw_data.size} bytes)"
134
+ end
135
+ "#<#{self.class.name} [#{@content_type}]:#{body}>"
136
+ end
137
+
138
+ # @api private
139
+ def load_map_reduce_value(hash)
140
+ metadata = hash['metadata']
141
+ extract_if_present(metadata, 'X-Riak-VTag', :etag)
142
+ extract_if_present(metadata, 'content-type', :content_type)
143
+ extract_if_present(metadata, 'X-Riak-Last-Modified', :last_modified) { |v| Time.httpdate( v ) }
144
+ extract_if_present(metadata, 'index', :indexes) do |entries|
145
+ Hash[ entries.map {|k, v| [k, Set.new(Array(v))] } ]
146
+ end
147
+ extract_if_present(metadata, 'Links', :links) do |links|
148
+ Set.new( links.map { |l| Link.new(*l) } )
149
+ end
150
+ extract_if_present(metadata, 'X-Riak-Meta', :meta) do |meta|
151
+ Hash[
152
+ meta.map do |k, v|
153
+ [k.sub(%r{^x-riak-meta-}i, ''), [v]]
154
+ end
155
+ ]
156
+ end
157
+ extract_if_present(hash, 'data', :data) { |v| deserialize(v) }
158
+ end
159
+
160
+ private
161
+ def extract_if_present(hash, key, attribute = nil)
162
+ if hash[key].present?
163
+ attribute ||= key
164
+ value = block_given? ? yield(hash[key]) : hash[key]
165
+ send("#{attribute}=", value)
166
+ end
167
+ end
168
+
169
+ def new_index_hash
170
+ Hash.new {|h, k| h[k] = Set.new }
171
+ end
172
+ end
173
+ end