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;
}
}
}