Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/ClientAdapter.cs @ 1175

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

Refactored DAL, avoided possible race conditions (#372)

File size: 7.9 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.Server.Core.InternalInterfaces.DataAccess;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using System.Linq.Expressions;
29
30namespace HeuristicLab.Hive.Server.ADODataAccess {
31  class ClientAdapter:
32    CachedDataAdapter<
33      dsHiveServerTableAdapters.ClientTableAdapter,
34      ClientInfo,
35      dsHiveServer.ClientRow,
36      dsHiveServer.ClientDataTable>,
37    IClientAdapter {
38    #region Fields
39    private IResourceAdapter resAdapter = null;
40
41    private IResourceAdapter ResAdapter {
42      get {
43        if (resAdapter == null)
44          resAdapter = ServiceLocator.GetResourceAdapter();
45
46        return resAdapter;
47      }
48    }
49
50    private IClientGroupAdapter clientGroupAdapter = null;
51
52    private IClientGroupAdapter ClientGroupAdapter {
53      get {
54        if (clientGroupAdapter == null) {
55          clientGroupAdapter = ServiceLocator.GetClientGroupAdapter();
56        }
57
58        return clientGroupAdapter;
59      }
60    }
61
62    private IJobAdapter jobAdapter = null;
63
64    private IJobAdapter JobAdapter {
65      get {
66        if (jobAdapter == null) {
67          jobAdapter = ServiceLocator.GetJobAdapter();
68        }
69
70        return jobAdapter;
71      }
72    }
73    #endregion
74
75    public ClientAdapter() {
76      parentAdapters.Add(this.ResAdapter as ICachedDataAdapter);
77    }
78
79    private void Preprocess(ClientInfo client) {
80      if (client != null && client.Id == default(long)) {
81        ClientInfo found = GetById(client.ClientId);
82        if (found != null)
83          client.Id = found.Id;
84      }
85    }
86
87    #region Overrides
88    protected override ClientInfo ConvertRow(dsHiveServer.ClientRow row,
89      ClientInfo client) {
90      if(row != null && client != null) {     
91        /*Parent - resource*/
92        client.Id = row.ResourceId;
93        ResAdapter.GetById(client);
94
95        /*ClientInfo*/
96        if (!row.IsGUIDNull())
97          client.ClientId = row.GUID;
98        else
99          client.ClientId = Guid.Empty;
100       
101        if (!row.IsCPUSpeedNull())
102          client.CpuSpeedPerCore = row.CPUSpeed;
103        else
104          client.CpuSpeedPerCore = 0;
105
106        if (!row.IsMemoryNull())
107          client.Memory = row.Memory;
108        else
109          client.Memory = 0;
110
111        if (!row.IsLoginNull())
112          client.Login = row.Login;
113        else
114          client.Login = DateTime.MinValue;
115
116        if (!row.IsStatusNull())
117          client.State = (State)Enum.Parse(typeof(State), row.Status, true);
118        else
119          client.State = State.nullState;
120
121        if (!row.IsNumberOfCoresNull())
122          client.NrOfCores = row.NumberOfCores;
123        else
124          client.NrOfCores = 0;
125
126        //todo: config adapter (client.config)
127
128        return client;
129      }
130      else
131        return null;
132    }
133
134    protected override dsHiveServer.ClientRow ConvertObj(ClientInfo client,
135      dsHiveServer.ClientRow row) {
136      if (client != null && row != null) {     
137        row.GUID = client.ClientId;
138        row.CPUSpeed = client.CpuSpeedPerCore;
139        row.Memory = client.Memory;
140        row.Login = client.Login;
141        if (client.State != State.nullState)
142          row.Status = client.State.ToString();
143        else
144          row.SetStatusNull();
145        row.NumberOfCores = client.NrOfCores;
146
147        //todo: config adapter
148        /*if (client.Config != null)
149          row.ClientConfigId = client.Config.ClientConfigId;
150         else
151          row.ClientConfigId = null;*/
152      }
153
154      return row;
155    }
156
157    protected override void UpdateRow(dsHiveServer.ClientRow row) {
158      Adapter.Update(row);
159    }
160
161    protected override dsHiveServer.ClientRow
162      InsertNewRow(ClientInfo client) {
163      dsHiveServer.ClientDataTable data =
164        new dsHiveServer.ClientDataTable();
165
166      dsHiveServer.ClientRow row = data.NewClientRow();
167      row.ResourceId = client.Id;
168      data.AddClientRow(row);
169
170      return row;
171    }
172
173    protected override dsHiveServer.ClientRow
174      InsertNewRowInCache(ClientInfo client) {
175      dsHiveServer.ClientRow row = cache.NewClientRow();
176      row.ResourceId = client.Id;
177      cache.AddClientRow(row);
178
179      return row;
180    }
181
182    protected override void FillCache() {
183      Adapter.FillByActive(cache);
184    }
185
186    protected override void SynchronizeWithDb() {
187      Adapter.Update(cache);
188    }
189
190    protected override bool PutInCache(ClientInfo obj) {
191      return (obj.State != State.offline && obj.State != State.nullState);
192    }
193
194    protected override IEnumerable<dsHiveServer.ClientRow>
195      FindById(long id) {
196      return Adapter.GetDataByResourceId(id);
197    }
198
199    protected override dsHiveServer.ClientRow
200      FindCachedById(long id) {
201      return cache.FindByResourceId(id);
202    }
203
204    protected override IEnumerable<dsHiveServer.ClientRow>
205      FindAll() {
206      return FindMultipleRows(
207        new Selector(Adapter.GetData),
208        new Selector(cache.AsEnumerable<dsHiveServer.ClientRow>));
209    }
210
211    #endregion
212
213    #region IClientAdapter Members
214    public override void Update(ClientInfo client) {
215      if (client != null) {
216        Guid locked = Guid.Empty;
217        if (client.ClientId != Guid.Empty) {
218          LockRow(client.ClientId);
219          locked = client.ClientId;
220        }
221
222        Preprocess(client);
223
224        ResAdapter.Update(client);
225
226        base.Update(client);
227
228        if (locked != Guid.Empty) {
229          UnlockRow(locked);
230        }
231      }
232    }
233
234    public ClientInfo GetById(Guid clientId) {
235      return base.FindSingle(
236        delegate() {
237          return Adapter.GetDataById(clientId);
238        },
239        delegate() {
240          return from c in
241                     cache.AsEnumerable<dsHiveServer.ClientRow>()
242                   where !c.IsGUIDNull() &&
243                          c.GUID == clientId
244                   select c;
245        });
246    }
247
248    public ClientInfo GetByName(string name) {
249      ClientInfo client = new ClientInfo();
250      Resource res =
251        ResAdapter.GetByName(name);
252
253      return GetById(res.Id);
254    }
255
256    public override bool Delete(ClientInfo client) {
257      bool success = false;
258      Guid locked = Guid.Empty;
259     
260      if (client != null) {
261        if (client.ClientId != Guid.Empty) {
262          LockRow(client.ClientId);
263          locked = client.ClientId;
264        }
265
266        Preprocess(client);
267
268        dsHiveServer.ClientRow row =
269          GetRowById(client.Id);
270
271        if (row != null) {
272          //Referential integrity with jobs - they are cached
273          ICollection<Job> jobs =
274            JobAdapter.GetJobsOf(client);
275          foreach (Job job in jobs) {
276            JobAdapter.Delete(job);
277          }
278
279          success = base.Delete(client) &&
280            ResAdapter.Delete(client);
281        }
282      }
283
284      if (locked != Guid.Empty) {
285        UnlockRow(locked);
286      }
287
288      return success;
289    }
290
291    #endregion
292  }
293}
Note: See TracBrowser for help on using the repository browser.