using LiteDB; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Tango.DataStore.Lite { public class LiteDBDataStoreCollection : IDataStoreCollection { private ILiteCollection _collection; public string Name { get; private set; } public LiteDBDataStoreCollection(ILiteCollection collection) { _collection = collection; } public void Put(string key, T value) { Put(key, (object)value); } public void Put(string key, object value) { Put(key, DataStoreHelper.GetDataType(value), value); } public void Put(string key, DataType type, object value) { _collection.Upsert(new LiteDBDataStoreItem() { Key = key, Date = DateTime.UtcNow, Type = type, Value = value, IsSynchronized = false, }); } public T Get(string key) { return (T)Convert.ChangeType(Get(key), typeof(T)); } public T Get(string key, T defaultValue) { return (T)Convert.ChangeType(Get(key, defaultValue), typeof(T)); } public object Get(string key) { return Get(key, null); } public object Get(string key, object defaultValue) { return GetItem(key, defaultValue).Value; } public IDataStoreItem GetItem(string key) { return GetItem(key, null); } public IDataStoreItem GetItem(string key, object defaultValue) { var item = _collection.FindById(key); if (item == null) { if (defaultValue == null) { throw new KeyNotFoundException("The specified key was not found on the data store."); } else { Put(key, defaultValue); return GetItem(key); } } return item; } public List GetAll() { return _collection.FindAll().ToList(); } public void Delete(string key) { _collection.Delete(key); } public void DeleteAll() { _collection.DeleteMany(x => true); } public int Count() { return _collection.Count(); } public List GetUnsynchronized() { return _collection.Find(x => !x.IsSynchronized).ToList(); } } }