Free cookie consent management tool by TermsFeed Policy Generator

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

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