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



116
117
118
119
120
121
122
# File 'lib/flock/client.rb', line 116

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



124
125
126
127
128
129
130
131
132
# File 'lib/flock/client.rb', line 124

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



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

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



134
135
136
137
# File 'lib/flock/client.rb', line 134

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



105
106
107
# File 'lib/flock/client.rb', line 105

def current_transaction
  Thread.current[:edge_transaction]
end

#in_transaction?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/flock/client.rb', line 109

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, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/flock/client.rb', line 87

def transaction(priority = Flock::Priority::High, &block)
  new_transaction = !in_transaction?

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

  result = yield self if block.arity == 1
  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) ⇒ Object

edge manipulation



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

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