Free cookie consent management tool by TermsFeed Policy Generator

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

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

Started implementation of Lifecycle Management for CLient Communicator (#453)

File size: 5.5 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    [MethodImpl(MethodImplOptions.Synchronized)]
88    protected virtual ObjT FindSingle(Selector dbSelector,
89      Selector cacheSelector) {
90      RowT row = FindSingleRow(dbSelector, cacheSelector);
91
92      if (row != null) {
93        ObjT obj = new ObjT();
94        Convert(row, obj);
95
96        return obj;
97      } else {
98        return default(ObjT);
99      }
100    }
101
102    [MethodImpl(MethodImplOptions.Synchronized)]
103    protected virtual ICollection<ObjT> FindMultiple(Selector dbSelector,
104      Selector cacheSelector) {
105      ICollection<ObjT> result =
106        FindMultiple(cacheSelector);
107
108      ICollection<ObjT> resultDb =
109        FindMultiple(dbSelector);
110
111      foreach (ObjT obj in resultDb) {
112        if (!result.Contains(obj))
113          result.Add(obj);
114      }
115
116      return result;
117    }
118
119    protected abstract RowT InsertNewRowInCache(ObjT obj);
120
121    protected abstract void FillCache();
122
123    public abstract void SyncWithDb();
124
125    protected abstract bool PutInCache(ObjT obj);
126
127    protected abstract RowT FindCachedById(long id);
128
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    protected virtual void RemoveRowFromCache(RowT row) {     
138      cache.Rows.Remove(row);
139    }
140
141    protected virtual bool IsCached(RowT row) {
142      if (row == null)
143        return false;
144     else
145        return FindCachedById((long)row[row.Table.PrimaryKey[0]]) != null;
146    }
147
148    [MethodImpl(MethodImplOptions.Synchronized)]
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          dataTable[obj.Id].ImportRow(row);
189          dataTable.Remove(obj.Id);
190
191          UpdateRow(row);
192          RemoveRowFromCache(row);
193        } else if (!IsCached(row) &&
194          PutInCache(obj)) {
195          //add to cache
196          cache.ImportRow(row);
197
198          dataTable[obj.Id] = row.Table;
199          row.Table.Rows.Remove(row);
200        }
201      }
202    }
203  }
204}
Note: See TracBrowser for help on using the repository browser.