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