Class: DCA::ElasticSearchStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/dca/storage/elasticsearch_storage.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, context, options = {}) ⇒ ElasticSearchStorage

Returns a new instance of ElasticSearchStorage.



5
6
7
8
9
# File 'lib/dca/storage/elasticsearch_storage.rb', line 5

def initialize(connection, context, options = {})
  @connection = connection
  @index = options[:index] || DCA.project_name.underscore
  @type = options[:type] || get_alias(context)
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/dca/storage/elasticsearch_storage.rb', line 3

def type
  @type
end

Class Method Details

.establish_connection(config) ⇒ Object



11
12
13
14
# File 'lib/dca/storage/elasticsearch_storage.rb', line 11

def self.establish_connection(config)
  RestClient.get("http://#{config[:host]}:#{config[:port]}")
  Tire.configure { url "http://#{config[:host]}:#{config[:port]}" }
end

Instance Method Details

#context(object) ⇒ Object



69
70
71
72
73
# File 'lib/dca/storage/elasticsearch_storage.rb', line 69

def context object
  result = self.clone
  result.instance_variable_set :@type, get_alias(object)
  result
end

#create(item) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/dca/storage/elasticsearch_storage.rb', line 36

def create(item)
  item.updated_at = item.created_at = Time.now.utc
  data = hash_from item

  result = Tire.index(@index).store data
  Tire.index(@index).refresh

  item.id = result['_id']
end

#find(position) ⇒ Object



26
27
28
29
30
# File 'lib/dca/storage/elasticsearch_storage.rb', line 26

def find position
  return nil if position.base_id.nil?
  query = Tire.search(@index, type: type) { query { term :base_id, position.base_id } }
  query.results.first
end

#index(&block) ⇒ Object



63
64
65
66
67
# File 'lib/dca/storage/elasticsearch_storage.rb', line 63

def index(&block)
  Tire.index @index do
    instance_eval(&block) if block_given?
  end
end

#refresh(item, state) ⇒ Object



32
33
34
# File 'lib/dca/storage/elasticsearch_storage.rb', line 32

def refresh(item, state)
  send state, item
end

#remove(item) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/dca/storage/elasticsearch_storage.rb', line 54

def remove(item)
  data = hash_from item

  Tire.index(@index) do
    remove data
    refresh
  end
end

#state(position) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/dca/storage/elasticsearch_storage.rb', line 16

def state position
  item = find position
  return :create if item.nil?

  position.id = item[:id]
  return :unmodified if item[:checksum] == position.checksum

  :update
end

#update(item) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/dca/storage/elasticsearch_storage.rb', line 46

def update(item)
  data = hash_from item
  Tire.index(@index) do
    store data
    refresh
  end
end