Free cookie consent management tool by TermsFeed Policy Generator

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

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

Improved caching (#372)

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