Store.cs
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license.
4 //
5 // Microsoft Bot Framework: http://botframework.com
6 //
7 // Bot Builder SDK GitHub:
8 // https://github.com/Microsoft/BotBuilder
9 //
10 // Copyright (c) Microsoft Corporation
11 // All rights reserved.
12 //
13 // MIT License:
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 
34 using System;
35 using System.Collections.Generic;
36 using System.IO;
37 using System.IO.Compression;
38 using System.Linq;
39 using System.Runtime.Serialization;
40 
41 namespace Microsoft.Bot.Builder.Internals.Fibers
42 {
43  public sealed class FormatterStore<T> : IStore<T>
44  {
45  private readonly Stream stream;
46  private readonly IFormatter formatter;
47  public FormatterStore(Stream stream, IFormatter formatter)
48  {
49  SetField.NotNull(out this.stream, nameof(stream), stream);
50  SetField.NotNull(out this.formatter, nameof(formatter), formatter);
51  }
52 
53  void IStore<T>.Reset()
54  {
55  this.stream.SetLength(0);
56  }
57 
58  bool IStore<T>.TryLoad(out T item)
59  {
60  if (this.stream.Length > 0)
61  {
62  this.stream.Position = 0;
63  using (var gzip = new GZipStream(this.stream, CompressionMode.Decompress, leaveOpen: true))
64  {
65  item = (T)this.formatter.Deserialize(gzip);
66  return true;
67  }
68  }
69 
70  item = default(T);
71  return false;
72  }
73 
74  void IStore<T>.Save(T item)
75  {
76  this.stream.Position = 0;
77  using (var gzip = new GZipStream(this.stream, CompressionMode.Compress, leaveOpen: true))
78  {
79  formatter.Serialize(gzip, item);
80  }
81 
82  this.stream.SetLength(this.stream.Position);
83  }
84 
85  void IStore<T>.Flush()
86  {
87  this.stream.Flush();
88  }
89  }
90 
91  public sealed class ErrorResilientStore<T> : IStore<T>
92  {
93  private readonly IStore<T> store;
95  {
96  SetField.NotNull(out this.store, nameof(store), store);
97  }
98 
99  void IStore<T>.Reset()
100  {
101  this.store.Reset();
102  }
103 
104  bool IStore<T>.TryLoad(out T item)
105  {
106  try
107  {
108  return this.store.TryLoad(out item);
109  }
110  catch (Exception)
111  {
112  // exception in loading the serialized data
113  item = default(T);
114  return false;
115  }
116  }
117 
118  void IStore<T>.Save(T item)
119  {
120  this.store.Save(item);
121  }
122 
123  void IStore<T>.Flush()
124  {
125  this.store.Flush();
126  }
127  }
128 
129  public sealed class FactoryStore<T> : IStore<T>
130  {
131  private readonly IStore<T> store;
132  private readonly Func<T> factory;
133  public FactoryStore(IStore<T> store, Func<T> factory)
134  {
135  SetField.NotNull(out this.store, nameof(store), store);
136  SetField.NotNull(out this.factory, nameof(factory), factory);
137  }
138 
139  void IStore<T>.Reset()
140  {
141  this.store.Reset();
142  }
143 
144  bool IStore<T>.TryLoad(out T item)
145  {
146  if (this.store.TryLoad(out item))
147  {
148  return true;
149  }
150 
151  item = this.factory();
152  return false;
153  }
154 
155  void IStore<T>.Save(T item)
156  {
157  this.store.Save(item);
158  }
159 
160  void IStore<T>.Flush()
161  {
162  this.store.Flush();
163  }
164  }
165 }
FactoryStore(IStore< T > store, Func< T > factory)
Definition: Store.cs:133
FormatterStore(Stream stream, IFormatter formatter)
Definition: Store.cs:47