em-riak 0.2.0 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  METAMAPPER={:sibling=>{"Accept"=>"multipart/mixed"}}
2
- SEARCH_SUPPORT=[:secondary_index,:full_text,:map_reduce,:key_filters]
2
+ SEARCH_SUPPORT=[:secondary_index,:full_text,:map_reduce]
3
3
  RETRY_TIMES=3
4
4
  EMMAX=2000
5
5
  EM_REQUEST_TIMEOUT=50
@@ -1,58 +1,58 @@
1
1
  module EmRiak
2
- module Grapher
3
- extend self
2
+ module Grapher
3
+ extend self
4
4
 
5
- class LinkWalk
6
- def add_link(key,*opts)
7
- data={}
8
- data[:head], data[:query] = EmRiak.handle_options("add_link",opts)
9
- data[:query]= data[:query] && data[:query].length>0 ? data[:query]+"&returnbody=true" : "?returnbody=true"
10
-
11
- url="/buckets/#{EmRiak.bucket}/keys/#{key}"
12
-
13
- response=open("#{RIAKHOST[rand(RIAKHOST.count)]}/buckets/#{EmRiak.bucket}/keys/#{key}?returnbody=true")
14
- body,links = rehandle_link_and_params(response)
15
-
16
- data[:head]['Link'].each{|link| links=links.gsub(link+", ","") }
17
- data[:head]['Link']=data[:head]['Link'].join(", ")+", "+links if data[:head]['Link'].count>0
18
- data[:body]=body
5
+ class LinkWalk
6
+ def add_link(key,*opts)
7
+ data={}
8
+ data[:head], data[:query] = EmRiak.handle_options("add_link",opts)
9
+ data[:query]= data[:query] && data[:query].length>0 ? data[:query]+"&returnbody=true" : "?returnbody=true"
10
+
11
+ url="/buckets/#{EmRiak.bucket}/keys/#{key}"
12
+
13
+ response=open("#{RIAKHOST[rand(RIAKHOST.count)]}/buckets/#{EmRiak.bucket}/keys/#{key}?returnbody=true")
14
+ body,links = rehandle_link_and_params(response)
15
+
16
+ data[:head]['Link'].each{|link| links=links.gsub(link+", ","") }
17
+ data[:head]['Link']=data[:head]['Link'].join(", ")+", "+links if data[:head]['Link'].count>0
18
+ data[:body]=body
19
19
 
20
- EmRiak.http("put",key,url,data)
21
- end
20
+ EmRiak.http("put",key,url,data)
21
+ end
22
22
 
23
- def remove_link(key,*opts)
24
- data={}
25
- data[:head], data[:query] = EmRiak.handle_options("add_link",opts)
26
- data[:query]= data[:query] && data[:query].length>0 ? data[:query]+"&returnbody=true" : "?returnbody=true"
27
-
28
- url="/buckets/#{EmRiak.bucket}/keys/#{key}"
23
+ def remove_link(key,*opts)
24
+ data={}
25
+ data[:head], data[:query] = EmRiak.handle_options("add_link",opts)
26
+ data[:query]= data[:query] && data[:query].length>0 ? data[:query]+"&returnbody=true" : "?returnbody=true"
27
+
28
+ url="/buckets/#{EmRiak.bucket}/keys/#{key}"
29
29
 
30
- response=open("#{RIAKHOST[rand(RIAKHOST.count)]}/buckets/#{EmRiak.bucket}/keys/#{key}?returnbody=true")
31
- body,links = rehandle_link_and_params(response)
32
-
30
+ response=open("#{RIAKHOST[rand(RIAKHOST.count)]}/buckets/#{EmRiak.bucket}/keys/#{key}?returnbody=true")
31
+ body,links = rehandle_link_and_params(response)
32
+
33
33
 
34
- data[:body]=body
34
+ data[:body]=body
35
35
 
36
- data[:head]['Link'].each{|link| links=links.gsub(link+", ","") }
37
- data[:head]['Link']=links
38
- EmRiak.http("put",key,url,data)
39
- end
36
+ data[:head]['Link'].each{|link| links=links.gsub(link+", ","") }
37
+ data[:head]['Link']=links
38
+ EmRiak.http("put",key,url,data)
39
+ end
40
40
 
41
- private
42
- def rehandle_link_and_params(response,body={},links=[])
43
- response.read.split("&").each{|params|
44
- key=params.split("=")[0]
45
- value=params.split("=")[1]
46
- body[key.to_sym]=value
47
- }
48
- links=response.meta["link"]#.split(",")
49
- #.each{|link|
50
- #key=link.split(";")[0].split("/").last.gsub(">","")
51
- #tag=link.split(";")[1].split("").gsub("\"","").gsub("\"","").gsub(" ","")
52
- #links << {:target=>"key",:tag=>tag}
53
- #}
54
- [body,links]
55
- end
56
- end
57
- end
41
+ private
42
+ def rehandle_link_and_params(response,body={},links=[])
43
+ response.read.split("&").each{|params|
44
+ key=params.split("=")[0]
45
+ value=params.split("=")[1]
46
+ body[key.to_sym]=value
47
+ }
48
+ links=response.meta["link"]#.split(",")
49
+ #.each{|link|
50
+ #key=link.split(";")[0].split("/").last.gsub(">","")
51
+ #tag=link.split(";")[1].split("").gsub("\"","").gsub("\"","").gsub(" ","")
52
+ #links << {:target=>"key",:tag=>tag}
53
+ #}
54
+ [body,links]
55
+ end
56
+ end
57
+ end
58
58
  end
@@ -1,39 +1,50 @@
1
1
  MRMAPPER={
2
- :secondary_index_single_query=>"{\"inputs\" => {\"bucket\"=> \"@!bucket!@\",\"index\"=> \"@!index!@\", \"key\"=>\"@!value!@\"},
3
- \"query\" => [{\"map\"=> {\"language\"=> \"javascript\",\"source\"=>\"function(riakObject){ var body={}; var body_arrays=riakObject.values[0].data.split('&head')[0].split('&'); for(i=0;i<body_arrays.length;i++){ body[body_arrays[i].split('=')[0]]=body_arrays[i].split('=')[1];}; return [[riakObject.key, body]]; }\"} },
4
- {\"reduce\"=> {\"language\"=> \"erlang\", \"module\"=>\"riak_kv_mapreduce\", \"function\"=>\"reduce_identity\", \"keep\"=>true} }
5
- ]}",
6
- :secondary_index_range_query=>"",
7
- :secondary_index_multiple_query=>"{\"inputs\"=> {\"bucket\"=>\"@!bucket!@\",\"index\"=> \"@!index!@\",\"key_filters\"=>[[\"greater_than\",0]]},
8
- \"query\" => [{\"map\"=> {\"language\"=>\"javascript\", \"source\"=>\"function(riakObject){var search_string='@!value!@';var search_values=search_string.split(',');var metadata=riakObject.values[0].metadata; var indexes=null; var match=false; if(metadata){ if(metadata.index){ indexes=metadata.index.@!index!@; var temp_index=indexes; if(typeof(indexes)!='object') indexes=[indexes];} if(indexes){ for(i=0;i<search_values.length; i++){if(indexes.indexOf(search_values[i])>0){match=true;break;};};};}; return (indexes && match) ? [[riakObject.bucket,riakObject.key]] : [[false,false]];}\" }},
9
- {\"reduce\"=> {\"language\"=> \"erlang\", \"module\"=>\"riak_kv_mapreduce\", \"function\"=>\"reduce_set_union\", \"keep\"=>true} }
10
- ]}",
11
- :full_text_query=>"",
12
- :clean_bucket=>"{\"inputs\"=> {\"bucket\"=>\"@!bucket!@\",\"key_filters\"=>[[\"greater_than\",0]]},
13
- \"query\" => [{\"map\"=> {\"language\"=>\"javascript\", \"source\"=>\"function(riakObject){return [[riakObject.key]];}\" }}
14
- ]}"
2
+ :secondary_index_single_query=>"{\"inputs\" => {\"bucket\"=> \"@!bucket!@\",\"index\"=> \"@!index!@\", \"key\"=>\"@!value!@\"},
3
+ \"query\" => [{\"map\"=> {\"language\"=> \"javascript\",\"source\"=>\"function(riakObject){ var body={}; var body_arrays=riakObject.values[0].data.split('&head')[0].split('&'); for(i=0;i<body_arrays.length;i++){ body[body_arrays[i].split('=')[0]]=body_arrays[i].split('=')[1];}; return [[riakObject.key, body]]; }\"} },
4
+ {\"reduce\"=> {\"language\"=> \"erlang\", \"module\"=>\"riak_kv_mapreduce\", \"function\"=>\"reduce_identity\", \"keep\"=>true} }
5
+ ]}",
6
+ :secondary_index_range_query=>"",
7
+ :secondary_index_multiple_query=>"{\"inputs\"=> {\"bucket\"=>\"@!bucket!@\",\"index\"=> \"@!index!@\",\"key_filters\"=>[[\"greater_than\",0]]},
8
+ \"query\" => [{\"map\"=> {\"language\"=>\"javascript\", \"source\"=>\"function(riakObject){var search_string='@!value!@';var search_values=search_string.split(',');var metadata=riakObject.values[0].metadata; var indexes=null; var match=false; if(metadata){ if(metadata.index){ indexes=metadata.index.@!index!@; var temp_index=indexes; if(typeof(indexes)!='object') indexes=[indexes];} if(indexes){ for(i=0;i<search_values.length; i++){if(indexes.indexOf(search_values[i])>0){match=true;break;};};};}; return (indexes && match) ? [[riakObject.bucket,riakObject.key]] : [[false,false]];}\" }},
9
+ {\"reduce\"=> {\"language\"=> \"erlang\", \"module\"=>\"riak_kv_mapreduce\", \"function\"=>\"reduce_set_union\", \"keep\"=>true} }
10
+ ]}",
11
+ :clean_bucket=>"{\"inputs\"=> {\"bucket\"=>\"@!bucket!@\",\"key_filters\"=>[[\"greater_than\",0]]},
12
+ \"query\" => [{\"map\"=> {\"language\"=>\"javascript\", \"source\"=>\"function(riakObject){return [[riakObject.key]];}\" }}
13
+ ]}"
15
14
  }
16
15
 
17
16
  module EmRiak
18
- module MapReduce
19
- extend self
20
- def submit(mode_or_string,params)
21
- if mode_or_string.class==Symbol
22
- map_reduce_work=MRMAPPER[mode_or_string]
23
- map_reduce_work.gsub!(/@!(.{1,}?)!@/){|m| params[$1] }
24
- map_reduce_work=eval(map_reduce_work)
25
- end
26
- url="/mapred"
27
- map_reduce_json=EmRiak.json_encode(map_reduce_work)
28
- data={:head=>{"Content-Type"=>"application/json"},:body=>map_reduce_json}
29
- res=EmRiak.http("post","mapred",url,data)
17
+ module MapReduce
18
+ extend self
19
+ def submit(*args)
20
+ mode_or_string,params,result_handler,callback,callback_proc=args.first
21
+ if mode_or_string.class==Hash
22
+ submit_work=mode_or_string
23
+ elsif mode_or_string.class==Symbol
24
+ map_reduce_work=MRMAPPER[mode_or_string]
25
+ submit_work=map_reduce_work.gsub(/@!(.{1,}?)!@/){|m| params[$1] }
26
+ submit_work=eval(submit_work)
27
+ end
28
+ url="/mapred"
29
+ map_reduce_json=EmRiak.json_encode(submit_work)
30
+ data={:head=>{"Content-Type"=>"application/json"},:body=>map_reduce_json}
31
+
32
+ callback_proc=Proc.new{|results| callback.call(callback_handler(results,result_handler)) } if callback
33
+ res=EmRiak.http("post","mapred",url,data,callback_proc)
34
+ res=callback_handler(res,result_handler) if !callback_proc
30
35
 
31
- if res.class==String
32
- res=EscapeUtils.unescape_url(res.encode('UTF-8').gsub("+","")).gsub("=>",":").gsub("\"{","{").gsub("}\"","}").gsub("%20"," ")
33
- res=EmRiak.json_decode(res)
34
- end
35
- res=EmRiak.convert_params_layers(res,:to_sym) if res.class==Hash
36
- res
37
- end
38
- end
36
+ res
37
+ end
38
+
39
+ private
40
+ def callback_handler(res,result_handler)
41
+ if res.class==String
42
+ res=EscapeUtils.unescape_url(res.encode('UTF-8').gsub("(","\(").gsub(")","\)").gsub("+","").gsub("=>",":").gsub("\"{","{").gsub("}\"","}").gsub("%20"," ").gsub("%22","@!quote!@")).gsub("@!quote!@","\\\"")
43
+ res=EmRiak.json_decode(res)
44
+ end
45
+ res=EmRiak.convert_params_layers(res,:to_sym) if res.class==Hash
46
+ res=result_handler.call(res,{}) if result_handler
47
+ res
48
+ end
49
+ end
39
50
  end
@@ -0,0 +1,7 @@
1
+ module EmRiak
2
+ module Model
3
+ extend self
4
+
5
+
6
+ end
7
+ end
@@ -1,68 +1,74 @@
1
1
  module EmRiak
2
- module SecondaryIndex
3
- extend self
4
- def add_tag(data_self,args)
5
- data,opts=handle_data_and_options(data_self,args)
6
- submit_work("add",data_self,data,opts)
7
- end
8
-
9
- def remove_tag(data_self,args)
10
- data,opts=handle_data_and_options(data_self,args)
11
- submit_work("remove",data_self,data,opts)
12
- end
2
+ module SecondaryIndex
3
+ extend self
4
+ def add_tag(data_self,args)
5
+ data,opts=handle_data_and_options(data_self,args)
6
+ submit_work("add",data_self,data,opts)
7
+ end
8
+
9
+ def remove_tag(data_self,args)
10
+ data,opts=handle_data_and_options(data_self,args)
11
+ submit_work("remove",data_self,data,opts)
12
+ end
13
13
 
14
- protected
15
- def submit_work(submit_mode,data_self,data,opts,res={:status=>"success"})
16
- self.send "#{submit_mode}_as_index",data_self,data,opts
17
- end
14
+ protected
15
+ def submit_work(submit_mode,data_self,data,opts,res={:status=>"success"})
16
+ self.send "#{submit_mode}_as_index",data_self,data,opts
17
+ end
18
18
 
19
- def handle_data_and_options(data_self,args)
20
- data={:head=>data_self[:head],:body=>data_self,:query=>{}}
21
- data[:body].delete(:head)
22
- args=args[0] if args[0].class==Array
23
- opts={:bin=>args[0]}
24
- opts[:secondary_index]= if args[1] && args[1].class==Hash
25
- args[1].each{|key,value| opts[key]=value }
26
- args[2..args.count]
27
- else
28
- args[1..args.count]
29
- end
30
- [data, opts]
31
- end
32
- def merge_tag(new_heads,old_heads,res={})
33
- return new_heads if !old_heads
34
- new_heads.each do |key,value|
35
- res[key]= key.match(/x-riak-index-/) && old_heads[key] ? value.split(", ").concat(old_heads[key].split(", ")).uniq.join(", ") : value
36
- end
37
- res
38
- end
19
+ def handle_data_and_options(data_self,args)
20
+ data={:head=>data_self[:head],:body=>data_self,:query=>{}}
21
+ data[:body].delete(:head)
22
+ args=args[0] if args[0].class==Array
23
+ opts={:bin=>args[0]}
24
+ opts[:secondary_index]= if args[1] && args[1].class==Hash
25
+ args[1].each{|key,value| opts[key]=value }
26
+ args[2..args.count]
27
+ else
28
+ args[1..args.count]
29
+ end
30
+ [data, opts]
31
+ end
32
+ def merge_tag(new_heads,old_heads,res={})
33
+ return new_heads if !old_heads
34
+ new_heads.each do |key,value|
35
+ res[key]= key.match(/x-riak-index-/) && old_heads[key] ? value.split(", ").concat(old_heads[key].split(", ")).uniq.join(", ") : value
36
+ end
37
+ res
38
+ end
39
39
 
40
- def add_as_index(data_self,data,opts)
41
- temp_head, data[:query] = EmRiak.handle_options("post",opts)
42
- data[:head]=merge_tag(temp_head,data[:head])
43
- data_self[:head]=data[:head]
40
+ def add_as_index(data_self,data,opts)
41
+ temp_head, data[:query] = EmRiak.handle_options("post",opts)
42
+ data[:head]=merge_tag(temp_head,data[:head])
43
+ data_self[:head]=data[:head]
44
44
 
45
- url="/buckets/#{EmRiak.bucket}/keys/#{data_self[:riak_key]}"
46
- EmRiak.http("post","2i",url,data)
47
- end
48
- def destroy_tag(new_heads,old_heads)
49
- return new_heads if !old_heads
50
- new_heads.each do |key,value|
51
- if key.match(/x-riak-index-/) && old_heads[key]
52
- temp_head=old_heads[key].split(", ")
53
- temp_head.delete(value)
54
- old_heads[key]=temp_head.uniq.join(", ")
55
- end
56
- end
57
- old_heads
58
- end
59
- def remove_as_index(data_self,data,opts)
60
- temp_head, data[:query] = EmRiak.handle_options("post",opts)
61
- data[:head]=destroy_tag(temp_head,data[:head])
62
- data_self[:head]=data[:head]
63
-
64
- url="/buckets/#{EmRiak.bucket}/keys/#{data_self[:riak_key]}"
65
- EmRiak.http("post","2i",url,data)
66
- end
67
- end
45
+ url="/buckets/#{EmRiak.bucket}/keys/#{data_self[:riak_key]}"
46
+ EmRiak.http("post","2i",url,data)
47
+ end
48
+ def destroy_tag(new_heads,old_heads)
49
+ return new_heads if !old_heads
50
+ new_heads.each do |key,value|
51
+ new_value= (value.class==String && value.split(", ").count>1) ? value.split(", ") : value
52
+ if key.match(/x-riak-index-/) && old_heads[key]
53
+ temp_head=old_heads[key].split(", ")
54
+ if new_value.class==Array
55
+ new_value.each{|v| temp_head.delete(v) }
56
+ else
57
+ temp_head.delete(new_value)
58
+ end
59
+ old_heads[key]=temp_head.uniq.join(", ")
60
+ end
61
+ end
62
+ old_heads
63
+ end
64
+ def remove_as_index(data_self,data,opts)
65
+ temp_head, data[:query] = EmRiak.handle_options("post",opts)
66
+ data[:head]=destroy_tag(temp_head,data[:head])
67
+ data_self[:head]=data[:head]
68
+
69
+ url="/buckets/#{EmRiak.bucket}/keys/#{data_self[:riak_key]}"
70
+ p "remove data #{data}"
71
+ EmRiak.http("post","2i",url,data)
72
+ end
73
+ end
68
74
  end
data/lib/em-riak/utils.rb CHANGED
@@ -1,22 +1,22 @@
1
1
  module EmRiak
2
- module Util
3
- extend self
4
- def clean_bucket(bucket_name)
5
- keys=EmRiak::MapReduce.submit(:clean_bucket,{"bucket"=>bucket_name})
6
- if keys
7
- if !(keys.class==Hash && keys[:error])
8
- keys=keys[0] if keys.class==Array && keys.count==1
9
- keys.each{|key|
10
- key=key[0].gsub("\\","\/")
11
- EmRiak.destroy(URI.escape(key[0]).to_s)
12
- }
13
- puts "clean done"
14
- else
15
- puts "Something went wrong." if EmRiak.debug
16
- end
17
- else
18
- puts "key empty"
19
- end
20
- end
21
- end
2
+ module Util
3
+ extend self
4
+ def clean_bucket(bucket_name)
5
+ keys=EmRiak::MapReduce.submit(:clean_bucket,{"bucket"=>bucket_name})
6
+ if keys
7
+ if !(keys.class==Hash && keys[:error])
8
+ keys=keys[0] if keys.class==Array && keys.count==1
9
+ keys.each{|key|
10
+ key=key[0].gsub("\\","\/")
11
+ EmRiak.destroy(URI.escape(key[0]).to_s)
12
+ }
13
+ puts "clean done"
14
+ else
15
+ puts "Something went wrong." if EmRiak.debug
16
+ end
17
+ else
18
+ puts "key empty"
19
+ end
20
+ end
21
+ end
22
22
  end
@@ -1,3 +1,3 @@
1
1
  module EmRiak
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.5"
3
3
  end
data/spec/unit/all.rb ADDED
File without changes
@@ -0,0 +1,81 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ require 'eventmachine'
4
+ require 'em-http-request'
5
+ require 'httpi'
6
+ require 'yajl'
7
+ require 'em-riak'
8
+ TEST_RIAK_HOSTS=["http://127.0.0.1:8091","http://127.0.0.1:8092","http://127.0.0.1:8093","http://127.0.0.1:8094"]
9
+ require File.expand_path('../../../lib/em-riak', __FILE__)
10
+ gem 'minitest'
11
+ require 'minitest/autorun'
12
+
13
+
14
+ class TestDirectCRUD < MiniTest::Unit::TestCase
15
+ def setup
16
+ EmRiak::Connection.new({:bucket=>"test-member",:async=>false,:hosts=>TEST_RIAK_HOSTS})
17
+ @data={:name=>"5",:riak_key=>"member5"}
18
+ end
19
+
20
+ def test_direct_create
21
+ member=EmRiak.create("member5",{:name=>5})
22
+ @data.each{|k,v| raise "error" if !member.send(k) || member.send(k)!=v }
23
+ rescue Exception => e
24
+ false
25
+ end
26
+
27
+ def test_direct_find
28
+ EmRiak.create("member5",{:name=>5})
29
+ member=EmRiak.find("member5")
30
+ assert_equal @data, member
31
+ end
32
+
33
+ def test_direct_destroy
34
+ assert_equal nil,EmRiak.destroy("member5")
35
+ assert_equal nil,EmRiak.find("member5")
36
+ end
37
+
38
+ def test_direct_create_and_find
39
+ member=EmRiak.create("member5",{:name=>5})
40
+ @data.each{|k,v| raise "error" if !member.send(k) || member.send(k)!=v }
41
+ assert_equal @data, EmRiak.find("member5")
42
+ rescue Exception => e
43
+ false
44
+ end
45
+ end
46
+
47
+ class TestAsyncCRUD < MiniTest::Unit::TestCase
48
+ def setup
49
+ EmRiak::Connection.new({:bucket=>"test-member",:async=>false,:hosts=>TEST_RIAK_HOSTS})
50
+ @data={:name=>"5",:riak_key=>"member5"}
51
+ end
52
+ def test_async_find
53
+ EM.run do
54
+ EmRiak.find("member5"){|result|
55
+ assert_equal @data, result
56
+ EM.stop
57
+ }
58
+ end
59
+ end
60
+ def test_async_create
61
+ EM.run do
62
+ EmRiak.create("member5",@data){|member|
63
+ assert_equal nil,member # Because Riak does not response data when create, so it's nil. Will response data with next release.
64
+ member=EmRiak.find("member5")
65
+ assert_equal @data, member
66
+ EM.stop
67
+ }
68
+ end
69
+ end
70
+
71
+ def test_async_destroy
72
+ EM.run do
73
+ EmRiak.destroy("member5",@data){|member|
74
+ assert_equal nil,member
75
+ member=EmRiak.find("member5")
76
+ assert_equal nil,member
77
+ EM.stop
78
+ }
79
+ end
80
+ end
81
+ end
data/spec/unit/core.rb ADDED
@@ -0,0 +1,70 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ require 'eventmachine'
4
+ require 'em-http-request'
5
+ require 'httpi'
6
+ require 'yajl'
7
+ require 'em-riak'
8
+ TEST_RIAK_HOSTS=["http://127.0.0.1:8091","http://127.0.0.1:8092","http://127.0.0.1:8093","http://127.0.0.1:8094"]
9
+ require File.expand_path('../../../lib/em-riak', __FILE__)
10
+ gem 'minitest'
11
+ require 'minitest/autorun'
12
+
13
+
14
+ class TestEmRiakCoreFunctions < MiniTest::Unit::TestCase
15
+ def setup
16
+ end
17
+
18
+ def test_hosts_weight
19
+ end
20
+
21
+ def test_consist_get_and_post
22
+ end
23
+
24
+ def test_vector_clock
25
+ end
26
+
27
+ def test_sibling
28
+ end
29
+
30
+ def test_key_handler
31
+ end
32
+
33
+ def test_convert_params_layers
34
+ end
35
+
36
+ def test_json_coder
37
+ end
38
+ end
39
+
40
+ class TestEmRiakStorageObjcetFunctionality < MiniTest::Unit::TestCase
41
+ def setup
42
+ EmRiak::Connection.new({:bucket=>"test-member",:async=>false,:hosts=>TEST_RIAK_HOSTS})
43
+ EmRiak.create("member5",{:name=>5})
44
+ @member=EmRiak.find("member5",{:name=>5})
45
+ end
46
+
47
+ def test_self_data
48
+ assert_equal "5",@member.name
49
+ assert_equal "member5", @member.riak_key
50
+ end
51
+
52
+ def test_self_update
53
+ @member.name="10"
54
+ @member.gender="male"
55
+ @member.save
56
+
57
+ assert_equal "10",@member.name
58
+ assert_equal "male",@member.gender
59
+
60
+ member=EmRiak.find("member5")
61
+ assert_equal "male",member.gender
62
+ end
63
+
64
+ def test_self_destroy
65
+ response=@member.destroy
66
+ member=EmRiak.find("member5")
67
+ assert_equal nil,response
68
+ assert_equal nil,member
69
+ end
70
+ end
@@ -0,0 +1,64 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ require 'eventmachine'
4
+ require 'em-http-request'
5
+ require 'httpi'
6
+ require 'yajl'
7
+ require 'em-riak'
8
+ TEST_RIAK_HOSTS=["http://127.0.0.1:8091","http://127.0.0.1:8092","http://127.0.0.1:8093","http://127.0.0.1:8094"]
9
+ require File.expand_path('../../../lib/em-riak', __FILE__)
10
+ gem 'minitest'
11
+ require 'minitest/autorun'
12
+
13
+ class TestMapReduce < MiniTest::Unit::TestCase
14
+ def setup
15
+ EmRiak::Connection.new({:bucket=>"test-member",:async=>false,:hosts=>TEST_RIAK_HOSTS})
16
+ @mrwork={"inputs"=> {"bucket"=>"test-member","index"=> "likes_bin","key_filters"=>[["greater_than",0]]},
17
+ "query" => [{"map"=> {"language"=>"javascript", "source"=>"function(riakObject){var search_string='cool,ruby,social,天天開心,寫程式好Happy';var search_values=search_string.split(',');var metadata=riakObject.values[0].metadata; var indexes=null; var match=false; if(metadata){ if(metadata.index){ indexes=metadata.index.likes_bin; var temp_index=indexes; if(typeof(indexes)!='object') indexes=[indexes];} if(indexes){ for(i=0;i<search_values.length; i++){if(indexes.indexOf(search_values[i])>0){match=true;break;};};};}; return (indexes && match) ? [[riakObject.bucket,riakObject.key]] : [[false,false]];}" }},
18
+ {"reduce"=> {"language"=> "erlang", "module"=>"riak_kv_mapreduce", "function"=>"reduce_set_union", "keep"=>true} }
19
+ ]
20
+ }
21
+ end
22
+
23
+ def test_sumbit_map_reduce
24
+ EmRiak.create("member8",{:name=>"Tester"})
25
+ @member=EmRiak.find("member8")
26
+
27
+ @member.add_tag(:likes,["cool","ruby","social","天天開心","寫程式好Happy"])
28
+ @member=EmRiak.find("member8")
29
+ EmRiak.search(:secondary_index,{:bin=>:likes},["cool","ruby","social","天天開心","寫程式好Happy"]).reject{|ary| ary==[false,false]}
30
+
31
+ data=EmRiak::MapReduce.submit(@mrwork).reject{|ary| ary==[false,false]}
32
+ assert_operator 0, :<, data.count
33
+
34
+ has_member=false
35
+ data.each{|ary| has_member=true if "member8"==ary[1] }
36
+ assert_equal true,has_member
37
+ end
38
+
39
+ def test_async_callback
40
+ EM.run do
41
+ callback=Proc.new{|data|
42
+ assert_operator 0, :<, data.count
43
+ has_member=false
44
+ data.each{|ary| has_member=true if "member8"==ary[1] }
45
+ assert_equal true,has_member
46
+ EM.stop
47
+ }
48
+ EmRiak::MapReduce.submit([@mrwork, nil, nil, callback])
49
+ end
50
+ end
51
+
52
+ def test_result_handler_callback
53
+ result_handler=Proc.new{|data|
54
+ data=data.reject{|ary| ary==[false,false]}
55
+ assert_operator 0, :<, data.count
56
+
57
+ has_member=false
58
+ data.each{|ary| has_member=true if "member8"==ary[1] }
59
+ assert_equal true,has_member
60
+ assert_equal true,true
61
+ }
62
+ EmRiak::MapReduce.submit([@mrwork, nil, result_handler])
63
+ end
64
+ end