Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/3.2/DataAdapterBase.cs @ 2311

Last change on this file since 2311 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 5.5 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;
[1131]23using System.Data;
[925]24using System.Collections.Generic;
25using System.Linq;
26using System.Text;
[995]27using HeuristicLab.Hive.Contracts.BusinessObjects;
[1134]28using System.Threading;
[925]29
30namespace HeuristicLab.Hive.Server.ADODataAccess {
[995]31  abstract class DataAdapterBase<AdapterT, ObjT, RowT>
[1128]32    where AdapterT : new()
33    where RowT : System.Data.DataRow
34    where ObjT : IHiveObject, new() {
[1134]35
36    private static Mutex lockersMutex =
37      new Mutex();
38
[1175]39    private static IDictionary<object, Mutex> lockers =
40      new Dictionary<object, Mutex>();
[1134]41
[1175]42    private static IDictionary<object, int> lockCount =
43      new Dictionary<object, int>();
[1134]44
[1175]45    protected void LockRow(object id) {
[1134]46      Mutex rowLock = null;
47
48      /////begin critical section////
49      lockersMutex.WaitOne();
50
51      if (!lockers.ContainsKey(id)) {
52        lockers[id] = new Mutex();
53        lockCount[id] = 0;
54      }
55      rowLock = lockers[id];
56      lockCount[id]++;
57     
58      lockersMutex.ReleaseMutex();
59      /////end critical section////
60
61      rowLock.WaitOne();
62    }
63
[1175]64    protected void UnlockRow(object id) {
[1134]65      Mutex rowLock = lockers[id];
66      rowLock.ReleaseMutex();
67
68      /////begin critical section////
69      lockersMutex.WaitOne();
70
71      lockCount[id]--;
72      if (lockCount[id] == 0)
73        lockers.Remove(id);
74
75      lockersMutex.ReleaseMutex();
76      /////end critical section////
77    }
78   
[1131]79    protected AdapterT Adapter {
80      get {
81        return new AdapterT();
82      }
83    }
[925]84
[995]85    #region Abstract methods
[1131]86    protected abstract RowT ConvertObj(ObjT obj, RowT row);
[995]87
[1131]88    protected abstract ObjT ConvertRow(RowT row, ObjT obj);
[995]89
90    protected abstract RowT InsertNewRow(ObjT obj);
91
92    protected abstract void UpdateRow(RowT row);
93
94    protected abstract IEnumerable<RowT> FindById(long id);
95
96    protected abstract IEnumerable<RowT> FindAll();
97    #endregion
98
99    protected delegate IEnumerable<RowT> Selector();
100
[1131]101    protected ObjT Convert(RowT row, ObjT obj) {
102      try {
103        obj = ConvertRow(row, obj);
104        return obj;
105      }
106      catch (DeletedRowInaccessibleException) {
107        return default(ObjT);
108      }
109      catch (RowNotInTableException) {
110        return default(ObjT);
111      }
112    }
113
[995]114    protected virtual RowT FindSingleRow(Selector selector) {
115      RowT row = default(RowT);
116
117      IEnumerable<RowT> found =
118        selector();
119
120      if (found.Count<RowT>() == 1)
121        row = found.First<RowT>();
122
123      return row;
[925]124    }
125
[995]126    protected virtual ObjT FindSingle(Selector selector) {
127      RowT row = FindSingleRow(selector);
128
129      if (row != null) {
130        ObjT obj = new ObjT();
[1131]131        obj = Convert(row, obj);
[995]132
133        return obj;
134      } else {
135        return default(ObjT);
136      }
[925]137    }
[995]138
139    protected virtual ICollection<ObjT> FindMultiple(Selector selector) {
140      IEnumerable<RowT> found =
141        selector();
142
143      IList<ObjT> result =
144        new List<ObjT>();
145
146      foreach (RowT row in found) {
147        ObjT obj = new ObjT();
[1131]148        obj = Convert(row, obj);
149        if(obj != null)
150          result.Add(obj);
[995]151      }
152
153      return result;
154    }
155
156    protected virtual RowT GetRowById(long id) {
157      return FindSingleRow(
158        delegate() {
159          return FindById(id);
160        });
161    }
162
163    public virtual void Update(ObjT obj) {
164      if (obj != null) {
[1175]165        RowT row = null;
166        long locked = default(long);
167       
168        if (obj.Id != default(long)) {
169           LockRow(obj.Id);
170           locked = obj.Id;
[995]171
[1175]172           row = GetRowById(obj.Id);
173        }         
174
[995]175        if (row == null) {
176          row = InsertNewRow(obj);
[1134]177          UpdateRow(row);
[1175]178
179          obj.Id = (long)row[row.Table.PrimaryKey[0]];
[995]180        }
181
[1175]182        if (locked == default(long)) {
183          LockRow(obj.Id);
184          locked = obj.Id;
185        }
[1134]186
[1131]187        ConvertObj(obj, row);
[995]188        UpdateRow(row);
189
[1175]190        UnlockRow(locked);
[995]191      }
192    }
193
194    public virtual ObjT GetById(long id) {
195      return FindSingle(delegate() {
196        return FindById(id);
197      });
198    }
199
200    public virtual ICollection<ObjT> GetAll() {
201      return new List<ObjT>(
202        FindMultiple(
203          new Selector(FindAll)));
204    }
205
206    public virtual bool Delete(ObjT obj) {
[1134]207      bool success = false;
208     
[995]209      if (obj != null) {
[1134]210        LockRow(obj.Id);
211
[995]212        RowT row =
213          GetRowById(obj.Id);
214
215        if (row != null) {
216          row.Delete();
217          UpdateRow(row);
[1025]218
[1134]219          success = true;
[995]220        }
[1134]221
222        UnlockRow(obj.Id);
[995]223      }
224
[1134]225      return success;
[995]226    }
[925]227  }
228}
[1128]229
Note: See TracBrowser for help on using the repository browser.