Last active
December 25, 2015 20:09
-
-
Save gerep/7033149 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RiakWrapper | |
def self.setup | |
walk_server = WalkServer.first | |
@client ||= Riak::Client.new(:protocol => "pbc", :host => walk_server.host, :pb_port => walk_server.pb_port) | |
end | |
def self.exists?(key, bucket_name) | |
get_bucket(bucket_name) | |
@bucket.exists?(key) | |
end | |
def self.get(key, bucket_name) | |
get_bucket(bucket_name) | |
@bucket.get(key) | |
end | |
def self.delete(key, bucket_name) | |
get_bucket(bucket_name) | |
@bucket.delete(key) if exists?(key, bucket_name) | |
end | |
def self.get_bucket(bucket_name) | |
setup | |
@bucket = @client.bucket(bucket_name) | |
end | |
end | |
# USAGE EXAMPLE | |
RiakWrapper.delete("#dg_123456", 'terminals') | |
RiakWrapper.exists?("bm_654321.dat", 'files') | |
RiakWrapper.get("ig_13579.xml", 'files') | |
# REAL USE | |
RiakWrapper.delete("#{acronym}_#{self.number}", 'terminals') | |
RiakWrapper.delete("#{acronym}_#{self.number}_params.dat", 'assets') | |
RiakWrapper.delete("#{acronym}_#{self.number}_params.dat", 'files') |
Author
gerep
commented
Oct 17, 2013
class RiakWrapper
def initialize
walk_server = WalkServer.first
@client = Riak::Client.new(:protocol => "pbc", :host => walk_server.host, :pb_port => walk_server.pb_port)
end
def exists?(key, bucket_name)
bucket(bucket_name).exists?(key)
end
def get(key, bucket_name)
bucket(bucket_name).get(key)
end
def delete(key, bucket_name)
bucket(bucket_name).delete(key) if exists?(key, bucket_name)
end
def bucket(bucket_name)
@client.bucket(bucket_name)
end
def self.current
Thread.local[:current_riak_wrapper] ||= new
end
def self.reset
Thread.local[:current_riak_wrapper] = nil
end
end
# USAGE EXAMPLE
RiakWrapper.current.delete("#dg_123456", 'terminals')
RiakWrapper.current.exists?("bm_654321.dat", 'files')
RiakWrapper.current.get("ig_13579.xml", 'files')
# REAL USE
RiakWrapper.current.delete("#{acronym}_#{self.number}", 'terminals')
RiakWrapper.current.delete("#{acronym}_#{self.number}_params.dat", 'assets')
RiakWrapper.current.delete("#{acronym}_#{self.number}_params.dat", 'files')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment