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
RevLine 
[826]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;
[925]28using System.Linq.Expressions;
[826]29
30namespace HeuristicLab.Hive.Server.ADODataAccess {
[995]31  class ClientAdapter:
32    CachedDataAdapter<
33      dsHiveServerTableAdapters.ClientTableAdapter,
34      ClientInfo,
35      dsHiveServer.ClientRow,
36      dsHiveServer.ClientDataTable>,
37    IClientAdapter {
38    #region Fields
[948]39    private IResourceAdapter resAdapter = null;
[925]40
[948]41    private IResourceAdapter ResAdapter {
42      get {
43        if (resAdapter == null)
44          resAdapter = ServiceLocator.GetResourceAdapter();
45
46        return resAdapter;
47      }
48    }
49
[965]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
[971]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    }
[995]73    #endregion
[971]74
[925]75    public ClientAdapter() {
[995]76      parentAdapters.Add(this.ResAdapter as ICachedDataAdapter);
[925]77    }
78
[1122]79    private void Preprocess(ClientInfo client) {
[1175]80      if (client != null && client.Id == default(long)) {
81        ClientInfo found = GetById(client.ClientId);
82        if (found != null)
83          client.Id = found.Id;
[1122]84      }
85    }
86
[995]87    #region Overrides
[1131]88    protected override ClientInfo ConvertRow(dsHiveServer.ClientRow row,
[899]89      ClientInfo client) {
90      if(row != null && client != null) {     
[845]91        /*Parent - resource*/
[995]92        client.Id = row.ResourceId;
93        ResAdapter.GetById(client);
[826]94
[845]95        /*ClientInfo*/
[995]96        if (!row.IsGUIDNull())
97          client.ClientId = row.GUID;
98        else
99          client.ClientId = Guid.Empty;
[899]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())
[845]117          client.State = (State)Enum.Parse(typeof(State), row.Status, true);
[899]118        else
[995]119          client.State = State.nullState;
[845]120
[899]121        if (!row.IsNumberOfCoresNull())
122          client.NrOfCores = row.NumberOfCores;
123        else
124          client.NrOfCores = 0;
125
[845]126        //todo: config adapter (client.config)
127
128        return client;
129      }
130      else
131        return null;
132    }
133
[1131]134    protected override dsHiveServer.ClientRow ConvertObj(ClientInfo client,
[845]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;
[995]141        if (client.State != State.nullState)
142          row.Status = client.State.ToString();
143        else
144          row.SetStatusNull();
[845]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
[995]157    protected override void UpdateRow(dsHiveServer.ClientRow row) {
[1131]158      Adapter.Update(row);
[995]159    }
[826]160
[995]161    protected override dsHiveServer.ClientRow
162      InsertNewRow(ClientInfo client) {
[1166]163      dsHiveServer.ClientDataTable data =
164        new dsHiveServer.ClientDataTable();
165
[995]166      dsHiveServer.ClientRow row = data.NewClientRow();
167      row.ResourceId = client.Id;
168      data.AddClientRow(row);
[826]169
[995]170      return row;
[826]171    }
172
[995]173    protected override dsHiveServer.ClientRow
174      InsertNewRowInCache(ClientInfo client) {
[1134]175      dsHiveServer.ClientRow row = cache.NewClientRow();
[995]176      row.ResourceId = client.Id;
177      cache.AddClientRow(row);
[925]178
[995]179      return row;
180    }
[925]181
[995]182    protected override void FillCache() {
[1131]183      Adapter.FillByActive(cache);
[995]184    }
[899]185
[1149]186    protected override void SynchronizeWithDb() {
[1131]187      Adapter.Update(cache);
[826]188    }
189
[995]190    protected override bool PutInCache(ClientInfo obj) {
191      return (obj.State != State.offline && obj.State != State.nullState);
192    }
[965]193
[995]194    protected override IEnumerable<dsHiveServer.ClientRow>
195      FindById(long id) {
[1131]196      return Adapter.GetDataByResourceId(id);
[995]197    }
[965]198
[995]199    protected override dsHiveServer.ClientRow
200      FindCachedById(long id) {
201      return cache.FindByResourceId(id);
202    }
[965]203
[995]204    protected override IEnumerable<dsHiveServer.ClientRow>
205      FindAll() {
206      return FindMultipleRows(
[1131]207        new Selector(Adapter.GetData),
[995]208        new Selector(cache.AsEnumerable<dsHiveServer.ClientRow>));
[965]209    }
[1094]210
[995]211    #endregion
[965]212
[995]213    #region IClientAdapter Members
214    public override void Update(ClientInfo client) {
215      if (client != null) {
[1175]216        Guid locked = Guid.Empty;
217        if (client.ClientId != Guid.Empty) {
218          LockRow(client.ClientId);
219          locked = client.ClientId;
220        }
221
[1122]222        Preprocess(client);
[1014]223
[995]224        ResAdapter.Update(client);
[971]225
[995]226        base.Update(client);
[1175]227
228        if (locked != Guid.Empty) {
229          UnlockRow(locked);
230        }
[971]231      }
[995]232    }
[971]233
[995]234    public ClientInfo GetById(Guid clientId) {
235      return base.FindSingle(
236        delegate() {
[1131]237          return Adapter.GetDataById(clientId);
[995]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        });
[971]246    }
247
[995]248    public ClientInfo GetByName(string name) {
249      ClientInfo client = new ClientInfo();
250      Resource res =
251        ResAdapter.GetByName(name);
[826]252
[995]253      return GetById(res.Id);
[826]254    }
255
[1175]256    public override bool Delete(ClientInfo client) {
257      bool success = false;
258      Guid locked = Guid.Empty;
259     
[925]260      if (client != null) {
[1175]261        if (client.ClientId != Guid.Empty) {
262          LockRow(client.ClientId);
263          locked = client.ClientId;
264        }
265
[1122]266        Preprocess(client);
267
[995]268        dsHiveServer.ClientRow row =
269          GetRowById(client.Id);
[925]270
271        if (row != null) {
[1009]272          //Referential integrity with jobs - they are cached
[971]273          ICollection<Job> jobs =
274            JobAdapter.GetJobsOf(client);
275          foreach (Job job in jobs) {
[995]276            JobAdapter.Delete(job);
[971]277          }
278
[1175]279          success = base.Delete(client) &&
[995]280            ResAdapter.Delete(client);
[925]281        }
282      }
283
[1175]284      if (locked != Guid.Empty) {
285        UnlockRow(locked);
286      }
287
288      return success;
[826]289    }
290
291    #endregion
292  }
293}
Note: See TracBrowser for help on using the repository browser.