Free cookie consent management tool by TermsFeed Policy Generator

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

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

fixed race condition issues, improved performance (#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    private void Preprocess(ClientInfo client) {
84      if (client != null) {
85        if (client.Id == default(long)) {
86          ClientInfo found = GetById(client.ClientId);
87          if (found != null)
88            client.Id = found.Id;
89        }
90      }
91    }
92
93    #region Overrides
94    protected override ClientInfo ConvertRow(dsHiveServer.ClientRow row,
95      ClientInfo client) {
96      if(row != null && client != null) {     
97        /*Parent - resource*/
98        client.Id = row.ResourceId;
99        ResAdapter.GetById(client);
100
101        /*ClientInfo*/
102        if (!row.IsGUIDNull())
103          client.ClientId = row.GUID;
104        else
105          client.ClientId = Guid.Empty;
106       
107        if (!row.IsCPUSpeedNull())
108          client.CpuSpeedPerCore = row.CPUSpeed;
109        else
110          client.CpuSpeedPerCore = 0;
111
112        if (!row.IsMemoryNull())
113          client.Memory = row.Memory;
114        else
115          client.Memory = 0;
116
117        if (!row.IsLoginNull())
118          client.Login = row.Login;
119        else
120          client.Login = DateTime.MinValue;
121
122        if (!row.IsStatusNull())
123          client.State = (State)Enum.Parse(typeof(State), row.Status, true);
124        else
125          client.State = State.nullState;
126
127        if (!row.IsNumberOfCoresNull())
128          client.NrOfCores = row.NumberOfCores;
129        else
130          client.NrOfCores = 0;
131
132        //todo: config adapter (client.config)
133
134        return client;
135      }
136      else
137        return null;
138    }
139
140    protected override dsHiveServer.ClientRow ConvertObj(ClientInfo client,
141      dsHiveServer.ClientRow row) {
142      if (client != null && row != null) {     
143        row.GUID = client.ClientId;
144        row.CPUSpeed = client.CpuSpeedPerCore;
145        row.Memory = client.Memory;
146        row.Login = client.Login;
147        if (client.State != State.nullState)
148          row.Status = client.State.ToString();
149        else
150          row.SetStatusNull();
151        row.NumberOfCores = client.NrOfCores;
152
153        //todo: config adapter
154        /*if (client.Config != null)
155          row.ClientConfigId = client.Config.ClientConfigId;
156         else
157          row.ClientConfigId = null;*/
158      }
159
160      return row;
161    }
162
163    protected override void UpdateRow(dsHiveServer.ClientRow row) {
164      Adapter.Update(row);
165    }
166
167    protected override dsHiveServer.ClientRow
168      InsertNewRow(ClientInfo client) {
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 = data.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    public override void SyncWithDb() {
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.