Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server.LINQDataAccess/3.3/JobDao.cs @ 4119

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

HiveExperiment is now able to send IOptimizers of an Experiment and receive the calculated result (#1115)

File size: 7.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Hive.Contracts.BusinessObjects;
6using System.Data.Linq;
7using HeuristicLab.Hive.Server.DataAccess;
8using System.IO;
9using System.Data.SqlClient;
10
11namespace HeuristicLab.Hive.Server.LINQDataAccess {
12  public class JobDao: BaseDao<JobDto, Job>, IJobDao {
13   
14    #region IGenericDao<JobDto,Job> Members
15
16    public JobDto FindById(Guid id) {
17      return (from job in Context.Jobs
18              where job.JobId.Equals(id)
19              select EntityToDto(job, null)).SingleOrDefault();
20    }
21
22    public IEnumerable<JobDto> FindAll() {
23      return (from job in Context.Jobs
24              select EntityToDto(job, null)).ToList();
25    }
26
27    public IEnumerable<JobDto> FindWithLimitations(State jobState, int offset, int count) {
28      IQueryable<JobDto> query = null;
29      if (jobState == State.Finished) {
30         query = from job in Context.Jobs
31                 where job.JobState == Enum.GetName(typeof (State), jobState)
32                 orderby job.DateFinished
33                 select EntityToDto(job, null);
34      } else if (jobState == State.Calculating || jobState == State.RequestSnapshot || jobState == State.RequestSnapshotSent) {
35        query = from job in Context.Jobs
36                    where job.JobState == Enum.GetName(typeof(State), jobState)
37                    orderby job.DateCalculated
38                    select EntityToDto(job, null);
39      } else {
40        query = from job in Context.Jobs
41                    where job.JobState == Enum.GetName(typeof(State), jobState)
42                    orderby job.DateCreated
43                    select EntityToDto(job, null);
44      }
45
46      return query.Skip(offset).Take(count).ToList();
47    }
48
49
50    public byte[] GetBinaryJobFile(Guid jobId) {
51      return (from job in Context.Jobs
52              where job.JobId.Equals(jobId)
53              select job.SerializedJob).SingleOrDefault().ToArray();
54    }
55
56    public JobDto Insert(JobDto bObj) {
57      Job j = DtoToEntity(bObj, null);
58      Context.Jobs.InsertOnSubmit(j);
59      CommitChanges();
60      bObj.Id = j.JobId;
61      return bObj;
62    }
63
64    public void SetBinaryJobFile(Guid jobId, byte[] data) {
65      Job j = (from job in Context.Jobs
66               where job.JobId.Equals(jobId)
67               select job).SingleOrDefault();
68      j.SerializedJob = data;
69      CommitChanges();
70    }
71
72    public SerializedJob InsertWithAttachedJob(SerializedJob job) {
73      Job j = DtoToEntity(job.JobInfo, null);
74      j.SerializedJob = job.SerializedJobData;
75      foreach (Guid assignRessourceId in job.JobInfo.AssignedResourceIds)
76        j.AssignedResources.Add(new AssignedResource { ResourceId = assignRessourceId});
77      Context.Jobs.InsertOnSubmit(j);
78      CommitChanges();
79      job.JobInfo.Id = j.JobId;
80      return job;
81    }
82
83    public void Delete(JobDto bObj) {
84      Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(bObj.Id));
85      Context.Jobs.DeleteOnSubmit(job);
86      CommitChanges();
87    }
88
89    public void Update(JobDto bObj) {
90      Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(bObj.Id));
91      DtoToEntity(bObj, job);   
92      CommitChanges();
93    }
94
95    public IEnumerable<JobDto> FindActiveJobsOfClient(ClientDto client) {
96      return (from j in Context.Jobs
97              where (j.JobState == Enum.GetName(typeof (State), State.Calculating) ||
98                     j.JobState == Enum.GetName(typeof (State), State.Abort) ||
99                     j.JobState == Enum.GetName(typeof (State), State.RequestSnapshot) ||
100                     j.JobState == Enum.GetName(typeof (State), State.RequestSnapshotSent)) &&
101                    (j.ResourceId.Equals(client.Id))
102              select EntityToDto(j, null)).ToList();
103    }
104
105    public IEnumerable<JobDto> FindFittingJobsForClient(State state, int freeCores, int freeMemory, Guid clientId) {
106      ClientGroupDao cgd = new ClientGroupDao();
107     
108      List<Guid> idList = new List<Guid>(cgd.FindAllGroupAndParentGroupIdsForClient(clientId));
109      //Add myself too - enables jobs for one specific host!
110      idList.Add(clientId);
111     
112      var q = (from ar in Context.AssignedResources               
113               where ar.Job.JobState == Enum.GetName(typeof (State), State.Offline) &&
114                     ar.Job.CoresNeeded <= freeCores &&
115                     ar.Job.MemoryNeeded <= freeMemory &&
116                     idList.Contains(ar.ResourceId)
117               orderby ar.Job.Priority descending                 
118               select EntityToDto(ar.Job, null));
119      return q.ToList();
120    }
121
122    public IEnumerable<JobDto> GetJobsByState(State state) {
123      return (from j in Context.Jobs
124              where (j.JobState == Enum.GetName(typeof (State), state))
125              select EntityToDto(j, null)).ToList();
126    }
127
128    public void AssignClientToJob(Guid clientId, Guid jobId) {
129      Client c = Context.Resources.OfType<Client>().SingleOrDefault(client => client.ResourceId.Equals(clientId));
130      Job j = Context.Jobs.SingleOrDefault(job => job.JobId.Equals(jobId));
131      c.Jobs.Add(j);
132      j.Client = c;
133      CommitChanges();     
134    }
135
136    public void SetJobOffline(JobDto job) {
137      Job j = Context.Jobs.SingleOrDefault(jq => jq.JobId.Equals(job.Id));
138      j.Client = null;
139      j.JobState = Enum.GetName(typeof(State), State.Offline);
140      CommitChanges();
141    }
142
143    public Stream GetSerializedJobStream(Guid jobId) {
144      VarBinarySource source =
145        new VarBinarySource(
146          (SqlConnection)Context.Connection, null,
147          "Job", "SerializedJob", "JobId", jobId);
148
149      return new VarBinaryStream(source);
150    }
151
152    #endregion
153
154    public override Job DtoToEntity(JobDto source, Job target) {
155      if (source == null)
156        return null;
157      if (target == null)
158        target = new Job();
159
160      target.CoresNeeded = source.CoresNeeded;
161      target.MemoryNeeded = source.MemoryNeeded;
162
163      target.DateCalculated = source.DateCalculated;
164      target.DateCreated = source.DateCreated;
165      target.DateFinished = source.DateFinished;
166      target.JobId = source.Id;
167
168      target.Percentage = source.Percentage;
169
170      target.Priority = source.Priority;
171      target.JobState = Enum.GetName(typeof(State), source.State);
172      return target;
173    }
174
175    //Assigned ressources are not used atm!
176    //Client is not used ATM - not sure when we stopped using those...
177    public override JobDto EntityToDto(Job source, JobDto target) {
178      if (source == null)
179        return null;
180      if(target == null)
181        target = new JobDto();
182   
183      //target.ParentJob = null;
184      //target.PluginsNeeded = null;
185      //target.Client = null;
186      //target.Project = null;
187     
188      target.CoresNeeded = source.CoresNeeded;
189      target.MemoryNeeded = source.MemoryNeeded;
190
191      target.DateCalculated = source.DateCalculated;
192      target.DateCreated = source.DateCreated;
193      target.DateFinished = source.DateFinished;
194      target.Id = source.JobId;
195       
196      target.Percentage = source.Percentage;
197     
198      target.Priority = source.Priority;
199      target.State = (State) Enum.Parse(typeof (State), source.JobState, true);
200      return target;
201    }
202  }
203}
Note: See TracBrowser for help on using the repository browser.