1#!/usr/bin/env ruby
2
3require 'rubygems'
4require 'eventmachine'
5require 'couchbase'
6require 'yajl'
7
8$client_map = Hash.new
9
10module RequestHandler 
11  def receive_data(data)
12    data = parse_data(data)
13    if $client_map.has_key?(data["bucket"]) == false
14        url = "http://"+data["cb_ip"]+":"+data["cb_port"]+"/pools/default/"
15        new_client = Couchbase.new url 
16        new_client.quiet = false 
17        $client_map[data["bucket"]] = new_client
18    end
19
20    res = exec_request(data)
21    # return response to worker
22    send_data(res)
23  end
24end
25
26def parse_data(data)
27    parser = Yajl::Parser.new
28    data = parser.parse(data)
29    return data
30end
31    
32def exec_request(data)
33    if data['command'] == 'set'
34        return do_set(data)
35    end
36
37    if data['command'] == 'mset'
38        return do_mset(data)
39    end
40
41    if data['command'] == 'mget'
42        return do_mget(data)
43    end
44
45    if data['command'] == 'get'
46        return do_get(data)
47    end
48    if data['command'] == 'delete'
49        return do_delete(data)
50    end
51    if data['command'] == 'mdelete'
52        return do_mdelete(data)
53    end
54    if data['command'] == 'query'
55        return do_query(data)
56    end
57
58end
59
60def do_set(data)
61
62    key = data['args'][0]
63    ttl = data['args'][1]
64    flags = data['args'][2]
65    value = data['args'][3]
66
67    return $client_map[data["bucket"]].set key, value, :flags => flags, :ttl => ttl 
68end
69
70def do_mset(data)
71
72
73    for kv in data['args']
74        key = kv[0]
75        ttl = kv[1]
76        flags = kv[2]
77        value = kv[3]
78        $client_map[data["bucket"]].set key, value, :flags => flags, :ttl => ttl 
79    end
80end
81
82
83
84def do_mget(data)
85
86    keys = data['args']
87    errors = nil
88    num_gets = 0
89    begin
90        res = $client_map[data["bucket"]].get keys
91        if res != nil
92            res = res.compact  # remove nil
93            num_gets = res.length
94        end
95    rescue Couchbase::Error::NotFound => e
96        errors = e
97    end
98
99    
100    return {"gets" => num_gets, "errors" => errors} 
101end
102
103
104
105def do_get(data)
106    res = nil
107    begin
108        key = data['args'][0]
109        res = $client_map[data["bucket"]].get key
110    rescue Couchbase::Error::NotFound => e
111        res = e
112    end
113
114    return res 
115end
116
117def do_delete(data)
118    res = nil
119    begin
120        key = data['args'][0]
121        res = $client_map[data["bucket"]].delete key
122    rescue Couchbase::Error::NotFound => e
123        res = e
124    end
125
126    return res
127end
128
129def do_mdelete(data)
130    res = nil
131    begin
132        for key in data['args']
133            res = $client_map[data["bucket"]].delete key
134        end
135    rescue Couchbase::Error::NotFound => e
136        res = e
137    end
138
139    return res
140end
141
142
143def do_query(data)
144    design_doc_name = data['args'][0]
145    view_name = data['args'][1]
146    bucket = data['args'][2]
147    params = data['args'][3]
148
149    #TODO: ruby sdk has removed views code!
150    return nil
151end
152
153
154EventMachine::run do
155  host = '127.0.0.1'
156  port = 50009 
157  EventMachine::start_server host, port, RequestHandler 
158  puts "Started Ruby RequestHandler on #{host}:#{port}..."
159end
160
161