-
Notifications
You must be signed in to change notification settings - Fork 50
/
ForgeDictionary.cs
187 lines (169 loc) · 7.14 KB
/
ForgeDictionary.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
//-----------------------------------------------------------------------
// <copyright file="ForgeDictionary.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <summary>
// The ForgeDictionary class.
// </summary>
//-----------------------------------------------------------------------
namespace Microsoft.Forge.TreeWalker
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
/// <summary>
/// The ForgeDictionary class defines the methods for accessing forge state.
/// Note: The KeyPrefix should always precede the key when using the forgeStateTable to limit the scope to the current SessionId.
/// </summary>
public class ForgeDictionary : IForgeDictionary
{
/// <summary>
/// The key prefix that should precede the keys when using the forgeStateTable.
/// This ensures the scope of the table is limited to the current SessionId.
/// </summary>
private string keyPrefix;
/// <summary>
/// The dictionary holding the forge state.
/// Maps the string key to object value.
/// The key should always be preceded by the KeyPrefix. This ensures the scope of the table is limited to the current SessionId.
/// </summary>
private IDictionary<string, object> forgeStateTable;
/// <summary>
/// ForgeDictionary Constructor.
/// </summary>
/// <param name="forgeStateTable">The forge state table dictionary object.</param>
/// <param name="rootSessionId">The unique identifier for the root/parent session.</param>
/// <param name="sessionId">The unique identifier for this session.</param>
public ForgeDictionary(IDictionary<string, object> forgeStateTable, Guid rootSessionId, Guid sessionId)
{
this.forgeStateTable = forgeStateTable;
this.UpdateKeyPrefix(rootSessionId, sessionId);
}
/// <summary>
/// Sets an element with the provided key and value to the backing store.
/// </summary>
/// <param name="key">The key of the element to set.</param>
/// <param name="value">The value of the element to be set.</param>
public Task Set<T>(string key, T value)
{
this.forgeStateTable[this.keyPrefix + key] = (object)value;
return Task.FromResult(0);
}
/// <summary>
/// Sets a list of key value pairs to the backing store.
/// </summary>
/// <param name="kvps">The list of key value pairs to set.</param>
public Task SetRange<T>(List<KeyValuePair<string, T>> kvps)
{
foreach(KeyValuePair<string, T> kvp in kvps)
{
this.forgeStateTable[this.keyPrefix + kvp.Key] = (object)kvp.Value;
}
return Task.FromResult(0);
}
/// <summary>
/// Gets an element with the provided key from the backing store.
/// </summary>
/// <param name="key">The key of the element to get.</param>
/// <returns>The value of the element to get.</returns>
public Task<T> GetValue<T>(string key)
{
return Task.FromResult((T)this.forgeStateTable[this.keyPrefix + key]);
}
/// <summary>
/// Removes an element with the provided key from the backing store.
/// </summary>
/// <param name="key">The key of the element to remove.</param>
/// <returns>True of the element was removed, False otherwise.</returns>
public Task<bool> RemoveKey(string key)
{
return Task.FromResult(this.forgeStateTable.Remove(this.keyPrefix + key));
}
/// <summary>
/// Removes a list of elements with the provided keys from the backing store.
/// </summary>
/// <param name="keys">The list of keys to remove.</param>
public Task RemoveKeys(List<string> keys)
{
foreach(string key in keys)
{
this.forgeStateTable.Remove(this.keyPrefix + key);
}
return Task.FromResult(0);
}
/// <summary>
/// Sets an element with the provided key and value to the backing store.
/// </summary>
/// <param name="key">The key of the element to set.</param>
/// <param name="value">The value of the element to be set.</param>
public void SetSync<T>(string key, T value)
{
this.forgeStateTable[this.keyPrefix + key] = (object)value;
}
/// <summary>
/// Sets a list of key value pairs to the backing store.
/// </summary>
/// <param name="kvps">The list of key value pairs to set.</param>
public void SetRangeSync<T>(List<KeyValuePair<string, T>> kvps)
{
foreach(KeyValuePair<string, T> kvp in kvps)
{
this.forgeStateTable[this.keyPrefix + kvp.Key] = (object)kvp.Value;
}
}
/// <summary>
/// Gets an element with the provided key from the backing store.
/// </summary>
/// <param name="key">The key of the element to get.</param>
/// <returns>The value of the element to get.</returns>
public T GetValueSync<T>(string key)
{
return (T)this.forgeStateTable[this.keyPrefix + key];
}
/// <summary>
/// Removes an element with the provided key from the backing store.
/// </summary>
/// <param name="key">The key of the element to remove.</param>
/// <returns>True of the element was removed, False otherwise.</returns>
public bool RemoveKeySync(string key)
{
return this.forgeStateTable.Remove(this.keyPrefix + key);
}
/// <summary>
/// Removes a list of elements with the provided keys from the backing store.
/// </summary>
/// <param name="keys">The list of keys to remove.</param>
public void RemoveKeysSync(List<string> keys)
{
foreach(string key in keys)
{
this.forgeStateTable.Remove(this.keyPrefix + key);
}
}
/// <summary>
/// Updates the key prefix.
/// Note: The KeyPrefix should always precede the key when using the forgeStateTable to limit the scope to the current SessionId.
/// </summary>
/// <param name="rootSessionId">The unique identifier for the root/parent session.</param>
/// <param name="sessionId">The unique identifier for this session.</param>
public void UpdateKeyPrefix(Guid rootSessionId, Guid sessionId)
{
if (rootSessionId == sessionId)
{
// For backwards compatibility, the parent session will maintain the existing key format: <SessionId>_
this.keyPrefix = rootSessionId + "_";
}
else
{
this.keyPrefix = rootSessionId + "_" + sessionId + "_";
}
}
/// <summary>
/// Gets the key prefix.
/// </summary>
public string GetKeyPrefix()
{
return this.keyPrefix;
}
}
}