Class: Chewy::Stash::Journal

Inherits:
Index show all
Defined in:
lib/chewy/stash.rb

Class Method Summary collapse

Methods inherited from Index

base_name, build_index_name, define_type, derivable_index_name, derivable_name, index_name, index_params, mappings_hash, method_missing, prefix, prefix_with_deprecation, scopes, settings, settings_hash, specification, specification_hash, type, type_names, types

Class Method Details

.clean(until_time = nil, only: []) ⇒ Object

Cleans up all the journal entries until the specified time. If nothing is specified - cleans up everything.

Parameters:

  • since_time (Time, DateTime)

    the time top boundary

  • only (Chewy::Index, Array<Chewy::Index>) (defaults to: [])

    indexes to clean up journal entries for



35
36
37
38
39
# File 'lib/chewy/stash.rb', line 35

def self.clean(until_time = nil, only: [])
  scope = self.for(only)
  scope = scope.filter(range: {created_at: {lte: until_time}}) if until_time
  scope.delete_all
end

.entries(since_time, only: []) ⇒ Object

Loads all entries since the specified time.

Parameters:

  • since_time (Time, DateTime)

    a timestamp from which we load a journal

  • only (Chewy::Index, Array<Chewy::Index>) (defaults to: [])

    journal entries related to these indices will be loaded only



26
27
28
# File 'lib/chewy/stash.rb', line 26

def self.entries(since_time, only: [])
  self.for(only).filter(range: {created_at: {gt: since_time}})
end

.for(*something) ⇒ Object

Selects all the journal entries for the specified indices.

Parameters:



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chewy/stash.rb', line 44

def self.for(*something)
  something = something.flatten.compact
  types = something.flat_map { |s| Chewy.derive_types(s) }
  return none if something.present? && types.blank?
  scope = all
  types.group_by(&:index).each do |index, index_types|
    scope = scope.or(
      filter(term: {index_name: index.derivable_name})
        .filter(terms: {type_name: index_types.map(&:type_name)})
    )
  end
  scope
end