Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1162 was 1158, checked in by svonolfe, 16 years ago

Updated recursion policy in the reader/writer lock (#372)

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