Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1096 was 1096, checked in by msteinbi, 15 years ago

Implementing Lifecycle Management (#453)

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    private DataTable temp = new DataTable();
45
46    protected CachedDataAdapter() {
47      FillCache();
48
49      ServiceLocator.GetTransactionManager().OnUpdate +=
50        new EventHandler(CachedDataAdapter_OnUpdate);
51    }
52
53    [MethodImpl(MethodImplOptions.Synchronized)]
54    protected virtual RowT FindSingleRow(Selector dbSelector,
55      Selector cacheSelector) {
56      RowT row =
57         FindSingleRow(cacheSelector);
58
59      //not in cache
60      if (row == null) {
61        row =
62          FindSingleRow(dbSelector);
63      }
64
65      return row;
66    }
67
68    [MethodImpl(MethodImplOptions.Synchronized)]
69    protected virtual IEnumerable<RowT> FindMultipleRows(Selector dbSelector,
70        Selector cacheSelector) {
71      IList<RowT> result =
72         new List<RowT>(cacheSelector());
73
74      IEnumerable<RowT> result2 =
75          dbSelector();
76
77      foreach (RowT row in result2) {
78        if (!IsCached(row)) {
79          result.Add(row);
80        }
81      }
82
83      return result;
84    }
85
86    protected virtual ObjT FindSingle(Selector dbSelector,
87      Selector cacheSelector) {
88      RowT row = FindSingleRow(dbSelector, cacheSelector);
89
90      if (row != null) {
91        ObjT obj = new ObjT();
92        Convert(row, obj);
93
94        return obj;
95      } else {
96        return default(ObjT);
97      }
98    }
99
100    [MethodImpl(MethodImplOptions.Synchronized)]
101    protected virtual ICollection<ObjT> FindMultiple(Selector dbSelector,
102      Selector cacheSelector) {
103      ICollection<ObjT> result =
104        FindMultiple(cacheSelector);
105
106      ICollection<ObjT> resultDb =
107        FindMultiple(dbSelector);
108
109      foreach (ObjT obj in resultDb) {
110        if (!result.Contains(obj))
111          result.Add(obj);
112      }
113
114      return result;
115    }
116
117    [MethodImpl(MethodImplOptions.Synchronized)]
118    protected abstract RowT InsertNewRowInCache(ObjT obj);
119
120    [MethodImpl(MethodImplOptions.Synchronized)]
121    protected abstract void FillCache();
122
123    [MethodImpl(MethodImplOptions.Synchronized)]
124    public abstract void SyncWithDb();
125
126    [MethodImpl(MethodImplOptions.Synchronized)]
127    protected abstract bool PutInCache(ObjT obj);
128
129    [MethodImpl(MethodImplOptions.Synchronized)]
130    protected abstract RowT FindCachedById(long id);
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          temp.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          row.Table.Rows.Remove(row);
203        }
204      }
205    }
206  }
207}
Note: See TracBrowser for help on using the repository browser.