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
RevLine 
[995]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;
[1026]28using System.Data;
[995]29
30namespace HeuristicLab.Hive.Server.ADODataAccess {
31  abstract class CachedDataAdapter<AdapterT, ObjT, RowT, CacheT> :
[1128]32    DataAdapterBase<AdapterT, ObjT, RowT>,
[995]33    ICachedDataAdapter
[1128]34    where CacheT : System.Data.TypedTableBase<RowT>, new()
[995]35    where AdapterT : new()
36    where RowT : System.Data.DataRow
37    where ObjT : IHiveObject, new() {
[1131]38    protected static CacheT cache =
[995]39      new CacheT();
40
[1131]41    private static bool cacheFilled = false;
[1128]42
[1131]43    protected DataTable dataTable =
44      new DataTable();
45
[995]46    protected ICollection<ICachedDataAdapter> parentAdapters =
47      new List<ICachedDataAdapter>();
48
[1131]49    [MethodImpl(MethodImplOptions.Synchronized)]
[995]50    protected CachedDataAdapter() {
[1131]51      if (!cacheFilled) {
52        FillCache();
53        cacheFilled = true;
54      }
[995]55
56      ServiceLocator.GetTransactionManager().OnUpdate +=
57        new EventHandler(CachedDataAdapter_OnUpdate);
58    }
59
[1128]60    protected virtual RowT FindSingleRow(Selector dbSelector,
[995]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();
[1131]97        obj = Convert(row, obj);
98       
[995]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
[1093]121    [MethodImpl(MethodImplOptions.Synchronized)]
[995]122    protected abstract RowT InsertNewRowInCache(ObjT obj);
123
[1093]124    [MethodImpl(MethodImplOptions.Synchronized)]
[995]125    protected abstract void FillCache();
126
[1093]127    [MethodImpl(MethodImplOptions.Synchronized)]
[995]128    public abstract void SyncWithDb();
129
[1093]130    [MethodImpl(MethodImplOptions.Synchronized)]
[995]131    protected abstract bool PutInCache(ObjT obj);
132
133    protected abstract RowT FindCachedById(long id);
134
[1093]135    [MethodImpl(MethodImplOptions.Synchronized)]
[995]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
[1093]144    [MethodImpl(MethodImplOptions.Synchronized)]
[1128]145    protected virtual void RemoveRowFromCache(RowT row) {
[995]146      cache.Rows.Remove(row);
147    }
148
149    protected virtual bool IsCached(RowT row) {
[1029]150      if (row == null)
151        return false;
[1128]152      else
[1029]153        return FindCachedById((long)row[row.Table.PrimaryKey[0]]) != null;
[995]154    }
155
156    protected override RowT GetRowById(long id) {
157      RowT row =
158        FindCachedById(id);
[1128]159
160      if (row == null)
[995]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) {
[1128]172        RowT row =
[995]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
[1029]185        obj.Id = (long)row[row.Table.PrimaryKey[0]];
186
[1131]187        ConvertObj(obj, row);
[995]188
[1029]189        if (!IsCached(row))
190          UpdateRow(row);
[1128]191
[995]192        if (IsCached(row) &&
193            !PutInCache(obj)) {
194          //remove from cache
[1131]195          dataTable.ImportRow(row);
196          RemoveRowFromCache(row);
[1029]197
198          UpdateRow(row);
[1021]199        } else if (!IsCached(row) &&
200          PutInCache(obj)) {
201          //add to cache
[1029]202          cache.ImportRow(row);
[1095]203          row.Table.Rows.Remove(row);
[1131]204        }
[995]205      }
206    }
207  }
208}
[1128]209
Note: See TracBrowser for help on using the repository browser.