[826] | 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;
|
---|
[925] | 28 | using System.Linq.Expressions;
|
---|
| 29 | using System.Runtime.CompilerServices;
|
---|
[826] | 30 |
|
---|
| 31 | namespace HeuristicLab.Hive.Server.ADODataAccess {
|
---|
[995] | 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();
|
---|
[826] | 42 |
|
---|
[948] | 43 | private IResourceAdapter resAdapter = null;
|
---|
[925] | 44 |
|
---|
[948] | 45 | private IResourceAdapter ResAdapter {
|
---|
| 46 | get {
|
---|
| 47 | if (resAdapter == null)
|
---|
| 48 | resAdapter = ServiceLocator.GetResourceAdapter();
|
---|
| 49 |
|
---|
| 50 | return resAdapter;
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[965] | 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 |
|
---|
[971] | 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 | }
|
---|
[995] | 77 | #endregion
|
---|
[971] | 78 |
|
---|
[925] | 79 | public ClientAdapter() {
|
---|
[995] | 80 | parentAdapters.Add(this.ResAdapter as ICachedDataAdapter);
|
---|
[925] | 81 | }
|
---|
| 82 |
|
---|
[1122] | 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 |
|
---|
[995] | 93 | #region Overrides
|
---|
[1131] | 94 | protected override ClientInfo ConvertRow(dsHiveServer.ClientRow row,
|
---|
[899] | 95 | ClientInfo client) {
|
---|
| 96 | if(row != null && client != null) {
|
---|
[845] | 97 | /*Parent - resource*/
|
---|
[995] | 98 | client.Id = row.ResourceId;
|
---|
| 99 | ResAdapter.GetById(client);
|
---|
[826] | 100 |
|
---|
[845] | 101 | /*ClientInfo*/
|
---|
[995] | 102 | if (!row.IsGUIDNull())
|
---|
| 103 | client.ClientId = row.GUID;
|
---|
| 104 | else
|
---|
| 105 | client.ClientId = Guid.Empty;
|
---|
[899] | 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())
|
---|
[845] | 123 | client.State = (State)Enum.Parse(typeof(State), row.Status, true);
|
---|
[899] | 124 | else
|
---|
[995] | 125 | client.State = State.nullState;
|
---|
[845] | 126 |
|
---|
[899] | 127 | if (!row.IsNumberOfCoresNull())
|
---|
| 128 | client.NrOfCores = row.NumberOfCores;
|
---|
| 129 | else
|
---|
| 130 | client.NrOfCores = 0;
|
---|
| 131 |
|
---|
[845] | 132 | //todo: config adapter (client.config)
|
---|
| 133 |
|
---|
| 134 | return client;
|
---|
| 135 | }
|
---|
| 136 | else
|
---|
| 137 | return null;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[1131] | 140 | protected override dsHiveServer.ClientRow ConvertObj(ClientInfo client,
|
---|
[845] | 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;
|
---|
[995] | 147 | if (client.State != State.nullState)
|
---|
| 148 | row.Status = client.State.ToString();
|
---|
| 149 | else
|
---|
| 150 | row.SetStatusNull();
|
---|
[845] | 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 |
|
---|
[995] | 163 | protected override void UpdateRow(dsHiveServer.ClientRow row) {
|
---|
[1131] | 164 | Adapter.Update(row);
|
---|
[995] | 165 | }
|
---|
[826] | 166 |
|
---|
[995] | 167 | protected override dsHiveServer.ClientRow
|
---|
| 168 | InsertNewRow(ClientInfo client) {
|
---|
| 169 | dsHiveServer.ClientRow row = data.NewClientRow();
|
---|
| 170 | row.ResourceId = client.Id;
|
---|
| 171 | data.AddClientRow(row);
|
---|
[826] | 172 |
|
---|
[995] | 173 | return row;
|
---|
[826] | 174 | }
|
---|
| 175 |
|
---|
[995] | 176 | protected override dsHiveServer.ClientRow
|
---|
| 177 | InsertNewRowInCache(ClientInfo client) {
|
---|
[1134] | 178 | dsHiveServer.ClientRow row = cache.NewClientRow();
|
---|
[995] | 179 | row.ResourceId = client.Id;
|
---|
| 180 | cache.AddClientRow(row);
|
---|
[925] | 181 |
|
---|
[995] | 182 | return row;
|
---|
| 183 | }
|
---|
[925] | 184 |
|
---|
[995] | 185 | protected override void FillCache() {
|
---|
[1131] | 186 | Adapter.FillByActive(cache);
|
---|
[995] | 187 | }
|
---|
[899] | 188 |
|
---|
[995] | 189 | public override void SyncWithDb() {
|
---|
[1131] | 190 | Adapter.Update(cache);
|
---|
[826] | 191 | }
|
---|
| 192 |
|
---|
[995] | 193 | protected override bool PutInCache(ClientInfo obj) {
|
---|
| 194 | return (obj.State != State.offline && obj.State != State.nullState);
|
---|
| 195 | }
|
---|
[965] | 196 |
|
---|
[995] | 197 | protected override IEnumerable<dsHiveServer.ClientRow>
|
---|
| 198 | FindById(long id) {
|
---|
[1131] | 199 | return Adapter.GetDataByResourceId(id);
|
---|
[995] | 200 | }
|
---|
[965] | 201 |
|
---|
[995] | 202 | protected override dsHiveServer.ClientRow
|
---|
| 203 | FindCachedById(long id) {
|
---|
| 204 | return cache.FindByResourceId(id);
|
---|
| 205 | }
|
---|
[965] | 206 |
|
---|
[995] | 207 | protected override IEnumerable<dsHiveServer.ClientRow>
|
---|
| 208 | FindAll() {
|
---|
| 209 | return FindMultipleRows(
|
---|
[1131] | 210 | new Selector(Adapter.GetData),
|
---|
[995] | 211 | new Selector(cache.AsEnumerable<dsHiveServer.ClientRow>));
|
---|
[965] | 212 | }
|
---|
[1094] | 213 |
|
---|
[995] | 214 | #endregion
|
---|
[965] | 215 |
|
---|
[995] | 216 | #region IClientAdapter Members
|
---|
| 217 | [MethodImpl(MethodImplOptions.Synchronized)]
|
---|
| 218 | public override void Update(ClientInfo client) {
|
---|
| 219 | if (client != null) {
|
---|
[1122] | 220 | Preprocess(client);
|
---|
[1014] | 221 |
|
---|
[995] | 222 | ResAdapter.Update(client);
|
---|
[971] | 223 |
|
---|
[995] | 224 | base.Update(client);
|
---|
[971] | 225 | }
|
---|
[995] | 226 | }
|
---|
[971] | 227 |
|
---|
[995] | 228 | public ClientInfo GetById(Guid clientId) {
|
---|
| 229 | return base.FindSingle(
|
---|
| 230 | delegate() {
|
---|
[1131] | 231 | return Adapter.GetDataById(clientId);
|
---|
[995] | 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 | });
|
---|
[971] | 240 | }
|
---|
| 241 |
|
---|
[995] | 242 | public ClientInfo GetByName(string name) {
|
---|
| 243 | ClientInfo client = new ClientInfo();
|
---|
| 244 | Resource res =
|
---|
| 245 | ResAdapter.GetByName(name);
|
---|
[826] | 246 |
|
---|
[995] | 247 | return GetById(res.Id);
|
---|
[826] | 248 | }
|
---|
| 249 |
|
---|
[925] | 250 | [MethodImpl(MethodImplOptions.Synchronized)]
|
---|
[995] | 251 | public override bool Delete(ClientInfo client) {
|
---|
[925] | 252 | if (client != null) {
|
---|
[1122] | 253 | Preprocess(client);
|
---|
| 254 |
|
---|
[995] | 255 | dsHiveServer.ClientRow row =
|
---|
| 256 | GetRowById(client.Id);
|
---|
[925] | 257 |
|
---|
| 258 | if (row != null) {
|
---|
[1009] | 259 | //Referential integrity with jobs - they are cached
|
---|
[971] | 260 | ICollection<Job> jobs =
|
---|
| 261 | JobAdapter.GetJobsOf(client);
|
---|
| 262 | foreach (Job job in jobs) {
|
---|
[995] | 263 | JobAdapter.Delete(job);
|
---|
[971] | 264 | }
|
---|
| 265 |
|
---|
[995] | 266 | return base.Delete(client) &&
|
---|
| 267 | ResAdapter.Delete(client);
|
---|
[925] | 268 | }
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | return false;
|
---|
[826] | 272 | }
|
---|
| 273 |
|
---|
| 274 | #endregion
|
---|
| 275 | }
|
---|
| 276 | }
|
---|