File: table_collection.rb

package info (click to toggle)
ruby-aws-sdk 1.67.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,840 kB
  • sloc: ruby: 28,436; makefile: 7
file content (165 lines) | stat: -rw-r--r-- 5,222 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

module AWS
  class DynamoDB

    # Represents the tables in your account.  Each table is
    # represented by an instance of the {Table} class.
    #
    # ## Schemas
    #
    # Before you can operate on items in a table you must specify the schema.
    # You do this by calling #hash_key= (and optionally #range_key=) on
    # a table.
    #
    #     table = dynamo_db.tables['mytable']
    #     table.hash_key = [:id, :string]
    #
    # @example Creating a Table
    #   table = dynamo_db.tables.create('mytable', 10, 10, :hash_key => { :id => :string })
    #
    # @example Enumerating Tables
    #   dynamo_db.tables.each {|table| puts table.name }
    #
    # @example Getting a Table by Name
    #   table = dynamo_db.tables['mytable']
    #
    class TableCollection

      include Core::Collection::WithLimitAndNextToken

      # Creates a new table.
      #
      #     table = dynamo_db.tables.create('mytable', 25, 25,
      #       :hash_key => { :id => :string })
      #
      # @note Creating a table is an eventualy consistent operation.  You
      #   can not interact with the table until its status
      #   ({Table#status}) is `:active`.
      #
      # @param [String] name The name of the table.
      #
      # @param [Integer] read_capacity_units Sets the minimum
      #   number of reads supported before read requests are throttled.
      #
      # @param [Integer] write_capacity_units Sets the minimum
      #   number of writes supported before writes requests are throttled.
      #
      # @param [Hash] options
      #
      # @option options [Hash] :hash_key A hash key is a combination
      #   of an attribute name and type.  If you want to have the
      #   hash key on the string attribute username you would call #create
      #   with:
      #
      #       :hash_key => { :username => :string }
      #
      #   The other supported types are `:number` and `:binary`.  If you
      #   wanted to set the hash key on a numeric (integer) attribute then you
      #   could call #create with:
      #
      #       :hash_key => { :id => :number }
      #
      #   All tables require a hash key.  If `:hash_key` is not provided
      #   then a default hash key will be provided.  The default hash
      #   key is:
      #
      #       :hash_key => { :id => :string }
      #
      # @option options [String] :range_key You can setup a table to use
      #   composite keys by providing a `:range_key`.  Range keys are
      #   configured the same way as hash keys.  They are useful
      #   for ordering items that share the same hash key.
      #
      # @return [Table] The newly created table.
      #
      def create name, read_capacity_units, write_capacity_units, options = {}

        client_opts = {
          :table_name => name.to_s,
          :key_schema => key_schema(options),
          :provisioned_throughput => {
            :read_capacity_units => read_capacity_units,
            :write_capacity_units => write_capacity_units,
          },
        }

        response = client.create_table(client_opts)

        Table.new(name, :config => config)

      end

      # References a table by name.
      #
      #     dynamo_db.tables["MyTable"]
      #
      # @param [String] name
      # @return [Table] Returns the table with the given name.
      def [] name
        Table.new(name, :config => config)
      end

      # @api private
      protected
      def _each_item next_token, limit, options = {}, &block

        options[:limit] = limit if limit
        options[:exclusive_start_table_name] = next_token if next_token

        response = client.list_tables(options)
        response.data['TableNames'].each do |name|
          yield Table.new(name, :config => config)
        end

        response.data['LastEvaluatedTableName']

      end

      private
      def key_schema options

        hash_key, range_key = options.values_at(:hash_key, :range_key)

        # default options for :hash_key
        hash_key ||= { :id => :string }

        schema = {}
        schema[:hash_key_element] = schema_element(hash_key, "hash")
        if range_key
          schema[:range_key_element] = schema_element(range_key, "range")
        end
        schema

      end

      private
      def schema_element desc, key_type

        (name, type) = desc.to_a.first

        unless [:string, :number, :binary].include?(type)
          msg = "invalid #{key_type} key type, expected :string, :number or :binary"
          raise ArgumentError, msg
        end

        { :attribute_name => name.to_s,
          :attribute_type => type.to_s[0,1].upcase }

      end

    end

  end
end