Class: Flock::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/flock/client.rb

Constant Summary collapse

STATES =

symbol => state_id map

Flock::Edges::EdgeState::VALUE_MAP.inject({}) do |states, (id, name)|
  states.update name.downcase.to_sym => id
end.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(servers = nil, options = {}) ⇒ Client

takes arguments a list of servers and an options hash to pass to the default service_class, or a service itself



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/flock/client.rb', line 17

def initialize(servers = nil, options = {})
  if graphs = (options.delete(:graphs) || Flock.graphs)
    @graphs = graphs
  end

  @service =
    if servers.nil? or servers.is_a? Array or servers.is_a? String
      self.class.service_class.new(servers, options)
    else
      servers
    end
end

Instance Attribute Details

#graphsObject

Returns the value of attribute graphs.



8
9
10
# File 'lib/flock/client.rb', line 8

def graphs
  @graphs
end

#serviceObject (readonly)

Returns the value of attribute service.



9
10
11
# File 'lib/flock/client.rb', line 9

def service
  @service
end

Class Method Details

.service_classObject



12
# File 'lib/flock/client.rb', line 12

def service_class; Flock.default_service_class ||= Flock::Service end

Instance Method Details

#_cache(op, query) ⇒ Object



40
41
42
43
# File 'lib/flock/client.rb', line 40

def _cache(op, query)
  return yield unless @cache
  @cache[[op, query]] ||= yield
end

#_cache_clearObject



45
46
47
# File 'lib/flock/client.rb', line 45

def _cache_clear
  @cache = {} if @cache
end

#_lookup_graph(key) ⇒ Object

graph name lookup utility methods



122
123
124
125
126
127
128
# File 'lib/flock/client.rb', line 122

def _lookup_graph(key)
  if @graphs.nil? or key.is_a? Integer
    key
  else
    @graphs[key] or raise UnknownGraphError.new(key)
  end
end

#_lookup_states(states) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/flock/client.rb', line 130

def _lookup_states(states)
  states = states.flatten.compact.map do |s|
    if s.is_a? Integer
      s
    else
      STATES[s] or raise UnknownStateError.new(s)
    end
  end
end

#_node_arg(node) ⇒ Object



145
146
147
148
# File 'lib/flock/client.rb', line 145

def _node_arg(node)
  return node.map {|n| n.to_i if n } if node.respond_to? :map
  node.to_i if node
end

#_query_args(args) ⇒ Object



140
141
142
143
# File 'lib/flock/client.rb', line 140

def _query_args(args)
  source, graph, destination, *states = *((args.length == 1) ? args.first : args)
  [_node_arg(source), _lookup_graph(graph), _node_arg(destination), _lookup_states(states)]
end

#cache_locallyObject

local results cache



33
34
35
36
37
38
# File 'lib/flock/client.rb', line 33

def cache_locally
  @cache = {}
  yield
ensure
  @cache = nil
end

#contains(*query) ⇒ Object



56
57
58
59
60
61
# File 'lib/flock/client.rb', line 56

def contains(*query)
  query = _query_args(query)[0, 3]
  _cache :contains, query do
    service.contains(*query)
  end
end

#current_transactionObject



111
112
113
# File 'lib/flock/client.rb', line 111

def current_transaction
  Thread.current[:edge_transaction]
end

#in_transaction?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/flock/client.rb', line 115

def in_transaction?
  !!Thread.current[:edge_transaction]
end

#select(*query) ⇒ Object

queries



52
53
54
# File 'lib/flock/client.rb', line 52

def select(*query)
  Flock::SimpleOperation.new(self, _query_args(query))
end

#size(*query) ⇒ Object Also known as: count



63
64
65
66
67
# File 'lib/flock/client.rb', line 63

def size(*query)
  _cache :size, query do
    select(*query).size
  end
end

#transaction(priority = Flock::Priority::High, options = {}, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/flock/client.rb', line 90

def transaction(priority = Flock::Priority::High, options = {}, &block)
  execute_at = options.delete(:execute_at)
  position = options.delete(:position)

  new_transaction = !in_transaction?

  ops =
    if new_transaction
      Thread.current[:edge_transaction] = Flock::ExecuteOperations.new(@service, priority, execute_at)
    else
      current_transaction
    end

  result = yield self
  ops.apply if new_transaction
  result

ensure
  Thread.current[:edge_transaction] = nil if new_transaction
end

#update(method, source_id, graph, destination_id, priority = Flock::Priority::High, options = {}) ⇒ Object

edge manipulation



73
74
75
76
77
78
79
80
81
# File 'lib/flock/client.rb', line 73

def update(method, source_id, graph, destination_id, priority = Flock::Priority::High, options = {})
  execute_at = options.delete(:execute_at)
  position = options.delete(:position)

  _cache_clear
  ops = current_transaction || Flock::ExecuteOperations.new(@service, priority, execute_at)
  ops.send(method, *(_query_args([source_id, graph, destination_id])[0, 3] << position))
  ops.apply unless in_transaction?
end