Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.IO;
11 : using System.Linq;
12 : using cdmdotnet.Logging;
13 : using Cqrs.Configuration;
14 : using Cqrs.Events;
15 : using Cqrs.Snapshots;
16 :
17 : namespace Cqrs.Azure.BlobStorage.Events
18 : {
19 : /// <summary>
20 : /// An Azure blob storage based <see cref="SnapshotStore"/>.
21 : /// </summary>
22 : public class BlobStorageSnapshotStore
23 : : SnapshotStore
24 1 : {
25 : /// <summary>
26 : /// Get the <see cref="BlobStorageStore"/>.
27 : /// </summary>
28 : protected BlobStorageStore<EventData> BlobStorageStore { get; private set; }
29 :
30 : /// <summary>
31 : /// Initializes a new instance of the <see cref="BlobStorageSnapshotStore"/> class using the specified container.
32 : /// </summary>
33 1 : public BlobStorageSnapshotStore(IConfigurationManager configurationManager, ISnapshotDeserialiser eventDeserialiser, ILogger logger, ICorrelationIdHelper correlationIdHelper, ISnapshotBuilder snapshotBuilder, IBlobStorageSnapshotStoreConnectionStringFactory blobStorageSnapshotStoreConnectionStringFactory)
34 : : base(configurationManager, eventDeserialiser, snapshotBuilder, logger, correlationIdHelper)
35 : {
36 : BlobStorageStore = new RawBlobStorageSnapshotStore(logger, blobStorageSnapshotStoreConnectionStringFactory);
37 : }
38 :
39 : #region Overrides of SnapshotStore
40 :
41 : /// <summary>
42 : /// Get the latest <see cref="Snapshot"/> from storage.
43 : /// </summary>
44 : /// <returns>The most recent <see cref="Snapshot"/> of</returns>
45 1 : protected override Snapshot Get(Type aggregateRootType, string streamName)
46 : {
47 : Snapshot result = BlobStorageStore
48 : .GetByFolder(streamName)
49 : .Where(eventData => eventData.AggregateId == streamName)
50 : .OrderByDescending(eventData => eventData.Version)
51 : .Take(1)
52 : .Select(EventDeserialiser.Deserialise)
53 : .SingleOrDefault();
54 :
55 : return result;
56 : }
57 :
58 : /// <summary>
59 : /// Saves the provided <paramref name="snapshot"/> into storage.
60 : /// </summary>
61 : /// <param name="snapshot">the <see cref="Snapshot"/> to save and store.</param>
62 1 : public override void Save(Snapshot snapshot)
63 : {
64 : Logger.LogDebug("Adding data to the blob storage snapshot-store aggregate folder", "BlobStorageSnapshotStore\\Save");
65 : BlobStorageStore.Add(BuildEventData(snapshot));
66 : Logger.LogDebug("Added data to the blob storage snapshot-store aggregate folder", "BlobStorageSnapshotStore\\Save");
67 : }
68 :
69 : #endregion
70 :
71 : /// <summary>
72 : /// The raw <see cref="Cqrs.Azure.BlobStorage.BlobStorageStore{TEventData}"/>.
73 : /// </summary>
74 : public class RawBlobStorageSnapshotStore
75 : : BlobStorageStore<EventData>
76 1 : {
77 : /// <summary>
78 : /// Initializes a new instance of the <see cref="RawBlobStorageSnapshotStore"/> class using the specified container.
79 : /// </summary>
80 1 : public RawBlobStorageSnapshotStore(ILogger logger, IBlobStorageSnapshotStoreConnectionStringFactory blobStorageSnapshotStoreConnectionStringFactory)
81 : : base(logger)
82 : {
83 : GetContainerName = blobStorageSnapshotStoreConnectionStringFactory.GetBaseContainerName;
84 : IsContainerPublic = () => false;
85 : GenerateFileName = data => Path.Combine(data.AggregateId, string.Format("{0:D10}\\{1}",data.Version, data.EventId.ToString("N")));
86 :
87 : // ReSharper disable DoNotCallOverridableMethodsInConstructor
88 : Initialise(blobStorageSnapshotStoreConnectionStringFactory);
89 : // ReSharper restore DoNotCallOverridableMethodsInConstructor
90 : }
91 : }
92 : }
93 : }
|