Class: Flock::Client

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

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



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/flock/client.rb', line 12

def initialize(servers = nil, options = {})
  options = options.dup
  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.



3
4
5
# File 'lib/flock/client.rb', line 3

def graphs
  @graphs
end

#serviceObject (readonly)

Returns the value of attribute service.



4
5
6
# File 'lib/flock/client.rb', line 4

def service
  @service
end

Class Method Details

.service_classObject



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

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

Instance Method Details

#_cache(op, query) ⇒ Object



36
37
38
39
# File 'lib/flock/client.rb', line 36

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

#_cache_clearObject



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

def _cache_clear
  @cache = {} if @cache
end

#cache_locallyObject

local results cache



29
30
31
32
33
34
# File 'lib/flock/client.rb', line 29

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

#contains(*query) ⇒ Object



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

def contains(*query)
  query = Flock::QueryTerm.new(query, graphs).unapply[0, 3]
  _cache :contains, query do
    service.contains(*query)
  end
end

#current_transactionObject



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

def current_transaction
  Thread.current[:edge_transaction]
end

#get(source, graph, destination) ⇒ Object

Raises:

  • (ArgumentError)


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

def get(source, graph, destination)
  raise ArgumentError unless source.is_a?(Fixnum) && destination.is_a?(Fixnum)

  select(source, graph, destination, [:positive, :removed, :negative, :archived]).edges.paginate(1).current_page.first
end

#in_transaction?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/flock/client.rb', line 120

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

#multi {|select_operations| ... } ⇒ Object

queries

Yields:

  • (select_operations)


47
48
49
50
51
# File 'lib/flock/client.rb', line 47

def multi(&block)
  select_operations = Flock::SelectOperations.new(self)
  yield select_operations
  select_operations
end

#select(*query) ⇒ Object



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

def select(*query)
  query = query.first if query.size == 1 # supports deprecated API [[1, 2, 3]]
  Flock::SimpleOperation.new(self, Flock::QueryTerm.new(query, graphs))
end

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



71
72
73
74
75
# File 'lib/flock/client.rb', line 71

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

#transaction(*args, &block) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/flock/client.rb', line 97

def transaction(*args, &block)
  priority, execute_at, _ = process_args(args)
  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, *args) ⇒ Object

edge manipulation



81
82
83
84
85
86
87
88
# File 'lib/flock/client.rb', line 81

def update(method, source_id, graph, destination_id, *args)
  priority, execute_at, position = process_args(args)

  _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