class Airspace::Store

The Store is the data access layer that knows how to persist and retrieve all data. There really should never be a need to interact directly with the store, it merely acts as an intermediary between the Redis client and the Dataset/Reader.

Attributes

client[R]

Public Class Methods

new(client) click to toggle source
# File lib/airspace/store.rb, line 17
def initialize(client)
  raise ArgumentError unless client

  @client = client
end

Public Instance Methods

chunk(key, chunk_index) click to toggle source
# File lib/airspace/store.rb, line 77
def chunk(key, chunk_index)
  chunk_key     = key.chunk(chunk_index)
  cached_chunk  = client.get(chunk_key)

  return [] unless cached_chunk

  JSON.parse(cached_chunk)
end
chunks(key, chunk_count) click to toggle source
# File lib/airspace/store.rb, line 65
def chunks(key, chunk_count)
  futures     = chunk_futures(key, chunk_count)
  all_chunks  = []

  futures.each do |chunk_future|
    cached_chunk = chunk_future.value
    all_chunks += JSON.parse(cached_chunk)
  end

  all_chunks
end
delete(key, chunk_count) click to toggle source
# File lib/airspace/store.rb, line 50
def delete(key, chunk_count)
  return false unless exist?(key)

  multi_pipeline do
    client.del(key.root)

    (0...chunk_count).each do |index|
      chunk_key = key.chunk(index)
      client.del(chunk_key)
    end
  end

  true
end
exist?(key) click to toggle source
# File lib/airspace/store.rb, line 23
def exist?(key)
  client.exists(key.root)
end
persist(key, info_hash, chunks, expires_in_seconds) click to toggle source
# File lib/airspace/store.rb, line 27
def persist(key, info_hash, chunks, expires_in_seconds)
  options = make_options(expires_in_seconds)

  multi_pipeline do
    client.set(key.root, info_hash.to_json, options)

    chunks.each_with_index do |chunk, index|
      chunk_key = key.chunk(index)
      client.set(chunk_key, chunk.to_json, options)
    end
  end

  nil
end
retrieve(key) click to toggle source
# File lib/airspace/store.rb, line 42
def retrieve(key)
  return nil unless exist?(key)

  data = client.get(key)

  JSON.parse(data)
end

Private Instance Methods

chunk_futures(key, chunk_count) click to toggle source
# File lib/airspace/store.rb, line 102
def chunk_futures(key, chunk_count)
  futures = []

  client.pipelined do
    (0...chunk_count).each do |index|
      chunk_key = key.chunk(index)
      futures << client.get(chunk_key)
    end
  end

  futures
end
make_options(expires_in_seconds) click to toggle source
# File lib/airspace/store.rb, line 96
def make_options(expires_in_seconds)
  {}.tap do |o|
    o[:ex] = expires_in_seconds if expires_in_seconds
  end
end
multi_pipeline() { || ... } click to toggle source
# File lib/airspace/store.rb, line 88
def multi_pipeline
  client.multi do
    client.pipelined do
      yield
    end
  end
end