Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added reader lock for the updating of the cache (#372)

File size: 6.1 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
[1134]43    private static ReaderWriterLock cacheLock =
44      new ReaderWriterLock();
45
[1131]46    protected DataTable dataTable =
47      new DataTable();
48
[995]49    protected ICollection<ICachedDataAdapter> parentAdapters =
50      new List<ICachedDataAdapter>();
51
52    protected CachedDataAdapter() {
[1134]53      cacheLock.AcquireWriterLock(Timeout.Infinite);
54
[1131]55      if (!cacheFilled) {
56        FillCache();
57        cacheFilled = true;
58      }
[995]59
[1134]60      cacheLock.ReleaseWriterLock();
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) {
[1134]68      cacheLock.AcquireReaderLock(Timeout.Infinite);
69     
[995]70      RowT row =
71         FindSingleRow(cacheSelector);
72
73      //not in cache
74      if (row == null) {
75        row =
76          FindSingleRow(dbSelector);
77      }
78
[1134]79      cacheLock.ReleaseReaderLock();
80
[995]81      return row;
82    }
83
84    protected virtual IEnumerable<RowT> FindMultipleRows(Selector dbSelector,
85        Selector cacheSelector) {
[1134]86      cacheLock.AcquireReaderLock(Timeout.Infinite);
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
[1134]100      cacheLock.ReleaseReaderLock();
101
[995]102      return result;
103    }
104
105    protected virtual ObjT FindSingle(Selector dbSelector,
106      Selector cacheSelector) {
107      RowT row = FindSingleRow(dbSelector, cacheSelector);
108
109      if (row != null) {
110        ObjT obj = new ObjT();
[1131]111        obj = Convert(row, obj);
112       
[995]113        return obj;
114      } else {
115        return default(ObjT);
116      }
117    }
118
119    protected virtual ICollection<ObjT> FindMultiple(Selector dbSelector,
120      Selector cacheSelector) {
[1134]121      cacheLock.AcquireReaderLock(Timeout.Infinite);
122
[995]123      ICollection<ObjT> result =
124        FindMultiple(cacheSelector);
125
126      ICollection<ObjT> resultDb =
127        FindMultiple(dbSelector);
128
[1134]129      cacheLock.ReleaseReaderLock();
130
[995]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    public abstract void SyncWithDb();
144
145    protected abstract bool PutInCache(ObjT obj);
146
147    protected abstract RowT FindCachedById(long id);
148
149    void CachedDataAdapter_OnUpdate(object sender, EventArgs e) {
150      foreach (ICachedDataAdapter parent in this.parentAdapters) {
151        parent.SyncWithDb();
152      }
153
[1146]154      cacheLock.AcquireReaderLock(Timeout.Infinite);
155
[995]156      this.SyncWithDb();
[1146]157
158      cacheLock.ReleaseReaderLock();
[995]159    }
160
161    protected virtual bool IsCached(RowT row) {
[1029]162      if (row == null)
163        return false;
[1128]164      else
[1029]165        return FindCachedById((long)row[row.Table.PrimaryKey[0]]) != null;
[995]166    }
167
168    protected override RowT GetRowById(long id) {
169      RowT row =
170        FindCachedById(id);
[1128]171
172      if (row == null)
[995]173        row = FindSingleRow(
174          delegate() {
175            return FindById(id);
176          });
177
178      return row;
179    }
180
[1134]181    private void AddToCache(RowT row) {
182      cacheLock.AcquireWriterLock(Timeout.Infinite);
183
184      cache.ImportRow(row);
185      row.Table.Rows.Remove(row);
186
187      cacheLock.ReleaseWriterLock();
188    }
189
190    private RowT AddToCache(ObjT obj) {
191      cacheLock.AcquireWriterLock(Timeout.Infinite);
192
193      RowT row =  InsertNewRowInCache(obj);
194
195      cacheLock.ReleaseWriterLock();
196
197      return row;
198    }
199
200    private void RemoveRowFromCache(RowT row) {
201      cacheLock.AcquireWriterLock(Timeout.Infinite);
202
203      dataTable.ImportRow(row);
204      cache.Rows.Remove(row);
205
206      cacheLock.ReleaseWriterLock();
207
208      UpdateRow(row);
209    }
210
[995]211    public override void Update(ObjT obj) {
212      if (obj != null) {
[1128]213        RowT row =
[995]214          GetRowById(obj.Id);
215
216        if (row == null) {
217          if (PutInCache(obj)) {
[1134]218            row = AddToCache(obj);
[995]219          } else {
220            row = InsertNewRow(obj);
221          }
222
223          UpdateRow(row);
224        }
225
[1029]226        obj.Id = (long)row[row.Table.PrimaryKey[0]];
[1134]227        LockRow(obj.Id);
[1029]228
[1131]229        ConvertObj(obj, row);
[995]230
[1029]231        if (!IsCached(row))
232          UpdateRow(row);
[1128]233
[995]234        if (IsCached(row) &&
[1134]235            !PutInCache(obj)) {         
[1131]236          RemoveRowFromCache(row);
[1021]237        } else if (!IsCached(row) &&
238          PutInCache(obj)) {
[1134]239          AddToCache(row);
240        }
241
242        UnlockRow(obj.Id);
[995]243      }
244    }
245  }
246}
[1128]247
Note: See TracBrowser for help on using the repository browser.