PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

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