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

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

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

File size: 6.1 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 ReaderWriterLock cacheLock =
44      new ReaderWriterLock();
45
46    protected DataTable dataTable =
47      new DataTable();
48
49    protected ICollection<ICachedDataAdapter> parentAdapters =
50      new List<ICachedDataAdapter>();
51
52    protected CachedDataAdapter() {
53      cacheLock.AcquireWriterLock(Timeout.Infinite);
54
55      if (!cacheFilled) {
56        FillCache();
57        cacheFilled = true;
58      }
59
60      cacheLock.ReleaseWriterLock();
61
62      ServiceLocator.GetTransactionManager().OnUpdate +=
63        new EventHandler(CachedDataAdapter_OnUpdate);
64    }
65
66    protected virtual RowT FindSingleRow(Selector dbSelector,
67      Selector cacheSelector) {
68      cacheLock.AcquireReaderLock(Timeout.Infinite);
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.ReleaseReaderLock();
80
81      return row;
82    }
83
84    protected virtual IEnumerable<RowT> FindMultipleRows(Selector dbSelector,
85        Selector cacheSelector) {
86      cacheLock.AcquireReaderLock(Timeout.Infinite);
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.ReleaseReaderLock();
101
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();
111        obj = Convert(row, obj);
112       
113        return obj;
114      } else {
115        return default(ObjT);
116      }
117    }
118
119    protected virtual ICollection<ObjT> FindMultiple(Selector dbSelector,
120      Selector cacheSelector) {
121      cacheLock.AcquireReaderLock(Timeout.Infinite);
122
123      ICollection<ObjT> result =
124        FindMultiple(cacheSelector);
125
126      ICollection<ObjT> resultDb =
127        FindMultiple(dbSelector);
128
129      cacheLock.ReleaseReaderLock();
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    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
154      cacheLock.AcquireReaderLock(Timeout.Infinite);
155
156      this.SyncWithDb();
157
158      cacheLock.ReleaseReaderLock();
159    }
160
161    protected virtual bool IsCached(RowT row) {
162      if (row == null)
163        return false;
164      else
165        return FindCachedById((long)row[row.Table.PrimaryKey[0]]) != null;
166    }
167
168    protected override RowT GetRowById(long id) {
169      RowT row =
170        FindCachedById(id);
171
172      if (row == null)
173        row = FindSingleRow(
174          delegate() {
175            return FindById(id);
176          });
177
178      return row;
179    }
180
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
211    public override void Update(ObjT obj) {
212      if (obj != null) {
213        RowT row =
214          GetRowById(obj.Id);
215
216        if (row == null) {
217          if (PutInCache(obj)) {
218            row = AddToCache(obj);
219          } else {
220            row = InsertNewRow(obj);
221          }
222
223          UpdateRow(row);
224        }
225
226        obj.Id = (long)row[row.Table.PrimaryKey[0]];
227        LockRow(obj.Id);
228
229        ConvertObj(obj, row);
230
231        if (!IsCached(row))
232          UpdateRow(row);
233
234        if (IsCached(row) &&
235            !PutInCache(obj)) {         
236          RemoveRowFromCache(row);
237        } else if (!IsCached(row) &&
238          PutInCache(obj)) {
239          AddToCache(row);
240        }
241
242        UnlockRow(obj.Id);
243      }
244    }
245  }
246}
247
Note: See TracBrowser for help on using the repository browser.