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.Collections.Generic;
11 : using System.Linq;
12 : using cdmdotnet.Logging;
13 : using Cqrs.Azure.BlobStorage;
14 : using Cqrs.Configuration;
15 : using Cqrs.Events;
16 : using Cqrs.Snapshots;
17 : using Microsoft.WindowsAzure.Storage;
18 : using Microsoft.WindowsAzure.Storage.Table;
19 :
20 : namespace Cqrs.Azure.Storage.Events
21 : {
22 : /// <summary>
23 : /// An Azure Storage based <see cref="SnapshotStore"/>.
24 : /// </summary>
25 : public class TableStorageSnapshotStore
26 : : BlobStorage.Events.TableStorageSnapshotStore
27 1 : {
28 : /// <summary>
29 : /// Initializes a new instance of the <see cref="TableStorageSnapshotStore"/> class using the specified container.
30 : /// </summary>
31 1 : public TableStorageSnapshotStore(IConfigurationManager configurationManager, ISnapshotDeserialiser eventDeserialiser, ILogger logger, ICorrelationIdHelper correlationIdHelper, ISnapshotBuilder snapshotBuilder, ITableStorageSnapshotStoreConnectionStringFactory tableStorageSnapshotStoreConnectionStringFactory)
32 : : base(configurationManager, eventDeserialiser, logger, correlationIdHelper, snapshotBuilder, tableStorageSnapshotStoreConnectionStringFactory, (logger1, tableStorageSnapshotStoreConnectionStringFactory1) => new RawTableStorageSnapshotStorer(logger1, tableStorageSnapshotStoreConnectionStringFactory1))
33 : {
34 : }
35 :
36 : #region Overrides of SnapshotStore
37 :
38 : /// <summary>
39 : /// Get the latest <see cref="Snapshot"/> from storage.
40 : /// </summary>
41 : /// <returns>The most recent <see cref="Snapshot"/> of</returns>
42 1 : protected override Snapshot Get(Type aggregateRootType, string streamName)
43 : {
44 : // Create the table query.
45 : var rangeQuery = new TableQuery<DynamicTableEntity>().Where
46 : (
47 : TableQuery.CombineFilters
48 : (
49 : TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, StorageStore<object, object>.GetSafeStorageKey(streamName)),
50 : TableOperators.And,
51 : TableQuery.GenerateFilterCondition("AggregateId", QueryComparisons.Equal, streamName)
52 : )
53 : );
54 :
55 : var operationContext = new OperationContext();
56 : Snapshot result = TableStorageStore.ReadableSource.ExecuteQuery(rangeQuery)
57 : #pragma warning disable 0436
58 : .Select(eventData => EntityPropertyConverter.ConvertBack<EventData>(eventData.Properties, operationContext))
59 : #pragma warning restore 0436
60 : .Where(eventData => eventData.AggregateId == streamName)
61 : .OrderByDescending(eventData => eventData.Version)
62 : .Take(1)
63 : .Select(EventDeserialiser.Deserialise)
64 : .SingleOrDefault();
65 :
66 : return result;
67 : }
68 :
69 : #endregion
70 :
71 : /// <summary>
72 : /// An Azure Storage based <see cref="TableStorageStore{TData,TCollectionItemData}"/>.
73 : /// </summary>
74 : public class RawTableStorageSnapshotStorer
75 : : RawTableStorageSnapshotStore
76 1 : {
77 : /// <summary>
78 : /// Initializes a new instance of the RawTableStorageSnapshotStorer class using the specified container.
79 : /// </summary>
80 1 : public RawTableStorageSnapshotStorer(ILogger logger, ITableStorageSnapshotStoreConnectionStringFactory tableStorageSnapshotStoreConnectionStringFactory)
81 : : base(logger, tableStorageSnapshotStoreConnectionStringFactory)
82 : {
83 : }
84 :
85 : #region Overrides of StorageStore<EventData,CloudTable>
86 :
87 : /// <summary>
88 : /// The value differs from RawTableStorageEventStore.GetSafeSourceName(string) in that it appends "V2" to the end of the name.
89 : /// </summary>
90 1 : protected override string GetSafeSourceName(string sourceName)
91 : {
92 : string tableName = base.GetSafeSourceName(sourceName);
93 : if (tableName.Length > 34)
94 : tableName = tableName.Substring(tableName.Length - 34);
95 : return string.Format("{0}V2", tableName);
96 : }
97 :
98 : #endregion
99 :
100 : #region Overrides of TableStorageStore<EventData>
101 :
102 : /// <summary>
103 : /// Creates a new <see cref="DynamicTableEntity"/> copying the provided <paramref name="data"/>
104 : /// into <see cref="DynamicTableEntity.Properties"/>.
105 : /// </summary>
106 1 : protected override ITableEntity CreateTableEntity(EventData data)
107 : {
108 : var tableEntity = new EventDataTableEntity<EventData>(data);
109 : //Flatten object of type TData and convert it to EntityProperty Dictionary
110 : #pragma warning disable 0436
111 : Dictionary<string, EntityProperty> flattenedProperties = EntityPropertyConverter.Flatten(data, new OperationContext());
112 : #pragma warning restore 0436
113 :
114 : // Create a DynamicTableEntity and set its PK and RK
115 : DynamicTableEntity dynamicTableEntity = new DynamicTableEntity(tableEntity.PartitionKey, tableEntity.RowKey)
116 : {
117 : Properties = flattenedProperties
118 : };
119 :
120 : return dynamicTableEntity;
121 : }
122 :
123 : #endregion
124 : }
125 : }
126 : }
|