Skip to content

Instantly share code, notes, and snippets.

@choonkeat
Created June 26, 2009 17:32
Show Gist options
  • Save choonkeat/136613 to your computer and use it in GitHub Desktop.
Save choonkeat/136613 to your computer and use it in GitHub Desktop.
# Monkey-patch to Rufus::Tokyo::Cabinet to work with some Array & Hash values
Rufus::Tokyo::Cabinet.class_eval do
# list of [[count1, value2], [count1, value2], ...] is sorted by count*
def append_list(key, object, sort = false, max_length = nil)
list = get_object(key) || []
list << object
list.sort! {|a,b| b[0] <=> a[0] } if sort
list.slice!(max_length..-1) if max_length
set_object(key, list)
list
end
# list of [[count1, value2], [count1, value2], ...] is sorted by count*
# assumes value* is unique
def merge_hash(key, object, sort = true, max_length = nil)
list = get_object(key) || []
# find a match on value* and replace the count*
found = list.find {|pair| (pair[1] == object[1] ? (pair[0] = object[0]; true) : false) }
list << object if not found
list.sort! {|a,b| b[0] <=> a[0] } if sort
list.slice!(max_length..-1) if max_length
set_object(key, list)
list
end
def set_object(key, object)
self[key] = object && Marshal.dump(object)
end
def get_object(key)
raw = self[key]
raw && Marshal.load(raw)
end
def getint(key)
self.addint(key, 0)
end
end
# Re-enabling functionality - Didn't think the wrapper library is the
# right place to disable functionality provided in underlying library
Rufus::Tokyo::Tyrant.class_eval do
def copy (target_path)
lib.abs_copy(@db, target_path)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment