namespace Firebase.Database.Offline { using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using LiteDB; /// /// The offline database. /// public class OfflineDatabase : IDictionary { private readonly LiteRepository db; private readonly IDictionary cache; /// /// Initializes a new instance of the class. /// /// The item type which is used to determine the database file name. /// Custom string which will get appended to the file name. public OfflineDatabase(Type itemType, string filenameModifier) { var fullName = this.GetFileName(itemType.ToString()); if(fullName.Length > 100) { fullName = fullName.Substring(0, 100); } BsonMapper mapper = BsonMapper.Global; mapper.Entity().Id(o => o.Key); string root = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); string filename = fullName + filenameModifier + ".db"; var path = Path.Combine(root, filename); this.db = new LiteRepository(new LiteDatabase(path, mapper)); this.cache = db.Database.GetCollection().FindAll() .ToDictionary(o => o.Key, o => o); } /// /// Gets the number of elements contained in the . /// /// The number of elements contained in the . public int Count => this.cache.Count; /// /// Gets a value indicating whether this is a read-only collection. /// public bool IsReadOnly => this.cache.IsReadOnly; /// /// Gets an containing the keys of the . /// /// An containing the keys of the object that implements . public ICollection Keys => this.cache.Keys; /// /// Gets an containing the values in the . /// /// An containing the values in the object that implements . public ICollection Values => this.cache.Values; /// /// Gets or sets the element with the specified key. /// /// The key of the element to get or set. /// The element with the specified key. public OfflineEntry this[string key] { get { return this.cache[key]; } set { this.cache[key] = value; this.db.Upsert(value); } } /// /// Returns an enumerator that iterates through the collection. /// /// An enumerator that can be used to iterate through the collection. public IEnumerator> GetEnumerator() { return this.cache.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); } /// /// Adds an item to the . /// /// The object to add to the . public void Add(KeyValuePair item) { this.Add(item.Key, item.Value); } /// /// Removes all items from the . /// public void Clear() { this.cache.Clear(); this.db.Delete(Query.All()); } /// /// Determines whether the contains a specific value. /// /// The object to locate in the . /// True if is found in the ; otherwise, false. public bool Contains(KeyValuePair item) { return this.ContainsKey(item.Key); } /// /// Copies the elements of the to an , starting at a particular index. /// /// The one-dimensional that is the destination of the elements copied from . The must have zero-based indexing. /// The zero-based index in at which copying begins. public void CopyTo(KeyValuePair[] array, int arrayIndex) { this.cache.CopyTo(array, arrayIndex); } /// /// Removes the first occurrence of a specific object from the . /// /// The object to remove from the . /// 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); } /// /// Determines whether the contains an element with the specified key. /// /// The key to locate in the . /// True if the contains an element with the key; otherwise, false. public bool ContainsKey(string key) { return this.cache.ContainsKey(key); } /// /// Adds an element with the provided key and value to the . /// /// The object to use as the key of the element to add. /// The object to use as the value of the element to add. public void Add(string key, OfflineEntry value) { this.cache.Add(key, value); this.db.Insert(value); } /// /// Removes the element with the specified key from the . /// /// The key of the element to remove. /// 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.cache.Remove(key); return this.db.Delete(key); } /// /// Gets the value associated with the specified key. /// /// The key whose value to get.When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the parameter. This parameter is passed uninitialized. /// 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.cache.TryGetValue(key, out value); } private string GetFileName(string fileName) { var invalidChars = new[] { '`', '[', ',', '=' }; foreach(char c in invalidChars.Concat(System.IO.Path.GetInvalidFileNameChars()).Distinct()) { fileName = fileName.Replace(c, '_'); } return fileName; } } }