Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Hive.Server.ADODataAccess/3.2/ClientAdapter.cs @ 2772

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

Avoid out of memory exceptions related to job objects (#372)

File size: 6.4 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.DataAccess;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using System.Linq.Expressions;
29using HeuristicLab.DataAccess.Interfaces;
30using HeuristicLab.DataAccess.ADOHelper;
31using HeuristicLab.Hive.Server.ADODataAccess.dsHiveServerTableAdapters;
32using System.Data.Common;
33using System.Data.SqlClient;
34using HeuristicLab.Hive.Server.ADODataAccess.TableAdapterWrapper;
35
36namespace HeuristicLab.Hive.Server.ADODataAccess {
37  class ClientAdapter:
38    DataAdapterBase<
39      dsHiveServerTableAdapters.ClientTableAdapter,
40      ClientInfo,
41      dsHiveServer.ClientRow>,
42    IClientAdapter {
43    #region Fields
44    private IResourceAdapter resAdapter = null;
45
46    private IResourceAdapter ResAdapter {
47      get {
48        if (resAdapter == null)
49          resAdapter =
50            this.Session.GetDataAdapter<Resource, IResourceAdapter>();
51
52        return resAdapter;
53      }
54    }
55
56    private IClientGroupAdapter clientGroupAdapter = null;
57
58    private IClientGroupAdapter ClientGroupAdapter {
59      get {
60        if (clientGroupAdapter == null) {
61          clientGroupAdapter =
62            this.Session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
63        }
64
65        return clientGroupAdapter;
66      }
67    }
68
69    private IJobAdapter jobAdapter = null;
70
71    private IJobAdapter JobAdapter {
72      get {
73        if (jobAdapter == null) {
74          this.Session.GetDataAdapter<Job, IJobAdapter>();
75        }
76
77        return jobAdapter;
78      }
79    }
80
81    private IClientConfigAdapter clientConfigAdapter = null;
82
83    private IClientConfigAdapter ClientConfigAdapter {
84      get {
85        if (clientConfigAdapter == null) {
86          clientConfigAdapter =
87            this.Session.GetDataAdapter<ClientConfig, IClientConfigAdapter>();
88        }
89
90        return clientConfigAdapter;
91      }
92    }
93    #endregion
94
95    public ClientAdapter():
96      base(new ClientAdapterWrapper()) {
97    }
98
99    #region Overrides
100    protected override ClientInfo ConvertRow(dsHiveServer.ClientRow row,
101      ClientInfo client) {
102      if(row != null && client != null) {     
103        /*Parent - resource*/
104        client.Id = row.ResourceId;
105        ResAdapter.GetById(client);
106
107        /*ClientInfo*/       
108        if (!row.IsCPUSpeedNull())
109          client.CpuSpeedPerCore = row.CPUSpeed;
110        else
111          client.CpuSpeedPerCore = 0;
112
113        if (!row.IsMemoryNull())
114          client.Memory = row.Memory;
115        else
116          client.Memory = 0;
117
118        if (!row.IsLoginNull())
119          client.Login = row.Login;
120        else
121          client.Login = DateTime.MinValue;
122
123        if (!row.IsStatusNull())
124          client.State = (State)Enum.Parse(typeof(State), row.Status, true);
125        else
126          client.State = State.nullState;
127
128        if (!row.IsNumberOfCoresNull())
129          client.NrOfCores = row.NumberOfCores;
130        else
131          client.NrOfCores = 0;
132
133        if (!row.IsNumberOfFreeCoresNull())
134          client.NrOfFreeCores = row.NumberOfFreeCores;
135        else
136          client.NrOfFreeCores = 0;
137
138        if (!row.IsFreeMemoryNull())
139          client.FreeMemory = row.FreeMemory;
140        else
141          client.FreeMemory = 0;
142
143        if (!row.IsClientConfigIdNull())
144          client.Config = ClientConfigAdapter.GetById(row.ClientConfigId);
145        else
146          client.Config = null;
147
148        return client;
149      }
150      else
151        return null;
152    }
153
154    protected override dsHiveServer.ClientRow ConvertObj(ClientInfo client,
155      dsHiveServer.ClientRow row) {
156      if (client != null && row != null) {
157        row.ResourceId = client.Id;
158        row.CPUSpeed = client.CpuSpeedPerCore;
159        row.Memory = client.Memory;
160        row.Login = client.Login;
161        if (client.State != State.nullState)
162          row.Status = client.State.ToString();
163        else
164          row.SetStatusNull();
165        row.NumberOfCores = client.NrOfCores;
166        row.NumberOfFreeCores = client.NrOfFreeCores;
167        row.FreeMemory = client.FreeMemory;
168
169        if (client.Config != null)
170          row.ClientConfigId = client.Config.Id;
171        else
172          row.SetClientConfigIdNull();
173      }
174
175      return row;
176    }
177
178    #endregion
179
180    #region IClientAdapter Members
181    protected override void doUpdate(ClientInfo client) {
182      if (client != null) {
183        ResAdapter.Update(client);
184        ClientConfigAdapter.Update(client.Config);
185
186        base.doUpdate(client);
187      }
188    }
189
190    public ClientInfo GetByName(string name) {
191      return (ClientInfo)
192        base.doInTransaction(
193        delegate() {
194          ClientInfo client = new ClientInfo();
195          Resource res =
196            ResAdapter.GetByName(name);
197
198          return GetById(res.Id);
199        });
200    }
201
202    protected override bool doDelete(ClientInfo client) {
203      return (bool)base.doInTransaction(
204        delegate() {
205          bool success = false;
206
207          if (client != null) {
208            dsHiveServer.ClientRow row =
209              GetRowById(client.Id);
210
211            if (row != null) {
212              success = base.doDelete(client) &&
213                ResAdapter.Delete(client);
214            }
215          }
216
217          return success;
218        });
219    }
220
221    public ICollection<ClientInfo> GetGrouplessClients() {
222      return
223        base.FindMultiple(
224          delegate() {
225            return Adapter.GetDataByGroupless();
226          });
227    }
228
229    #endregion
230  }
231}
Note: See TracBrowser for help on using the repository browser.