Class: CloudKit::MemoryTable

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

Overview

A MemoryTable implements the essential pieces of the Rufus Tokyo Table API required for CloudKit’s operation. It is basically a hash of hashes with querying capabilities. None of the data is persisted to disk nor is it designed with production use in mind. The primary purpose is to enable testing and development with CloudKit without depending on binary Tokyo Cabinet dependencies.

Implementing a new adapter for CloudKit means writing an adapter that passes the specs for this one.

Instance Method Summary collapse

Constructor Details

#initializeMemoryTable

Create a new MemoryTable instance.



15
16
17
18
# File 'lib/cloudkit/store/memory_table.rb', line 15

def initialize
  @serial_id = 0
  clear
end

Instance Method Details

#[](key) ⇒ Object

Retrieve the hash record for a given key.



32
33
34
# File 'lib/cloudkit/store/memory_table.rb', line 32

def [](key)
  @hash[key]
end

#[]=(key, record) ⇒ Object

Create a hash record for the given key. Returns the record if valid or nil otherwise. Records are valid if they are hashses with both string keys and string values.



23
24
25
26
27
28
29
# File 'lib/cloudkit/store/memory_table.rb', line 23

def []=(key, record)
  if valid?(record)
    @keys << key unless @hash[key]
    return @hash[key] = record
  end
  nil
end

#clearObject

Clear the contents of the store.



37
38
39
40
# File 'lib/cloudkit/store/memory_table.rb', line 37

def clear
  @hash = {}
  @keys = []
end

#generate_unique_idObject

Generate a unique ID within the scope of this store.



48
49
50
# File 'lib/cloudkit/store/memory_table.rb', line 48

def generate_unique_id
  @serial_id += 1
end

#keysObject

Return an ordered set of all keys in the store.



43
44
45
# File 'lib/cloudkit/store/memory_table.rb', line 43

def keys
  @keys
end

#query(&block) ⇒ Object

Run a query configured by the provided block. If no block is provided, all records are returned. Each record contains the original hash key/value pairs, plus the primary key (indexed by :pk => value).



55
56
57
58
59
60
# File 'lib/cloudkit/store/memory_table.rb', line 55

def query(&block)
  return @keys.map { |key| @hash[key].merge(:pk => key) } unless block
  q = MemoryQuery.new
  block.call(q)
  q.run(self)
end