Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.LINQDataAccess/3.2/JobDao.cs @ 3317

Last change on this file since 3317 was 3220, checked in by kgrading, 15 years ago

improved the DAL further, changed minor details for the presentation (#830)

File size: 6.0 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 byte[] GetBinaryJobFile(Guid jobId) {
28      return (from job in Context.Jobs
29              where job.JobId.Equals(jobId)
30              select job.SerializedJob).SingleOrDefault().ToArray();
31    }
32
33    public JobDto Insert(JobDto bObj) {
34      Job j = DtoToEntity(bObj, null);
35      Context.Jobs.InsertOnSubmit(j);
36      Context.SubmitChanges();
37      bObj.Id = j.JobId;
38      return bObj;
39    }
40
41    public SerializedJob InsertWithAttachedJob(SerializedJob job) {
42      Job j = DtoToEntity(job.JobInfo, null);
43      j.SerializedJob = job.SerializedJobData;
44      foreach (Guid assignRessourceId in job.JobInfo.AssignedResourceIds)
45        j.AssignedResources.Add(new AssignedResource { ResourceId = assignRessourceId});
46      Context.Jobs.InsertOnSubmit(j);
47      Context.SubmitChanges();
48      job.JobInfo.Id = j.JobId;
49      return job;
50
51    }
52
53    public void Delete(JobDto bObj) {
54      Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(bObj.Id));
55      Context.Jobs.DeleteOnSubmit(job);
56      Context.SubmitChanges();
57    }
58
59    public void Update(JobDto bObj) {
60      Job job = Context.Jobs.SingleOrDefault(j => j.JobId.Equals(bObj.Id));
61      DtoToEntity(bObj, job);
62      try {
63        Context.SubmitChanges();
64      } catch (ChangeConflictException cfe) {
65       
66      }
67    }
68
69    public IEnumerable<JobDto> FindActiveJobsOfClient(ClientDto client) {
70      return (from j in Context.Jobs
71              where (j.JobState == Enum.GetName(typeof (State), State.calculating) ||
72                     j.JobState == Enum.GetName(typeof (State), State.abort) ||
73                     j.JobState == Enum.GetName(typeof (State), State.requestSnapshot) ||
74                     j.JobState == Enum.GetName(typeof (State), State.requestSnapshotSent)) &&
75                    (j.ResourceId.Equals(client.Id))
76              select EntityToDto(j, null)).ToList();
77    }
78    public IEnumerable<JobDto> FindFittingJobsForClient(State state, int freeCores, int freeMemory, Guid clientId) {
79      ClientGroupDao cgd = new ClientGroupDao();
80     
81      List<Guid> idList = new List<Guid>(cgd.FindAllGroupAndParentGroupIdsForClient(clientId));
82      //Add myself too - enables jobs for one specific host!
83      idList.Add(clientId);
84     
85      var q = (from ar in Context.AssignedResources               
86               where ar.Job.JobState == Enum.GetName(typeof (State), State.offline) &&
87                     ar.Job.CoresNeeded <= freeCores &&
88                     ar.Job.MemoryNeeded <= freeMemory &&
89                     idList.Contains(ar.ResourceId)
90               orderby ar.Job.Priority descending                 
91               select EntityToDto(ar.Job, null));
92      return q.ToList();
93     
94    }
95
96    public IEnumerable<JobDto> GetJobsByState(State state) {
97      return (from j in Context.Jobs
98              where (j.JobState == Enum.GetName(typeof (State), state))
99              select EntityToDto(j, null)).ToList();
100    }
101
102    public void AssignClientToJob(Guid clientId, Guid jobId) {
103      Client c = Context.Clients.SingleOrDefault(client => client.ResourceId.Equals(clientId));
104      Job j = Context.Jobs.SingleOrDefault(job => job.JobId.Equals(jobId));
105      c.Jobs.Add(j);
106      j.Client = c;
107      Context.SubmitChanges();     
108    }
109
110    public void SetJobOffline(JobDto job) {
111      Job j = Context.Jobs.SingleOrDefault(jq => jq.JobId.Equals(job.Id));
112      j.Client = null;
113      j.JobState = Enum.GetName(typeof(State), State.offline);
114      Context.SubmitChanges();
115    }
116
117    public Stream GetSerializedJobStream(Guid jobId) {
118      VarBinarySource source =
119        new VarBinarySource(
120          (SqlConnection)Context.Connection, null,
121          "Job", "SerializedJob", "JobId", jobId);
122
123      return new VarBinaryStream(source);
124    }
125
126    #endregion
127
128    public override Job DtoToEntity(JobDto source, Job target) {
129      if (source == null)
130        return null;
131      if (target == null)
132        target = new Job();
133
134      target.CoresNeeded = source.CoresNeeded;
135      target.MemoryNeeded = source.MemoryNeeded;
136
137      target.DateCalculated = source.DateCalculated;
138      target.DateCreated = source.DateCreated;
139      target.JobId = source.Id;
140
141      target.Percentage = source.Percentage;
142
143      target.Priority = source.Priority;
144      target.JobState = Enum.GetName(typeof(State), source.State);
145      return target;
146    }
147
148    //Assigned ressources are not used atm!
149    //Client is not used ATM - not sure when we stopped using those...
150    public override JobDto EntityToDto(Job source, JobDto target) {
151      if (source == null)
152        return null;
153      if(target == null)
154        target = new JobDto();
155   
156      //target.ParentJob = null;
157      //target.PluginsNeeded = null;
158      //target.Client = null;
159      //target.Project = null;
160     
161      target.CoresNeeded = source.CoresNeeded;
162      target.MemoryNeeded = source.MemoryNeeded;
163
164
165
166      target.DateCalculated = source.DateCalculated;
167      target.DateCreated = source.DateCreated;
168      target.Id = source.JobId;
169     
170         
171      target.Percentage = source.Percentage;
172     
173      target.Priority = source.Priority;
174      target.State = (State) Enum.Parse(typeof (State), source.JobState);
175      return target;
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.