diff options
Diffstat (limited to 'FireBase/Offline/ConcurrentOfflineDatabase.cs')
-rw-r--r-- | FireBase/Offline/ConcurrentOfflineDatabase.cs | 70 |
1 files changed, 30 insertions, 40 deletions
diff --git a/FireBase/Offline/ConcurrentOfflineDatabase.cs b/FireBase/Offline/ConcurrentOfflineDatabase.cs index 226892d..5527168 100644 --- a/FireBase/Offline/ConcurrentOfflineDatabase.cs +++ b/FireBase/Offline/ConcurrentOfflineDatabase.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; - using LiteDB; /// <summary> @@ -24,34 +23,30 @@ /// <param name="filenameModifier"> Custom string which will get appended to the file name. </param> public ConcurrentOfflineDatabase(Type itemType, string filenameModifier) { - var fullName = this.GetFileName(itemType.ToString()); - if(fullName.Length > 100) - { - fullName = fullName.Substring(0, 100); - } + var fullName = GetFileName(itemType.ToString()); + if (fullName.Length > 100) fullName = fullName.Substring(0, 100); - BsonMapper mapper = BsonMapper.Global; + var mapper = BsonMapper.Global; mapper.Entity<OfflineEntry>().Id(o => o.Key); - string root = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); - string filename = fullName + filenameModifier + ".db"; + var root = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + var filename = fullName + filenameModifier + ".db"; var path = Path.Combine(root, filename); - this.db = new LiteRepository(new LiteDatabase(path, mapper)); + db = new LiteRepository(new LiteDatabase(path, mapper)); var cache = db.Database .GetCollection<OfflineEntry>() .FindAll() .ToDictionary(o => o.Key, o => o); - this.ccache = new ConcurrentDictionary<string, OfflineEntry>(cache); - + ccache = new ConcurrentDictionary<string, OfflineEntry>(cache); } /// <summary> /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>. /// </summary> /// <returns> The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>. </returns> - public int Count => this.ccache.Count; + public int Count => ccache.Count; /// <summary> /// Gets a value indicating whether this is a read-only collection. @@ -62,13 +57,13 @@ /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the <see cref="T:System.Collections.Generic.IDictionary`2"/>. /// </summary> /// <returns> An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the keys of the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>. </returns> - public ICollection<string> Keys => this.ccache.Keys; + public ICollection<string> Keys => ccache.Keys; /// <summary> /// Gets an <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the <see cref="T:System.Collections.Generic.IDictionary`2"/>. /// </summary> /// <returns> An <see cref="T:System.Collections.Generic.ICollection`1"/> containing the values in the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/>. </returns> - public ICollection<OfflineEntry> Values => this.ccache.Values; + public ICollection<OfflineEntry> Values => ccache.Values; /// <summary> /// Gets or sets the element with the specified key. @@ -77,15 +72,12 @@ /// <returns> The element with the specified key. </returns> public OfflineEntry this[string key] { - get - { - return this.ccache[key]; - } + get => ccache[key]; set { - this.ccache.AddOrUpdate(key, value, (k, existing) => value); - this.db.Upsert(value); + ccache.AddOrUpdate(key, value, (k, existing) => value); + db.Upsert(value); } } @@ -95,12 +87,12 @@ /// <returns> An enumerator that can be used to iterate through the collection. </returns> public IEnumerator<KeyValuePair<string, OfflineEntry>> GetEnumerator() { - return this.ccache.GetEnumerator(); + return ccache.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { - return this.GetEnumerator(); + return GetEnumerator(); } /// <summary> @@ -109,7 +101,7 @@ /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param> public void Add(KeyValuePair<string, OfflineEntry> item) { - this.Add(item.Key, item.Value); + Add(item.Key, item.Value); } /// <summary> @@ -117,8 +109,8 @@ /// </summary> public void Clear() { - this.ccache.Clear(); - this.db.Delete<OfflineEntry>(Query.All()); + ccache.Clear(); + db.Delete<OfflineEntry>(Query.All()); } /// <summary> @@ -128,7 +120,7 @@ /// <returns> True if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. </returns> public bool Contains(KeyValuePair<string, OfflineEntry> item) { - return this.ContainsKey(item.Key); + return ContainsKey(item.Key); } /// <summary> @@ -138,7 +130,7 @@ /// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param> public void CopyTo(KeyValuePair<string, OfflineEntry>[] array, int arrayIndex) { - this.ccache.ToList().CopyTo(array, arrayIndex); + ccache.ToList().CopyTo(array, arrayIndex); } /// <summary> @@ -148,7 +140,7 @@ /// <returns> True if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>. </returns> public bool Remove(KeyValuePair<string, OfflineEntry> item) { - return this.Remove(item.Key); + return Remove(item.Key); } /// <summary> @@ -158,7 +150,7 @@ /// <returns> True if the <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the key; otherwise, false. </returns> public bool ContainsKey(string key) { - return this.ccache.ContainsKey(key); + return ccache.ContainsKey(key); } /// <summary> @@ -168,8 +160,8 @@ /// <param name="value">The object to use as the value of the element to add.</param> public void Add(string key, OfflineEntry value) { - this.ccache.AddOrUpdate(key, value, (k, existing) => value); - this.db.Upsert(value); + ccache.AddOrUpdate(key, value, (k, existing) => value); + db.Upsert(value); } /// <summary> @@ -179,8 +171,8 @@ /// <returns> True if the element is successfully removed; otherwise, false. This method also returns false if <paramref name="key"/> was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"/>. </returns> public bool Remove(string key) { - this.ccache.TryRemove(key, out OfflineEntry _); - return this.db.Delete<OfflineEntry>(key); + ccache.TryRemove(key, out _); + return db.Delete<OfflineEntry>(key); } /// <summary> @@ -190,18 +182,16 @@ /// <returns> True if the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"/> contains an element with the specified key; otherwise, false. </returns> public bool TryGetValue(string key, out OfflineEntry value) { - return this.ccache.TryGetValue(key, out value); + return ccache.TryGetValue(key, out value); } private string GetFileName(string fileName) { - var invalidChars = new[] { '`', '[', ',', '=' }; - foreach(char c in invalidChars.Concat(System.IO.Path.GetInvalidFileNameChars()).Distinct()) - { + var invalidChars = new[] {'`', '[', ',', '='}; + foreach (var c in invalidChars.Concat(Path.GetInvalidFileNameChars()).Distinct()) fileName = fileName.Replace(c, '_'); - } return fileName; } } -} +}
\ No newline at end of file |