From f89f308c525e9deebc6d2cf6416e27dfe1a299dc Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Sun, 19 May 2019 16:03:38 +0200 Subject: Cleanup DiscoBot Project --- FireBase/Offline/ConcurrentOfflineDatabase.cs | 70 ++++++++++++--------------- 1 file changed, 30 insertions(+), 40 deletions(-) (limited to 'FireBase/Offline/ConcurrentOfflineDatabase.cs') 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; /// @@ -24,34 +23,30 @@ /// Custom string which will get appended to the file name. 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().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() .FindAll() .ToDictionary(o => o.Key, o => o); - this.ccache = new ConcurrentDictionary(cache); - + ccache = new ConcurrentDictionary(cache); } /// /// Gets the number of elements contained in the . /// /// The number of elements contained in the . - public int Count => this.ccache.Count; + public int Count => ccache.Count; /// /// Gets a value indicating whether this is a read-only collection. @@ -62,13 +57,13 @@ /// Gets an containing the keys of the . /// /// An containing the keys of the object that implements . - public ICollection Keys => this.ccache.Keys; + public ICollection Keys => ccache.Keys; /// /// Gets an containing the values in the . /// /// An containing the values in the object that implements . - public ICollection Values => this.ccache.Values; + public ICollection Values => ccache.Values; /// /// Gets or sets the element with the specified key. @@ -77,15 +72,12 @@ /// The element with the specified key. 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 @@ /// An enumerator that can be used to iterate through the collection. public IEnumerator> GetEnumerator() { - return this.ccache.GetEnumerator(); + return ccache.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { - return this.GetEnumerator(); + return GetEnumerator(); } /// @@ -109,7 +101,7 @@ /// The object to add to the . public void Add(KeyValuePair item) { - this.Add(item.Key, item.Value); + Add(item.Key, item.Value); } /// @@ -117,8 +109,8 @@ /// public void Clear() { - this.ccache.Clear(); - this.db.Delete(Query.All()); + ccache.Clear(); + db.Delete(Query.All()); } /// @@ -128,7 +120,7 @@ /// True if is found in the ; otherwise, false. public bool Contains(KeyValuePair item) { - return this.ContainsKey(item.Key); + return ContainsKey(item.Key); } /// @@ -138,7 +130,7 @@ /// The zero-based index in at which copying begins. public void CopyTo(KeyValuePair[] array, int arrayIndex) { - this.ccache.ToList().CopyTo(array, arrayIndex); + ccache.ToList().CopyTo(array, arrayIndex); } /// @@ -148,7 +140,7 @@ /// True if was successfully removed from the ; otherwise, false. This method also returns false if is not found in the original . public bool Remove(KeyValuePair item) { - return this.Remove(item.Key); + return Remove(item.Key); } /// @@ -158,7 +150,7 @@ /// True if the contains an element with the key; otherwise, false. public bool ContainsKey(string key) { - return this.ccache.ContainsKey(key); + return ccache.ContainsKey(key); } /// @@ -168,8 +160,8 @@ /// The object to use as the value of the element to add. 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); } /// @@ -179,8 +171,8 @@ /// True if the element is successfully removed; otherwise, false. This method also returns false if was not found in the original . public bool Remove(string key) { - this.ccache.TryRemove(key, out OfflineEntry _); - return this.db.Delete(key); + ccache.TryRemove(key, out _); + return db.Delete(key); } /// @@ -190,18 +182,16 @@ /// True if the object that implements contains an element with the specified key; otherwise, false. 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 -- cgit v1.2.3-54-g00ecf