`rescue in keys': uninitialized constant RiakClient::Error (NameError)

Anand Hegde anandghegde at gmail.com
Sat Jun 30 04:13:54 EDT 2012


Here is my script

require 'riak'
require 'logger'

$log=Logger.new(STDOUT)


class RiakClient
	attr_accessor :client, :bucket

	def initialize(bucket)
		begin
			@client=Riak::Client.new(:nodes => [{:http_port =>
8091},{:http_port =>8092},{:http_port=>8093},{:http_port =>8094}])
			@bucket=@client.bucket(bucket)
		rescue RuntimeError => e
			if $log.info e.message.match(/Connection refused/)
				$log.info "Riak returned with RuntimeError - Connection refused,
possibly due to non availaibility of nodes "
			end
			exit
		end
	end

	def change_bucket(bucket)
		@bucket=bucket
	end

	def bucket_hash()
		return @client[@bucket.name.to_s]
	end

	def get( key)
		if exists?(key)
			obj =@bucket.get(key)
			obj.raw_data
		else
			$log.info "The given key=\" #{key} \" doesnt exist"
		end
	end

	def put( key, data, content_type)
		begin
			my_obj=@bucket.get_or_new(key)
			my_obj.raw_data=data
			my_obj.content_type=content_type
			my_obj.store
		rescue RuntimeError => e
			if $log.info e.message.match(/Connection refused/)
				$log.info "Riak returned with RuntimeError - Connection refused,
possibly due to non availaibility of nodes "
			end
			exit
		end

	end

	def exists?(key, options={})
		begin
			return @bucket.exists?(key, options)
		rescue RuntimeError => e
			$log.info e.message
			$log.info "Riak returned with RuntimeError - Connection refused,
possibly due to non availaibility of nodes "
			exit
		rescue Error => e
			$log.info e.class
		end
	end



	def delete(key, options={})
		begin
			result=@bucket.delete(key, options)
			if result[:code]==404
				$log.info "The given key=\"#{key}\" could not be found"
			end
		rescue Error => e
			$log.info "something went wrong while deleting key=\"#{key}\""
		end
	end

	def keys()
		#NOTE - this is an expensive operation and should never be used.
		begin
			return @bucket.keys
		rescue Error => e
			$log.info "something went wrong when listing keys"
		end
	end

end



if __FILE__ == $0
	my_client=RiakClient.new("doc")
	my_client.put("index2.html", "<html>some other data here</html>", "text/html")
	puts my_client.get("whatever")
	puts my_client.get( "index.html")
	puts "these are the keys in your bucket #{my_client.bucket}"
	puts my_client.keys
	#puts my_client.delete("index.html")

end

I am gettin the following output when only one node is running .


I, [2012-06-30T13:34:28.343606 #4324]  INFO -- : The given key="
whatever " doesnt exist
true
<html>some data here</html>
these are the keys in your bucket #<Riak::Bucket:0x007f8a5b969c58>
Riak::Bucket#keys is an expensive operation that should not be used in
production.
    riak_client.rb:82:in `keys'
    riak_client.rb:98:in `<main>'
riak_client.rb:83:in `rescue in keys': uninitialized constant
RiakClient::Error (NameError)
	from riak_client.rb:81:in `keys'
	from riak_client.rb:98:in `<main>'


But this is my output when 2 of my nodes are up. What is the reason for
this?

I, [2012-06-30T13:40:47.304636 #4723]  INFO -- : The given key="
whatever " doesnt exist
true
<html>some data here</html>
these are the keys in your bucket #<Riak::Bucket:0x007fcbfb163a40>
Riak::Bucket#keys is an expensive operation that should not be used in
production.
    riak_client.rb:82:in `keys'
    riak_client.rb:98:in `<main>'
index.html
index2.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.basho.com/pipermail/riak-users_lists.basho.com/attachments/20120630/0148e433/attachment.html>


More information about the riak-users mailing list