Skip to content

Commit 6919d03

Browse files
committed
Merge pull request basho#169 from evanmcc/pevm-mr-list-add
add list-consuming convenience function for mr.add
2 parents 885d1d9 + 8cc2bc4 commit 6919d03

File tree

2 files changed

+93
-6
lines changed

2 files changed

+93
-6
lines changed

riak/mapreduce.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import urllib
2121
from riak_object import RiakObject
2222
from bucket import RiakBucket
23+
from collections import Iterable
2324

2425

2526
class RiakMapReduce(object):
@@ -46,7 +47,7 @@ def add(self, arg1, arg2=None, arg3=None):
4647
specify either a RiakObject, a string bucket name, or a bucket,
4748
key, and additional arg.
4849
@param mixed arg1 - RiakObject or Bucket
49-
@param mixed arg2 - Key or blank
50+
@param mixed arg2 - Key or List or blank
5051
@param mixed arg3 - Arg or blank
5152
@return RiakMapReduce
5253
"""
@@ -63,11 +64,16 @@ def add_object(self, obj):
6364

6465
def add_bucket_key_data(self, bucket, key, data):
6566
if self._input_mode == 'bucket':
66-
raise Exception('Already added a bucket, can\'t add an object.')
67+
raise ValueError('Already added a bucket, can\'t add an object.')
6768
elif self._input_mode == 'query':
68-
raise Exception('Already added a query, can\'t add an object.')
69+
raise ValueError('Already added a query, can\'t add an object.')
6970
else:
70-
self._inputs.append([bucket, key, data])
71+
if isinstance(key, Iterable) and \
72+
not isinstance(key, basestring):
73+
for k in key:
74+
self._inputs.append([bucket, k, data])
75+
else:
76+
self._inputs.append([bucket, key, data])
7177
return self
7278

7379
def add_bucket(self, bucket):
@@ -77,14 +83,14 @@ def add_bucket(self, bucket):
7783

7884
def add_key_filters(self, key_filters):
7985
if self._input_mode == 'query':
80-
raise Exception('Key filters are not supported in a query.')
86+
raise ValueError('Key filters are not supported in a query.')
8187

8288
self._key_filters.extend(key_filters)
8389
return self
8490

8591
def add_key_filter(self, *args):
8692
if self._input_mode == 'query':
87-
raise Exception('Key filters are not supported in a query.')
93+
raise ValueError('Key filters are not supported in a query.')
8894

8995
self._key_filters.append(args)
9096
return self

riak/tests/test_mapreduce.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,27 @@ def test_erlang_map_reduce(self):
8383
.run()
8484
self.assertEqual(len(result), 2)
8585

86+
def test_client_exceptional_paths(self):
87+
bucket = self.client.bucket('bucket')
88+
bucket.new("foo", 2).store()
89+
bucket.new("bar", 2).store()
90+
bucket.new("baz", 4).store()
91+
92+
#adding a b-key pair to a bucket input
93+
with self.assertRaises(ValueError):
94+
mr = self.client.add('bucket')
95+
mr.add('bucket', 'bar')
96+
97+
#adding a b-key pair to a query input
98+
with self.assertRaises(ValueError):
99+
mr = self.client.search('bucket', 'fleh')
100+
mr.add('bucket', 'bar')
101+
102+
#adding a key filter to a query input
103+
with self.assertRaises(ValueError):
104+
mr = self.client.search('bucket', 'fleh')
105+
mr.add_key_filter("tokenize", "-", 1)
106+
86107

87108
class JSMapReduceTests(object):
88109
def test_javascript_source_map(self):
@@ -229,6 +250,66 @@ def test_map_reduce_from_object(self):
229250
result = obj.map("Riak.mapValuesJson").run()
230251
self.assertEqual(result, [2])
231252

253+
def test_mr_list_add(self):
254+
bucket = self.client.bucket("abucket")
255+
for x in range(20):
256+
bucket.new('baz' + str(x),
257+
'bazval' + str(x)).store()
258+
mr = self.client.add('abucket', ['baz' + str(x)
259+
for x in range(2, 5)])
260+
results = mr.map_values().run()
261+
results.sort()
262+
self.assertEqual(results,
263+
[u'"bazval2"',
264+
u'"bazval3"',
265+
u'"bazval4"'])
266+
267+
def test_mr_list_add_two_buckets(self):
268+
bucket = self.client.bucket("bucket_a")
269+
for x in range(10):
270+
bucket.new('foo' + str(x),
271+
'fooval' + str(x)).store()
272+
bucket = self.client.bucket("bucket_b")
273+
for x in range(10):
274+
bucket.new('bar' + str(x),
275+
'barval' + str(x)).store()
276+
277+
mr = self.client.add('bucket_a', ['foo' + str(x)
278+
for x in range(2, 4)])
279+
mr.add('bucket_b', ['bar' + str(x)
280+
for x in range(5, 7)])
281+
results = mr.map_values().run()
282+
results.sort()
283+
284+
self.assertEqual(results,
285+
[u'"barval5"',
286+
u'"barval6"',
287+
u'"fooval2"',
288+
u'"fooval3"'])
289+
290+
def test_mr_list_add_mix(self):
291+
bucket = self.client.bucket("bucket_a")
292+
for x in range(10):
293+
bucket.new('foo' + str(x),
294+
'fooval' + str(x)).store()
295+
bucket = self.client.bucket("bucket_b")
296+
for x in range(10):
297+
bucket.new('bar' + str(x),
298+
'barval' + str(x)).store()
299+
300+
mr = self.client.add('bucket_a', ['foo' + str(x)
301+
for x in range(2, 4)])
302+
mr.add('bucket_b', 'bar9')
303+
mr.add('bucket_b', 'bar2')
304+
results = mr.map_values().run()
305+
results.sort()
306+
307+
self.assertEqual(results,
308+
[u'"barval2"',
309+
u'"barval9"',
310+
u'"fooval2"',
311+
u'"fooval3"'])
312+
232313

233314
class MapReduceAliasTests(object):
234315
"""This tests the map reduce aliases"""

0 commit comments

Comments
 (0)