Class: Delfos::Neo4j::Batch::Execution

Inherits:
Object
  • Object
show all
Defined in:
lib/delfos/neo4j/batch/execution.rb

Constant Summary collapse

BATCH_MUTEX =
Mutex.new

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size:, clock: Time) ⇒ Execution

Returns a new instance of Execution.



34
35
36
37
38
39
40
41
# File 'lib/delfos/neo4j/batch/execution.rb', line 34

def initialize(size:, clock: Time)
  @size                    = size
  @clock                   = clock
  @queries                 = []
  @expires                 = nil
  @commit_url              = nil
  @current_transaction_url = nil
end

Class Attribute Details

.batch=(value) ⇒ Object (writeonly)

Sets the attribute batch

Parameters:

  • value

    the value to set the attribute batch to.



31
32
33
# File 'lib/delfos/neo4j/batch/execution.rb', line 31

def batch=(value)
  @batch = value
end

Instance Attribute Details

#commit_urlObject (readonly)

Returns the value of attribute commit_url.



43
44
45
# File 'lib/delfos/neo4j/batch/execution.rb', line 43

def commit_url
  @commit_url
end

#current_transaction_urlObject (readonly)

Returns the value of attribute current_transaction_url.



43
44
45
# File 'lib/delfos/neo4j/batch/execution.rb', line 43

def current_transaction_url
  @current_transaction_url
end

#expiresObject (readonly)

Returns the value of attribute expires.



43
44
45
# File 'lib/delfos/neo4j/batch/execution.rb', line 43

def expires
  @expires
end

#queriesObject (readonly)

Returns the value of attribute queries.



43
44
45
# File 'lib/delfos/neo4j/batch/execution.rb', line 43

def queries
  @queries
end

#sizeObject (readonly)

Returns the value of attribute size.



43
44
45
# File 'lib/delfos/neo4j/batch/execution.rb', line 43

def size
  @size
end

Class Method Details

.execute!(query, params: {}, size: nil) ⇒ Object



11
12
13
14
15
# File 'lib/delfos/neo4j/batch/execution.rb', line 11

def execute!(query, params: {}, size: nil)
  batch = @batch || new_batch(size || 1_000)

  batch.execute!(query, params: params)
end

.flush!Object



21
22
23
24
25
# File 'lib/delfos/neo4j/batch/execution.rb', line 21

def flush!
  @batch&.flush!
rescue
  reset!
end

.new_batch(size) ⇒ Object



17
18
19
# File 'lib/delfos/neo4j/batch/execution.rb', line 17

def new_batch(size)
  @batch = new(size: size)
end

.reset!Object



27
28
29
# File 'lib/delfos/neo4j/batch/execution.rb', line 27

def reset!
  @batch = nil
end

Instance Method Details

#execute!(query, params: {}, retrying: false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/delfos/neo4j/batch/execution.rb', line 45

def execute!(query, params: {}, retrying: false)
  queries.push([query, params]) unless retrying

  with_retry(retrying) do
    BATCH_MUTEX.synchronize do
      check_for_expiry!

      perform_query(query, params)
      flush_if_required!
    end
  end
end

#flush!Object



58
59
60
61
62
63
64
# File 'lib/delfos/neo4j/batch/execution.rb', line 58

def flush!
  return unless query_count.positive?
  return unless @commit_url
  QueryExecution::Transactional.flush!(@commit_url)

  reset!
end

#query_countObject



66
67
68
# File 'lib/delfos/neo4j/batch/execution.rb', line 66

def query_count
  queries.length
end