From de0f076ef9ff546c9a90513259ad6c42cd2224b3 Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Sat, 29 Sep 2018 16:51:26 +0200 Subject: added firebase api --- FireBase/Offline/ConcurrentOfflineDatabase.cs | 207 ++++++++++ FireBase/Offline/DatabaseExtensions.cs | 195 +++++++++ FireBase/Offline/ISetHandler.cs | 11 + FireBase/Offline/InitialPullStrategy.cs | 23 ++ FireBase/Offline/Internals/MemberAccessVisitor.cs | 51 +++ FireBase/Offline/OfflineCacheAdapter.cs | 165 ++++++++ FireBase/Offline/OfflineDatabase.cs | 201 ++++++++++ FireBase/Offline/OfflineEntry.cs | 116 ++++++ FireBase/Offline/RealtimeDatabase.cs | 459 ++++++++++++++++++++++ FireBase/Offline/SetHandler.cs | 24 ++ FireBase/Offline/StreamingOptions.cs | 21 + FireBase/Offline/SyncOptions.cs | 28 ++ 12 files changed, 1501 insertions(+) create mode 100644 FireBase/Offline/ConcurrentOfflineDatabase.cs create mode 100644 FireBase/Offline/DatabaseExtensions.cs create mode 100644 FireBase/Offline/ISetHandler.cs create mode 100644 FireBase/Offline/InitialPullStrategy.cs create mode 100644 FireBase/Offline/Internals/MemberAccessVisitor.cs create mode 100644 FireBase/Offline/OfflineCacheAdapter.cs create mode 100644 FireBase/Offline/OfflineDatabase.cs create mode 100644 FireBase/Offline/OfflineEntry.cs create mode 100644 FireBase/Offline/RealtimeDatabase.cs create mode 100644 FireBase/Offline/SetHandler.cs create mode 100644 FireBase/Offline/StreamingOptions.cs create mode 100644 FireBase/Offline/SyncOptions.cs (limited to 'FireBase/Offline') diff --git a/FireBase/Offline/ConcurrentOfflineDatabase.cs b/FireBase/Offline/ConcurrentOfflineDatabase.cs new file mode 100644 index 0000000..226892d --- /dev/null +++ b/FireBase/Offline/ConcurrentOfflineDatabase.cs @@ -0,0 +1,207 @@ +namespace Firebase.Database.Offline +{ + using System; + using System.Collections; + using System.Collections.Concurrent; + using System.Collections.Generic; + using System.IO; + using System.Linq; + + using LiteDB; + + /// + /// The offline database. + /// + public class ConcurrentOfflineDatabase : IDictionary + { + private readonly LiteRepository db; + private readonly ConcurrentDictionary ccache; + + /// + /// 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 ConcurrentOfflineDatabase(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)); + + var cache = db.Database + .GetCollection() + .FindAll() + .ToDictionary(o => o.Key, o => o); + + this.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; + + /// + /// Gets a value indicating whether this is a read-only collection. + /// + public bool IsReadOnly => false; + + /// + /// Gets an containing the keys of the . + /// + /// An containing the keys of the object that implements . + public ICollection Keys => this.ccache.Keys; + + /// + /// Gets an containing the values in the . + /// + /// An containing the values in the object that implements . + public ICollection Values => this.ccache.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.ccache[key]; + } + + set + { + this.ccache.AddOrUpdate(key, value, (k, existing) => 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.ccache.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.ccache.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.ccache.ToList().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.ccache.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.ccache.AddOrUpdate(key, value, (k, existing) => value); + this.db.Upsert(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.ccache.TryRemove(key, out OfflineEntry _); + 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.ccache.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; + } + } +} diff --git a/FireBase/Offline/DatabaseExtensions.cs b/FireBase/Offline/DatabaseExtensions.cs new file mode 100644 index 0000000..4b04314 --- /dev/null +++ b/FireBase/Offline/DatabaseExtensions.cs @@ -0,0 +1,195 @@ +namespace Firebase.Database.Offline +{ + using System; + using System.Collections; + using System.Collections.Generic; + using System.Linq.Expressions; + using System.Reflection; + using Firebase.Database.Query; + + public static class DatabaseExtensions + { + /// + /// Create new instances of the . + /// + /// Type of elements. + /// Custom string which will get appended to the file name. + /// Optional custom root element of received json items. + /// Realtime streaming options. + /// Specifies what strategy should be used for initial pulling of server data. + /// Specifies whether changed items should actually be pushed to the server. It this is false, then Put / Post / Delete will not affect server data. + /// The . + public static RealtimeDatabase AsRealtimeDatabase(this ChildQuery query, string filenameModifier = "", string elementRoot = "", StreamingOptions streamingOptions = StreamingOptions.LatestOnly, InitialPullStrategy initialPullStrategy = InitialPullStrategy.MissingOnly, bool pushChanges = true) + where T : class + { + return new RealtimeDatabase(query, elementRoot, query.Client.Options.OfflineDatabaseFactory, filenameModifier, streamingOptions, initialPullStrategy, pushChanges); + } + + /// + /// Create new instances of the . + /// + /// Type of elements. + /// Type of the custom to use. + /// Custom string which will get appended to the file name. + /// Optional custom root element of received json items. + /// Realtime streaming options. + /// Specifies what strategy should be used for initial pulling of server data. + /// Specifies whether changed items should actually be pushed to the server. It this is false, then Put / Post / Delete will not affect server data. + /// The . + public static RealtimeDatabase AsRealtimeDatabase(this ChildQuery query, string filenameModifier = "", string elementRoot = "", StreamingOptions streamingOptions = StreamingOptions.LatestOnly, InitialPullStrategy initialPullStrategy = InitialPullStrategy.MissingOnly, bool pushChanges = true) + where T : class + where TSetHandler : ISetHandler, new() + { + return new RealtimeDatabase(query, elementRoot, query.Client.Options.OfflineDatabaseFactory, filenameModifier, streamingOptions, initialPullStrategy, pushChanges, Activator.CreateInstance()); + } + + /// + /// Overwrites existing object with given key leaving any missing properties intact in firebase. + /// + /// The key. + /// The object to set. + /// Indicates whether the item should be synced online. + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + public static void Patch(this RealtimeDatabase db, string key, T obj, bool syncOnline = true, int priority = 1) + where T: class + { + db.Set(key, obj, syncOnline ? SyncOptions.Patch : SyncOptions.None, priority); + } + + /// + /// Overwrites existing object with given key. + /// + /// The key. + /// The object to set. + /// Indicates whether the item should be synced online. + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + public static void Put(this RealtimeDatabase db, string key, T obj, bool syncOnline = true, int priority = 1) + where T: class + { + db.Set(key, obj, syncOnline ? SyncOptions.Put : SyncOptions.None, priority); + } + + /// + /// Adds a new entity to the Database. + /// + /// The object to add. + /// Indicates whether the item should be synced online. + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// The generated key for this object. + public static string Post(this RealtimeDatabase db, T obj, bool syncOnline = true, int priority = 1) + where T: class + { + var key = FirebaseKeyGenerator.Next(); + + db.Set(key, obj, syncOnline ? SyncOptions.Put : SyncOptions.None, priority); + + return key; + } + + /// + /// Deletes the entity with the given key. + /// + /// The key. + /// Indicates whether the item should be synced online. + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + public static void Delete(this RealtimeDatabase db, string key, bool syncOnline = true, int priority = 1) + where T: class + { + db.Set(key, null, syncOnline ? SyncOptions.Put : SyncOptions.None, priority); + } + + /// + /// Do a Put for a nested property specified by of an object with key . + /// + /// Type of the root elements. + /// Type of the property being modified + /// Database instance. + /// Key of the root element to modify. + /// Expression on the root element leading to target value to modify. + /// Value to put. + /// Indicates whether the item should be synced online. + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + public static void Put(this RealtimeDatabase db, string key, Expression> propertyExpression, TProperty value, bool syncOnline = true, int priority = 1) + where T: class + { + db.Set(key, propertyExpression, value, syncOnline ? SyncOptions.Put : SyncOptions.None, priority); + } + + /// + /// Do a Patch for a nested property specified by of an object with key . + /// + /// Type of the root elements. + /// Type of the property being modified + /// Database instance. + /// Key of the root element to modify. + /// Expression on the root element leading to target value to modify. + /// Value to patch. + /// Indicates whether the item should be synced online. + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + public static void Patch(this RealtimeDatabase db, string key, Expression> propertyExpression, TProperty value, bool syncOnline = true, int priority = 1) + where T: class + { + db.Set(key, propertyExpression, value, syncOnline ? SyncOptions.Patch : SyncOptions.None, priority); + } + + /// + /// Delete a nested property specified by of an object with key . This basically does a Put with null value. + /// + /// Type of the root elements. + /// Type of the property being modified + /// Database instance. + /// Key of the root element to modify. + /// Expression on the root element leading to target value to modify. + /// Value to put. + /// Indicates whether the item should be synced online. + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + public static void Delete(this RealtimeDatabase db, string key, Expression> propertyExpression, bool syncOnline = true, int priority = 1) + where T: class + where TProperty: class + { + db.Set(key, propertyExpression, null, syncOnline ? SyncOptions.Put : SyncOptions.None, priority); + } + + /// + /// Post a new entity into the nested dictionary specified by of an object with key . + /// The key of the new entity is automatically generated. + /// + /// Type of the root elements. + /// Type of the dictionary being modified + /// Type of the value within the dictionary being modified + /// Database instance. + /// Key of the root element to modify. + /// Expression on the root element leading to target value to modify. + /// Value to put. + /// Indicates whether the item should be synced online. + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + public static void Post(this RealtimeDatabase db, string key, Expression> propertyExpression, TProperty value, bool syncOnline = true, int priority = 1) + where T: class + where TSelector: IDictionary + { + var nextKey = FirebaseKeyGenerator.Next(); + var expression = Expression.Lambda>(Expression.Call(propertyExpression.Body, typeof(TSelector).GetRuntimeMethod("get_Item", new[] { typeof(string) }), Expression.Constant(nextKey)), propertyExpression.Parameters); + db.Set(key, expression, value, syncOnline ? SyncOptions.Put : SyncOptions.None, priority); + } + + /// + /// Delete an entity with key in the nested dictionary specified by of an object with key . + /// The key of the new entity is automatically generated. + /// + /// Type of the root elements. + /// Type of the dictionary being modified + /// Type of the value within the dictionary being modified + /// Database instance. + /// Key of the root element to modify. + /// Expression on the root element leading to target value to modify. + /// Key within the nested dictionary to delete. + /// Indicates whether the item should be synced online. + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + public static void Delete(this RealtimeDatabase db, string key, Expression>> propertyExpression, string dictionaryKey, bool syncOnline = true, int priority = 1) + where T: class + { + var expression = Expression.Lambda>(Expression.Call(propertyExpression.Body, typeof(IDictionary).GetRuntimeMethod("get_Item", new[] { typeof(string) }), Expression.Constant(dictionaryKey)), propertyExpression.Parameters); + db.Set(key, expression, null, syncOnline ? SyncOptions.Put : SyncOptions.None, priority); + } + } +} diff --git a/FireBase/Offline/ISetHandler.cs b/FireBase/Offline/ISetHandler.cs new file mode 100644 index 0000000..477c36b --- /dev/null +++ b/FireBase/Offline/ISetHandler.cs @@ -0,0 +1,11 @@ +namespace Firebase.Database.Offline +{ + using Firebase.Database.Query; + + using System.Threading.Tasks; + + public interface ISetHandler + { + Task SetAsync(ChildQuery query, string key, OfflineEntry entry); + } +} diff --git a/FireBase/Offline/InitialPullStrategy.cs b/FireBase/Offline/InitialPullStrategy.cs new file mode 100644 index 0000000..70f6b8c --- /dev/null +++ b/FireBase/Offline/InitialPullStrategy.cs @@ -0,0 +1,23 @@ +namespace Firebase.Database.Offline +{ + /// + /// Specifies the strategy for initial pull of server data. + /// + public enum InitialPullStrategy + { + /// + /// Don't pull anything. + /// + None, + + /// + /// Pull only what isn't already stored offline. + /// + MissingOnly, + + /// + /// Pull everything that exists on the server. + /// + Everything, + } +} diff --git a/FireBase/Offline/Internals/MemberAccessVisitor.cs b/FireBase/Offline/Internals/MemberAccessVisitor.cs new file mode 100644 index 0000000..1f7cb11 --- /dev/null +++ b/FireBase/Offline/Internals/MemberAccessVisitor.cs @@ -0,0 +1,51 @@ +namespace Firebase.Database.Offline.Internals +{ + using System.Collections.Generic; + using System.Linq.Expressions; + using System.Reflection; + + using Newtonsoft.Json; + + public class MemberAccessVisitor : ExpressionVisitor + { + private readonly IList propertyNames = new List(); + + private bool wasDictionaryAccess; + + public IEnumerable PropertyNames => this.propertyNames; + + public MemberAccessVisitor() + { + } + + public override Expression Visit(Expression expr) + { + if (expr?.NodeType == ExpressionType.MemberAccess) + { + if (this.wasDictionaryAccess) + { + this.wasDictionaryAccess = false; + } + else + { + var memberExpr = (MemberExpression)expr; + var jsonAttr = memberExpr.Member.GetCustomAttribute(); + + this.propertyNames.Add(jsonAttr?.PropertyName ?? memberExpr.Member.Name); + } + } + else if (expr?.NodeType == ExpressionType.Call) + { + var callExpr = (MethodCallExpression)expr; + if (callExpr.Method.Name == "get_Item" && callExpr.Arguments.Count == 1) + { + var e = Expression.Lambda(callExpr.Arguments[0]).Compile(); + this.propertyNames.Add(e.DynamicInvoke().ToString()); + this.wasDictionaryAccess = callExpr.Arguments[0].NodeType == ExpressionType.MemberAccess; + } + } + + return base.Visit(expr); + } + } +} diff --git a/FireBase/Offline/OfflineCacheAdapter.cs b/FireBase/Offline/OfflineCacheAdapter.cs new file mode 100644 index 0000000..a3761a0 --- /dev/null +++ b/FireBase/Offline/OfflineCacheAdapter.cs @@ -0,0 +1,165 @@ +namespace Firebase.Database.Offline +{ + using System; + using System.Collections; + using System.Collections.Generic; + using System.Linq; + + internal class OfflineCacheAdapter : IDictionary, IDictionary + { + private readonly IDictionary database; + + public OfflineCacheAdapter(IDictionary database) + { + this.database = database; + } + + public void CopyTo(Array array, int index) + { + throw new NotImplementedException(); + } + + public int Count => this.database.Count; + + public bool IsSynchronized { get; } + + public object SyncRoot { get; } + + public bool IsReadOnly => this.database.IsReadOnly; + + object IDictionary.this[object key] + { + get + { + return this.database[key.ToString()].Deserialize(); + } + + set + { + var keyString = key.ToString(); + if (this.database.ContainsKey(keyString)) + { + this.database[keyString] = new OfflineEntry(keyString, value, this.database[keyString].Priority, this.database[keyString].SyncOptions); + } + else + { + this.database[keyString] = new OfflineEntry(keyString, value, 1, SyncOptions.None); + } + } + } + + public ICollection Keys => this.database.Keys; + + ICollection IDictionary.Values { get; } + + ICollection IDictionary.Keys { get; } + + public ICollection Values => this.database.Values.Select(o => o.Deserialize()).ToList(); + + public T this[string key] + { + get + { + return this.database[key].Deserialize(); + } + + set + { + if (this.database.ContainsKey(key)) + { + this.database[key] = new OfflineEntry(key, value, this.database[key].Priority, this.database[key].SyncOptions); + } + else + { + this.database[key] = new OfflineEntry(key, value, 1, SyncOptions.None); + } + } + } + + public bool Contains(object key) + { + return this.ContainsKey(key.ToString()); + } + + IDictionaryEnumerator IDictionary.GetEnumerator() + { + throw new NotImplementedException(); + } + + public void Remove(object key) + { + this.Remove(key.ToString()); + } + + public bool IsFixedSize => false; + + public IEnumerator> GetEnumerator() + { + return this.database.Select(d => new KeyValuePair(d.Key, d.Value.Deserialize())).GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return this.GetEnumerator(); + } + + public void Add(KeyValuePair item) + { + this.Add(item.Key, item.Value); + } + + public void Add(object key, object value) + { + this.Add(key.ToString(), (T)value); + } + + public void Clear() + { + this.database.Clear(); + } + + public bool Contains(KeyValuePair item) + { + return this.ContainsKey(item.Key); + } + + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + throw new NotImplementedException(); + } + + public bool Remove(KeyValuePair item) + { + return this.database.Remove(item.Key); + } + + public void Add(string key, T value) + { + this.database.Add(key, new OfflineEntry(key, value, 1, SyncOptions.None)); + } + + public bool ContainsKey(string key) + { + return this.database.ContainsKey(key); + } + + public bool Remove(string key) + { + return this.database.Remove(key); + } + + public bool TryGetValue(string key, out T value) + { + OfflineEntry val; + + if (this.database.TryGetValue(key, out val)) + { + value = val.Deserialize(); + return true; + } + + value = default(T); + return false; + } + } +} diff --git a/FireBase/Offline/OfflineDatabase.cs b/FireBase/Offline/OfflineDatabase.cs new file mode 100644 index 0000000..9cebf9c --- /dev/null +++ b/FireBase/Offline/OfflineDatabase.cs @@ -0,0 +1,201 @@ +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; + } + } +} diff --git a/FireBase/Offline/OfflineEntry.cs b/FireBase/Offline/OfflineEntry.cs new file mode 100644 index 0000000..3b862cb --- /dev/null +++ b/FireBase/Offline/OfflineEntry.cs @@ -0,0 +1,116 @@ +namespace Firebase.Database.Offline +{ + using System; + + using Newtonsoft.Json; + + /// + /// Represents an object stored in offline storage. + /// + public class OfflineEntry + { + private object dataInstance; + + /// + /// Initializes a new instance of the class with an already serialized object. + /// + /// The key. + /// The object. + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// The sync options. + public OfflineEntry(string key, object obj, string data, int priority, SyncOptions syncOptions, bool isPartial = false) + { + this.Key = key; + this.Priority = priority; + this.Data = data; + this.Timestamp = DateTime.UtcNow; + this.SyncOptions = syncOptions; + this.IsPartial = isPartial; + + this.dataInstance = obj; + } + + /// + /// Initializes a new instance of the class. + /// + /// The key. + /// The object. + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// The sync options. + public OfflineEntry(string key, object obj, int priority, SyncOptions syncOptions, bool isPartial = false) + : this(key, obj, JsonConvert.SerializeObject(obj), priority, syncOptions, isPartial) + { + } + + /// + /// Initializes a new instance of the class. + /// + public OfflineEntry() + { + } + + /// + /// Gets or sets the key of this entry. + /// + public string Key + { + get; + set; + } + + /// + /// Gets or sets the priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + /// + public int Priority + { + get; + set; + } + + /// + /// Gets or sets the timestamp when this entry was last touched. + /// + public DateTime Timestamp + { + get; + set; + } + + /// + /// Gets or sets the which define what sync state this entry is in. + /// + public SyncOptions SyncOptions + { + get; + set; + } + + /// + /// Gets or sets serialized JSON data. + /// + public string Data + { + get; + set; + } + + /// + /// Specifies whether this is only a partial object. + /// + public bool IsPartial + { + get; + set; + } + + /// + /// Deserializes into . The result is cached. + /// + /// Type of object to deserialize into. + /// Instance of . + public T Deserialize() + { + return (T)(this.dataInstance ?? (this.dataInstance = JsonConvert.DeserializeObject(this.Data))); + } + } +} diff --git a/FireBase/Offline/RealtimeDatabase.cs b/FireBase/Offline/RealtimeDatabase.cs new file mode 100644 index 0000000..61a7010 --- /dev/null +++ b/FireBase/Offline/RealtimeDatabase.cs @@ -0,0 +1,459 @@ +namespace Firebase.Database.Offline +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Reactive.Linq; + using System.Reactive.Subjects; + using System.Threading; + using System.Threading.Tasks; + + using Firebase.Database.Extensions; + using Firebase.Database.Query; + using Firebase.Database.Streaming; + using System.Reactive.Threading.Tasks; + using System.Linq.Expressions; + using Internals; + using Newtonsoft.Json; + using System.Reflection; + using System.Reactive.Disposables; + + /// + /// The real-time Database which synchronizes online and offline data. + /// + /// Type of entities. + public partial class RealtimeDatabase : IDisposable where T : class + { + private readonly ChildQuery childQuery; + private readonly string elementRoot; + private readonly StreamingOptions streamingOptions; + private readonly Subject> subject; + private readonly InitialPullStrategy initialPullStrategy; + private readonly bool pushChanges; + private readonly FirebaseCache firebaseCache; + + private bool isSyncRunning; + private IObservable> observable; + private FirebaseSubscription firebaseSubscription; + + /// + /// Initializes a new instance of the class. + /// + /// The child query. + /// The element Root. + /// The offline database factory. + /// Custom string which will get appended to the file name. + /// Specifies whether changes should be streamed from the server. + /// Specifies if everything should be pull from the online storage on start. It only makes sense when is set to true. + /// Specifies whether changed items should actually be pushed to the server. If this is false, then Put / Post / Delete will not affect server data. + public RealtimeDatabase(ChildQuery childQuery, string elementRoot, Func> offlineDatabaseFactory, string filenameModifier, StreamingOptions streamingOptions, InitialPullStrategy initialPullStrategy, bool pushChanges, ISetHandler setHandler = null) + { + this.childQuery = childQuery; + this.elementRoot = elementRoot; + this.streamingOptions = streamingOptions; + this.initialPullStrategy = initialPullStrategy; + this.pushChanges = pushChanges; + this.Database = offlineDatabaseFactory(typeof(T), filenameModifier); + this.firebaseCache = new FirebaseCache(new OfflineCacheAdapter(this.Database)); + this.subject = new Subject>(); + + this.PutHandler = setHandler ?? new SetHandler(); + + this.isSyncRunning = true; + Task.Factory.StartNew(this.SynchronizeThread, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); + } + + /// + /// Event raised whenever an exception is thrown in the synchronization thread. Exception thrown in there are swallowed, so this event is the only way to get to them. + /// + public event EventHandler SyncExceptionThrown; + + /// + /// Gets the backing Database. + /// + public IDictionary Database + { + get; + private set; + } + + public ISetHandler PutHandler + { + private get; + set; + } + + /// + /// Overwrites existing object with given key. + /// + /// The key. + /// The object to set. + /// Indicates whether the item should be synced online. + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + public void Set(string key, T obj, SyncOptions syncOptions, int priority = 1) + { + this.SetAndRaise(key, new OfflineEntry(key, obj, priority, syncOptions)); + } + + public void Set(string key, Expression> propertyExpression, object value, SyncOptions syncOptions, int priority = 1) + { + var fullKey = this.GenerateFullKey(key, propertyExpression, syncOptions); + var serializedObject = JsonConvert.SerializeObject(value).Trim('"', '\\'); + + if (fullKey.Item3) + { + if (typeof(TProperty) != typeof(string) || value == null) + { + // don't escape non-string primitives and null; + serializedObject = $"{{ \"{fullKey.Item2}\" : {serializedObject} }}"; + } + else + { + serializedObject = $"{{ \"{fullKey.Item2}\" : \"{serializedObject}\" }}"; + } + } + + var setObject = this.firebaseCache.PushData("/" + fullKey.Item1, serializedObject).First(); + + if (!this.Database.ContainsKey(key) || this.Database[key].SyncOptions != SyncOptions.Patch && this.Database[key].SyncOptions != SyncOptions.Put) + { + this.Database[fullKey.Item1] = new OfflineEntry(fullKey.Item1, value, serializedObject, priority, syncOptions, true); + } + + this.subject.OnNext(new FirebaseEvent(key, setObject.Object, setObject == null ? FirebaseEventType.Delete : FirebaseEventType.InsertOrUpdate, FirebaseEventSource.Offline)); + } + + /// + /// Fetches an object with the given key and adds it to the Database. + /// + /// The key. + /// The priority. Objects with higher priority will be synced first. Higher number indicates higher priority. + public void Pull(string key, int priority = 1) + { + if (!this.Database.ContainsKey(key)) + { + this.Database[key] = new OfflineEntry(key, null, priority, SyncOptions.Pull); + } + else if (this.Database[key].SyncOptions == SyncOptions.None) + { + // pull only if push isn't pending + this.Database[key].SyncOptions = SyncOptions.Pull; + } + } + + /// + /// Fetches everything from the remote database. + /// + public async Task PullAsync() + { + var existingEntries = await this.childQuery + .OnceAsync() + .ToObservable() + .RetryAfterDelay>, FirebaseException>( + this.childQuery.Client.Options.SyncPeriod, + ex => ex.StatusCode == System.Net.HttpStatusCode.OK) // OK implies the request couldn't complete due to network error. + .Select(e => this.ResetDatabaseFromInitial(e, false)) + .SelectMany(e => e) + .Do(e => + { + this.Database[e.Key] = new OfflineEntry(e.Key, e.Object, 1, SyncOptions.None); + this.subject.OnNext(new FirebaseEvent(e.Key, e.Object, FirebaseEventType.InsertOrUpdate, FirebaseEventSource.OnlinePull)); + }) + .ToList(); + + // Remove items not stored online + foreach (var item in this.Database.Keys.Except(existingEntries.Select(f => f.Key)).ToList()) + { + this.Database.Remove(item); + this.subject.OnNext(new FirebaseEvent(item, null, FirebaseEventType.Delete, FirebaseEventSource.OnlinePull)); + } + } + + /// + /// Retrieves all offline items currently stored in local database. + /// + public IEnumerable> Once() + { + return this.Database + .Where(kvp => !string.IsNullOrEmpty(kvp.Value.Data) && kvp.Value.Data != "null" && !kvp.Value.IsPartial) + .Select(kvp => new FirebaseObject(kvp.Key, kvp.Value.Deserialize())) + .ToList(); + } + + /// + /// Starts observing the real-time Database. Events will be fired both when change is done locally and remotely. + /// + /// Stream of . + public IObservable> AsObservable() + { + if (!this.isSyncRunning) + { + this.isSyncRunning = true; + Task.Factory.StartNew(this.SynchronizeThread, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); + } + + if (this.observable == null) + { + var initialData = Observable.Return(FirebaseEvent.Empty(FirebaseEventSource.Offline)); + if(this.Database.TryGetValue(this.elementRoot, out OfflineEntry oe)) + { + initialData = Observable.Return(oe) + .Where(offlineEntry => !string.IsNullOrEmpty(offlineEntry.Data) && offlineEntry.Data != "null" && !offlineEntry.IsPartial) + .Select(offlineEntry => new FirebaseEvent(offlineEntry.Key, offlineEntry.Deserialize(), FirebaseEventType.InsertOrUpdate, FirebaseEventSource.Offline)); + } + else if(this.Database.Count > 0) + { + initialData = this.Database + .Where(kvp => !string.IsNullOrEmpty(kvp.Value.Data) && kvp.Value.Data != "null" && !kvp.Value.IsPartial) + .Select(kvp => new FirebaseEvent(kvp.Key, kvp.Value.Deserialize(), FirebaseEventType.InsertOrUpdate, FirebaseEventSource.Offline)) + .ToList() + .ToObservable(); + } + + this.observable = initialData + .Merge(this.subject) + .Merge(this.GetInitialPullObservable() + .RetryAfterDelay>, FirebaseException>( + this.childQuery.Client.Options.SyncPeriod, + ex => ex.StatusCode == System.Net.HttpStatusCode.OK) // OK implies the request couldn't complete due to network error. + .Select(e => this.ResetDatabaseFromInitial(e)) + .SelectMany(e => e) + .Do(this.SetObjectFromInitialPull) + .Select(e => new FirebaseEvent(e.Key, e.Object, e.Object == null ? FirebaseEventType.Delete : FirebaseEventType.InsertOrUpdate, FirebaseEventSource.OnlineInitial)) + .Concat(Observable.Create>(observer => this.InitializeStreamingSubscription(observer)))) + .Do(next => { }, e => this.observable = null, () => this.observable = null) + .Replay() + .RefCount(); + } + + return this.observable; + } + + public void Dispose() + { + this.subject.OnCompleted(); + this.firebaseSubscription?.Dispose(); + } + + private IReadOnlyCollection> ResetDatabaseFromInitial(IReadOnlyCollection> collection, bool onlyWhenInitialEverything = true) + { + if (onlyWhenInitialEverything && this.initialPullStrategy != InitialPullStrategy.Everything) + { + return collection; + } + + // items which are in local db, but not in the online collection + var extra = this.Once() + .Select(f => f.Key) + .Except(collection.Select(c => c.Key)) + .Select(k => new FirebaseObject(k, null)); + + return collection.Concat(extra).ToList(); + } + + private void SetObjectFromInitialPull(FirebaseObject e) + { + // set object with no sync only if it doesn't exist yet + // and the InitialPullStrategy != Everything + // this attempts to deal with scenario when you are offline, have local changes and go online + // in this case having the InitialPullStrategy set to everything would basically purge all local changes + if (!this.Database.ContainsKey(e.Key) || this.Database[e.Key].SyncOptions == SyncOptions.None || this.Database[e.Key].SyncOptions == SyncOptions.Pull || this.initialPullStrategy != InitialPullStrategy.Everything) + { + this.Database[e.Key] = new OfflineEntry(e.Key, e.Object, 1, SyncOptions.None); + } + } + + private IObservable>> GetInitialPullObservable() + { + FirebaseQuery query; + switch (this.initialPullStrategy) + { + case InitialPullStrategy.MissingOnly: + query = this.childQuery.OrderByKey().StartAt(() => this.GetLatestKey()); + break; + case InitialPullStrategy.Everything: + query = this.childQuery; + break; + case InitialPullStrategy.None: + default: + return Observable.Empty>>(); + } + + if (string.IsNullOrWhiteSpace(this.elementRoot)) + { + return Observable.Defer(() => query.OnceAsync().ToObservable()); + } + + // there is an element root, which indicates the target location is not a collection but a single element + return Observable.Defer(async () => Observable.Return(await query.OnceSingleAsync()).Select(e => new[] { new FirebaseObject(this.elementRoot, e) })); + } + + private IDisposable InitializeStreamingSubscription(IObserver> observer) + { + var completeDisposable = Disposable.Create(() => this.isSyncRunning = false); + + switch (this.streamingOptions) + { + case StreamingOptions.LatestOnly: + // stream since the latest key + var queryLatest = this.childQuery.OrderByKey().StartAt(() => this.GetLatestKey()); + this.firebaseSubscription = new FirebaseSubscription(observer, queryLatest, this.elementRoot, this.firebaseCache); + this.firebaseSubscription.ExceptionThrown += this.StreamingExceptionThrown; + + return new CompositeDisposable(this.firebaseSubscription.Run(), completeDisposable); + case StreamingOptions.Everything: + // stream everything + var queryAll = this.childQuery; + this.firebaseSubscription = new FirebaseSubscription(observer, queryAll, this.elementRoot, this.firebaseCache); + this.firebaseSubscription.ExceptionThrown += this.StreamingExceptionThrown; + + return new CompositeDisposable(this.firebaseSubscription.Run(), completeDisposable); + default: + break; + } + + return completeDisposable; + } + + private void SetAndRaise(string key, OfflineEntry obj, FirebaseEventSource eventSource = FirebaseEventSource.Offline) + { + this.Database[key] = obj; + this.subject.OnNext(new FirebaseEvent(key, obj?.Deserialize(), string.IsNullOrEmpty(obj?.Data) || obj?.Data == "null" ? FirebaseEventType.Delete : FirebaseEventType.InsertOrUpdate, eventSource)); + } + + private async void SynchronizeThread() + { + while (this.isSyncRunning) + { + try + { + var validEntries = this.Database.Where(e => e.Value != null); + await this.PullEntriesAsync(validEntries.Where(kvp => kvp.Value.SyncOptions == SyncOptions.Pull)); + + if (this.pushChanges) + { + await this.PushEntriesAsync(validEntries.Where(kvp => kvp.Value.SyncOptions == SyncOptions.Put || kvp.Value.SyncOptions == SyncOptions.Patch)); + } + } + catch (Exception ex) + { + this.SyncExceptionThrown?.Invoke(this, new ExceptionEventArgs(ex)); + } + + await Task.Delay(this.childQuery.Client.Options.SyncPeriod); + } + } + + private string GetLatestKey() + { + var key = this.Database.OrderBy(o => o.Key, StringComparer.Ordinal).LastOrDefault().Key ?? string.Empty; + + if (!string.IsNullOrWhiteSpace(key)) + { + key = key.Substring(0, key.Length - 1) + (char)(key[key.Length - 1] + 1); + } + + return key; + } + + private async Task PushEntriesAsync(IEnumerable> pushEntries) + { + var groups = pushEntries.GroupBy(pair => pair.Value.Priority).OrderByDescending(kvp => kvp.Key).ToList(); + + foreach (var group in groups) + { + var tasks = group.OrderBy(kvp => kvp.Value.IsPartial).Select(kvp => + kvp.Value.IsPartial ? + this.ResetSyncAfterPush(this.PutHandler.SetAsync(this.childQuery, kvp.Key, kvp.Value), kvp.Key) : + this.ResetSyncAfterPush(this.PutHandler.SetAsync(this.childQuery, kvp.Key, kvp.Value), kvp.Key, kvp.Value.Deserialize())); + + try + { + await Task.WhenAll(tasks).WithAggregateException(); + } + catch (Exception ex) + { + this.SyncExceptionThrown?.Invoke(this, new ExceptionEventArgs(ex)); + } + } + } + + private async Task PullEntriesAsync(IEnumerable> pullEntries) + { + var taskGroups = pullEntries.GroupBy(pair => pair.Value.Priority).OrderByDescending(kvp => kvp.Key); + + foreach (var group in taskGroups) + { + var tasks = group.Select(pair => this.ResetAfterPull(this.childQuery.Child(pair.Key == this.elementRoot ? string.Empty : pair.Key).OnceSingleAsync(), pair.Key, pair.Value)); + + try + { + await Task.WhenAll(tasks).WithAggregateException(); + } + catch (Exception ex) + { + this.SyncExceptionThrown?.Invoke(this, new ExceptionEventArgs(ex)); + } + } + } + + private async Task ResetAfterPull(Task task, string key, OfflineEntry entry) + { + await task; + this.SetAndRaise(key, new OfflineEntry(key, task.Result, entry.Priority, SyncOptions.None), FirebaseEventSource.OnlinePull); + } + + private async Task ResetSyncAfterPush(Task task, string key, T obj) + { + await this.ResetSyncAfterPush(task, key); + + if (this.streamingOptions == StreamingOptions.None) + { + this.subject.OnNext(new FirebaseEvent(key, obj, obj == null ? FirebaseEventType.Delete : FirebaseEventType.InsertOrUpdate, FirebaseEventSource.OnlinePush)); + } + } + + private async Task ResetSyncAfterPush(Task task, string key) + { + await task; + this.ResetSyncOptions(key); + } + + private void ResetSyncOptions(string key) + { + var item = this.Database[key]; + + if (item.IsPartial) + { + this.Database.Remove(key); + } + else + { + item.SyncOptions = SyncOptions.None; + this.Database[key] = item; + } + } + + private void StreamingExceptionThrown(object sender, ExceptionEventArgs e) + { + this.SyncExceptionThrown?.Invoke(this, new ExceptionEventArgs(e.Exception)); + } + + private Tuple GenerateFullKey(string key, Expression> propertyGetter, SyncOptions syncOptions) + { + var visitor = new MemberAccessVisitor(); + visitor.Visit(propertyGetter); + var propertyType = typeof(TProperty).GetTypeInfo(); + var prefix = key == string.Empty ? string.Empty : key + "/"; + + // primitive types + if (syncOptions == SyncOptions.Patch && (propertyType.IsPrimitive || Nullable.GetUnderlyingType(typeof(TProperty)) != null || typeof(TProperty) == typeof(string))) + { + return Tuple.Create(prefix + string.Join("/", visitor.PropertyNames.Skip(1).Reverse()), visitor.PropertyNames.First(), true); + } + + return Tuple.Create(prefix + string.Join("/", visitor.PropertyNames.Reverse()), visitor.PropertyNames.First(), false); + } + + } +} diff --git a/FireBase/Offline/SetHandler.cs b/FireBase/Offline/SetHandler.cs new file mode 100644 index 0000000..1efa7b6 --- /dev/null +++ b/FireBase/Offline/SetHandler.cs @@ -0,0 +1,24 @@ +namespace Firebase.Database.Offline +{ + using Firebase.Database.Query; + + using System.Threading.Tasks; + + public class SetHandler : ISetHandler + { + public virtual async Task SetAsync(ChildQuery query, string key, OfflineEntry entry) + { + using (var child = query.Child(key)) + { + if (entry.SyncOptions == SyncOptions.Put) + { + await child.PutAsync(entry.Data); + } + else + { + await child.PatchAsync(entry.Data); + } + } + } + } +} diff --git a/FireBase/Offline/StreamingOptions.cs b/FireBase/Offline/StreamingOptions.cs new file mode 100644 index 0000000..9ed4e54 --- /dev/null +++ b/FireBase/Offline/StreamingOptions.cs @@ -0,0 +1,21 @@ +namespace Firebase.Database.Offline +{ + public enum StreamingOptions + { + /// + /// No realtime streaming. + /// + None, + + /// + /// Streaming of only new items - not the existing ones. + /// + LatestOnly, + + /// + /// Streaming of all items. This will also pull all existing items on start, so be mindful about the number of items in your DB. + /// When used, consider not setting the to because you would pointlessly pull everything twice. + /// + Everything + } +} diff --git a/FireBase/Offline/SyncOptions.cs b/FireBase/Offline/SyncOptions.cs new file mode 100644 index 0000000..b2f382a --- /dev/null +++ b/FireBase/Offline/SyncOptions.cs @@ -0,0 +1,28 @@ +namespace Firebase.Database.Offline +{ + /// + /// Specifies type of sync requested for given data. + /// + public enum SyncOptions + { + /// + /// No sync needed for given data. + /// + None, + + /// + /// Data should be pulled from firebase. + /// + Pull, + + /// + /// Data should be put to firebase. + /// + Put, + + /// + /// Data should be patched in firebase. + /// + Patch + } +} -- cgit v1.2.3-54-g00ecf