aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.DataStore.LiteDB/LiteDBDataStoreCollection.cs
blob: c76a3b6d9701841da03c6221a80cc1d456a8a447 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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<IDataStoreItem> _collection;

        public string Name { get; private set; }

        public LiteDBDataStoreCollection(ILiteCollection<IDataStoreItem> collection)
        {
            _collection = collection;
        }

        public void Put<T>(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<T>(string key)
        {
            return (T)Convert.ChangeType(Get(key), typeof(T));
        }

        public T Get<T>(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<IDataStoreItem> 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<IDataStoreItem> GetUnsynchronized()
        {
            return _collection.Find(x => !x.IsSynchronized).ToList();
        }
    }
}