Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.Hive.Server.ADODataAccess/3.2/CachedDataAdapter.cs @ 2115

Last change on this file since 2115 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 6.5 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 = null;
230        long locked = default(long);
231
232        if (obj.Id != default(long)) {
233          LockRow(obj.Id);
234          locked = obj.Id;
235
236          row = GetRowById(obj.Id);
237        }
238
239        if (row == null) {
240          if (PutInCache(obj)) {
241            row = AddToCache(obj);
242          } else {
243            row = InsertNewRow(obj);
244          }
245
246          UpdateRow(row);
247          obj.Id = (long)row[row.Table.PrimaryKey[0]];
248        }
249
250        if (locked == default(long)) {
251          LockRow(obj.Id);
252          locked = obj.Id;
253        }
254
255        ConvertObj(obj, row);
256
257        if (!IsCached(row))
258          UpdateRow(row);
259
260        if (IsCached(row) &&
261            !PutInCache(obj)) {         
262          RemoveRowFromCache(row);
263        } else if (!IsCached(row) &&
264          PutInCache(obj)) {
265          AddToCache(row);
266        }
267
268        UnlockRow(locked);
269      }
270    }
271  }
272}
273
Note: See TracBrowser for help on using the repository browser.