PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/ToMigrate/Raven.Database/Bundles/Replication/Triggers/AncestryPutTrigger.cs

http://github.com/ayende/ravendb
C# | 104 lines | 90 code | 9 blank | 5 comment | 17 complexity | 619a19ac9d307c0b39eb4b13cd32fddb MD5 | raw file
Possible License(s): GPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, Apache-2.0, BSD-3-Clause, CC-BY-SA-3.0
  1. //-----------------------------------------------------------------------
  2. // <copyright file="AncestryPutTrigger.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. //-----------------------------------------------------------------------
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel.Composition;
  9. using System.Linq;
  10. using Raven.Abstractions.Data;
  11. using Raven.Abstractions.Json.Linq;
  12. using Raven.Bundles.Replication.Impl;
  13. using Raven.Database.Bundles.Replication.Impl;
  14. using Raven.Database.Plugins;
  15. using Raven.Json.Linq;
  16. namespace Raven.Database.Bundles.Replication.Triggers
  17. {
  18. [ExportMetadata("Bundle", "Replication")]
  19. [ExportMetadata("Order", 10000)]
  20. [InheritedExport(typeof(AbstractPutTrigger))]
  21. public class AncestryPutTrigger : AbstractPutTrigger
  22. {
  23. public override void OnPut(string key, RavenJObject jsonReplicationDocument, RavenJObject metadata)
  24. {
  25. if (key.StartsWith("Raven/", StringComparison.OrdinalIgnoreCase) && // we don't deal with system documents
  26. key.StartsWith("Raven/Hilo/", StringComparison.OrdinalIgnoreCase) == false) // except for hilos
  27. return;
  28. using (Database.DisableAllTriggersForCurrentThread())
  29. {
  30. var documentMetadata = GetDocumentMetadata(key);
  31. if (documentMetadata != null)
  32. {
  33. var history = new RavenJArray(ReplicationData.GetHistory(documentMetadata));
  34. metadata[Constants.RavenReplicationHistory] = history;
  35. if (documentMetadata.ContainsKey(Constants.RavenReplicationVersion) &&
  36. documentMetadata.ContainsKey(Constants.RavenReplicationSource))
  37. {
  38. var historyEntry = new RavenJObject
  39. {
  40. {Constants.RavenReplicationVersion, documentMetadata[Constants.RavenReplicationVersion]},
  41. {Constants.RavenReplicationSource, documentMetadata[Constants.RavenReplicationSource]}
  42. };
  43. if (history.Contains(historyEntry, RavenJTokenEqualityComparer.Default) == false)
  44. history.Add(historyEntry);
  45. }
  46. else
  47. {
  48. history.Add(new RavenJObject
  49. {
  50. {Constants.RavenReplicationVersion, 0},
  51. {Constants.RavenReplicationSource, RavenJToken.FromObject(Database.TransactionalStorage.Id)}
  52. });
  53. }
  54. while (history.Length > Constants.ChangeHistoryLength)
  55. {
  56. history.RemoveAt(0);
  57. }
  58. }
  59. metadata[Constants.RavenReplicationVersion] = RavenJToken.FromObject(ReplicationHiLo.NextId(Database));
  60. metadata[Constants.RavenReplicationSource] = RavenJToken.FromObject(Database.TransactionalStorage.Id);
  61. }
  62. }
  63. private RavenJObject GetDocumentMetadata(string key)
  64. {
  65. var doc = Database.Documents.GetDocumentMetadata(key);
  66. if (doc != null)
  67. {
  68. var doesNotExist = doc.Metadata.Value<bool>(Constants.RavenDocumentDoesNotExists); // occurs when in transaction
  69. if (doesNotExist == false)
  70. return doc.Metadata;
  71. }
  72. RavenJObject result = null;
  73. Database.TransactionalStorage.Batch(accessor =>
  74. {
  75. var tombstone = accessor.Lists.Read(Constants.RavenReplicationDocsTombstones, key);
  76. if (tombstone == null)
  77. return;
  78. result = tombstone.Data;
  79. accessor.Lists.Remove(Constants.RavenReplicationDocsTombstones, key);
  80. });
  81. return result;
  82. }
  83. public override IEnumerable<string> GeneratedMetadataNames
  84. {
  85. get
  86. {
  87. return new[]
  88. {
  89. Constants.RavenReplicationVersion,
  90. Constants.RavenReplicationSource
  91. };
  92. }
  93. }
  94. }
  95. }