Class: MatViews::Services::BaseService Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/mat_views/services/base_service.rb

Overview

This class is abstract.

Base class for service objects that operate on PostgreSQL materialised views (create/refresh/delete, schema discovery, quoting, and common response helpers).

Concrete services (e.g., CreateView, RegularRefresh) should inherit from this class.

Examples:

Subclassing BaseService

class MyService < MatViews::Services::BaseService
  private
  def assign_request
  # assign @request hash keys
  end

  def prepare
  # perform pre-flight checks, raise StandardError on failure
  end

  def _run
  # perform the operation, return a MatViews::ServiceResponse
  end
end

Constant Summary collapse

UNKNOWN_ROW_COUNT =

Constant indicating unknown row count

-1
ALLOWED_ROW_STRATEGIES =

Allowed row count strategies

i[none estimated exact].freeze
DEFAULT_ROW_STRATEGY =

Default row count strategy

:estimated
DEFAULT_NIL_STRATEGY =

Default strategy when nil or unrecognized value is given

:none

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, row_count_strategy: DEFAULT_ROW_STRATEGY) ⇒ BaseService

Returns a new instance of BaseService.

Parameters:

  • definition (MatViews::MatViewDefinition)
  • row_count_strategy (Symbol, nil) (defaults to: DEFAULT_ROW_STRATEGY)

    one of ‘:estimated`, `:exact`, or `nil` (default: `:estimated`)



78
79
80
81
82
83
84
# File 'lib/mat_views/services/base_service.rb', line 78

def initialize(definition, row_count_strategy: DEFAULT_ROW_STRATEGY)
  @definition = definition
  @row_count_strategy = extract_row_strategy(row_count_strategy)
  @request = {}
  @response = {}
  @use_transaction = true
end

Instance Attribute Details

#definitionMatViews::MatViewDefinition (readonly)

Returns The target materialised view definition.

Returns:



51
52
53
# File 'lib/mat_views/services/base_service.rb', line 51

def definition
  @definition
end

#requestHash

request hash to be returned in service response

Returns:

  • (Hash)


62
63
64
# File 'lib/mat_views/services/base_service.rb', line 62

def request
  @request
end

#responseHash

response hash to be returned in service response

Returns:

  • (Hash)


67
68
69
# File 'lib/mat_views/services/base_service.rb', line 67

def response
  @response
end

#row_count_strategySymbol? (readonly)

Row count strategy (‘:estimated`, `:exact`, `nil`).

Returns:

  • (Symbol, nil)


57
58
59
# File 'lib/mat_views/services/base_service.rb', line 57

def row_count_strategy
  @row_count_strategy
end

#use_transactionBoolean

wrap in transaction

Returns:

  • (Boolean)


72
73
74
# File 'lib/mat_views/services/base_service.rb', line 72

def use_transaction
  @use_transaction
end

Instance Method Details

#callMatViews::ServiceResponse

Execute the service operation.

Calls #assign_request, #prepare and #_run in order.

Concrete subclasses must implement these methods.

Returns:

Raises:

  • (NotImplementedError)

    if not implemented in subclass



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/mat_views/services/base_service.rb', line 95

def call
  if use_transaction
    ActiveRecord::Base.transaction { run_core }
  else
    run_core
  end
rescue StandardError => e
  # finish pending transaction if any
  # eg: current transaction is aborted, commands ignored until end of transaction block
  error_response(e)
end