Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1031 was 1029, checked in by msteinbi, 16 years ago

Fixed caching (#372)

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