DeleteByKeysAgent.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // ----------------------------------------------------------
  2. // <copyright file="DeleteByKeysAgent.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // ----------------------------------------------------------
  6. namespace Microsoft.Hawaii.KeyValue.Client
  7. {
  8. using System;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Runtime.Serialization.Json;
  12. /// <summary>
  13. /// Class agent to delete key value item by keys.
  14. /// </summary>
  15. internal class DeleteByKeysAgent : ServiceAgent<DeleteResult>
  16. {
  17. /// <summary>
  18. /// The keys array to search.
  19. /// </summary>
  20. private string[] keys;
  21. /// <summary>
  22. /// Initializes a new instance of the DeleteByKeysAgent class.
  23. /// </summary>
  24. /// <param name="hostName">Specifies a host name of the service.</param>
  25. /// <param name="clientIdentity">Specifies the client identity.</param>
  26. /// <param name="keys">Specifies the key to search</param>
  27. public DeleteByKeysAgent(string hostName, ClientIdentity clientIdentity, string[] keys) :
  28. this(hostName, clientIdentity, keys, null)
  29. {
  30. }
  31. /// <summary>
  32. /// Initializes a new instance of the DeleteByKeysAgent class.
  33. /// </summary>
  34. /// <param name="hostName">Specifies a host name of the service.</param>
  35. /// <param name="clientIdentity">Specifies the client identity.</param>
  36. /// <param name="keys">Specifies the key to search</param>
  37. /// <param name="stateObject">Specifies a user-defined object.</param>
  38. public DeleteByKeysAgent(string hostName, ClientIdentity clientIdentity, string[] keys, object stateObject) :
  39. base(HttpMethod.Delete, stateObject)
  40. {
  41. if (string.IsNullOrEmpty(hostName))
  42. {
  43. throw new ArgumentNullException("hostName");
  44. }
  45. if (keys == null || keys.Length == 0)
  46. {
  47. throw new ArgumentNullException("keys");
  48. }
  49. // Set the client identity.
  50. this.ClientIdentity = clientIdentity;
  51. this.keys = keys;
  52. // Create the service uri.
  53. this.Uri = this.CreateServiceUri(hostName);
  54. }
  55. /// <summary>
  56. /// An overriden property to set the request content type to be Json.
  57. /// </summary>
  58. protected override string RequestContentType
  59. {
  60. get
  61. {
  62. return "application/json";
  63. }
  64. }
  65. /// <summary>
  66. /// An overriden method to parse the result from the service.
  67. /// </summary>
  68. /// <param name="responseStream">
  69. /// A valid response stream.
  70. /// </param>
  71. protected override void ParseOutput(System.IO.Stream responseStream)
  72. {
  73. if (responseStream == null)
  74. {
  75. throw new ArgumentException("Response stream is null");
  76. }
  77. Debug.Assert(this.Result != null, "result is null");
  78. DeleteResult result = DeserializeResponseJson<DeleteResult>(responseStream);
  79. this.Result.AvailableByteCount = result.AvailableByteCount;
  80. this.Result.DeletedItemNumber = result.DeletedItemNumber;
  81. this.Result.MoreItemsToDelete = result.MoreItemsToDelete;
  82. this.Result.TotalKeyValuePairCount = result.TotalKeyValuePairCount;
  83. this.Result.Status = Status.Success;
  84. }
  85. /// <summary>
  86. /// An overriden method to get the data for POST service call.
  87. /// </summary>
  88. /// <returns>Return the byte array of the Post data.</returns>
  89. protected override byte[] GetPostData()
  90. {
  91. using (MemoryStream stream = new MemoryStream())
  92. {
  93. DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(DeleteByKeysRequest));
  94. serializer.WriteObject(stream, new DeleteByKeysRequest() { Keys = this.keys });
  95. return stream.ToArray();
  96. }
  97. }
  98. /// <summary>
  99. /// Method creates the service uri.
  100. /// </summary>
  101. /// <param name="hostName">Specifies a host name of the service.</param>
  102. /// <returns>
  103. /// A valid service uri object.
  104. /// </returns>
  105. private Uri CreateServiceUri(string hostName)
  106. {
  107. string signature = string.Format("{0}/{1}", KeyValueService.KeyValueSignature, KeyValueService.GetBatchKey);
  108. ServiceUri uri = new ServiceUri(hostName, Uri.EscapeUriString(signature));
  109. // Return the URI object.
  110. return new Uri(uri.ToString());
  111. }
  112. }
  113. }