Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored DAL, Improved Caching (#372)

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