Class: ApiClientBulkLoader::Client::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/api_client_bulk_loader/client/loader.rb

Overview

Storage class for our bulk loading. Is one per thread.

Instance Method Summary collapse

Constructor Details

#initializeLoader

Returns a new instance of Loader.



5
6
7
# File 'lib/api_client_bulk_loader/client/loader.rb', line 5

def initialize
  reset!
end

Instance Method Details

#fetch(model, values, attribute = :id) ⇒ Object

Fetch the values, given the adapter that pertains to those values.



36
37
38
39
40
41
42
# File 'lib/api_client_bulk_loader/client/loader.rb', line 36

def fetch(model, values, attribute = :id)
  #If any of these values are in the queue, we have to retrieve it.
  retrieve_model_by_attribute!(model, attribute) unless (values & @queued_model_store[model][attribute]).empty?

  #Return all values for keys matching our value(s)
  return  @model_store[model].values_at(*values).compact || []
end

#push(model, values, attribute = :id) ⇒ Object

Push the values into the appropriate queue store, only if they haven’t been run.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/api_client_bulk_loader/client/loader.rb', line 21

def push(model, values, attribute = :id)
  # Idempotent.
  create_model_attribute_store(model, attribute)

  # THIS IS DEBATABLE - NOW WE ASSUME THAT WE ALWAYS QUERY BY A NUMBER! 
  values = values.map{|v| v.to_i }

  # Eliminate those that have already been checked
  values -= @checked_model_store[model][attribute] if(@checked_model_store[model][attribute])

  # or in those values to the queue store.
  @queued_model_store[model][attribute] |= values
end

#reset!Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/api_client_bulk_loader/client/loader.rb', line 9

def reset!
  #Hash with keys being a client model, pointing to hashes with id's as keys and loaded objects as values.
  @model_store = {}

  #Contains each bulk stored model, then containing the attribute to be queried by which then holds an array of values that have yet to be fetched
  @queued_model_store = {}

  #Contains a structure similar to queued model store, but stores which values have already been queried.
  @checked_model_store = {}
end