Class: AlchemyServer::Phylactery

Inherits:
Object
  • Object
show all
Defined in:
lib/alchemy/phylactery.rb

Instance Method Summary collapse

Constructor Details

#initializePhylactery

Returns a new instance of Phylactery.



8
9
10
11
12
13
# File 'lib/alchemy/phylactery.rb', line 8

def initialize
  @shutdown_mutex = Mutex.new
  @lists = {}
  @list_init_mutexes = {}
  @stats = Hash.new(0)
end

Instance Method Details

#add(key, data) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/alchemy/phylactery.rb', line 26

def add(key, data)
  list = lists(key)
  return false unless list
  
  value = value_for_data(data)
  return false if list.include?(value)
  list.push(value)
  
  return true
end

#closeObject

Safely close all lists.



130
131
132
# File 'lib/alchemy/phylactery.rb', line 130

def close
  @shutdown_mutex.lock
end

#delete(key) ⇒ Object



74
75
76
# File 'lib/alchemy/phylactery.rb', line 74

def delete(key)
	@lists.delete(key)
end

#flush_allObject



78
79
80
81
# File 'lib/alchemy/phylactery.rb', line 78

def flush_all
  @lists = {}
  @list_init_mutexes = {}
end

#get(key) ⇒ Object

CAS command not supported



62
63
64
65
66
# File 'lib/alchemy/phylactery.rb', line 62

def get(key)
  list = lists(key).to_a
  return false if list.empty?
  return list.to_json
end

#gets(keys) ⇒ Object



68
69
70
71
72
# File 'lib/alchemy/phylactery.rb', line 68

def gets(keys)
  all_lists = {}
  keys.each { |key| all_lists[key] = lists(key).to_a }
  return all_lists.to_json
end

#lists(key = nil) ⇒ Object

Returns all active lists.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/alchemy/phylactery.rb', line 86

def lists(key=nil)
  return nil if @shutdown_mutex.locked?

  return @lists if key.nil?
  # First try to return the list named 'key' if it's available.
  return @lists[key] if @lists[key]
  
  @list_init_mutexes[key] ||= Mutex.new
  
  if @list_init_mutexes[key].locked?
    return nil
  else
    @list_init_mutexes[key].lock
    if @lists[key].nil?
      @lists[key] = []
    end
    @list_init_mutexes[key].unlock
  end

  return @lists[key]
end

#prepend(key, data) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/alchemy/phylactery.rb', line 50

def prepend(key, data)
  list = lists(key)
  return false unless list
  
  value = value_for_data(data)
  list.unshift(value)
  
  return true
end

#replace(key, data) ⇒ Object

Special. Expects data to be a JSON array



38
39
40
41
42
43
44
45
46
# File 'lib/alchemy/phylactery.rb', line 38

def replace(key, data)
  list = lists(key)
  return false unless list
  value = JSON.parse(data)
  return false unless value.is_a? Array
  list.replace(value)
  
  return true
end

#set(key, data) ⇒ Object Also known as: append



15
16
17
18
19
20
21
22
23
# File 'lib/alchemy/phylactery.rb', line 15

def set(key, data)
  list = lists(key)
  return false unless list
  
  value = value_for_data(data)
  list.push(value)
  
  return true
end

#stats(stat_name) ⇒ Object

Returns statistic stat_name for the Recipes.

Valid statistics are:

[:get_misses]    Total number of get requests with empty responses
[:get_hits]      Total number of get requests that returned data
[:current_bytes] Current size in bytes of items in the lists
[:current_size]  Current number of items across all lists
[:total_items]   Total number of items stored in lists.


119
120
121
122
123
124
125
# File 'lib/alchemy/phylactery.rb', line 119

def stats(stat_name)
  case stat_name
  when nil; @stats
  when :current_size; current_size
  else; @stats[stat_name]
  end
end