Free cookie consent management tool by TermsFeed Policy Generator

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

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

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