Free cookie consent management tool by TermsFeed Policy Generator

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

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

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