Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive.DataAccess/3.3/HiveDao.cs @ 4629

Last change on this file since 4629 was 4629, checked in by cneumuel, 14 years ago
  • worked on new hive structure
  • created IIS hostable website for hive (old structure)

(#1233)

File size: 8.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Linq.Expressions;
6
7namespace HeuristicLab.Services.Hive.DataAccess {
8  using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
9  using HeuristicLab.Services.Hive.Common.DataTransfer;
10
11  public class HiveDao : IHiveDao {
12    private IContextFactory<HiveDataContext> contextFactory;
13    private DataAccess.HiveDataContext db { get { return contextFactory.CurrentContext; } }
14
15    public HiveDao(IContextFactory<HiveDataContext> contextFactory) {
16      this.contextFactory = contextFactory;
17    }
18
19    #region Job Methods
20    public DT.Job GetJob(Guid id) {
21      return Convert.ToDto(db.Jobs.SingleOrDefault(x => x.JobId == id));
22    }
23
24    public IEnumerable<DT.Job> GetJobs(Expression<Func<Job, bool>> predicate) {
25      return db.Jobs.Where(predicate).Select(x => Convert.ToDto(x));
26    }
27
28    public Guid AddJob(DT.Job dto) {
29      var entity = Convert.ToEntity(dto);
30      db.Jobs.InsertOnSubmit(entity);
31      db.SubmitChanges();
32      return entity.JobId;
33    }
34
35    public void UpdateJob(DT.Job dto) {
36      var entity = db.Jobs.FirstOrDefault(x => x.JobId == dto.Id);
37      if (entity == null) db.Jobs.InsertOnSubmit(Convert.ToEntity(dto));
38      else Convert.ToEntity(dto, entity);
39      db.SubmitChanges();
40    }
41
42    public void DeleteJob(Guid id) {
43      var entity = db.Jobs.FirstOrDefault(x => x.JobId == id);
44      if (entity != null) db.Jobs.DeleteOnSubmit(entity);
45      db.SubmitChanges();
46    }
47
48    public IEnumerable<DT.Job> GetAvailableParentJobs(Guid slaveId) {
49      // todo: slaveId is unused!
50      var query = from ar in db.AssignedResources
51                  where ar.Job.JobState == JobState.WaitForChildJobs &&
52                    (from child in db.Jobs
53                     where child.ParentJobId == ar.Job.JobId
54                     select child.JobState == JobState.Finished).All(x => x) &&
55                    (from child in db.Jobs // avoid returning WaitForChildJobs jobs where no child-jobs exist (yet)
56                     where child.ParentJobId == ar.Job.JobId
57                     select child).Count() > 0
58                  orderby ar.Job.Priority descending
59                  select Convert.ToDto(ar.Job);
60      return query;
61    }
62
63    public IEnumerable<DT.Job> GetAvailableJobs(DT.Slave slave) {
64      var query = from j in db.Jobs
65                  where j.JobState == JobState.Waiting && j.CoresNeeded <= slave.FreeCores && j.MemoryNeeded <= slave.FreeMemory
66                  orderby j.Priority descending
67                  select Convert.ToDto(j);
68      return query.Union(GetAvailableParentJobs(slave.Id)).OrderByDescending(x => x.Priority).ToArray();
69    }
70    #endregion
71
72    #region JobData Methods
73
74    public DT.JobData GetJobData(Guid id) {
75      return Convert.ToDto(db.JobDatas.SingleOrDefault(x => x.JobId == id));
76    }
77
78    public IEnumerable<DT.JobData> GetJobDatas(Expression<Func<JobData, bool>> predicate) {
79      return db.JobDatas.Where(predicate).Select(x => Convert.ToDto(x));
80    }
81
82    public Guid AddJobData(DT.JobData dto) {
83      var entity = Convert.ToEntity(dto);
84      db.JobDatas.InsertOnSubmit(entity);
85      db.SubmitChanges();
86      return entity.JobId;
87    }
88
89    public void UpdateJobData(DT.JobData dto) {
90      var entity = db.JobDatas.FirstOrDefault(x => x.JobId == dto.Id);
91      if (entity == null) db.JobDatas.InsertOnSubmit(Convert.ToEntity(dto));
92      else Convert.ToEntity(dto, entity);
93      db.SubmitChanges();
94    }
95
96    public void DeleteJobData(Guid id) {
97      var entity = db.JobDatas.FirstOrDefault(x => x.JobId == id); // check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
98      if (entity != null) db.JobDatas.DeleteOnSubmit(entity);
99      db.SubmitChanges();
100    }
101    #endregion
102
103    #region HiveExperiment Methods
104    public DT.HiveExperiment GetHiveExperiment(Guid id) {
105      return Convert.ToDto(db.HiveExperiments.SingleOrDefault(x => x.HiveExperimentId == id));
106    }
107
108    public IEnumerable<DT.HiveExperiment> GetHiveExperiments(Expression<Func<HiveExperiment, bool>> predicate) {
109      return db.HiveExperiments.Where(predicate).Select(x => Convert.ToDto(x));
110    }
111
112    public Guid AddHiveExperiment(DT.HiveExperiment dto) {
113      var entity = Convert.ToEntity(dto);
114      db.HiveExperiments.InsertOnSubmit(entity);
115      db.SubmitChanges();
116      return entity.HiveExperimentId;
117    }
118
119    public void UpdateHiveExperiment(DT.HiveExperiment dto) {
120      var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == dto.Id);
121      if (entity == null) db.HiveExperiments.InsertOnSubmit(Convert.ToEntity(dto));
122      else Convert.ToEntity(dto, entity);
123      db.SubmitChanges();
124    }
125
126    public void DeleteHiveExperiment(Guid id) {
127      var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == id);
128      if (entity != null) db.HiveExperiments.DeleteOnSubmit(entity);
129      db.SubmitChanges();
130    }
131    #endregion
132
133
134    #region Slave Methods
135    public DT.Slave GetSlave(Guid id) {
136      return Convert.ToDto(db.Resources.OfType<Slave>().SingleOrDefault(x => x.ResourceId == id));
137    }
138
139    public IEnumerable<DT.Slave> GetSlaves(Expression<Func<Slave, bool>> predicate) {
140      return db.Resources.OfType<Slave>().Where(predicate).Select(x => Convert.ToDto(x));
141    }
142
143    public Guid AddSlave(DT.Slave dto) {
144      var entity = Convert.ToEntity(dto);
145      db.Resources.InsertOnSubmit(entity);
146      db.SubmitChanges();
147      return entity.ResourceId;
148    }
149
150    public void UpdateSlave(DT.Slave dto) {
151      var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == dto.Id);
152      if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
153      else Convert.ToEntity(dto, entity);
154      db.SubmitChanges();
155    }
156
157    public void DeleteSlave(Guid id) {
158      var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == id);
159      if (entity != null) db.Resources.DeleteOnSubmit(entity);
160      db.SubmitChanges();
161    }
162    #endregion
163
164    #region SlaveGroup Methods
165    public DT.SlaveGroup GetSlaveGroup(Guid id) {
166      return Convert.ToDto(db.Resources.OfType<SlaveGroup>().SingleOrDefault(x => x.ResourceId == id));
167    }
168
169    public IEnumerable<DT.SlaveGroup> GetSlaveGroups(Expression<Func<SlaveGroup, bool>> predicate) {
170      return db.Resources.OfType<SlaveGroup>().Where(predicate).Select(x => Convert.ToDto(x));
171    }
172
173    public Guid AddSlaveGroup(DT.SlaveGroup dto) {
174      var entity = Convert.ToEntity(dto);
175      db.Resources.InsertOnSubmit(entity);
176      db.SubmitChanges();
177      return entity.ResourceId;
178    }
179
180    public void UpdateSlaveGroup(DT.SlaveGroup dto) {
181      var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == dto.Id);
182      if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
183      else Convert.ToEntity(dto, entity);
184      db.SubmitChanges();
185    }
186
187    public void DeleteSlaveGroup(Guid id) {
188      var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == id);
189      if (entity != null) db.Resources.DeleteOnSubmit(entity);
190      db.SubmitChanges();
191    }
192    #endregion
193
194    #region Resource Methods
195    public DT.Resource GetResource(Guid id) {
196      return Convert.ToDto(db.Resources.SingleOrDefault(x => x.ResourceId == id));
197    }
198
199    public IEnumerable<DT.Resource> GetResources(Expression<Func<Resource, bool>> predicate) {
200      return db.Resources.Where(predicate).Select(x => Convert.ToDto(x));
201    }
202
203    public Guid AddResource(DT.Resource dto) {
204      var entity = Convert.ToEntity(dto);
205      db.Resources.InsertOnSubmit(entity);
206      db.SubmitChanges();
207      return entity.ResourceId;
208    }
209
210    public void UpdateResource(DT.Resource dto) {
211      var entity = db.Resources.FirstOrDefault(x => x.ResourceId == dto.Id);
212      if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
213      else Convert.ToEntity(dto, entity);
214      db.SubmitChanges();
215    }
216
217    public void DeleteResource(Guid id) {
218      var entity = db.Resources.FirstOrDefault(x => x.ResourceId == id);
219      if (entity != null) db.Resources.DeleteOnSubmit(entity);
220      db.SubmitChanges();
221    }
222    #endregion
223
224    #region Authorization Methods
225    public bool IsUserAuthorizedForJobs(Guid userId, params Guid[] jobIds) {
226      var userIds = from job in db.Jobs // this needs to be fast!
227                    where jobIds.Contains(job.JobId)
228                    select job.UserId;
229      return userIds.All(x => x == userId);
230    }
231    #endregion
232
233
234  }
235}
Note: See TracBrowser for help on using the repository browser.