class STRC

Constants

Exception

Public Class Methods

new() click to toggle source
# File lib/strc.rb, line 6
def initialize
        @dict = {}
end

Public Instance Methods

append(key, value) click to toggle source

Append a value to a key

# File lib/strc.rb, line 31
def append(key, value)
        if exists(key)
                item = get(key)
                if item.class == String and value.class == String
                        set(key, item + value)
                else
                        raise STRC::Exception.new "ERR Operation against a key holding the wrong kind of value"
                end
        else
                set(key, value)
        end
        return get(key).length
end
decr(key) click to toggle source

Decrement an integer

# File lib/strc.rb, line 46
def decr(key)
        decrby(key, 1)
end
decrby(key, decrement) click to toggle source

Decrement an integer by a certain amount

# File lib/strc.rb, line 51
def decrby(key, decrement)
        unless exists(key)
                set(key, 0)
        end
        value = get(key)
        if value.class == Fixnum
                set(key, value - decrement)
        else
                raise STRC::Exception.new "ERR Operation against a key holding the wrong kind of value"
        end
        get(key)
end
del(*keys) click to toggle source

Delete a key

# File lib/strc.rb, line 86
def del(*keys)
        keys.each do |key|
                @dict.delete(key)
        end
        return keys.length
end
exists(key) click to toggle source

Returns if key exists

# File lib/strc.rb, line 94
def exists(key)
        @dict.has_key?(key)
end
get(key) click to toggle source

Get the value for the given key

# File lib/strc.rb, line 26
def get(key)
        @dict[key]
end
hdel(key, *fields) click to toggle source

Deletes fields from hash

# File lib/strc.rb, line 399
def hdel(key, *fields)
        hash = get_h(key)
        deleted = 0
        fields.each do |field|
                unless hash.delete(field).nil?
                        deleted += 1
                end
        end
        set(key, hash)
        return deleted
end
hexists(key, field) click to toggle source

Returns whether or not the field exists in key

# File lib/strc.rb, line 412
def hexists(key, field)
        get_h(key).has_key?(field)
end
hget(key, field) click to toggle source

Get value at field in hash at key

# File lib/strc.rb, line 394
def hget(key, field)
        get_h(key)[field]
end
hgetall(key) click to toggle source

Returns an array of all fields and values in hash

# File lib/strc.rb, line 417
def hgetall(key)
        get_h(key).flatten
end
hkeys(key) click to toggle source

Returns array of all keys in hash at key

# File lib/strc.rb, line 422
def hkeys(key)
        get_h(key).keys
end
hlen(key) click to toggle source

Returns number of fields in the hash

# File lib/strc.rb, line 432
def hlen(key)
        get_h(key).length
end
hset(key, field, value) click to toggle source

Set file in hash stored at key to value.

# File lib/strc.rb, line 378
def hset(key, field, value)
        hash = get_h(key)
        hash[field] = value
        set(key, hash)
end
hsetnx(key, field, value) click to toggle source

Sets field in key only if it doesn't exist

# File lib/strc.rb, line 385
def hsetnx(key, field, value)
        unless hexists(key, field)
                hset(key, field, value)
                return true
        end
        return false
end
hvals(key) click to toggle source

Returns array of all values in hash at key

# File lib/strc.rb, line 427
def hvals(key)
        get_h(key).values
end
incr(key) click to toggle source

Increment an integer

# File lib/strc.rb, line 65
def incr(key)
        incrby(key, 1)
end
incrby(key, increment) click to toggle source

Increment an integer by a certain amount

# File lib/strc.rb, line 70
def incrby(key, increment)
        unless exists(key)
                set(key, 0)
        end
        value = get(key)
        if value.class == Fixnum
                set(key, value + increment)
        else
                raise STRC::Exception.new "ERR Operation against a key holding the wrong kind of value"
        end
        get(key)
end
lindex(key, index) click to toggle source

Get an element from a list by its index

# File lib/strc.rb, line 339
def lindex(key, index)
        get_l(key)[index]
end
llen(key) click to toggle source

Returns length of list

# File lib/strc.rb, line 288
def llen(key)
        get_l(key).length
end
lpop(key) click to toggle source

Remove and get the first element in a list

# File lib/strc.rb, line 331
def lpop(key)
        list = get_l(key)
        element = list.shift
        set(key, list)
        return element
end
lpush(key, *values) click to toggle source

Prepend values to a list

# File lib/strc.rb, line 316
def lpush(key, *values)
        list = get_l(key)
        list = values + list
        set(key, list)
        return list.length
end
lpushx(key, *values) click to toggle source

LPUSH if the list exists

# File lib/strc.rb, line 324
def lpushx(key, *values)
        if exists(key)
                lpush(key, *values)
        end
end
lrange(key, start, stop) click to toggle source

Returns elements from key in the given range

# File lib/strc.rb, line 282
def lrange(key, start, stop)
        list = get_l(key)[start..stop]
        return list.nil? ? [] : list
end
lset(key, index, value) click to toggle source

Set value for an element at index in a list

# File lib/strc.rb, line 344
def lset(key, index, value)
        list = get_l(key)
        list[index] = value
        set(key, list)
end
ltrim(key, start, stop) click to toggle source

Trim a list to the specified range

# File lib/strc.rb, line 351
def ltrim(key, start, stop)
        set(key, lrange(key, start, stop))
end
randomkey() click to toggle source

Returns a random key

# File lib/strc.rb, line 99
def randomkey
        @dict.keys.sample
end
rename(key, newkey) click to toggle source

Renames a key. Overwrites destination.

# File lib/strc.rb, line 104
def rename(key, newkey)
        if key == newkey
                raise STRC::Exception.new "ERR Source and destination objects are the same"
        end
        @dict[newkey] = @dict[key]
        del(key)
        return "OK"
end
renamenx(key, newkey) click to toggle source

Renames a key. Does not overwrite destination.

# File lib/strc.rb, line 114
def renamenx(key, newkey)
        if exists(newkey)
                return false
        else
                rename(key, newkey)
                return true
        end
end
rpop(key) click to toggle source

Remove and get the last element in a list

# File lib/strc.rb, line 308
def rpop(key)
        list = get_l(key)
        element = list.pop
        set(key, list)
        return element
end
rpoplpush(source, destination) click to toggle source

Removes an element from the end of one list and puts it at the beginning of another

# File lib/strc.rb, line 357
def rpoplpush(source, destination)
        lpush(destination, rpop(source))
end
rpush(key, *values) click to toggle source

Append values to a list

# File lib/strc.rb, line 293
def rpush(key, *values)
        list = get_l(key)
        list += values
        set(key, list)
        return list.length
end
rpushx(key, *values) click to toggle source

RPUSH if the list exists

# File lib/strc.rb, line 301
def rpushx(key, *values)
        if exists(key)
                rpush(key, *values)
        end
end
sadd(key, *args) click to toggle source

Add a member to a set

# File lib/strc.rb, line 128
def sadd(key, *args)
        if args.length < 1
                raise STRC::Exception.new "Wrong number of arguments (1 for 2)"
        end
        set = get_s(key)
        added = 0
        args.each do |arg|
                unless set.include?(arg)
                        set << arg
                        added += 1
                end
        end
        @dict[key] = set
        return added
end
scard(key) click to toggle source

Gets the length of a set

# File lib/strc.rb, line 248
def scard(key)
        set = get_s(key)
        return set.length
end
sdiff(*keys) click to toggle source

Returns a list of the difference between the first set and subsequent sets

# File lib/strc.rb, line 175
def sdiff(*keys)
        sets = []
        keys.each do |key|
                sets << get_s(key)
        end
        diff = sets.shift
        sets.each do |set|
                diff -= set
        end
        return diff.to_a
end
sdiffstore(destination, *keys) click to toggle source

Similar to SDIFF, but stores in destination instead of returning

# File lib/strc.rb, line 188
def sdiffstore(destination, *keys)
        elements = sdiff(*keys)
        sadd(destination, *elements)
end
set(key, value) click to toggle source

Set a given key to a value

# File lib/strc.rb, line 11
def set(key, value)
        @dict[key] = value
        return "OK"
end
setnx(key, value) click to toggle source

Set the value of a key only if it doesn't already exist.

# File lib/strc.rb, line 17
def setnx(key, value)
        unless exists(key)
                set(key, value)
                return true
        end
        return false
end
sinter(*keys) click to toggle source

Returns a list of m

# File lib/strc.rb, line 155
def sinter(*keys)
        sets = []
        keys.each do |key|
                sets << get_s(key)
        end
        inter = sets.shift
        sets.each do |set|
                inter &= set
        end
        return inter.to_a
end
sinterstore(destination, *keys) click to toggle source

Similar to SINTER, but stores in destination instead of returning

# File lib/strc.rb, line 168
def sinterstore(destination, *keys)
        elements = sinter(*keys)
        sadd(destination, *elements)
end
sismember(key, member) click to toggle source

Determine if the given value is a member of the set at key

# File lib/strc.rb, line 237
def sismember(key, member)
        set = get_s(key)
        return set.include?(member)
end
smembers(key) click to toggle source

Returns an array of members of the set

# File lib/strc.rb, line 243
def smembers(key)
        get_s(key).to_a
end
smove(source, destination, member) click to toggle source

Move an item from one set to another

# File lib/strc.rb, line 194
def smove(source, destination, member)
        unless sismember(source, member)
                return false
        end
        srem(source, member)
        unless sismember(destination, member)
                sadd(destination, member)
        end
        return true
end
spop(key) click to toggle source

Randomly remove and return an item from the set

# File lib/strc.rb, line 230
def spop(key)
        element = srandmember(key)
        srem(key, element)
        return element
end
srandmember(key) click to toggle source

Returns a random element from the set

# File lib/strc.rb, line 225
def srandmember(key)
        get_s(key).to_a.sample
end
srem(key, member) click to toggle source

Remove a member from a set

# File lib/strc.rb, line 145
def srem(key, member)
        if sismember(key, member)
                @dict[key].delete(member)
                return true
        else
                return false
        end
end
sunion(*keys) click to toggle source

Returs a list of all unique items in the given sets

# File lib/strc.rb, line 206
def sunion(*keys)
        sets = []
        keys.each do |key|
                sets << get_s(key)
        end
        union = sets.shift
        sets.each do |set|
                union += set
        end
        return union.to_a
end
sunionstore(destination, *keys) click to toggle source

Similar to SUNION, but stores in destination instead of returning

# File lib/strc.rb, line 219
def sunionstore(destination, *keys)
        elements = sunion(*keys)
        sadd(destination, *elements)
end

Private Instance Methods

get_h(key) click to toggle source

Gets the hash at key. Private.

# File lib/strc.rb, line 366
def get_h(key)
        hash = {}
        if exists(key)
                hash = get(key)
                unless hash.class == Hash
                        raise STRC::Exception.new "ERR Operation against a key holding the wrong kind of value"     
                end
        end
        return hash
end
get_l(key) click to toggle source

Gets a list. Private

# File lib/strc.rb, line 270
def get_l(key)
        list = []
        if exists(key)
                list = get(key)
                unless list.class == Array
                        raise STRC::Exception.new "ERR Operation against a key holding the wrong kind of value"     
                end
        end
        return list
end
get_s(key) click to toggle source

Gets a set. Private

# File lib/strc.rb, line 254
def get_s(key)
        set = Set.new
        if exists(key)
                set = get(key)
                unless set.class == Set
                        raise STRC::Exception.new "ERR Operation against a key holding the wrong kind of value"
                end
        end
        return set
end