Class: RiakRaw::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/riak_raw.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = "127.0.0.1", port = 8098, prefix = 'riak', client_id = SecureRandom.base64) ⇒ Client

Returns a new instance of Client.



23
24
25
26
27
28
# File 'lib/riak_raw.rb', line 23

def initialize(host="127.0.0.1", port=8098, prefix='riak', client_id=SecureRandom.base64)
  @host = host
  @port = port
  @prefix = prefix
  @client_id = client_id
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



21
22
23
# File 'lib/riak_raw.rb', line 21

def client_id
  @client_id
end

#hostObject

Returns the value of attribute host.



21
22
23
# File 'lib/riak_raw.rb', line 21

def host
  @host
end

#portObject

Returns the value of attribute port.



21
22
23
# File 'lib/riak_raw.rb', line 21

def port
  @port
end

#prefixObject

Returns the value of attribute prefix.



21
22
23
# File 'lib/riak_raw.rb', line 21

def prefix
  @prefix
end

Instance Method Details

#bucket(bucket_name, keys = false) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/riak_raw.rb', line 30

def bucket(bucket_name,keys=false)
  #request(:get, build_path(bucket_name))
  response = request(:get, 
    build_path(bucket_name),
    nil, nil,
    {"returnbody" => "true", "keys" => keys})
  if response.code == 200
    if json = response.body
      return Yajl::Parser.parse(json)
    end
  end; nil
end

#delete(bucket_name, key, dw = 2) ⇒ Object

there could be concurrency issues if we don’t force a short sleep after delete. see: issues.basho.com/show_bug.cgi?id=52



81
82
83
84
85
# File 'lib/riak_raw.rb', line 81

def delete(bucket_name, key, dw=2)
  response = request(:delete,
    build_path(bucket_name, key),
    nil, {}, {"dw" => dw})
end

#fetch(bucket_name, key, _params = {}) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/riak_raw.rb', line 70

def fetch(bucket_name, key, _params={})
  params = {}
  _params.each { |k,v| params[k.to_s] = v }
  params["r"] ||= 2
  response = request(:get,
    build_path(bucket_name, key),
    nil, {}, params)
end

#set_bucket_properties(name, props) ⇒ Object



43
44
45
46
47
# File 'lib/riak_raw.rb', line 43

def set_bucket_properties(name,props)
  response = request(:put,
    build_path(name),
    Yajl::Encoder.encode(props), { 'Content-Type' => 'application/json' })
end

#store(bucket_name, key, content, vclock = nil, links = [], content_type = 'application/json', w = 2, dw = 2, r = 2) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/riak_raw.rb', line 49

def store(bucket_name, key, content, vclock=nil, links=[], content_type='application/json', w=2, dw=2, r=2)
  headers = { 'Content-Type' => content_type,
              'X-Riak-ClientId' => self.client_id }
  if vclock
    headers['X-Riak-Vclock'] = vclock
  end

  response = request(:put, 
    build_path(bucket_name,key),
    content, headers,
    {"returnbody" => "false", "w" => w, "dw" => dw})
  
  # returnbody=true could cause issues. instead we'll do a
  # separate fetch. see: https://issues.basho.com/show_bug.cgi?id=52
  if response.code == 204
    response = fetch(bucket_name, key, {:r => r})
  end
  
  response
end