class Franz::Sash

Sash - A threadsafe hash/array hybrid with access times

@example

s = Sash.new           # => #<Sash...>
s.keys                 # => []
s.insert :key, :value  # => value
s.get :key             # => [:value]
s.insert :key, :crazy  # => :crazy
s.mtime :key           # => 2014-02-18 21:24:30 -0800
s.flush :key           # => [:value, :crazy]

Think of it like a Hash where the keys map to “value buffers”

Public Class Methods

new() click to toggle source

Create a new, empty Sash.

# File lib/franz/sash.rb, line 20
def initialize
  @mutex = Mutex.new
  @mtime = Hash.new { |default, key| default[key] = nil }
  @hash  = Hash.new { |default, key| default[key] = []  }
  @size  = Hash.new { |default, key| default[key] = 0   }
end

Public Instance Methods

flush(key) click to toggle source

Flush and return a key’s value buffer.

@param [Object] key

@return [Array<Object>]

# File lib/franz/sash.rb, line 81
def flush key
  value = nil
  @mutex.synchronize do
    value       = @hash[key]
    @hash[key]  = []
    @size[key]  = 0
    @mtime[key] = Time.now
  end
  return value
end
get(key ;) click to toggle source

Return a key’s value buffer.

@param [Object] key

@return [Array<Object>]

# File lib/franz/sash.rb, line 57
def get key ; @hash[key] end
insert(key, value) click to toggle source

Insert a value into a key’s value buffer.

@param key [Object] @param value [Object]

@return [Object] the value

# File lib/franz/sash.rb, line 43
def insert key, value
  @mutex.synchronize do
    @hash[key] << value
    @size[key] += 1
    @mtime[key] = Time.now
  end
  return value
end
keys() click to toggle source

Grab a list of known keys.

@return [Array<Object>]

# File lib/franz/sash.rb, line 30
def keys ; @hash.keys end
length() click to toggle source

Return the number of keys.

@return [Integer]

# File lib/franz/sash.rb, line 35
def length ; keys.length ; end
mtime(key ;) click to toggle source

Return the last time the key’s value buffer was modified.

@param [Object] key

@return [Time]

# File lib/franz/sash.rb, line 74
def mtime key ; @mtime[key] end
remove(key) click to toggle source

Remove and return a key’s value buffer.

@param [Object] key

@return [Array<Object>]

# File lib/franz/sash.rb, line 64
def remove key
  @size[key] -= 1
  @hash.delete(key)
end
size(key) click to toggle source

Return the size of a key’s value buffer.

@param [Object] key

@return [Integer]

# File lib/franz/sash.rb, line 97
def size key
  @size[key]
end