sdbcli 0.2.8 → 0.2.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README +84 -75
- data/bin/sdbcli +6 -4
- data/lib/sdbcli/sdb-client.rb +132 -128
- data/lib/sdbcli/sdb-driver.rb +184 -173
- data/lib/sdbcli/sdb-parser.tab.rb +149 -118
- data/lib/sdbcli/sdb-parser.y +253 -242
- data/lib/sdbcli/sdb-runner.rb +81 -79
- data/lib/sdbcli.rb +3 -3
- metadata +49 -33
data/lib/sdbcli/sdb-driver.rb
CHANGED
@@ -1,173 +1,184 @@
|
|
1
|
-
require 'sdbcli/sdb-client'
|
2
|
-
|
3
|
-
module SimpleDB
|
4
|
-
class Error < StandardError; end
|
5
|
-
|
6
|
-
class Driver
|
7
|
-
attr_accessor :iteratable
|
8
|
-
|
9
|
-
def initialize(accessKeyId, secretAccessKey, endpoint = 'sdb.amazonaws.com')
|
10
|
-
@client = Client.new(accessKeyId, secretAccessKey, endpoint)
|
11
|
-
end
|
12
|
-
|
13
|
-
def endpoint
|
14
|
-
@client.endpoint
|
15
|
-
end
|
16
|
-
|
17
|
-
def endpoint=(v)
|
18
|
-
@client.endpoint = v
|
19
|
-
end
|
20
|
-
|
21
|
-
# domain action
|
22
|
-
|
23
|
-
def create_domain(domain_name)
|
24
|
-
@client.create_domain(domain_name)
|
25
|
-
end
|
26
|
-
|
27
|
-
def show_domains
|
28
|
-
domains = []
|
29
|
-
|
30
|
-
iterate(:list_domains) do |doc|
|
31
|
-
doc.css('DomainName').each do |i|
|
32
|
-
domains << i.content
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
return domains
|
37
|
-
end
|
38
|
-
|
39
|
-
def drop_domain(domain_name)
|
40
|
-
@client.delete_domain(domain_name)
|
41
|
-
end
|
42
|
-
|
43
|
-
# attr action
|
44
|
-
|
45
|
-
def insert(domain_name, item_name, attrs = {}, consistent = false)
|
46
|
-
params = {:ConsistentRead => consistent}
|
47
|
-
i = 0
|
48
|
-
|
49
|
-
attrs.each do |name, values|
|
50
|
-
[values].flatten.each do |v|
|
51
|
-
i += 1
|
52
|
-
params["Attribute.#{i}.Name"] = name
|
53
|
-
params["Attribute.#{i}.Value"] = v
|
54
|
-
params["Attribute.#{i}.Replace"] = false
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
@client.put_attributes(domain_name, item_name, params)
|
59
|
-
end
|
60
|
-
|
61
|
-
def update(domain_name, items = {}, consistent = false)
|
62
|
-
params = {:ConsistentRead => consistent}
|
63
|
-
i = j = 0
|
64
|
-
|
65
|
-
items.each do |item_name, attrs|
|
66
|
-
i += 1
|
67
|
-
params["Item.#{i}.ItemName"] = item_name
|
68
|
-
|
69
|
-
(attrs || {}).each do |attr_name, values|
|
70
|
-
[values].flatten.each do |v|
|
71
|
-
j += 1
|
72
|
-
params["Item.#{i}.Attribute.#{j}.Name"] = attr_name
|
73
|
-
params["Item.#{i}.Attribute.#{j}.Value"] = v
|
74
|
-
params["Item.#{i}.Attribute.#{j}.Replace"] = true
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
@client.batch_put_attributes(domain_name, params)
|
80
|
-
end
|
81
|
-
|
82
|
-
def get(domain_name, item_name, attr_names = [], consistent = false)
|
83
|
-
params = {:ConsistentRead => consistent}
|
84
|
-
attr_names.each_with_index {|name, i| params["AttributeName.#{i}"] = name }
|
85
|
-
doc = @client.get_attributes(domain_name, item_name, params)
|
86
|
-
attrs_to_hash(doc)
|
87
|
-
end
|
88
|
-
|
89
|
-
def select(expr, consistent = false)
|
90
|
-
params = {:SelectExpression => expr, :ConsistentRead => consistent}
|
91
|
-
items = []
|
92
|
-
|
93
|
-
iterate(:select, params) do |doc|
|
94
|
-
doc.css('Item').map do |i|
|
95
|
-
items << [i.at_css('Name').content, attrs_to_hash(i)]
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
return items
|
100
|
-
end
|
101
|
-
|
102
|
-
def delete(domain_name, items = {}, consistent = false)
|
103
|
-
params = {:ConsistentRead => consistent}
|
104
|
-
i = j = 0
|
105
|
-
|
106
|
-
items.each do |item_name, attrs|
|
107
|
-
i += 1
|
108
|
-
params["Item.#{i}.ItemName"] = item_name
|
109
|
-
|
110
|
-
(attrs || []).each do |attr_name, values|
|
111
|
-
[values].flatten.each do |v|
|
112
|
-
j += 1
|
113
|
-
params["Item.#{i}.Attribute.#{j}.Name"] = attr_name
|
114
|
-
params["Item.#{i}.Attribute.#{j}.Value"] = v if v
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
@client.batch_delete_attributes(domain_name, params)
|
120
|
-
end
|
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
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
1
|
+
require 'sdbcli/sdb-client'
|
2
|
+
|
3
|
+
module SimpleDB
|
4
|
+
class Error < StandardError; end
|
5
|
+
|
6
|
+
class Driver
|
7
|
+
attr_accessor :iteratable
|
8
|
+
|
9
|
+
def initialize(accessKeyId, secretAccessKey, endpoint = 'sdb.amazonaws.com')
|
10
|
+
@client = Client.new(accessKeyId, secretAccessKey, endpoint)
|
11
|
+
end
|
12
|
+
|
13
|
+
def endpoint
|
14
|
+
@client.endpoint
|
15
|
+
end
|
16
|
+
|
17
|
+
def endpoint=(v)
|
18
|
+
@client.endpoint = v
|
19
|
+
end
|
20
|
+
|
21
|
+
# domain action
|
22
|
+
|
23
|
+
def create_domain(domain_name)
|
24
|
+
@client.create_domain(domain_name)
|
25
|
+
end
|
26
|
+
|
27
|
+
def show_domains
|
28
|
+
domains = []
|
29
|
+
|
30
|
+
iterate(:list_domains) do |doc|
|
31
|
+
doc.css('DomainName').each do |i|
|
32
|
+
domains << i.content
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
return domains
|
37
|
+
end
|
38
|
+
|
39
|
+
def drop_domain(domain_name)
|
40
|
+
@client.delete_domain(domain_name)
|
41
|
+
end
|
42
|
+
|
43
|
+
# attr action
|
44
|
+
|
45
|
+
def insert(domain_name, item_name, attrs = {}, consistent = false)
|
46
|
+
params = {:ConsistentRead => consistent}
|
47
|
+
i = 0
|
48
|
+
|
49
|
+
attrs.each do |name, values|
|
50
|
+
[values].flatten.each do |v|
|
51
|
+
i += 1
|
52
|
+
params["Attribute.#{i}.Name"] = name
|
53
|
+
params["Attribute.#{i}.Value"] = v
|
54
|
+
params["Attribute.#{i}.Replace"] = false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
@client.put_attributes(domain_name, item_name, params)
|
59
|
+
end
|
60
|
+
|
61
|
+
def update(domain_name, items = {}, consistent = false)
|
62
|
+
params = {:ConsistentRead => consistent}
|
63
|
+
i = j = 0
|
64
|
+
|
65
|
+
items.each do |item_name, attrs|
|
66
|
+
i += 1
|
67
|
+
params["Item.#{i}.ItemName"] = item_name
|
68
|
+
|
69
|
+
(attrs || {}).each do |attr_name, values|
|
70
|
+
[values].flatten.each do |v|
|
71
|
+
j += 1
|
72
|
+
params["Item.#{i}.Attribute.#{j}.Name"] = attr_name
|
73
|
+
params["Item.#{i}.Attribute.#{j}.Value"] = v
|
74
|
+
params["Item.#{i}.Attribute.#{j}.Replace"] = true
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
@client.batch_put_attributes(domain_name, params)
|
80
|
+
end
|
81
|
+
|
82
|
+
def get(domain_name, item_name, attr_names = [], consistent = false)
|
83
|
+
params = {:ConsistentRead => consistent}
|
84
|
+
attr_names.each_with_index {|name, i| params["AttributeName.#{i}"] = name }
|
85
|
+
doc = @client.get_attributes(domain_name, item_name, params)
|
86
|
+
attrs_to_hash(doc)
|
87
|
+
end
|
88
|
+
|
89
|
+
def select(expr, consistent = false)
|
90
|
+
params = {:SelectExpression => expr, :ConsistentRead => consistent}
|
91
|
+
items = []
|
92
|
+
|
93
|
+
iterate(:select, params) do |doc|
|
94
|
+
doc.css('Item').map do |i|
|
95
|
+
items << [i.at_css('Name').content, attrs_to_hash(i)]
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
return items
|
100
|
+
end
|
101
|
+
|
102
|
+
def delete(domain_name, items = {}, consistent = false)
|
103
|
+
params = {:ConsistentRead => consistent}
|
104
|
+
i = j = 0
|
105
|
+
|
106
|
+
items.each do |item_name, attrs|
|
107
|
+
i += 1
|
108
|
+
params["Item.#{i}.ItemName"] = item_name
|
109
|
+
|
110
|
+
(attrs || []).each do |attr_name, values|
|
111
|
+
[values].flatten.each do |v|
|
112
|
+
j += 1
|
113
|
+
params["Item.#{i}.Attribute.#{j}.Name"] = attr_name
|
114
|
+
params["Item.#{i}.Attribute.#{j}.Value"] = v if v
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
@client.batch_delete_attributes(domain_name, params)
|
120
|
+
end
|
121
|
+
|
122
|
+
def describe(domain_name)
|
123
|
+
doc = @client.domain_metadata(domain_name)
|
124
|
+
h = {}
|
125
|
+
|
126
|
+
doc.at_css('DomainMetadataResult').children.each do |child|
|
127
|
+
h[child.name] = child.content
|
128
|
+
end
|
129
|
+
|
130
|
+
return h
|
131
|
+
end
|
132
|
+
|
133
|
+
private
|
134
|
+
|
135
|
+
def attrs_to_hash(node)
|
136
|
+
h = {}
|
137
|
+
|
138
|
+
node.css('Attribute').map do |i|
|
139
|
+
name = i.at_css('Name').content
|
140
|
+
value = i.at_css('Value').content
|
141
|
+
|
142
|
+
if h[name].kind_of?(Array)
|
143
|
+
h[name] << value
|
144
|
+
elsif h.has_key?(name)
|
145
|
+
h[name] = [h[name], value]
|
146
|
+
else
|
147
|
+
h[name] = value
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
return h
|
152
|
+
end
|
153
|
+
|
154
|
+
def iterate(method, params = {})
|
155
|
+
Iterator.new(@client, method, params, @iteratable).each do |doc|
|
156
|
+
yield(doc)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
class Iterator
|
161
|
+
def initialize(client, method, params = {}, iteratable = false)
|
162
|
+
@client = client
|
163
|
+
@method = method
|
164
|
+
@params = params.dup
|
165
|
+
@token = :first
|
166
|
+
@iteratable = iteratable
|
167
|
+
end
|
168
|
+
|
169
|
+
def each
|
170
|
+
while @token
|
171
|
+
@params.update(:NextToken => @token.content) if @token != :first
|
172
|
+
doc = @client.send(@method, @params)
|
173
|
+
yield(doc)
|
174
|
+
|
175
|
+
if @iteratable
|
176
|
+
@token = doc.at_css('NextToken')
|
177
|
+
else
|
178
|
+
@token = nil
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end # Iterator
|
183
|
+
end # Driver
|
184
|
+
end # SimpleDB
|