Free cookie consent management tool by TermsFeed Policy Generator

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

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

removed locking from reading db methods (#372)

File size: 5.4 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    private DataTable temp = new DataTable();
45
46    protected CachedDataAdapter() {
47      FillCache();
48
49      ServiceLocator.GetTransactionManager().OnUpdate +=
50        new EventHandler(CachedDataAdapter_OnUpdate);
51    }
52
53    protected virtual RowT FindSingleRow(Selector dbSelector,
54      Selector cacheSelector) {
55      RowT row =
56         FindSingleRow(cacheSelector);
57
58      //not in cache
59      if (row == null) {
60        row =
61          FindSingleRow(dbSelector);
62      }
63
64      return row;
65    }
66
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    protected virtual ICollection<ObjT> FindMultiple(Selector dbSelector,
99      Selector cacheSelector) {
100      ICollection<ObjT> result =
101        FindMultiple(cacheSelector);
102
103      ICollection<ObjT> resultDb =
104        FindMultiple(dbSelector);
105
106      foreach (ObjT obj in resultDb) {
107        if (!result.Contains(obj))
108          result.Add(obj);
109      }
110
111      return result;
112    }
113
114    [MethodImpl(MethodImplOptions.Synchronized)]
115    protected abstract RowT InsertNewRowInCache(ObjT obj);
116
117    [MethodImpl(MethodImplOptions.Synchronized)]
118    protected abstract void FillCache();
119
120    [MethodImpl(MethodImplOptions.Synchronized)]
121    public abstract void SyncWithDb();
122
123    [MethodImpl(MethodImplOptions.Synchronized)]
124    protected abstract bool PutInCache(ObjT obj);
125
126    protected abstract RowT FindCachedById(long id);
127
128    [MethodImpl(MethodImplOptions.Synchronized)]
129    void CachedDataAdapter_OnUpdate(object sender, EventArgs e) {
130      foreach (ICachedDataAdapter parent in this.parentAdapters) {
131        parent.SyncWithDb();
132      }
133
134      this.SyncWithDb();
135    }
136
137    [MethodImpl(MethodImplOptions.Synchronized)]
138    protected virtual void RemoveRowFromCache(RowT row) {     
139      cache.Rows.Remove(row);
140    }
141
142    protected virtual bool IsCached(RowT row) {
143      if (row == null)
144        return false;
145     else
146        return FindCachedById((long)row[row.Table.PrimaryKey[0]]) != null;
147    }
148
149    protected override RowT GetRowById(long id) {
150      RowT row =
151        FindCachedById(id);
152     
153      if(row == null)
154        row = FindSingleRow(
155          delegate() {
156            return FindById(id);
157          });
158
159      return row;
160    }
161
162    [MethodImpl(MethodImplOptions.Synchronized)]
163    public override void Update(ObjT obj) {
164      if (obj != null) {
165        RowT row =
166          GetRowById(obj.Id);
167
168        if (row == null) {
169          if (PutInCache(obj)) {
170            row = InsertNewRowInCache(obj);
171          } else {
172            row = InsertNewRow(obj);
173          }
174
175          UpdateRow(row);
176        }
177
178        obj.Id = (long)row[row.Table.PrimaryKey[0]];
179
180        Convert(obj, row);
181
182        if (!IsCached(row))
183          UpdateRow(row);
184       
185        if (IsCached(row) &&
186            !PutInCache(obj)) {
187          //remove from cache
188          temp.ImportRow(row);
189
190          UpdateRow(row);
191          RemoveRowFromCache(row);
192        } else if (!IsCached(row) &&
193          PutInCache(obj)) {
194          //add to cache
195          cache.ImportRow(row);
196
197          row.Table.Rows.Remove(row);
198        }
199      }
200    }
201  }
202}
Note: See TracBrowser for help on using the repository browser.