Free cookie consent management tool by TermsFeed Policy Generator

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

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

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