Class: Mara::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/mara/batch.rb

Overview

Note:

This is not the same as a transaction. It only saves on the number of API calls to DynamoDB.

Perform operations in batches.

Examples:

Saving Multiple Records

Mara::Batch.in_batch do
  person1.save
  person2.save
end

Author:

  • Maddie Schipper

Since:

  • 1.0.0

Constant Summary collapse

BATCH_STACK_VAR_NAME =

The name of the thread variable that holds the current batch spec.

Since:

  • 1.0.0

'mara_batch'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBatch

Create a new batch.

Since:

  • 1.0.0



160
161
162
163
# File 'lib/mara/batch.rb', line 160

def initialize
  @batch_id = SecureRandom.uuid
  @operations = []
end

Instance Attribute Details

#batch_idString (readonly)

The current batch id.

Returns:

  • (String)

Since:

  • 1.0.0



154
155
156
# File 'lib/mara/batch.rb', line 154

def batch_id
  @batch_id
end

#operationsArray<Array<Symbol, Hash>> (readonly)

The queue of operations to perform on commit.

Returns:

  • (Array<Array<Symbol, Hash>>)

Since:

  • 1.0.0



148
149
150
# File 'lib/mara/batch.rb', line 148

def operations
  @operations
end

Class Method Details

.delete_model(item) ⇒ Object

Perform a delete model. If there is a current batch it is added to the operation queue. If there is no current batch, this will be forwarded directly to the { Mara::Persistence}.

Parameters:

  • item (Hash)

    The model to perform the action with.

Since:

  • 1.0.0



95
96
97
# File 'lib/mara/batch.rb', line 95

def delete_model(item)
  perform_for_model(:delete_model, item)
end

.delete_model!(item) ⇒ Object

Perform a delete model. If there is a current batch it is added to the operation queue. If there is no current batch, this will be forwarded directly to the { Mara::Persistence}.

Parameters:

  • item (Hash)

    The model to perform the action with.

Since:

  • 1.0.0



107
108
109
# File 'lib/mara/batch.rb', line 107

def delete_model!(item)
  perform_for_model(:delete_model!, item)
end

.in_batch { ... } ⇒ Object

Perform in a batch.

All save/destroy calls on a model will be routed into the current batch.

If there is a error raised all operations will be dropped.

If the error is a { Mara::Rollback} the batch will silently rollback. If not, it will be re-thrown after the rollback.

Yields:

  • The batch operation.

Since:

  • 1.0.0



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mara/batch.rb', line 47

def in_batch
  begin_new_batch
  begin
    yield
  rescue  Mara::Rollback
    abort_current_batch
  # rubocop:disable Lint/RescueException
  rescue Exception => exception
    # rubocop:enable Lint/RescueException
    abort_current_batch
    raise exception
  else
    commit_current_batch
  end
end

.save_model(item) ⇒ Object

Perform a save model. If there is a current batch it is added to the operation queue. If there is no current batch, this will be forwarded directly to the { Mara::Persistence}.

Parameters:

  • item (Hash)

    The model to perform the action with.

Since:

  • 1.0.0



71
72
73
# File 'lib/mara/batch.rb', line 71

def save_model(item)
  perform_for_model(:save_model, item)
end

.save_model!(item) ⇒ Object

Perform a save model. If there is a current batch it is added to the operation queue. If there is no current batch, this will be forwarded directly to the { Mara::Persistence}.

Parameters:

  • item (Hash)

    The model to perform the action with.

Since:

  • 1.0.0



83
84
85
# File 'lib/mara/batch.rb', line 83

def save_model!(item)
  perform_for_model(:save_model!, item)
end

Instance Method Details

#abort_batchvoid

This method returns an undefined value.

Abort the batch and clear the current batch operations.

Since:

  • 1.0.0



198
199
200
201
# File 'lib/mara/batch.rb', line 198

def abort_batch
  @operations = []
   Mara.instrument('batch.abort', batch_id: batch_id)
end

#add(action_name, item) ⇒ void

This method returns an undefined value.

Add an item to the operation queue.

Parameters:

  • action_name (Symbol)

    The action to perform for the item.

  • item (Hash)

    The hash of data for the action.

Since:

  • 1.0.0



174
175
176
177
178
# File 'lib/mara/batch.rb', line 174

def add(action_name, item)
   Mara.instrument('batch.add_item', batch_id: batch_id, action: action_name, item: item) do
    operations << [action_name, item]
  end
end

#commit_batchvoid

This method returns an undefined value.

Perform all the operations in the queue.

Since:

  • 1.0.0



186
187
188
189
190
# File 'lib/mara/batch.rb', line 186

def commit_batch
   Mara.instrument('batch.commit', batch_id: batch_id) do
    execute_commit
  end
end