module Lorentz::Keys

Public Instance Methods

del(*keys) click to toggle source
# File lib/lorentz/keys.rb, line 12
def del(*keys)
  run do
    keys.map{ |k| @db.delete(k) }.compact.size
  end
end
exists(key) click to toggle source
# File lib/lorentz/keys.rb, line 6
def exists(key)
  run do
    !!@db[key]
  end
end
keys(pattern) click to toggle source
# File lib/lorentz/keys.rb, line 18
def keys(pattern)
  run do
    @db.keys.grep(compile(pattern))
  end
end
randomkey() click to toggle source
# File lib/lorentz/keys.rb, line 46
def randomkey
  return nil if @db.empty?
  run do
    @db.keys[rand(@db.keys.length)]
  end
end
rename(key, newkey) click to toggle source
# File lib/lorentz/keys.rb, line 24
def rename(key, newkey)
  if key == newkey
    raise LorentzException, "newkey: #{newkey} must be different than key: #{key}"
  end
  raise LorentzException, "key: #{key} does not exist" unless exists(key)
  save do
    val = get(key)
    del(key)
    set(newkey, val)
  end
end
renamenx(key, newkey) click to toggle source
# File lib/lorentz/keys.rb, line 36
def renamenx(key, newkey)
  if key == newkey
    raise LorentzException, "newkey: #{newkey} must be different than key: #{key}"
  end
  raise LorentzException, "key: #{key} does not exist" unless exists(key)
  return 0 if exists(newkey)
  rename(key, newkey)
  return 1
end