Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1000 was 995, checked in by svonolfe, 16 years ago

Refactored DAL, Improved Caching (#372)

File size: 4.7 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.Runtime.CompilerServices;
28
29namespace HeuristicLab.Hive.Server.ADODataAccess {
30  abstract class CachedDataAdapter<AdapterT, ObjT, RowT, CacheT> :
31    DataAdapterBase<AdapterT, ObjT, RowT>,
32    ICachedDataAdapter
33    where CacheT: System.Data.TypedTableBase<RowT>, new()
34    where AdapterT : new()
35    where RowT : System.Data.DataRow
36    where ObjT : IHiveObject, new() {
37    protected CacheT cache =
38      new CacheT();
39
40    protected ICollection<ICachedDataAdapter> parentAdapters =
41      new List<ICachedDataAdapter>();
42
43    protected CachedDataAdapter() {
44      FillCache();
45
46      ServiceLocator.GetTransactionManager().OnUpdate +=
47        new EventHandler(CachedDataAdapter_OnUpdate);
48    }
49
50    protected virtual RowT FindSingleRow(Selector dbSelector,
51      Selector cacheSelector) {
52      RowT row =
53         FindSingleRow(cacheSelector);
54
55      //not in cache
56      if (row == null) {
57        row =
58          FindSingleRow(dbSelector);
59      }
60
61      return row;
62    }
63
64    protected virtual IEnumerable<RowT> FindMultipleRows(Selector dbSelector,
65        Selector cacheSelector) {
66      IList<RowT> result =
67         new List<RowT>(cacheSelector());
68
69      IEnumerable<RowT> result2 =
70          dbSelector();
71
72      foreach (RowT row in result2) {
73        if (!IsCached(row)) {
74          result.Add(row);
75        }
76      }
77
78      return result;
79    }
80
81    protected virtual ObjT FindSingle(Selector dbSelector,
82      Selector cacheSelector) {
83      RowT row = FindSingleRow(dbSelector, cacheSelector);
84
85      if (row != null) {
86        ObjT obj = new ObjT();
87        Convert(row, obj);
88
89        return obj;
90      } else {
91        return default(ObjT);
92      }
93    }
94
95    protected virtual ICollection<ObjT> FindMultiple(Selector dbSelector,
96      Selector cacheSelector) {
97      ICollection<ObjT> result =
98        FindMultiple(cacheSelector);
99
100      ICollection<ObjT> resultDb =
101        FindMultiple(dbSelector);
102
103      foreach (ObjT obj in resultDb) {
104        if (!result.Contains(obj))
105          result.Add(obj);
106      }
107
108      return result;
109    }
110
111    protected abstract RowT InsertNewRowInCache(ObjT obj);
112
113    protected abstract void FillCache();
114
115    public abstract void SyncWithDb();
116
117    protected abstract bool PutInCache(ObjT obj);
118
119    protected abstract RowT FindCachedById(long id);
120
121    void CachedDataAdapter_OnUpdate(object sender, EventArgs e) {
122      foreach (ICachedDataAdapter parent in this.parentAdapters) {
123        parent.SyncWithDb();
124      }
125
126      this.SyncWithDb();
127    }
128
129    protected virtual void RemoveRowFromCache(RowT row) {
130      cache.Rows.Remove(row);
131    }
132
133    protected virtual bool IsCached(RowT row) {
134      return cache.Contains<RowT>(row);
135    }
136
137    protected override RowT GetRowById(long id) {
138      RowT row =
139        FindCachedById(id);
140     
141      if(row == null)
142        row = FindSingleRow(
143          delegate() {
144            return FindById(id);
145          });
146
147      return row;
148    }
149
150    [MethodImpl(MethodImplOptions.Synchronized)]
151    public override void Update(ObjT obj) {
152      if (obj != null) {
153        RowT row =
154          GetRowById(obj.Id);
155
156        if (row == null) {
157          if (PutInCache(obj)) {
158            row = InsertNewRowInCache(obj);
159          } else {
160            row = InsertNewRow(obj);
161          }
162
163          UpdateRow(row);
164        }
165
166        Convert(obj, row);
167
168        if (IsCached(row) &&
169            !PutInCache(obj)) {
170          //remove from cache
171          UpdateRow(row);
172          RemoveRowFromCache(row);
173        }
174
175        obj.Id = (long)row[row.Table.PrimaryKey[0]];
176
177        if (!IsCached(row))
178          UpdateRow(row);
179      }
180    }
181  }
182}
Note: See TracBrowser for help on using the repository browser.