ravendb /Raven.Database/Server/RavenFS/Infrastructure/Historian.cs

Language C# Lines 109
MD5 Hash 1644dc12e8a1c3d20d7d1959862961b5
Repository https://github.com/nwendel/ravendb.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using Raven.Database.Server.RavenFS.Extensions;
using Raven.Database.Server.RavenFS.Storage;
using Raven.Database.Server.RavenFS.Storage.Esent;
using Raven.Database.Server.RavenFS.Synchronization;
using Raven.Json.Linq;
using Raven.Abstractions.Extensions;
using Raven.Abstractions.FileSystem;
using Raven.Abstractions.Data;

namespace Raven.Database.Server.RavenFS.Infrastructure
{
	public class Historian
	{
		private readonly ITransactionalStorage storage;
		private readonly SynchronizationHiLo synchronizationHiLo;
		private readonly UuidGenerator uuidGenerator;

		public Historian(ITransactionalStorage storage, SynchronizationHiLo synchronizationHiLo, UuidGenerator uuidGenerator)
		{
			this.storage = storage;
			this.uuidGenerator = uuidGenerator;
			this.synchronizationHiLo = synchronizationHiLo;
		}

		public void Update(string fileName, RavenJObject sourceMetadata)
		{
			var fileMetadata = GetMetadata(fileName);
            var serverId = fileMetadata.Value<string>(SynchronizationConstants.RavenSynchronizationSource);
			var history = new List<HistoryItem>();
			// if there is RavenReplicationVersion metadata it means that file is not new and we have to add a new item to the history
			if (!String.IsNullOrEmpty(serverId))
			{
				var currentVersion = fileMetadata.Value<long>(SynchronizationConstants.RavenSynchronizationVersion);               
                history = DeserializeHistory(fileMetadata);
				history.Add(new HistoryItem { ServerId = serverId, Version = currentVersion });
			}

			if (history.Count > SynchronizationConstants.ChangeHistoryLength)
				history.RemoveAt(0);

            sourceMetadata[SynchronizationConstants.RavenSynchronizationHistory] = SerializeHistory(history);            
			sourceMetadata[SynchronizationConstants.RavenSynchronizationVersion] = synchronizationHiLo.NextId();
			sourceMetadata[SynchronizationConstants.RavenSynchronizationSource] = new RavenJValue(storage.Id);
		}

        public void UpdateLastModified(RavenJObject metadata)
        {
            // internally keep last modified date with millisecond precision
            metadata[Constants.LastModified] = DateTimeOffset.UtcNow; 
            metadata[Constants.MetadataEtagField] = new RavenJValue(uuidGenerator.CreateSequentialUuid());
        }

        private RavenJObject GetMetadata(string fileName)
		{
            try
            {
                FileAndPagesInformation fileAndPages = null;
                storage.Batch(accessor => fileAndPages = accessor.GetFile(fileName, 0, 0));
                return fileAndPages.Metadata;
            }
            catch (FileNotFoundException)
            {
                return new RavenJObject();
            }
		}

        public static List<HistoryItem> DeserializeHistory(RavenJObject metadata)
        {
            var history = new List<HistoryItem>();
            if (metadata.ContainsKey(SynchronizationConstants.RavenSynchronizationHistory))
            {
                var array = (RavenJArray) metadata[SynchronizationConstants.RavenSynchronizationHistory];
                var items = array.Values<RavenJObject>().Select(x => JsonExtensions.JsonDeserialization<HistoryItem>(x));
                return new List<HistoryItem>(items);
            }

            return history;
        }

        public static RavenJArray SerializeHistory(List<HistoryItem> history)
        {
            return JsonExtensions.ToJArray(history);
        }

        public static bool IsDirectChildOfCurrent(RavenJObject destinationMetadata, RavenJObject sourceMetadata)
		{
            long destVersion = destinationMetadata.Value<long>(SynchronizationConstants.RavenSynchronizationVersion);
            var destServerId = destinationMetadata.Value<string>(SynchronizationConstants.RavenSynchronizationSource);

			var version = new HistoryItem { ServerId = destServerId, Version = destVersion };

			var history = DeserializeHistory(sourceMetadata);
            long sourceVersion = sourceMetadata.Value<long>(SynchronizationConstants.RavenSynchronizationVersion);
			var sourceServerId = sourceMetadata.Value<string>(SynchronizationConstants.RavenSynchronizationSource);

			history.Add(new HistoryItem { ServerId = sourceServerId, Version = sourceVersion });

			return history.Contains(version);
		}
	}
}
Back to Top