summaryrefslogtreecommitdiff
path: root/FireBase/Offline/OfflineCacheAdapter.cs
blob: a3761a00e36bd44724636ececcb4391684a64e3c (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
namespace Firebase.Database.Offline
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;

    internal class OfflineCacheAdapter<TKey, T> : IDictionary<string, T>, IDictionary 
    {
        private readonly IDictionary<string, OfflineEntry> database;

        public OfflineCacheAdapter(IDictionary<string, OfflineEntry> 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<T>();
            }

            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<string> Keys => this.database.Keys;

        ICollection IDictionary.Values { get; }

        ICollection IDictionary.Keys { get; }

        public ICollection<T> Values => this.database.Values.Select(o => o.Deserialize<T>()).ToList();

        public T this[string key]
        {
            get
            {
                return this.database[key].Deserialize<T>();
            }

            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<KeyValuePair<string, T>> GetEnumerator()
        {
            return this.database.Select(d => new KeyValuePair<string, T>(d.Key, d.Value.Deserialize<T>())).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        public void Add(KeyValuePair<string, T> 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<string, T> item)
        {
            return this.ContainsKey(item.Key);
        }

        public void CopyTo(KeyValuePair<string, T>[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public bool Remove(KeyValuePair<string, T> 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<T>();
                return true;
            }

            value = default(T);
            return false;
        }
    }
}