Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.3/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server.LINQDataAccess/3.3/SlaveDao.cs @ 5511

Last change on this file since 5511 was 5179, checked in by cneumuel, 14 years ago

#1260

  • migrated to .NET 4.0
  • moved state-information about heartbeat timestamps into DB to reduce IIS-recycling issues
  • optimized memory usage of ExperimentManager when lots of large jobs are downloaded
File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Hive.Contracts.BusinessObjects;
26using HeuristicLab.Hive.Server.DataAccess;
27
28namespace HeuristicLab.Hive.Server.LINQDataAccess {
29  public class SlaveDao: BaseDao<SlaveDto, Slave>, ISlaveDao {
30
31    public SlaveDao() {     
32    }
33
34    public SlaveDto FindById(Guid id) {
35      return (from slave in Context.Resources.OfType<Slave>()                         
36              where slave.ResourceId.Equals(id)
37              select EntityToDto(slave, null)
38            ).SingleOrDefault();     
39    }
40
41    public IEnumerable<SlaveDto> FindAll() {
42      return (from slave in Context.Resources.OfType<Slave>()
43              select
44                EntityToDto(slave, null)
45             ).ToList();
46    }
47
48    public IEnumerable<SlaveDto> FindAllSlavesWithoutGroup() {
49      return (from slave in Context.Resources.OfType<Slave>()
50              where slave.SlaveGroup_Resources_Parents.Count == 0
51              select EntityToDto(slave, null)).ToList();
52    }
53
54    public SlaveDto GetSlaveForJob(Guid jobId) {
55      return (from job in Context.Jobs
56              where job.JobId.Equals(jobId)
57              select EntityToDto(job.Slave, null)).SingleOrDefault();
58    }
59
60    public void SetServerSideCalendar(SlaveDto slave, Guid slaveGroupId) {
61      Slave dbslave = Context.Resources.OfType<Slave>().SingleOrDefault(c => c.ResourceId.Equals(slave.Id));
62      dbslave.UseCalendarFromResourceId = slaveGroupId;
63      dbslave.CalendarSyncStatus = Enum.GetName(typeof(CalendarState), CalendarState.Fetch);
64      CommitChanges();
65    }
66
67    public SlaveDto Insert(SlaveDto info) {
68      Slave c = DtoToEntity(info, null);     
69      Context.Resources.InsertOnSubmit(c);
70      CommitChanges();
71      info.Id = c.ResourceId;
72      return info;
73    }
74
75    //Cascading delete takes care of the rest
76    public void Delete(Guid id) {
77      Resource res = Context.Resources.SingleOrDefault(c => c.ResourceId.Equals(id));           
78      Context.Resources.DeleteOnSubmit(res);
79      CommitChanges();
80    }
81
82    public void Update(SlaveDto info) {
83      Slave slave = Context.Resources.OfType<Slave>().SingleOrDefault(c => c.ResourceId.Equals(info.Id));
84      DtoToEntity(info, slave);
85      CommitChanges();     
86    }
87
88    public override Slave DtoToEntity(SlaveDto source, Slave target) {
89      if (source == null)
90        return null;
91      if (target == null)
92        target = new Slave();
93     
94      target.CPUSpeed = source.CpuSpeedPerCore;
95     
96      target.FreeMemory = source.FreeMemory;
97      target.Name = source.Name;
98      target.ResourceId = source.Id;
99      target.CalendarSyncStatus = Enum.GetName(typeof(CalendarState), source.CalendarSyncStatus);
100      target.Login = source.Login;
101      target.Memory = source.Memory;
102      target.NumberOfCores = source.NrOfCores;
103      target.NumberOfFreeCores = source.NrOfFreeCores;
104      target.Status = source.State.ToString();
105      target.LastHeartbeat = source.LastHeartbeat;
106      return target;
107    }
108
109    public override SlaveDto EntityToDto(Slave source, SlaveDto target) {
110      if (source == null)
111        return null;
112      if(target == null)
113        target = new SlaveDto();
114      target.CpuSpeedPerCore = source.CPUSpeed;
115      target.FreeMemory = source.FreeMemory ?? 0;
116      target.Id = source.ResourceId;
117      target.CalendarSyncStatus = (CalendarState) Enum.Parse(typeof (CalendarState), source.CalendarSyncStatus);
118      target.Login = source.Login;
119      target.Memory = source.Memory ?? 0;
120      target.Name = source.Name;
121      target.NrOfCores = source.NumberOfCores ?? 0;
122      target.NrOfFreeCores = source.NumberOfFreeCores ?? 0;
123      target.State = (SlaveState)Enum.Parse(typeof(SlaveState), source.Status, true);
124      target.IsAllowedToCalculate = source.IsAllowedToCalculate;
125      target.LastHeartbeat = source.LastHeartbeat;
126      return target;
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.