Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save keithrbennett/1007142 to your computer and use it in GitHub Desktop.

Select an option

Save keithrbennett/1007142 to your computer and use it in GitHub Desktop.
Script that illustrates that when an RObject is created, saved, created, deleted, then created, saved again, it is not successfully saved to the riak data store.
#!/usr/bin/env ruby
# Script that illustrates that when an RObject is created, saved,
# created, deleted, then created, saved again, it is not
# successfully saved to the riak data store.
require 'rubygems'
require 'riak'
CLIENT = Riak::Client.new
# Convenience method to create an RObject in a single call
# * *Args*
# - bucket to access
# - key to select content
# - contents to store in riak
# - content_type of data to be stored.
# - custom_metadata - hash of metadata key, values to store with document content
# - links - set of Riak::Link(s) to store with document content
def create_robject(bucket_name, key, contents,
content_type = 'application/octet-stream', custom_metadata=nil, links=nil)
obj = Riak::RObject.new(CLIENT[bucket_name], key)
obj.data = contents
obj.content_type = content_type
obj.meta = custom_metadata
obj.links = links
obj
end
def key_exists?(bucket, key)
exists = true
begin
CLIENT[bucket][key]
puts 'found it!'
rescue Riak::HTTPFailedRequest
puts 'didnt find it!'
exists = false
end
exists
end
BUCKET = 'mybucket'
KEY = 'mykey'
CONTENT = 'my content'
MIME_TYPE = 'application/text'
def create_and_store_an_object
x = create_robject(BUCKET, KEY, CONTENT, MIME_TYPE)
x.store
puts key_exists?(BUCKET, KEY)
end
def delete_an_object
obj = CLIENT[BUCKET][KEY]
obj.delete
puts key_exists?(BUCKET, KEY)
end
create_and_store_an_object
delete_an_object
create_and_store_an_object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment