Class: Corpshort::Backends::Dynamodb

Inherits:
Base
  • Object
show all
Defined in:
lib/corpshort/backends/dynamodb.rb

Instance Method Summary collapse

Methods inherited from Base

#rename_link

Constructor Details

#initialize(table:, region:) ⇒ Dynamodb

Returns a new instance of Dynamodb.



10
11
12
13
# File 'lib/corpshort/backends/dynamodb.rb', line 10

def initialize(table:, region:)
  @table_name = table
  @region = region
end

Instance Method Details



57
58
59
60
61
62
63
64
# File 'lib/corpshort/backends/dynamodb.rb', line 57

def delete_link(link)
  name = link.is_a?(String) ? link : link.name
  table.delete_item(
    key: {
      'name' => name,
    },
  )
end

#dynamodbObject



19
20
21
# File 'lib/corpshort/backends/dynamodb.rb', line 19

def dynamodb
  @dynamodb ||= Aws::DynamoDB::Resource.new(region: @region)
end


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/corpshort/backends/dynamodb.rb', line 41

def get_link(name)
  item = table.query(
    limit: 1,
    select: 'ALL_ATTRIBUTES',
    key_condition_expression: '#n = :name',
    expression_attribute_names: {'#n' => 'name'},
    expression_attribute_values: {":name" => name},
  ).items.first

  if item && !item.empty?
    Link.new(item, backend: self)
  else
    nil
  end
end


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/corpshort/backends/dynamodb.rb', line 76

def list_links(token: nil, limit: 30)
  partition, last_key = parse_token(token)
  limit.times do
    result = table.query(
      index_name: 'updated_at_partition-updated_at-index',
      select: 'ALL_PROJECTED_ATTRIBUTES',
      scan_index_forward: false,
      exclusive_start_key: last_key ? last_key : nil,
      key_condition_expression: 'updated_at_partition = :partition',
      expression_attribute_values: {":partition" => partition.strftime('%Y-%m')},
      limit: limit,
    )

    unless result.items.empty?
      return [result.items.map{ |_| _['name'] }, result.last_evaluated_key&.values_at('updated_at_partition', 'name', 'updated_at')&.join(?:)]
    end

    partition = partition.to_date.prev_month
    last_key = nil
    sleep 0.05
  end
  return [[], nil]
end


66
67
68
69
70
71
72
73
74
# File 'lib/corpshort/backends/dynamodb.rb', line 66

def list_links_by_url(url)
  table.query(
    index_name: 'url-updated_at-index',
    select: 'ALL_PROJECTED_ATTRIBUTES',
    key_condition_expression: '#u = :url',
    expression_attribute_names: {"#u" => 'url'},
    expression_attribute_values: {":url" => url},
  ).items.map { |_| _['name'] }
end


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/corpshort/backends/dynamodb.rb', line 23

def put_link(link, create_only: false)
  table.update_item(
    key: {
      'name' => link.name,
    },
    update_expression: 'SET #u = :url, updated_at_partition = :ts_partition, updated_at = :updated_at',
    condition_expression: create_only ? 'attribute_not_exists(#n)' : nil,
    expression_attribute_names: create_only ? {'#u' => 'url', '#n' => 'name'} : {'#u' => 'url'},
    expression_attribute_values: {
      ':url' => link.url,
      ':ts_partition' => ts_partition(link.updated_at),
      ':updated_at' => link.updated_at.iso8601,
    },
  )
rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException
  raise ConflictError
end

#tableObject



15
16
17
# File 'lib/corpshort/backends/dynamodb.rb', line 15

def table
  @table ||= dynamodb.table(@table_name)
end