-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleDBContext.cs
259 lines (230 loc) · 8.29 KB
/
SimpleDBContext.cs
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using System;
using System.Linq;
using System.Threading;
using System.Collections.Generic;
using Amazon;
using Amazon.SimpleDB;
using Amazon.SimpleDB.Model;
namespace SimpleDBAdmin
{
public class DomainMetaData
{
public int AttributeNameCount { get; set; }
public long AttributeNamesSizeBytes { get; set; }
public int AttributeValueCount { get; set; }
public long AttributeValuesSizeBytes { get; set; }
public int ItemCount { get; set; }
public long ItemNamesSizeBytes { get; set; }
}
[Serializable]
public struct KeyValuePair<K, V>
{
public K Key { get; set; }
public V Value { get; set; }
}
[Serializable]
public class SimpleDBItem : List<KeyValuePair<string, string>>
{
public string Name { get; set; }
public SimpleDBItem()
{
}
public SimpleDBItem( IEnumerable<KeyValuePair<string, string>> Attributes ) : base( Attributes )
{
}
public IEnumerable<string> this[string key]
{
get
{
return
from m in this
where m.Key.Equals( key )
orderby m.Key, m.Value
select m.Value;
}
}
public string this[string key, int index]
{
get
{
return this[key].Skip( index ).FirstOrDefault();
}
}
public Dictionary<string, int> GetAttributeCounts()
{
return
(
from a in this.Select( m => m.Key )
group a by a into g select g
)
.ToDictionary( g => g.Key, g => g.Count() );
}
}
public class SimpleDBContext
{
private const int MaxRetryCount = 3;
private IAmazonSimpleDB Client { get; set; }
/// <summary>
/// A list of all the domains currently present in the SimpleDB service.
/// </summary>
public List<string> Domains { get; private set; }
/// <summary>
/// Creates the SimpleDB context on which queries, inserts, updates, and
/// deletes can be made.
/// </summary>
/// <param name="awsAccessKey">Your public AWS key.</param>
/// <param name="awsSecretAccessKey">Your secret AWS key.</param>
/// <param name="serviceUrl">http://docs.aws.amazon.com/general/latest/gr/rande.html#sdb_region</param>
public SimpleDBContext( string awsAccessKey, string awsSecretAccessKey, string serviceUrl )
{
// Create the SimpleDB client
this.Client = AWSClientFactory.CreateAmazonSimpleDBClient
(
awsAccessKey, awsSecretAccessKey,
new Amazon.SimpleDB.AmazonSimpleDBConfig
{
ServiceURL = serviceUrl
}
);
// Get the current domain names
var r = this.Client.ListDomains();
this.Domains = r.DomainNames;
this.Domains.Sort();
}
public bool DomainExists( string name )
{
return this.Domains.Any( n => n.Equals( name, System.StringComparison.OrdinalIgnoreCase ) );
}
public void CreateDomain( string name )
{
if( this.DomainExists( name ) ) return;
this.Client.CreateDomain( new CreateDomainRequest { DomainName = name } );
this.Domains.Add( name );
this.Domains.Sort();
}
public DomainMetaData GetDomainDetails( string name )
{
var r = this.Client.DomainMetadata( new DomainMetadataRequest { DomainName = name } );
return new DomainMetaData
{
AttributeNameCount = r.AttributeNameCount,
AttributeNamesSizeBytes = r.AttributeNamesSizeBytes,
AttributeValueCount = r.AttributeValueCount,
AttributeValuesSizeBytes = r.AttributeValuesSizeBytes,
ItemCount = r.ItemCount,
ItemNamesSizeBytes = r.ItemNamesSizeBytes,
};
}
public void DeleteDomain( string name )
{
if( !this.DomainExists( name ) ) return;
this.Client.DeleteDomain( new DeleteDomainRequest { DomainName = name } );
this.Domains.Remove( name );
}
public void Insert( string Domain, List<KeyValuePair<string, string>> Attributes )
{
this.Client.PutAttributes
(
new PutAttributesRequest
{
DomainName = Domain,
ItemName = Guid.NewGuid().ToString(),
Attributes =
(
from a in Attributes
select new ReplaceableAttribute
{
Name = a.Key, Value = a.Value
}
)
.ToList()
}
);
}
public void Update( string Domain, string Item, List<KeyValuePair<string, string>> Attributes )
{
this.Client.PutAttributes
(
new PutAttributesRequest
{
DomainName = Domain,
ItemName = Item,
Attributes =
(
from a in Attributes
select new ReplaceableAttribute
{
Name = a.Key,
Value = a.Value,
Replace = true
}
)
.ToList()
}
);
}
public void Delete( string Domain, string Item )
{
this.Client.DeleteAttributes( new DeleteAttributesRequest
{
ItemName = Item,
DomainName = Domain
} );
}
public IEnumerable<SimpleDBItem> Select( string query )
{
string nextToken = null;
var random = new Random();
do
{
bool retry;
var retryCount = 0;
do
{
var r = this.Client.Select( new SelectRequest
{
SelectExpression = query,
NextToken = nextToken
} );
switch( r.HttpStatusCode )
{
case System.Net.HttpStatusCode.OK:
// Convert the items to Key/Value pairs.
// There will be multiple entries with the
// same key so a dictionary wont work.
foreach( var i in r.Items )
{
yield return new SimpleDBItem
(
from a in i.Attributes
select new KeyValuePair<string, string>
{
Key = a.Name,
Value = a.Value
}
)
{
Name = i.Name
};
}
nextToken = r.NextToken;
retry = false;
break;
case System.Net.HttpStatusCode.ServiceUnavailable:
case System.Net.HttpStatusCode.InternalServerError:
retry = true;
retryCount++;
Thread.Sleep( random.Next( 0, (int)Math.Pow( 4, retryCount ) * 100 ) );
break;
default:
// Process 4xx (Client) error
// Cancel the IEnumerable loop
yield break;
}
}
while( retry && retryCount < MaxRetryCount );
}
while( !string.IsNullOrEmpty( nextToken ) );
}
}
}