-
Notifications
You must be signed in to change notification settings - Fork 37
/
HBaseClient.cs
702 lines (635 loc) · 31.6 KB
/
HBaseClient.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
// Copyright (c) Microsoft Corporation
// 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. You may obtain a copy
// of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
namespace Microsoft.HBase.Client
{
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.HBase.Client.Internal;
using Microsoft.HBase.Client.LoadBalancing;
using Microsoft.HBase.Client.Requester;
using org.apache.hadoop.hbase.rest.protobuf.generated;
using ProtoBuf;
using System.Globalization;
using System.Collections.Generic;
/// <summary>
/// A C# connector to HBase.
/// </summary>
/// <remarks>
/// It currently targets HBase 0.96.2 and HDInsight 3.0 on Microsoft Azure.
/// The communication works through HBase REST (StarGate) which uses ProtoBuf as a serialization format.
///
/// The usage is quite simple:
///
/// <code>
/// var credentials = ClusterCredentials.FromFile("credentials.txt");
/// var client = new HBaseClient(credentials);
/// var version = await client.GetVersionAsync();
///
/// Console.WriteLine(version);
/// </code>
/// </remarks>
public sealed class HBaseClient : IHBaseClient, IDisposable
{
private IWebRequester _requester;
private readonly RequestOptions _globalRequestOptions;
private const string CheckAndPutQuery = "check=put";
private const string CheckAndDeleteQuery = "check=delete";
private const string RowKeyColumnFamilyTimeStampFormat = "{0}/{1}/{2}";
/// <summary>
/// Used to detect redundant calls to <see cref="IDisposable.Dispose"/>.
/// </summary>
private bool _disposed;
/// <summary>
/// Initializes a new instance of the <see cref="HBaseClient"/> class.
/// </summary>
/// <param name="credentials">The credentials.</param>
public HBaseClient(ClusterCredentials credentials)
: this(credentials, RequestOptions.GetDefaultOptions())
{ }
/// <summary>
/// Initializes a new instance of the <see cref="HBaseClient"/> class.
/// </summary>
/// <remarks>
/// To find the cluster vnet domain visit:
/// https://azure.microsoft.com/en-us/documentation/articles/hdinsight-hbase-provision-vnet/
/// </remarks>
/// <param name="numRegionServers">The number of region servers in the cluster.</param>
/// <param name="clusterDomain">The fully-qualified domain name of the cluster.</param>
public HBaseClient(int numRegionServers, string clusterDomain = null)
: this(null, RequestOptions.GetDefaultOptions(), new LoadBalancerRoundRobin(numRegionServers: numRegionServers, clusterDomain: clusterDomain))
{ }
/// <summary>
/// Initializes a new instance of the <see cref="HBaseClient"/> class.
/// </summary>
/// <param name="credentials">The credentials.</param>
/// <param name="globalRequestOptions">The global request options.</param>
/// <param name="loadBalancer">load balancer for vnet modes</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "_requester disposed of in Dispose() if it is an IDisposable")]
public HBaseClient(ClusterCredentials credentials, RequestOptions globalRequestOptions = null, ILoadBalancer loadBalancer = null)
{
_globalRequestOptions = globalRequestOptions ?? RequestOptions.GetDefaultOptions();
_globalRequestOptions.Validate();
if (credentials != null) // gateway mode
{
_requester = new GatewayWebRequester(credentials);
}
else // vnet mode
{
_requester = new VNetWebRequester(loadBalancer);
}
}
/// <summary>
/// Creates a scanner on the server side.
/// The resulting ScannerInformation can be used to read query the CellSets returned by this scanner in the #ScannerGetNext/Async method.
/// </summary>
/// <param name="tableName">the table to scan</param>
/// <param name="scannerSettings">the settings to e.g. set the batch size of this scan</param>
/// <param name="options">the request options, scan requests must set endpoint(Gateway mode) or host(VNET mode) to receive the scan request</param>
/// <returns>A ScannerInformation which contains the continuation url/token and the table name</returns>
public async Task<ScannerInformation> CreateScannerAsync(string tableName, Scanner scannerSettings, RequestOptions options)
{
tableName.ArgumentNotNullNorEmpty("tableName");
scannerSettings.ArgumentNotNull("scannerSettings");
options.ArgumentNotNull("options");
return await options.RetryPolicy.ExecuteAsync(() => CreateScannerAsyncInternal(tableName, scannerSettings, options));
}
private async Task<ScannerInformation> CreateScannerAsyncInternal(string tableName, Scanner scannerSettings, RequestOptions options)
{
using (Response response = await PostRequestAsync(tableName + "/scanner", scannerSettings, options))
{
if (response.WebResponse.StatusCode != HttpStatusCode.Created)
{
using (var output = new StreamReader(response.WebResponse.GetResponseStream()))
{
string message = output.ReadToEnd();
throw new WebException(
string.Format(
"Couldn't create a scanner for table {0}! Response code was: {1}, expected 201! Response body was: {2}",
tableName,
response.WebResponse.StatusCode,
message));
}
}
string location = response.WebResponse.Headers.Get("Location");
if (location == null)
{
throw new ArgumentException("Couldn't find header 'Location' in the response!");
}
return new ScannerInformation(new Uri(location), tableName, response.WebResponse.Headers);
}
}
/// <summary>
/// Deletes scanner.
/// </summary>
/// <param name="tableName">the table the scanner is associated with.</param>
/// <param name="scannerInfo">the scanner information retrieved by #CreateScanner()</param>
/// <param name="options">the request options, scan requests must set endpoint(Gateway mode) or host(VNET mode) to receive the scan request</param>
public async Task DeleteScannerAsync(string tableName, ScannerInformation scannerInfo, RequestOptions options)
{
tableName.ArgumentNotNullNorEmpty("tableName");
scannerInfo.ArgumentNotNull("scannerInfo");
options.ArgumentNotNull("options");
await options.RetryPolicy.ExecuteAsync(() => DeleteScannerAsyncInternal(tableName, scannerInfo, options));
}
private async Task DeleteScannerAsyncInternal(string tableName, ScannerInformation scannerInfo, RequestOptions options)
{
using (Response webResponse = await DeleteRequestAsync<Scanner>(tableName + "/scanner/" + scannerInfo.ScannerId, null, options))
{
if (webResponse.WebResponse.StatusCode != HttpStatusCode.OK)
{
using (var output = new StreamReader(webResponse.WebResponse.GetResponseStream()))
{
string message = output.ReadToEnd();
throw new WebException(
string.Format(
"Couldn't delete scanner {0} associated with {1} table.! Response code was: {2}, expected 200! Response body was: {3}",
scannerInfo.ScannerId,
tableName,
webResponse.WebResponse.StatusCode,
message));
}
}
}
}
public async Task DeleteCellsAsync(string tableName, string rowKey, RequestOptions options = null)
{
tableName.ArgumentNotNullNorEmpty("tableName");
rowKey.ArgumentNotNullNorEmpty("rowKey");
var optionToUse = options ?? _globalRequestOptions;
await optionToUse.RetryPolicy.ExecuteAsync(() => DeleteCellsAsyncInternal(tableName, rowKey, optionToUse));
}
public async Task DeleteCellsAsync(string tableName, string rowKey, string columnFamily, long timestamp, RequestOptions options = null)
{
tableName.ArgumentNotNullNorEmpty("tableName");
rowKey.ArgumentNotNullNorEmpty("rowKey");
columnFamily.ArgumentNotNullNorEmpty("columnFamily");
var optionToUse = options ?? _globalRequestOptions;
await optionToUse.RetryPolicy.ExecuteAsync(() => DeleteCellsAsyncInternal(tableName, String.Format(CultureInfo.InvariantCulture, RowKeyColumnFamilyTimeStampFormat, rowKey, columnFamily, timestamp), optionToUse));
}
private async Task DeleteCellsAsyncInternal(string tableName, string path, RequestOptions options)
{
using (Response webResponse = await DeleteRequestAsync<Scanner>(tableName + "/" + path, null, options))
{
if (webResponse.WebResponse.StatusCode != HttpStatusCode.OK)
{
using (var output = new StreamReader(webResponse.WebResponse.GetResponseStream()))
{
string message = output.ReadToEnd();
throw new WebException(
string.Format(
"Couldn't delete row {0} associated with {1} table.! Response code was: {2}, expected 200! Response body was: {3}",
path,
tableName,
webResponse.WebResponse.StatusCode,
message));
}
}
}
}
/// <summary>
/// Creates a table and/or fully replaces its schema.
/// </summary>
/// <param name="schema">the schema</param>
/// <returns>returns true if the table was created, false if the table already exists. In case of any other error it throws a WebException.</returns>
public async Task<bool> CreateTableAsync(TableSchema schema, RequestOptions options = null)
{
schema.ArgumentNotNull("schema");
var optionToUse = options ?? _globalRequestOptions;
return await optionToUse.RetryPolicy.ExecuteAsync(() => CreateTableAsyncInternal(schema, optionToUse));
}
private async Task<bool> CreateTableAsyncInternal(TableSchema schema, RequestOptions options)
{
if (string.IsNullOrEmpty(schema.name))
{
throw new ArgumentException("schema.name was either null or empty!", "schema");
}
using (Response webResponse = await PutRequestAsync(schema.name + "/schema", null, schema, options))
{
if (webResponse.WebResponse.StatusCode == HttpStatusCode.Created)
{
return true;
}
// table already exits
if (webResponse.WebResponse.StatusCode == HttpStatusCode.OK)
{
return false;
}
// throw the exception otherwise
using (var output = new StreamReader(webResponse.WebResponse.GetResponseStream()))
{
string message = output.ReadToEnd();
throw new WebException(
string.Format(
"Couldn't create table {0}! Response code was: {1}, expected either 200 or 201! Response body was: {2}",
schema.name,
webResponse.WebResponse.StatusCode,
message));
}
}
}
/// <summary>
/// Deletes a table.
/// If something went wrong, a WebException is thrown.
/// </summary>
/// <param name="table">the table name</param>
public async Task DeleteTableAsync(string table, RequestOptions options = null)
{
table.ArgumentNotNullNorEmpty("table");
var optionToUse = options ?? _globalRequestOptions;
await optionToUse.RetryPolicy.ExecuteAsync(() => DeleteTableAsyncInternal(table, optionToUse));
}
public async Task DeleteTableAsyncInternal(string table, RequestOptions options)
{
using (Response webResponse = await DeleteRequestAsync<TableSchema>(table + "/schema", null, options))
{
if (webResponse.WebResponse.StatusCode != HttpStatusCode.OK)
{
using (var output = new StreamReader(webResponse.WebResponse.GetResponseStream()))
{
string message = output.ReadToEnd();
throw new WebException(
string.Format(
"Couldn't delete table {0}! Response code was: {1}, expected 200! Response body was: {2}",
table,
webResponse.WebResponse.StatusCode,
message));
}
}
}
}
/// <summary>
/// Gets the cells asynchronously.
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <param name="rowKey">The row key.</param>
/// <returns></returns>
public async Task<CellSet> GetCellsAsync(string tableName, string rowKey, string columnName = null, string numOfVersions = null, RequestOptions options = null)
{
tableName.ArgumentNotNullNorEmpty("tableName");
rowKey.ArgumentNotNull("rowKey");
var optionToUse = options ?? _globalRequestOptions;
string endpoint = tableName + "/" + rowKey;
if (columnName != null)
{
endpoint += "/" + columnName;
}
string query = null;
if (numOfVersions != null)
{
query = "v=" + numOfVersions;
}
return await optionToUse.RetryPolicy.ExecuteAsync(() => GetRequestAndDeserializeAsync<CellSet>(endpoint, query, optionToUse));
}
/// <summary>
/// Gets the cells asynchronous.
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <param name="rowKeys">The row keys.</param>
/// <param name="options">The request options.</param>
/// <returns>A cell set</returns>
public async Task<CellSet> GetCellsAsync(string tableName, string[] rowKeys, RequestOptions options = null)
{
tableName.ArgumentNotNullNorEmpty("tableName");
rowKeys.ArgumentNotNull("rowKey");
var optionToUse = options ?? _globalRequestOptions;
string endpoint = tableName + "/multiget";
string query = null;
for (var i = 0; i < rowKeys.Length; i++)
{
var prefix = "&";
if (i == 0)
{
prefix = "";
}
query += prefix + "row=" + rowKeys[i];
}
return await optionToUse.RetryPolicy.ExecuteAsync(() => GetRequestAndDeserializeAsync<CellSet>(endpoint, query, optionToUse));
}
/// <summary>
/// Gets the storage cluster status asynchronous.
/// </summary>
/// <returns>
/// </returns>
public async Task<StorageClusterStatus> GetStorageClusterStatusAsync(RequestOptions options = null)
{
var optionToUse = options ?? _globalRequestOptions;
return await optionToUse.RetryPolicy.ExecuteAsync(() => GetRequestAndDeserializeAsync<StorageClusterStatus>("/status/cluster", null, optionToUse));
}
/// <summary>
/// Gets the table information asynchronously.
/// </summary>
/// <param name="table">The table.</param>
/// <returns></returns>
public async Task<TableInfo> GetTableInfoAsync(string table, RequestOptions options = null)
{
table.ArgumentNotNullNorEmpty("table");
var optionToUse = options ?? _globalRequestOptions;
return await optionToUse.RetryPolicy.ExecuteAsync(() => GetRequestAndDeserializeAsync<TableInfo>(table + "/regions", null, optionToUse));
}
/// <summary>
/// Gets the table schema asynchronously.
/// </summary>
/// <param name="table">The table.</param>
/// <returns>
/// </returns>
public async Task<TableSchema> GetTableSchemaAsync(string table, RequestOptions options = null)
{
table.ArgumentNotNullNorEmpty("table");
var optionToUse = options ?? _globalRequestOptions;
return await optionToUse.RetryPolicy.ExecuteAsync(() => GetRequestAndDeserializeAsync<TableSchema>(table + "/schema", null, optionToUse));
}
/// <summary>
/// Gets the version asynchronously.
/// </summary>
/// <returns>
/// </returns>
public async Task<org.apache.hadoop.hbase.rest.protobuf.generated.Version> GetVersionAsync(RequestOptions options = null)
{
var optionToUse = options ?? _globalRequestOptions;
return await optionToUse.RetryPolicy.ExecuteAsync(() => GetRequestAndDeserializeAsync<org.apache.hadoop.hbase.rest.protobuf.generated.Version>("version", null, optionToUse));
}
/// <summary>
/// Lists the tables asynchronously.
/// </summary>
/// <returns>
/// </returns>
public async Task<TableList> ListTablesAsync(RequestOptions options = null)
{
var optionToUse = options ?? _globalRequestOptions;
return await optionToUse.RetryPolicy.ExecuteAsync(() => GetRequestAndDeserializeAsync<TableList>("", null, optionToUse));
}
/// <summary>
/// Modifies a table schema.
/// If necessary it creates a new table with the given schema.
/// If something went wrong, a WebException is thrown.
/// </summary>
/// <param name="table">the table name</param>
/// <param name="schema">the schema</param>
public async Task ModifyTableSchemaAsync(string table, TableSchema schema, RequestOptions options = null)
{
table.ArgumentNotNullNorEmpty("table");
schema.ArgumentNotNull("schema");
var optionToUse = options ?? _globalRequestOptions;
await optionToUse.RetryPolicy.ExecuteAsync(() => ModifyTableSchemaAsyncInternal(table, schema, optionToUse));
}
private async Task ModifyTableSchemaAsyncInternal(string table, TableSchema schema, RequestOptions options)
{
using (Response webResponse = await PostRequestAsync(table + "/schema", schema, options))
{
if (webResponse.WebResponse.StatusCode != HttpStatusCode.OK && webResponse.WebResponse.StatusCode != HttpStatusCode.Created)
{
using (var output = new StreamReader(webResponse.WebResponse.GetResponseStream()))
{
string message = output.ReadToEnd();
throw new WebException(
string.Format(
"Couldn't modify table schema {0}! Response code was: {1}, expected either 200 or 201! Response body was: {2}",
table,
webResponse.WebResponse.StatusCode,
message));
}
}
}
}
/// <summary>
/// Scans the next set of messages.
/// </summary>
/// <param name="scannerInfo">the scanner information retrieved by #CreateScanner()</param>
/// <param name="options">the request options, scan requests must set endpoint(Gateway mode) or host(VNET mode) to receive the scan request</param>
/// <returns>a cellset, or null if the scanner is exhausted</returns>
public async Task<CellSet> ScannerGetNextAsync(ScannerInformation scannerInfo, RequestOptions options)
{
scannerInfo.ArgumentNotNull("scannerInfo");
options.ArgumentNotNull("options");
return await options.RetryPolicy.ExecuteAsync(() => ScannerGetNextAsyncInternal(scannerInfo, options));
}
private async Task<CellSet> ScannerGetNextAsyncInternal(ScannerInformation scannerInfo, RequestOptions options)
{
using (Response webResponse = await GetRequestAsync(scannerInfo.TableName + "/scanner/" + scannerInfo.ScannerId, null, options))
{
if (webResponse.WebResponse.StatusCode == HttpStatusCode.OK)
{
return Serializer.Deserialize<CellSet>(webResponse.WebResponse.GetResponseStream());
}
return null;
}
}
public async Task<IEnumerable<CellSet>> StatelessScannerAsync(string tableName, string optionalRowPrefix = null, string scanParameters = null, RequestOptions options = null)
{
tableName.ArgumentNotNullNorEmpty("tableName");
var optionToUse = options ?? _globalRequestOptions;
var rowPrefix = optionalRowPrefix ?? string.Empty;
return await optionToUse.RetryPolicy.ExecuteAsync(() => StatelessScannerAsyncInternal(tableName, rowPrefix, scanParameters, optionToUse));
}
private async Task<IEnumerable<CellSet>> StatelessScannerAsyncInternal(string tableName, string optionalRowPrefix, string scanParameters, RequestOptions options)
{
using (Response webResponse = await GetRequestAsync(tableName + "/" + optionalRowPrefix + "*", scanParameters, options))
{
if (webResponse.WebResponse.StatusCode == HttpStatusCode.OK)
{
return ReadProtobufStream(webResponse.WebResponse.GetResponseStream());
}
return null;
}
}
private IEnumerable<CellSet> ReadProtobufStream(Stream stream)
{
List<CellSet> cells = new List<CellSet>();
var reader = new BinaryReader(stream);
while (true)
{
// read chunck length
byte[] lengthBytes = new byte[2];
int readBytes = reader.Read(lengthBytes, 0, lengthBytes.Length);
if (readBytes <= 0)
{
break;
}
sbyte[] slengthBytes = new sbyte[2];
Buffer.BlockCopy(lengthBytes, 0, slengthBytes, 0, lengthBytes.Length);
short length = (short)(((slengthBytes[0] & 0xFF) << 8) | (slengthBytes[1] & 0xFF));
byte[] cellSet = new byte[length];
int byteread = stream.Read(cellSet, 0, length);
CellSet sc = Serializer.Deserialize<CellSet>(new MemoryStream(cellSet, 0, byteread));
cells.Add(sc);
}
return cells;
}
/// <summary>
/// Atomically checks if a row/family/qualifier value matches the expected value and updates
/// </summary>
/// <param name="table">the table</param>
/// <param name="row">row to update</param>
/// <param name="cellToCheck">cell to check</param>
/// <returns>true if the record was updated; false if condition failed at check</returns>
public async Task<bool> CheckAndPutAsync(string table, CellSet.Row row, Cell cellToCheck, RequestOptions options = null)
{
table.ArgumentNotNullNorEmpty("table");
row.ArgumentNotNull("row");
row.values.Add(cellToCheck);
var cellSet = new CellSet();
cellSet.rows.Add(row);
var optionToUse = options ?? _globalRequestOptions;
return await optionToUse.RetryPolicy.ExecuteAsync<bool>(() => StoreCellsAsyncInternal(table, cellSet, optionToUse, Encoding.UTF8.GetString(row.key), CheckAndPutQuery));
}
/// <summary>
/// Automically checks if a row/family/qualifier value matches the expected value and deletes
/// </summary>
/// <param name="table">the table</param>
/// <param name="cellToCheck">cell to check for deleting the row</param>
/// <returns>true if the record was deleted; false if condition failed at check</returns>
public async Task<bool> CheckAndDeleteAsync(string table, Cell cellToCheck, CellSet.Row rowToDelete = null, RequestOptions options = null)
{
table.ArgumentNotNullNorEmpty("table");
cellToCheck.ArgumentNotNull("cellToCheck");
CellSet.Row row;
if (rowToDelete != null)
{
row = rowToDelete;
}
else
{
row = new CellSet.Row() { key = cellToCheck.row };
}
row.values.Add(cellToCheck);
var cellSet = new CellSet();
cellSet.rows.Add(row);
var optionToUse = options ?? _globalRequestOptions;
return await optionToUse.RetryPolicy.ExecuteAsync<bool>(() => StoreCellsAsyncInternal(table, cellSet, optionToUse, Encoding.UTF8.GetString(row.key), CheckAndDeleteQuery));
}
/// <summary>
/// Stores the given cells in the supplied table.
/// </summary>
/// <param name="table">the table</param>
/// <param name="cells">the cells to insert</param>
/// <returns>a task that is awaitable, signifying the end of this operation</returns>
public async Task StoreCellsAsync(string table, CellSet cells, RequestOptions options = null)
{
table.ArgumentNotNullNorEmpty("table");
cells.ArgumentNotNull("cells");
var optionToUse = options ?? _globalRequestOptions;
await optionToUse.RetryPolicy.ExecuteAsync(() => StoreCellsAsyncInternal(table, cells, optionToUse));
}
private async Task<bool> StoreCellsAsyncInternal(string table, CellSet cells, RequestOptions options, string key = null, string query = null)
{
string path = key == null ? table + "/somefalsekey" : table + "/" + key;
// note the fake row key to insert a set of cells
using (Response webResponse = await PutRequestAsync(path, query, cells, options))
{
if (webResponse.WebResponse.StatusCode == HttpStatusCode.NotModified)
{
return false;
}
if (webResponse.WebResponse.StatusCode != HttpStatusCode.OK)
{
using (var output = new StreamReader(webResponse.WebResponse.GetResponseStream()))
{
string message = output.ReadToEnd();
throw new WebException(
string.Format(
"Couldn't insert into table {0}! Response code was: {1}, expected 200! Response body was: {2}",
table,
webResponse.WebResponse.StatusCode,
message));
}
}
}
return true;
}
private async Task<Response> DeleteRequestAsync<TReq>(string endpoint, TReq request, RequestOptions options)
where TReq : class
{
return await ExecuteMethodAsync("DELETE", null, endpoint, request, options);
}
private async Task<Response> ExecuteMethodAsync<TReq>(
string method,
string query,
string endpoint,
TReq request,
RequestOptions options) where TReq : class
{
using (var input = new MemoryStream(options.SerializationBufferSize))
{
if (request != null)
{
Serializer.Serialize(input, request);
}
input.Seek(0, SeekOrigin.Begin);
return await _requester.IssueWebRequestAsync(endpoint, query, method, input, options);
}
}
private async Task<T> GetRequestAndDeserializeAsync<T>(string endpoint, string query, RequestOptions options)
{
options.ArgumentNotNull("request options");
endpoint.ArgumentNotNull("endpoint");
using (Response response = await _requester.IssueWebRequestAsync(endpoint, query, "GET", null, options))
{
using (Stream responseStream = response.WebResponse.GetResponseStream())
{
return Serializer.Deserialize<T>(responseStream);
}
}
}
private async Task<Response> GetRequestAsync(string endpoint, string query, RequestOptions options)
{
options.ArgumentNotNull("request options");
endpoint.ArgumentNotNull("endpoint");
return await _requester.IssueWebRequestAsync(endpoint, query, "GET", null, options);
}
private async Task<Response> PostRequestAsync<TReq>(string endpoint, TReq request, RequestOptions options)
where TReq : class
{
options.ArgumentNotNull("request options");
endpoint.ArgumentNotNull("endpoint");
return await ExecuteMethodAsync("POST", null, endpoint, request, options);
}
private async Task<Response> PutRequestAsync<TReq>(string endpoint, string query, TReq request, RequestOptions options)
where TReq : class
{
options.ArgumentNotNull("request options");
endpoint.ArgumentNotNull("endpoint");
return await ExecuteMethodAsync("PUT", query, endpoint, request, options);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting
/// unmanaged resources.
/// </summary>
/// <remarks>
/// Since this class is <see langword="sealed"/>, the standard <see
/// cref="IDisposable.Dispose"/> pattern is not required. Also, <see
/// cref="GC.SuppressFinalize"/> is not needed.
/// </remarks>
public void Dispose()
{
if (!_disposed)
{
if (_requester != null)
{
var disposable = _requester as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
_requester = null;
}
_disposed = true;
}
}
}
}