Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/CachedDataAdapter.cs @ 1155

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

Replaced the ReaderWriterLock with a ReaderWriterLockSlim (improved version) (#372)

File size: 6.3 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.Linq;
25using System.Text;
26using HeuristicLab.Hive.Contracts.BusinessObjects;
27using System.Data;
28using System.Threading;
29
30namespace HeuristicLab.Hive.Server.ADODataAccess {
31  abstract class CachedDataAdapter<AdapterT, ObjT, RowT, CacheT> :
32    DataAdapterBase<AdapterT, ObjT, RowT>,
33    ICachedDataAdapter
34    where CacheT : System.Data.TypedTableBase<RowT>, new()
35    where AdapterT : new()
36    where RowT : System.Data.DataRow
37    where ObjT : IHiveObject, new() {
38    protected static CacheT cache =
39      new CacheT();
40
41    private static bool cacheFilled = false;
42
43    private static ReaderWriterLockSlim cacheLock =
44      new ReaderWriterLockSlim();
45
46    protected DataTable dataTable =
47      new DataTable();
48
49    protected ICollection<ICachedDataAdapter> parentAdapters =
50      new List<ICachedDataAdapter>();
51
52    protected CachedDataAdapter() {
53      cacheLock.EnterWriteLock();
54
55      if (!cacheFilled) {
56        FillCache();
57        cacheFilled = true;
58      }
59
60      cacheLock.ExitWriteLock();
61
62      ServiceLocator.GetTransactionManager().OnUpdate +=
63        new EventHandler(CachedDataAdapter_OnUpdate);
64    }
65
66    protected virtual RowT FindSingleRow(Selector dbSelector,
67      Selector cacheSelector) {
68      cacheLock.EnterReadLock();
69     
70      RowT row =
71         FindSingleRow(cacheSelector);
72
73      //not in cache
74      if (row == null) {
75        row =
76          FindSingleRow(dbSelector);
77      }
78
79      cacheLock.ExitReadLock();
80
81      return row;
82    }
83
84    protected virtual IEnumerable<RowT> FindMultipleRows(Selector dbSelector,
85        Selector cacheSelector) {
86      cacheLock.EnterReadLock();
87
88      IList<RowT> result =
89         new List<RowT>(cacheSelector());
90
91      IEnumerable<RowT> result2 =
92          dbSelector();
93
94      foreach (RowT row in result2) {
95        if (!IsCached(row)) {
96          result.Add(row);
97        }
98      }
99
100      cacheLock.ExitReadLock();
101
102      return result;
103    }
104
105    protected virtual ObjT FindSingle(Selector dbSelector,
106      Selector cacheSelector) {
107      ObjT obj = default(ObjT);
108
109      cacheLock.EnterReadLock();
110
111      RowT row = FindSingleRow(dbSelector, cacheSelector);
112
113      if (row != null) {
114        obj = new ObjT();
115        obj = Convert(row, obj);
116      }
117
118      cacheLock.ExitReadLock();
119
120      return obj;
121    }
122
123    protected virtual ICollection<ObjT> FindMultiple(Selector dbSelector,
124      Selector cacheSelector) {
125      cacheLock.EnterReadLock();
126
127      ICollection<ObjT> result =
128        FindMultiple(cacheSelector);
129
130      ICollection<ObjT> resultDb =
131        FindMultiple(dbSelector);
132
133      cacheLock.ExitReadLock();
134
135      foreach (ObjT obj in resultDb) {
136        if (!result.Contains(obj))
137          result.Add(obj);
138      }
139
140      return result;
141    }
142
143    protected abstract RowT InsertNewRowInCache(ObjT obj);
144
145    protected abstract void FillCache();
146
147    protected abstract void SynchronizeWithDb();
148
149    protected abstract bool PutInCache(ObjT obj);
150
151    protected abstract RowT FindCachedById(long id);
152
153    public void SyncWithDb() {
154      foreach (ICachedDataAdapter parent in this.parentAdapters) {       
155        parent.SyncWithDb();
156      }
157
158      cacheLock.EnterReadLock();
159
160      this.SynchronizeWithDb();
161
162      cacheLock.ExitReadLock();
163    }
164
165    void CachedDataAdapter_OnUpdate(object sender, EventArgs e) {
166      this.SyncWithDb();
167    }
168
169    protected virtual bool IsCached(RowT row) {
170      if (row == null)
171        return false;
172      else {
173        cacheLock.EnterReadLock();
174
175        bool cached = FindCachedById((long)row[row.Table.PrimaryKey[0]]) != null;
176
177        cacheLock.ExitReadLock();
178
179        return cached;
180      }
181    }
182
183    protected override RowT GetRowById(long id) {
184      cacheLock.EnterReadLock();
185
186      RowT row =
187        FindCachedById(id);
188
189      //not in cache
190      if (row == null)
191        row = FindSingleRow(
192          delegate() {
193            return FindById(id);
194          });
195
196      cacheLock.ExitReadLock();
197
198      return row;
199    }
200
201    private void AddToCache(RowT row) {
202      cacheLock.EnterWriteLock();
203
204      cache.ImportRow(row);
205      row.Table.Rows.Remove(row);
206
207      cacheLock.ExitWriteLock();
208    }
209
210    private RowT AddToCache(ObjT obj) {
211      cacheLock.EnterWriteLock();
212
213      RowT row =  InsertNewRowInCache(obj);
214
215      cacheLock.ExitWriteLock();
216
217      return row;
218    }
219
220    private void RemoveRowFromCache(RowT row) {
221      cacheLock.EnterWriteLock();
222
223      dataTable.ImportRow(row);
224      cache.Rows.Remove(row);
225
226      cacheLock.ExitWriteLock();
227
228      UpdateRow(row);
229    }
230
231    public override void Update(ObjT obj) {
232      if (obj != null) {
233        RowT row =
234          GetRowById(obj.Id);
235
236        if (row == null) {
237          if (PutInCache(obj)) {
238            row = AddToCache(obj);
239          } else {
240            row = InsertNewRow(obj);
241          }
242
243          UpdateRow(row);
244        }
245
246        obj.Id = (long)row[row.Table.PrimaryKey[0]];
247        LockRow(obj.Id);
248
249        ConvertObj(obj, row);
250
251        if (!IsCached(row))
252          UpdateRow(row);
253
254        if (IsCached(row) &&
255            !PutInCache(obj)) {         
256          RemoveRowFromCache(row);
257        } else if (!IsCached(row) &&
258          PutInCache(obj)) {
259          AddToCache(row);
260        }
261
262        UnlockRow(obj.Id);
263      }
264    }
265  }
266}
267
Note: See TracBrowser for help on using the repository browser.