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.Core.InternalInterfaces.DataAccess;
|
---|
27 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
28 | using System.Linq.Expressions;
|
---|
29 | using System.Runtime.CompilerServices;
|
---|
30 |
|
---|
31 | namespace 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 | if (client.Id == default(long)) {
|
---|
210 | ClientInfo found = GetById(client.ClientId);
|
---|
211 | if (found != null)
|
---|
212 | client = found;
|
---|
213 | }
|
---|
214 |
|
---|
215 | ResAdapter.Update(client);
|
---|
216 |
|
---|
217 | base.Update(client);
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | public ClientInfo GetById(Guid clientId) {
|
---|
222 | return base.FindSingle(
|
---|
223 | delegate() {
|
---|
224 | return adapter.GetDataById(clientId);
|
---|
225 | },
|
---|
226 | delegate() {
|
---|
227 | return from c in
|
---|
228 | cache.AsEnumerable<dsHiveServer.ClientRow>()
|
---|
229 | where !c.IsGUIDNull() &&
|
---|
230 | c.GUID == clientId
|
---|
231 | select c;
|
---|
232 | });
|
---|
233 | }
|
---|
234 |
|
---|
235 | public ClientInfo GetByName(string name) {
|
---|
236 | ClientInfo client = new ClientInfo();
|
---|
237 | Resource res =
|
---|
238 | ResAdapter.GetByName(name);
|
---|
239 |
|
---|
240 | return GetById(res.Id);
|
---|
241 | }
|
---|
242 |
|
---|
243 | [MethodImpl(MethodImplOptions.Synchronized)]
|
---|
244 | public override bool Delete(ClientInfo client) {
|
---|
245 | if (client != null) {
|
---|
246 | dsHiveServer.ClientRow row =
|
---|
247 | GetRowById(client.Id);
|
---|
248 |
|
---|
249 | if (row != null) {
|
---|
250 | //Referential integrity with jobs - they are cached
|
---|
251 | ICollection<Job> jobs =
|
---|
252 | JobAdapter.GetJobsOf(client);
|
---|
253 | foreach (Job job in jobs) {
|
---|
254 | JobAdapter.Delete(job);
|
---|
255 | }
|
---|
256 |
|
---|
257 | return base.Delete(client) &&
|
---|
258 | ResAdapter.Delete(client);
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | return false;
|
---|
263 | }
|
---|
264 |
|
---|
265 | #endregion
|
---|
266 | }
|
---|
267 | }
|
---|