DeleteAgent.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // ----------------------------------------------------------
  2. // <copyright file="DeleteAgent.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. /// <summary>
  11. /// Class agent to delete key value item by key.
  12. /// </summary>
  13. internal class DeleteAgent : ServiceAgent<DeleteResult>
  14. {
  15. /// <summary>
  16. /// Initializes a new instance of the DeleteAgent class.
  17. /// </summary>
  18. /// <param name="hostName">Specifies a host name of the service.</param>
  19. /// <param name="clientIdentity">Specifies the client identity.</param>
  20. /// <param name="prefix">Specifies the search prefix keyword.</param>
  21. /// <param name="before">Specifies the timpstamp for delete operation.</param>
  22. public DeleteAgent(string hostName, ClientIdentity clientIdentity, string prefix, DateTime before) :
  23. this(hostName, clientIdentity, prefix, before, null)
  24. {
  25. }
  26. /// <summary>
  27. /// Initializes a new instance of the DeleteAgent class.
  28. /// </summary>
  29. /// <param name="hostName">Specifies a host name of the service.</param>
  30. /// <param name="clientIdentity">Specifies the client identity.</param>
  31. /// <param name="prefix">Specifies the search prefix keyword.</param>
  32. /// <param name="before">Specifies the timpstamp for delete operation.</param>
  33. /// <param name="stateObject">Specifies a user-defined object.</param>
  34. public DeleteAgent(string hostName, ClientIdentity clientIdentity, string prefix, DateTime before, object stateObject) :
  35. base(HttpMethod.Delete, stateObject)
  36. {
  37. if (string.IsNullOrEmpty(hostName))
  38. {
  39. throw new ArgumentNullException("hostName");
  40. }
  41. // Set the client identity.
  42. this.ClientIdentity = clientIdentity;
  43. // Create the service uri.
  44. this.Uri = this.CreateServiceUri(hostName, prefix, before);
  45. }
  46. /// <summary>
  47. /// An overriden property to set the request content type to be Json.
  48. /// </summary>
  49. protected override string RequestContentType
  50. {
  51. get
  52. {
  53. return "application/json";
  54. }
  55. }
  56. /// <summary>
  57. /// An overriden method to parse the result from the service.
  58. /// </summary>
  59. /// <param name="responseStream">
  60. /// A valid response stream.
  61. /// </param>
  62. protected override void ParseOutput(System.IO.Stream responseStream)
  63. {
  64. if (responseStream == null)
  65. {
  66. throw new ArgumentException("Response stream is null");
  67. }
  68. Debug.Assert(this.Result != null, "result is null");
  69. DeleteResult result = DeserializeResponseJson<DeleteResult>(responseStream);
  70. this.Result.AvailableByteCount = result.AvailableByteCount;
  71. this.Result.DeletedItemNumber = result.DeletedItemNumber;
  72. this.Result.MoreItemsToDelete = result.MoreItemsToDelete;
  73. this.Result.TotalKeyValuePairCount = result.TotalKeyValuePairCount;
  74. this.Result.Status = Status.Success;
  75. }
  76. /// <summary>
  77. /// Method creates the service uri.
  78. /// </summary>
  79. /// <param name="hostName">Specifies a host name of the service.</param>
  80. /// <param name="prefix">Specifies the search prefix keyword.</param>
  81. /// <param name="before">Specifies the timpstamp for delete operation.</param>
  82. /// <returns>
  83. /// A valid service uri object.
  84. /// </returns>
  85. private Uri CreateServiceUri(string hostName, string prefix, DateTime before)
  86. {
  87. ServiceUri uri = new ServiceUri(hostName, Uri.EscapeUriString(KeyValueService.KeyValueSignature));
  88. uri.AddQueryString(KeyValueService.PrefixKey, prefix);
  89. uri.AddQueryString(KeyValueService.BeforeKey, before.ToString());
  90. // Return the URI object.
  91. return new Uri(uri.ToString());
  92. }
  93. }
  94. }