Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1093 was 1093, checked in by svonolfe, 15 years ago

Added lock to avoid race conditions (#372).

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