Class: RocketJob::LookupCollection::BatchUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/rocket_job/lookup_collection.rb

Overview

Internal class for uploading records in batches

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, batch_size:) ⇒ BatchUploader

Returns a new instance of BatchUploader.



37
38
39
40
41
42
43
# File 'lib/rocket_job/lookup_collection.rb', line 37

def initialize(collection, batch_size:)
  @batch_size   = batch_size
  @record_count = 0
  @batch_count  = 0
  @documents    = []
  @collection   = collection
end

Instance Attribute Details

#record_countObject (readonly)

Returns the value of attribute record_count.



27
28
29
# File 'lib/rocket_job/lookup_collection.rb', line 27

def record_count
  @record_count
end

Class Method Details

.upload(collection, **args) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/rocket_job/lookup_collection.rb', line 29

def self.upload(collection, **args)
  writer = new(collection, **args)
  yield(writer)
  writer.record_count
ensure
  writer&.close
end

Instance Method Details

#<<(record) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rocket_job/lookup_collection.rb', line 45

def <<(record)
  raise(ArgumentError, "Record must be a Hash") unless record.is_a?(Hash)

  unless record.key?(:id) || record.key?("id") || record.key?("_id")
    raise(ArgumentError, "Record must include an :id key")
  end

  @documents << record
  @record_count += 1
  @batch_count  += 1
  if @batch_count >= @batch_size
    @collection.insert_many(@documents)
    @documents.clear
    @batch_count = 0
  end

  self
end

#closeObject



64
65
66
# File 'lib/rocket_job/lookup_collection.rb', line 64

def close
  @collection.insert_many(@documents) unless @documents.empty?
end