Class: Airspace::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/airspace/store.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Store

Returns a new instance of Store.

Raises:

  • (ArgumentError)


17
18
19
20
21
# File 'lib/airspace/store.rb', line 17

def initialize(client)
  raise ArgumentError unless client

  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



15
16
17
# File 'lib/airspace/store.rb', line 15

def client
  @client
end

Instance Method Details

#chunk(key, chunk_index) ⇒ Object



77
78
79
80
81
82
83
84
# 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) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/airspace/store.rb', line 23

def exist?(key)
  client.exists(key.root)
end

#persist(key, info_hash, chunks, expires_in_seconds) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/airspace/store.rb', line 42

def retrieve(key)
  return nil unless exist?(key)

  data = client.get(key)

  JSON.parse(data)
end