Module: EmRiak

Extended by:
EmRiak
Included in:
EmRiak
Defined in:
lib/em-riak/basic.rb,
lib/em-riak/model.rb,
lib/em-riak/utils.rb,
lib/em-riak/grapher.rb,
lib/em-riak/version.rb,
lib/em-riak/map_reduce.rb,
lib/em-riak/secondary_index.rb

Defined Under Namespace

Modules: Grapher, MapReduce, Model, SecondaryIndex, Util Classes: Connection, StorageObject

Constant Summary collapse

VERSION =
"0.2.92"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



10
11
12
# File 'lib/em-riak/basic.rb', line 10

def adapter
  @adapter
end

#asyncObject

Returns the value of attribute async.



10
11
12
# File 'lib/em-riak/basic.rb', line 10

def async
  @async
end

#bucketObject

Returns the value of attribute bucket.



10
11
12
# File 'lib/em-riak/basic.rb', line 10

def bucket
  @bucket
end

#clusterObject

Returns the value of attribute cluster.



10
11
12
# File 'lib/em-riak/basic.rb', line 10

def cluster
  @cluster
end

#debugObject

Returns the value of attribute debug.



10
11
12
# File 'lib/em-riak/basic.rb', line 10

def debug
  @debug
end

#em_enqueue_poolObject

Returns the value of attribute em_enqueue_pool.



10
11
12
# File 'lib/em-riak/basic.rb', line 10

def em_enqueue_pool
  @em_enqueue_pool
end

#em_enqueue_pool_maxObject

Returns the value of attribute em_enqueue_pool_max.



10
11
12
# File 'lib/em-riak/basic.rb', line 10

def em_enqueue_pool_max
  @em_enqueue_pool_max
end

#hostsObject

Returns the value of attribute hosts.



10
11
12
# File 'lib/em-riak/basic.rb', line 10

def hosts
  @hosts
end

#http_clientObject

Returns the value of attribute http_client.



10
11
12
# File 'lib/em-riak/basic.rb', line 10

def http_client
  @http_client
end

#interfaceObject

Returns the value of attribute interface.



10
11
12
# File 'lib/em-riak/basic.rb', line 10

def interface
  @interface
end

#replicationObject

Returns the value of attribute replication.



10
11
12
# File 'lib/em-riak/basic.rb', line 10

def replication
  @replication
end

#vclockObject

Returns the value of attribute vclock.



10
11
12
# File 'lib/em-riak/basic.rb', line 10

def vclock
  @vclock
end

#weightObject

Returns the value of attribute weight.



10
11
12
# File 'lib/em-riak/basic.rb', line 10

def weight
  @weight
end

Instance Method Details

#create(key, *opts, &callback) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/em-riak/basic.rb', line 53

def create(key,*opts,&callback)
    data,opts=handle_opts_for_body(opts)
    data,callback=handle_callback(key,"save",opts,callback,data)
    operate_bucket= select_bucket(data)
    url="/buckets/#{operate_bucket}/keys/#{key_handler(key)}"
    http("put",key,url,data,callback)
end

#destroy(key, *opts, &callback) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/em-riak/basic.rb', line 81

def destroy(key,*opts,&callback)
    obj_key=defined?(obj) ? obj[:riak_key] :  (key.class==String ? key : key[:riak_key])
    data,callback=handle_callback(obj_key,"delete",opts,callback)
    operate_bucket= select_bucket(data)
    url="/buckets/#{operate_bucket}/keys/#{key_handler(obj_key)}"
    http("delete",obj_key,url,data,callback)
    key=nil
end

#find(key, *opts, &callback) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/em-riak/basic.rb', line 60

def find(key,*opts,&callback)
    link=""
    data,callback=handle_callback(key,"get",opts,callback)

    if data[:head][:link]
        link=data[:head][:link]
        data[:head].reject!{|o| o==:link}
    end
    operate_bucket= select_bucket(data)
    url="/buckets/#{operate_bucket}/keys/#{key_handler(key)}#{link}"
    http("get",key,url,data,callback)
end

#http(method, key, url, data, callback = nil, res = nil) ⇒ Object



90
91
92
# File 'lib/em-riak/basic.rb', line 90

def http(method,key,url,data,callback=nil,res=nil)
    (callback || self.async && ["delete","put","post"].index(method) && key!="mapred") ? em_http(method,key,url,data,callback) : open_http(method,key,url,data)
end

#save(obj, *opts, &callback) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/em-riak/basic.rb', line 73

def save(obj,*opts,&callback)
    data,nothing=handle_opts_for_body([obj])
    data,callback=handle_callback(obj[:riak_key],"save",opts,callback,data)
    operate_bucket= select_bucket(data)
    url="/buckets/#{operate_bucket}/keys/#{obj[:riak_key]}"
    http("put",obj[:riak_key],url,data,callback)
end

#search(method_name, *args, &callback) ⇒ Object

– Search



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/em-riak/basic.rb', line 95

def search(method_name,*args,&callback)
    result_handler=nil
    response=[]
    return "you must provide search mode" if !SEARCH_SUPPORT.index(method_name)

    collect_data=   case 
                    when args[0].class==Hash && args.last.class==Proc
                        opts=args[0]
                        callback=args.last
                        args[1..args.count-2]
                    when args[0].class==Hash
                        opts=args[0]
                        args[1..args.count]
                    when args.last.class==Proc
                        callback=args.last
                        args[0..args.count-2]
                    else
                        args[0..args.count]
                    end

    collect_data=collect_data.first if collect_data.count==1

    return "you must provide some vars to do search, for example, bucket name, bin name..." if !defined?(opts) && method_name!=:map_reduce || opts[:bin].nil? && method_name==:secondary_index

    case method_name
    when :secondary_index
        bin=opts[:bin].to_s.index("_bin") ? opts[:bin] : "#{opts[:bin]}_bin"
        bucket=opts[:bucket] ? opts[:bucket] : EmRiak.bucket
        mapper_key=collect_data.count>1 ? :secondary_index_multiple_query : :secondary_index_single_query

        result_handler=Proc.new{|map_reduce_results,results|
            map_reduce_results.each do |object| 
                if object[1]["body"] 
                    results[object[0].to_sym]=object[1]["body"]
                else 
                    results[object[0].to_sym]=[] if !results[object[0].to_sym]
                    results[object[0].to_sym] << object[1]
                end if object[0]!=false
            end
        }
        map_reduce_work=[mapper_key,{'bucket'=>bucket,'index'=>bin,'value'=>collect_data.join(',')},result_handler,callback]
    when :full_text # TODO : full-text search is still under implement...
        map_reduce_work=[]
    end

    response=EmRiak::MapReduce.submit(map_reduce_work) if map_reduce_work.count>0
    response
end