Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataAccess.ADOHelper/CachedDataAdapter.cs @ 1377

Last change on this file since 1377 was 1377, checked in by svonolfe, 15 years ago

Created Heuristiclab DB Core (refactoring) #527

File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Data;
26using System.Threading;
27using HeuristicLab.DataAccess.Interfaces;
28
29namespace HeuristicLab.DataAccess.ADOHelper {
30  public abstract class CachedDataAdapter<AdapterT, ObjT, RowT, CacheT> :
31    DataAdapterBase<AdapterT, ObjT, RowT>,
32    ICachedDataAdapter
33    where CacheT : System.Data.TypedTableBase<RowT>, new()
34    where AdapterT : new()
35    where RowT : System.Data.DataRow
36    where ObjT : IPersistableObject, new() {
37    protected static CacheT cache =
38      new CacheT();
39
40    private static bool cacheFilled = false;
41
42    private static ReaderWriterLockSlim cacheLock =
43      new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
44
45    protected ICollection<ICachedDataAdapter> parentAdapters =
46      new List<ICachedDataAdapter>();
47
48    protected CachedDataAdapter(IDBSynchronizer synchronizer) {
49      cacheLock.EnterWriteLock();
50
51      if (!cacheFilled) {
52        FillCache();
53        cacheFilled = true;
54      }
55
56      cacheLock.ExitWriteLock();
57
58      synchronizer.OnUpdate +=
59        new EventHandler(CachedDataAdapter_OnUpdate);
60    }
61
62    protected virtual RowT FindSingleRow(Selector dbSelector,
63      Selector cacheSelector) {
64      cacheLock.EnterReadLock();
65     
66      RowT row =
67         FindSingleRow(cacheSelector);
68
69      //not in cache
70      if (row == null) {
71        row =
72          FindSingleRow(dbSelector);
73      }
74
75      cacheLock.ExitReadLock();
76
77      return row;
78    }
79
80    protected virtual IEnumerable<RowT> FindMultipleRows(Selector dbSelector,
81        Selector cacheSelector) {
82      cacheLock.EnterReadLock();
83
84      IList<RowT> result =
85         new List<RowT>(cacheSelector());
86
87      IEnumerable<RowT> result2 =
88          dbSelector();
89
90      foreach (RowT row in result2) {
91        if (!IsCached(row)) {
92          result.Add(row);
93        }
94      }
95
96      cacheLock.ExitReadLock();
97
98      return result;
99    }
100
101    protected virtual ObjT FindSingle(Selector dbSelector,
102      Selector cacheSelector) {
103      ObjT obj = default(ObjT);
104
105      cacheLock.EnterReadLock();
106
107      RowT row = FindSingleRow(dbSelector, cacheSelector);
108
109      if (row != null) {
110        obj = new ObjT();
111        obj = Convert(row, obj);
112      }
113
114      cacheLock.ExitReadLock();
115
116      return obj;
117    }
118
119    protected virtual ICollection<ObjT> FindMultiple(Selector dbSelector,
120      Selector cacheSelector) {
121      cacheLock.EnterReadLock();
122
123      ICollection<ObjT> result =
124        FindMultiple(cacheSelector);
125
126      ICollection<ObjT> resultDb =
127        FindMultiple(dbSelector);
128
129      cacheLock.ExitReadLock();
130
131      foreach (ObjT obj in resultDb) {
132        if (!result.Contains(obj))
133          result.Add(obj);
134      }
135
136      return result;
137    }
138
139    protected abstract RowT InsertNewRowInCache(ObjT obj);
140
141    protected abstract void FillCache();
142
143    protected abstract void SynchronizeWithDb();
144
145    protected abstract bool PutInCache(ObjT obj);
146
147    protected abstract RowT FindCachedById(long id);
148
149    public void SyncWithDb() {
150      foreach (ICachedDataAdapter parent in this.parentAdapters) {       
151        parent.SyncWithDb();
152      }
153
154      cacheLock.EnterReadLock();
155
156      this.SynchronizeWithDb();
157
158      cacheLock.ExitReadLock();
159    }
160
161    void CachedDataAdapter_OnUpdate(object sender, EventArgs e) {
162      this.SyncWithDb();
163    }
164
165    protected virtual bool IsCached(RowT row) {
166      if (row == null)
167        return false;
168      else {
169        cacheLock.EnterReadLock();
170
171        bool cached = FindCachedById((long)row[row.Table.PrimaryKey[0]]) != null;
172
173        cacheLock.ExitReadLock();
174
175        return cached;
176      }
177    }
178
179    protected override RowT GetRowById(long id) {
180      cacheLock.EnterReadLock();
181
182      RowT row =
183        FindCachedById(id);
184
185      //not in cache
186      if (row == null)
187        row = FindSingleRow(
188          delegate() {
189            return FindById(id);
190          });
191
192      cacheLock.ExitReadLock();
193
194      return row;
195    }
196
197    private void AddToCache(RowT row) {
198      cacheLock.EnterWriteLock();
199
200      cache.ImportRow(row);
201      row.Table.Rows.Remove(row);
202
203      cacheLock.ExitWriteLock();
204    }
205
206    private RowT AddToCache(ObjT obj) {
207      cacheLock.EnterWriteLock();
208
209      RowT row =  InsertNewRowInCache(obj);
210
211      cacheLock.ExitWriteLock();
212
213      return row;
214    }
215
216    private void RemoveRowFromCache(RowT row) {
217      cacheLock.EnterWriteLock();
218
219      UpdateRow(row);
220
221      row.Table.Rows.Remove(row);     
222
223      cacheLock.ExitWriteLock();
224    }
225
226    public override void Update(ObjT obj) {
227      if (obj != null) {
228        RowT row = null;
229        long locked = default(long);
230
231        if (obj.Id != default(long)) {
232          LockRow(obj.Id);
233          locked = obj.Id;
234
235          row = GetRowById(obj.Id);
236        }
237
238        if (row == null) {
239          if (PutInCache(obj)) {
240            row = AddToCache(obj);
241          } else {
242            row = InsertNewRow(obj);
243          }
244
245          UpdateRow(row);
246          obj.Id = (long)row[row.Table.PrimaryKey[0]];
247        }
248
249        if (locked == default(long)) {
250          LockRow(obj.Id);
251          locked = obj.Id;
252        }
253
254        ConvertObj(obj, row);
255
256        if (!IsCached(row))
257          UpdateRow(row);
258
259        if (IsCached(row) &&
260            !PutInCache(obj)) {         
261          RemoveRowFromCache(row);
262        } else if (!IsCached(row) &&
263          PutInCache(obj)) {
264          AddToCache(row);
265        }
266
267        UnlockRow(locked);
268      }
269    }
270  }
271}
272
Note: See TracBrowser for help on using the repository browser.