-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathAzureTableRoutingDataStore.cs
264 lines (218 loc) · 9.61 KB
/
AzureTableRoutingDataStore.cs
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using Microsoft.Bot.Schema;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Underscore.Bot.MessageRouting.Models;
using Underscore.Bot.MessageRouting.Models.Azure;
namespace Underscore.Bot.MessageRouting.DataStore.Azure
{
/// <summary>
/// Routing data store that stores the data in Azure Table Storage.
/// See the IRoutingDataStore interface for the general documentation of properties and methods.
/// </summary>
[Serializable]
public class AzureTableRoutingDataStore : IRoutingDataStore
{
protected const string DefaultPartitionKey = "BotMessageRouting";
protected const string TableNameBotInstances = "BotInstances";
protected const string TableNameUsers = "Users";
protected const string TableNameAggregationChannels = "AggregationChannels";
protected const string TableNameConnectionRequests = "ConnectionRequests";
protected const string TableNameConnections = "Connections";
protected CloudTable _botInstancesTable;
protected CloudTable _usersTable;
protected CloudTable _aggregationChannelsTable;
protected CloudTable _connectionRequestsTable;
protected CloudTable _connectionsTable;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="connectionString">The connection string associated with an Azure Table Storage.</param>
public AzureTableRoutingDataStore(string connectionString)
{
if (string.IsNullOrEmpty(connectionString))
{
throw new ArgumentNullException("The connection string cannot be null or empty");
}
_botInstancesTable = AzureStorageHelper.GetTable(connectionString, TableNameBotInstances);
_usersTable = AzureStorageHelper.GetTable(connectionString, TableNameUsers);
_aggregationChannelsTable = AzureStorageHelper.GetTable(connectionString, TableNameAggregationChannels);
_connectionRequestsTable = AzureStorageHelper.GetTable(connectionString, TableNameConnectionRequests);
_connectionsTable = AzureStorageHelper.GetTable(connectionString, TableNameConnections);
MakeSureTablesExistAsync();
}
#region Get region
public IList<ConversationReference> GetUsers()
{
var entities = GetAllEntitiesFromTable(_usersTable).Result;
return GetAllConversationReferencesFromEntities(entities);
}
public IList<ConversationReference> GetBotInstances()
{
var entities = GetAllEntitiesFromTable(_botInstancesTable).Result;
return GetAllConversationReferencesFromEntities(entities);
}
public IList<ConversationReference> GetAggregationChannels()
{
var entities = GetAllEntitiesFromTable(_aggregationChannelsTable).Result;
return GetAllConversationReferencesFromEntities(entities);
}
public IList<ConnectionRequest> GetConnectionRequests()
{
var entities = GetAllEntitiesFromTable(_connectionRequestsTable).Result;
var connectionRequests = new List<ConnectionRequest>();
foreach (RoutingDataEntity entity in entities)
{
var connectionRequest =
JsonConvert.DeserializeObject<ConnectionRequest>(entity.Body);
connectionRequests.Add(connectionRequest);
}
return connectionRequests;
}
public IList<Connection> GetConnections()
{
var entities = GetAllEntitiesFromTable(_connectionsTable).Result;
var connections = new List<Connection>();
foreach (RoutingDataEntity entity in entities)
{
var connection =
JsonConvert.DeserializeObject<Connection>(entity.Body);
connections.Add(connection);
}
return connections;
}
#endregion Get region
#region Add region
public bool AddConversationReference(ConversationReference conversationReference)
{
CloudTable table = null;
if (conversationReference.Bot != null)
{
table = _botInstancesTable;
}
else
{
table = _usersTable;
}
string rowKey = conversationReference.Conversation.Id;
string body = JsonConvert.SerializeObject(conversationReference);
return InsertEntityToTable(rowKey, body, table);
}
public bool AddAggregationChannel(ConversationReference aggregationChannel)
{
string rowKey = aggregationChannel.Conversation.Id;
string body = JsonConvert.SerializeObject(aggregationChannel);
return InsertEntityToTable(rowKey, body, _aggregationChannelsTable);
}
public bool AddConnectionRequest(ConnectionRequest connectionRequest)
{
string rowKey = connectionRequest.Requestor.Conversation.Id;
string body = JsonConvert.SerializeObject(connectionRequest);
return InsertEntityToTable(rowKey, body, _connectionRequestsTable);
}
public bool AddConnection(Connection connection)
{
string rowKey = connection.ConversationReference1.Conversation.Id +
connection.ConversationReference2.Conversation.Id;
string body = JsonConvert.SerializeObject(connection);
return InsertEntityToTable(rowKey, body, _connectionsTable);
}
#endregion Add region
#region Remove region
public bool RemoveConversationReference(ConversationReference conversationReference)
{
CloudTable table = null;
if (conversationReference.Bot != null)
{
table = _botInstancesTable;
}
else
{
table = _usersTable;
}
string rowKey = conversationReference.Conversation.Id;
return AzureStorageHelper.DeleteEntryAsync<RoutingDataEntity>(
table, DefaultPartitionKey, rowKey).Result;
}
public bool RemoveAggregationChannel(ConversationReference aggregationChannel)
{
string rowKey = aggregationChannel.Conversation.Id;
return AzureStorageHelper.DeleteEntryAsync<RoutingDataEntity>(
_aggregationChannelsTable, DefaultPartitionKey, rowKey).Result;
}
public bool RemoveConnectionRequest(ConnectionRequest connectionRequest)
{
string rowKey = connectionRequest.Requestor.Conversation.Id;
return AzureStorageHelper.DeleteEntryAsync<RoutingDataEntity>(
_connectionRequestsTable, DefaultPartitionKey, rowKey).Result;
}
public bool RemoveConnection(Connection connection)
{
string rowKey = connection.ConversationReference1.Conversation.Id +
connection.ConversationReference2.Conversation.Id;
return AzureStorageHelper.DeleteEntryAsync<RoutingDataEntity>(
_connectionsTable, DefaultPartitionKey, rowKey).Result;
}
#endregion Remove region
#region Validators and helpers
/// <summary>
/// Makes sure the required tables exist.
/// </summary>
protected virtual async void MakeSureTablesExistAsync()
{
CloudTable[] cloudTables =
{
_botInstancesTable,
_usersTable,
_aggregationChannelsTable,
_connectionRequestsTable,
_connectionsTable
};
foreach (CloudTable cloudTable in cloudTables)
{
try
{
await cloudTable.CreateIfNotExistsAsync();
Debug.WriteLine($"Table '{cloudTable.Name}' created or did already exist");
}
catch (StorageException e)
{
Debug.WriteLine($"Failed to create table '{cloudTable.Name}' (perhaps it already exists): {e.Message}");
}
}
}
private List<ConversationReference> GetAllConversationReferencesFromEntities(IList<RoutingDataEntity> entities)
{
var conversationReferences = new List<ConversationReference>();
foreach (RoutingDataEntity entity in entities)
{
var conversationReference =
JsonConvert.DeserializeObject<ConversationReference>(entity.Body);
conversationReferences.Add(conversationReference);
}
return conversationReferences;
}
private async Task<IList<RoutingDataEntity>> GetAllEntitiesFromTable(CloudTable table)
{
var query = new TableQuery<RoutingDataEntity>()
.Where(TableQuery.GenerateFilterCondition(
"PartitionKey", QueryComparisons.Equal, DefaultPartitionKey));
return await table.ExecuteTableQueryAsync(query);
}
private static bool InsertEntityToTable(string rowKey, string body, CloudTable table)
{
return AzureStorageHelper.InsertAsync<RoutingDataEntity>(table,
new RoutingDataEntity()
{
Body = body,
PartitionKey = DefaultPartitionKey,
RowKey = rowKey
}).Result;
}
#endregion Validators and helpers
}
}