Class: Chewy::Journal

Inherits:
Object show all
Defined in:
lib/chewy/journal.rb,
lib/chewy/journal/apply.rb,
lib/chewy/journal/clean.rb,
lib/chewy/journal/entry.rb,
lib/chewy/journal/query.rb

Defined Under Namespace

Modules: Apply, Clean Classes: Entry, Query

Constant Summary collapse

JOURNAL_MAPPING =
{
  journal: {
    properties: {
      index_name: { type: 'string', index: 'not_analyzed' },
      type_name: { type: 'string', index: 'not_analyzed' },
      action: { type: 'string', index: 'not_analyzed' },
      object_ids: { type: 'string', index: 'not_analyzed' },
      created_at: { type: 'date' }
    }
  }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index) ⇒ Journal

Returns a new instance of Journal.



20
21
22
23
# File 'lib/chewy/journal.rb', line 20

def initialize(index)
  @records = []
  @index = index
end

Class Method Details

.apply_changes_from(*args) ⇒ Object



94
95
96
# File 'lib/chewy/journal.rb', line 94

def apply_changes_from(*args)
  Apply.since(*args)
end

.clean_until(*args) ⇒ Object



102
103
104
# File 'lib/chewy/journal.rb', line 102

def clean_until(*args)
  Clean.until(*args)
end

.createObject



76
77
78
79
80
# File 'lib/chewy/journal.rb', line 76

def create
  return if exists?
  Chewy.client.indices.create index: index_name, body: { settings: { index: Chewy.configuration[:index] }, mappings: JOURNAL_MAPPING }
  Chewy.wait_for_status
end

.deleteObject



86
87
88
89
90
91
92
# File 'lib/chewy/journal.rb', line 86

def delete
  result = Chewy.client.indices.delete index: index_name
  Chewy.wait_for_status if result
  result
rescue Elasticsearch::Transport::Transport::Errors::NotFound
  false
end

.delete!Object



82
83
84
# File 'lib/chewy/journal.rb', line 82

def delete!
  delete or raise Elasticsearch::Transport::Transport::Errors::NotFound
end

.entries_from(*args) ⇒ Object



98
99
100
# File 'lib/chewy/journal.rb', line 98

def entries_from(*args)
  Entry.since(*args)
end

.exists?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/chewy/journal.rb', line 61

def exists?
  Chewy.client.indices.exists? index: index_name
end

.index_nameObject



65
66
67
68
69
70
# File 'lib/chewy/journal.rb', line 65

def index_name
  [
    Chewy.configuration[:prefix],
    Chewy.configuration[:journal_name] || 'chewy_journal'
  ].reject(&:blank?).join('_')
end

.type_nameObject



72
73
74
# File 'lib/chewy/journal.rb', line 72

def type_name
  JOURNAL_MAPPING.keys.first
end

Instance Method Details

#add(action_objects) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/chewy/journal.rb', line 25

def add(action_objects)
  @records +=
    action_objects.map do |action, objects|
      {
        index_name: @index.derivable_index_name,
        type_name: @index.type_name,
        action: action,
        object_ids: identify(objects),
        created_at: Time.now.to_i
      }
    end
end

#any_records?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/chewy/journal.rb', line 50

def any_records?
  @records.any?
end

#bulk_bodyObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chewy/journal.rb', line 38

def bulk_body
  @records.map do |record|
    {
      create: {
        _index: self.class.index_name,
        _type: self.class.type_name,
        data: record
      }
    }
  end
end