PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/Raven.Database/Bundles/Replication/Tasks/ReplicationStrategy.cs

https://github.com/nwendel/ravendb
C# | 117 lines | 96 code | 17 blank | 4 comment | 26 complexity | 941a40c8c4d93898f64ea05c42c22485 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, CC-BY-SA-3.0
  1. using System;
  2. using System.Linq;
  3. using Raven.Abstractions.Data;
  4. using Raven.Abstractions.Logging;
  5. using Raven.Abstractions.Replication;
  6. using Raven.Abstractions.Util;
  7. using Raven.Database;
  8. using Raven.Database.Data;
  9. using Raven.Json.Linq;
  10. namespace Raven.Bundles.Replication.Tasks
  11. {
  12. public class ReplicationStrategy
  13. {
  14. private static readonly ILog log = LogManager.GetCurrentClassLogger();
  15. public bool FilterDocuments(string destinationId, string key, RavenJObject metadata)
  16. {
  17. if (IsSystemDocumentId(key))
  18. {
  19. log.Debug("Will not replicate document '{0}' to '{1}' because it is a system document", key, destinationId);
  20. return false;
  21. }
  22. if (metadata.ContainsKey(Constants.NotForReplication) && metadata.Value<bool>(Constants.NotForReplication))
  23. // not explicitly marked to skip
  24. {
  25. log.Debug("Will not replicate document '{0}' to '{1}' because it was marked as not for replication", key, destinationId);
  26. return false;
  27. }
  28. if (metadata[Constants.RavenReplicationConflict] != null)
  29. // don't replicate conflicted documents, that just propagate the conflict
  30. {
  31. log.Debug("Will not replicate document '{0}' to '{1}' because it a conflict document", key, destinationId);
  32. return false;
  33. }
  34. if (OriginsFromDestination(destinationId, metadata)) // prevent replicating back to source
  35. {
  36. log.Debug("Will not replicate document '{0}' to '{1}' because the destination server is the same server it originated from", key, destinationId);
  37. return false;
  38. }
  39. switch (ReplicationOptionsBehavior)
  40. {
  41. case TransitiveReplicationOptions.None:
  42. var value = metadata.Value<string>(Constants.RavenReplicationSource);
  43. if (value != null && (value != CurrentDatabaseId))
  44. {
  45. log.Debug("Will not replicate document '{0}' to '{1}' because it was not created on the current server, and TransitiveReplicationOptions = none", key, destinationId);
  46. return false;
  47. }
  48. break;
  49. }
  50. log.Debug("Will replicate '{0}' to '{1}'", key, destinationId);
  51. return true;
  52. }
  53. public bool OriginsFromDestination(string destinationId, RavenJObject metadata)
  54. {
  55. return metadata.Value<string>(Constants.RavenReplicationSource) == destinationId;
  56. }
  57. public bool IsSystemDocumentId(string key)
  58. {
  59. if (key.StartsWith("Raven/", StringComparison.OrdinalIgnoreCase)) // don't replicate system docs
  60. {
  61. if (key.StartsWith("Raven/Hilo/", StringComparison.OrdinalIgnoreCase) == false) // except for hilo documents
  62. return true;
  63. }
  64. return false;
  65. }
  66. public bool FilterAttachments(AttachmentInformation attachment, string destinationInstanceId)
  67. {
  68. if (attachment.Key.StartsWith("Raven/", StringComparison.OrdinalIgnoreCase) || // don't replicate system attachments
  69. attachment.Key.StartsWith("transactions/recoveryInformation", StringComparison.OrdinalIgnoreCase)) // don't replicate transaction recovery information
  70. return false;
  71. // explicitly marked to skip
  72. if (attachment.Metadata.ContainsKey(Constants.NotForReplication) && attachment.Metadata.Value<bool>(Constants.NotForReplication))
  73. return false;
  74. if (attachment.Metadata.ContainsKey(Constants.RavenReplicationConflict))// don't replicate conflicted documents, that just propagate the conflict
  75. return false;
  76. // we don't replicate stuff that was created there
  77. if (attachment.Metadata.Value<string>(Constants.RavenReplicationSource) == destinationInstanceId)
  78. return false;
  79. switch (ReplicationOptionsBehavior)
  80. {
  81. case TransitiveReplicationOptions.None:
  82. return attachment.Metadata.Value<string>(Constants.RavenReplicationSource) == null ||
  83. (attachment.Metadata.Value<string>(Constants.RavenReplicationSource) == CurrentDatabaseId);
  84. }
  85. return true;
  86. }
  87. public string CurrentDatabaseId { get; set; }
  88. public TransitiveReplicationOptions ReplicationOptionsBehavior { get; set; }
  89. public RavenConnectionStringOptions ConnectionStringOptions { get; set; }
  90. public override string ToString()
  91. {
  92. return string.Join(" ", new[]
  93. {
  94. ConnectionStringOptions.Url,
  95. ConnectionStringOptions.DefaultDatabase,
  96. ConnectionStringOptions.ApiKey
  97. }.Where(x => x != null));
  98. }
  99. }
  100. }