Class: DynamoDB::Driver

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ddbcli/ddb-driver.rb

Defined Under Namespace

Classes: Rownum

Constant Summary collapse

MAX_NUMBER_BATCH_PROCESS_ITEMS =
25

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(accessKeyId, secretAccessKey, endpoint_or_region) ⇒ Driver

Rownum



24
25
26
27
28
# File 'lib/ddbcli/ddb-driver.rb', line 24

def initialize(accessKeyId, secretAccessKey, endpoint_or_region)
  @client = DynamoDB::Client.new(accessKeyId, secretAccessKey, endpoint_or_region)
  @consistent = false
  @iteratable = false
end

Instance Attribute Details

#consistentObject

Returns the value of attribute consistent.



40
41
42
# File 'lib/ddbcli/ddb-driver.rb', line 40

def consistent
  @consistent
end

#iteratableObject

Returns the value of attribute iteratable.



41
42
43
# File 'lib/ddbcli/ddb-driver.rb', line 41

def iteratable
  @iteratable
end

Instance Method Details

#execute(query, opts = {}) ⇒ Object



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
# File 'lib/ddbcli/ddb-driver.rb', line 43

def execute(query, opts = {})
  parsed, script_type, script = Parser.parse(query)
  command = parsed.class.name.split('::').last.to_sym

  if command != :NEXT
    @last_action = nil
    @last_parsed = nil
    @last_evaluated_key = nil
  end

  retval = case command
           when :SHOW_TABLES
             do_show_tables(parsed)
           when :SHOW_TABLE_STATUS
             do_show_table_status(parsed)
           when :SHOW_REGIONS
             do_show_regions(parsed)
           when :SHOW_CREATE_TABLE
             do_show_create_table(parsed)
           when :ALTER_TABLE
             do_alter_table(parsed)
           when :ALTER_TABLE_INDEX
             do_alter_table_index(parsed)
           when :USE
             do_use(parsed)
           when :CREATE
             do_create(parsed)
           when :CREATE_LIKE
             do_create_like(parsed)
           when :DROP
             do_drop(parsed)
           when :DESCRIBE
             do_describe(parsed)
           when :SELECT
             do_select('Query', parsed)
           when :SCAN
             do_select('Scan', parsed)
           when :GET
             do_get(parsed)
           when :UPDATE
             do_update(parsed)
           when :UPDATE_ALL
             do_update_all(parsed)
           when :DELETE
             do_delete(parsed)
           when :DELETE_ALL
             do_delete_all(parsed)
           when :INSERT
             do_insert(parsed)
           when :INSERT_SELECT
             do_insert_select('Query', parsed)
           when :INSERT_SCAN
             do_insert_select('Scan', parsed)
           when :NEXT
             if @last_action and @last_parsed and @last_evaluated_key
               do_select(@last_action, @last_parsed, :last_evaluated_key => @last_evaluated_key)
             else
               []
             end
           when :NULL
             nil
           else
             raise 'must not happen'
           end

  begin
    case script_type
    when :ruby
      retval = retval.data if retval.kind_of?(DynamoDB::Iteratorable)
      retval.instance_eval(script)
    when :shell
      retval = retval.data if retval.kind_of?(DynamoDB::Iteratorable)
      IO.popen(script, "r+") do |f|
        f.puts(retval.kind_of?(Array) ? retval.map {|i| i.to_s }.join("\n") : retval.to_s)
        f.close_write
        f.read
      end
    when :overwrite
      open(script, 'wb') {|f| print_json(retval, f, opts.merge(:show_rows => false, :strip => true)) }
      retval = nil
    when :append
      open(script, 'ab') {|f| print_json(retval, f, opts.merge(:show_rows => false, :strip => true)) }
      retval = nil
    else
      retval
    end
  rescue Exception => e
    raise DynamoDB::Error, e.message, e.backtrace
  end
end

#import(table, items) ⇒ Object



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
# File 'lib/ddbcli/ddb-driver.rb', line 134

def import(table, items)
  n = 0

  until (chunk = items.slice!(0, MAX_NUMBER_BATCH_PROCESS_ITEMS)).empty?
    operations = []

    req_hash = {
      'RequestItems' => {
        table => operations,
      },
    }

    chunk.each do |item|
      h = {}

      operations << {
        'PutRequest' => {
          'Item' => h,
        },
      }

      item.each do |name, val|
        h[name] = convert_to_attribute_value(val)
      end
    end

    batch_write_item(req_hash)
    n += chunk.length
  end

  return n
end