Class: S3arch::Indexer

Inherits:
Object
  • Object
show all
Defined in:
lib/s3arch/indexer.rb

Instance Method Summary collapse

Constructor Details

#initialize(config: S3arch.configuration) ⇒ Indexer

Returns a new instance of Indexer.



10
11
12
13
14
15
# File 'lib/s3arch/indexer.rb', line 10

def initialize(config: S3arch.configuration)
  config.validate!
  @config = config
  @dynamodb = Aws::DynamoDB::Client.new
  @s3 = Aws::S3::Client.new
end

Instance Method Details

#process_event(event) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/s3arch/indexer.rb', line 30

def process_event(event)
  records = event['Records'] || []
  owner_ids = records.filter_map { |r|
    body = JSON.parse(r['body'])
    @config.owner_extractor.call(body)
  }.uniq

  log(:info, 'Rebuilding indexes', owner_ids: owner_ids, record_count: records.size)
  owner_ids.each { |id| rebuild(id) }
  { statusCode: 200, body: JSON.generate(rebuilt: owner_ids.size) }
end

#rebuild(owner_id) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/s3arch/indexer.rb', line 17

def rebuild(owner_id)
  records = fetch_records(owner_id)
  db_path = "/tmp/s3arch_#{owner_id}.sqlite3"

  build_database(db_path, records)
  upload(owner_id, db_path)
  increment_version(owner_id, records.size)

  log(:info, 'Index rebuilt', owner_id: owner_id, record_count: records.size)
ensure
  File.delete(db_path) if db_path && File.exist?(db_path)
end