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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Linq.Expressions;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Services.Hive.DataAccess {
|
---|
28 | using HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
29 | using HeuristicLab.Services.Hive.DataAccess.Properties;
|
---|
30 | using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
31 |
|
---|
32 | public class HiveDao : IHiveDao {
|
---|
33 | public static HiveDataContext CreateContext() {
|
---|
34 | return new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
|
---|
35 | }
|
---|
36 |
|
---|
37 | public HiveDao() {
|
---|
38 | }
|
---|
39 |
|
---|
40 | #region Job Methods
|
---|
41 | public DT.Job GetJob(Guid id) {
|
---|
42 | using (var db = CreateContext()) {
|
---|
43 | return Convert.ToDto(db.Jobs.SingleOrDefault(x => x.JobId == id));
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public IEnumerable<DT.Job> GetJobs(Expression<Func<Job, bool>> predicate) {
|
---|
48 | using (var db = CreateContext()) {
|
---|
49 | return db.Jobs.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public Guid AddJob(DT.Job dto) {
|
---|
54 | using (var db = CreateContext()) {
|
---|
55 | var entity = Convert.ToEntity(dto);
|
---|
56 | db.Jobs.InsertOnSubmit(entity);
|
---|
57 | db.SubmitChanges();
|
---|
58 | foreach (Guid pluginId in dto.PluginsNeededIds) {
|
---|
59 | db.RequiredPlugins.InsertOnSubmit(new RequiredPlugin() { JobId = entity.JobId, PluginId = pluginId });
|
---|
60 | }
|
---|
61 | db.SubmitChanges();
|
---|
62 | return entity.JobId;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void UpdateJob(DT.Job dto) {
|
---|
67 | using (var db = CreateContext()) {
|
---|
68 | var entity = db.Jobs.FirstOrDefault(x => x.JobId == dto.Id);
|
---|
69 | if (entity == null) db.Jobs.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
70 | else Convert.ToEntity(dto, entity);
|
---|
71 | // todo: update required plugins
|
---|
72 | db.SubmitChanges();
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void DeleteJob(Guid id) {
|
---|
77 | using (var db = CreateContext()) {
|
---|
78 | var entity = db.Jobs.FirstOrDefault(x => x.JobId == id);
|
---|
79 | if (entity != null) db.Jobs.DeleteOnSubmit(entity);
|
---|
80 | db.SubmitChanges(); // JobData and child jobs are deleted by db-trigger
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | public IEnumerable<DT.Job> GetWaitingParentJobs(Guid slaveId) {
|
---|
85 | using (var db = CreateContext()) {
|
---|
86 | // todo: slaveId is unused!
|
---|
87 | var query = from ar in db.AssignedResources
|
---|
88 | where ar.Job.JobState == JobState.WaitingForChildJobs &&
|
---|
89 | (from child in db.Jobs
|
---|
90 | where child.ParentJobId == ar.Job.JobId
|
---|
91 | select child.JobState == JobState.Finished).All(x => x) &&
|
---|
92 | (from child in db.Jobs // avoid returning WaitForChildJobs jobs where no child-jobs exist (yet)
|
---|
93 | where child.ParentJobId == ar.Job.JobId
|
---|
94 | select child).Count() > 0
|
---|
95 | orderby ar.Job.Priority descending
|
---|
96 | select Convert.ToDto(ar.Job);
|
---|
97 | return query.ToArray();
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | public IEnumerable<DT.Job> GetWaitingJobs(DT.Slave slave) {
|
---|
102 | using (var db = CreateContext()) {
|
---|
103 | var query = from j in db.Jobs
|
---|
104 | where j.JobState == JobState.Waiting && j.CoresNeeded <= slave.FreeCores && j.MemoryNeeded <= slave.FreeMemory
|
---|
105 | orderby j.Priority descending
|
---|
106 | select Convert.ToDto(j);
|
---|
107 | var waitingJobs = query.ToArray();
|
---|
108 | var waitingParentJobs = GetWaitingParentJobs(slave.Id);
|
---|
109 | return waitingJobs.Union(waitingParentJobs).OrderByDescending(x => x.Priority);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | #endregion
|
---|
113 |
|
---|
114 | #region JobData Methods
|
---|
115 |
|
---|
116 | public DT.JobData GetJobData(Guid id) {
|
---|
117 | using (var db = CreateContext()) {
|
---|
118 | return Convert.ToDto(db.JobDatas.SingleOrDefault(x => x.JobId == id));
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | public IEnumerable<DT.JobData> GetJobDatas(Expression<Func<JobData, bool>> predicate) {
|
---|
123 | using (var db = CreateContext()) {
|
---|
124 | return db.JobDatas.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | public Guid AddJobData(DT.JobData dto) {
|
---|
129 | using (var db = CreateContext()) {
|
---|
130 | var entity = Convert.ToEntity(dto);
|
---|
131 | db.JobDatas.InsertOnSubmit(entity);
|
---|
132 | db.SubmitChanges();
|
---|
133 | return entity.JobId;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | public void UpdateJobData(DT.JobData dto) {
|
---|
138 | using (var db = CreateContext()) {
|
---|
139 | var entity = db.JobDatas.FirstOrDefault(x => x.JobId == dto.JobId);
|
---|
140 | if (entity == null) db.JobDatas.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
141 | else Convert.ToEntity(dto, entity);
|
---|
142 | db.SubmitChanges();
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | public void DeleteJobData(Guid id) {
|
---|
147 | using (var db = CreateContext()) {
|
---|
148 | 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
|
---|
149 | if (entity != null) db.JobDatas.DeleteOnSubmit(entity);
|
---|
150 | db.SubmitChanges();
|
---|
151 | }
|
---|
152 | }
|
---|
153 | #endregion
|
---|
154 |
|
---|
155 | #region HiveExperiment Methods
|
---|
156 | public DT.HiveExperiment GetHiveExperiment(Guid id) {
|
---|
157 | using (var db = CreateContext()) {
|
---|
158 | return Convert.ToDto(db.HiveExperiments.SingleOrDefault(x => x.HiveExperimentId == id));
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | public IEnumerable<DT.HiveExperiment> GetHiveExperiments(Expression<Func<HiveExperiment, bool>> predicate) {
|
---|
163 | using (var db = CreateContext()) {
|
---|
164 | return db.HiveExperiments.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | public Guid AddHiveExperiment(DT.HiveExperiment dto) {
|
---|
169 | using (var db = CreateContext()) {
|
---|
170 | var entity = Convert.ToEntity(dto);
|
---|
171 | db.HiveExperiments.InsertOnSubmit(entity);
|
---|
172 | db.SubmitChanges();
|
---|
173 | return entity.HiveExperimentId;
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | public void UpdateHiveExperiment(DT.HiveExperiment dto) {
|
---|
178 | using (var db = CreateContext()) {
|
---|
179 | var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == dto.Id);
|
---|
180 | if (entity == null) db.HiveExperiments.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
181 | else Convert.ToEntity(dto, entity);
|
---|
182 | db.SubmitChanges();
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | public void DeleteHiveExperiment(Guid id) {
|
---|
187 | using (var db = CreateContext()) {
|
---|
188 | var entity = db.HiveExperiments.FirstOrDefault(x => x.HiveExperimentId == id);
|
---|
189 | if (entity != null) db.HiveExperiments.DeleteOnSubmit(entity);
|
---|
190 | db.SubmitChanges();
|
---|
191 | }
|
---|
192 | }
|
---|
193 | #endregion
|
---|
194 |
|
---|
195 | #region Plugin Methods
|
---|
196 | public DT.Plugin GetPlugin(Guid id) {
|
---|
197 | using (var db = CreateContext()) {
|
---|
198 | return Convert.ToDto(db.Plugins.SingleOrDefault(x => x.PluginId == id));
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | public IEnumerable<DT.Plugin> GetPlugins(Expression<Func<Plugin, bool>> predicate) {
|
---|
203 | using (var db = CreateContext()) {
|
---|
204 | return db.Plugins.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | public Guid AddPlugin(DT.Plugin dto) {
|
---|
209 | using (var db = CreateContext()) {
|
---|
210 | var entity = Convert.ToEntity(dto);
|
---|
211 | db.Plugins.InsertOnSubmit(entity);
|
---|
212 | db.SubmitChanges();
|
---|
213 | return entity.PluginId;
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | public void UpdatePlugin(DT.Plugin dto) {
|
---|
218 | using (var db = CreateContext()) {
|
---|
219 | var entity = db.Plugins.FirstOrDefault(x => x.PluginId == dto.Id);
|
---|
220 | if (entity == null) db.Plugins.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
221 | else Convert.ToEntity(dto, entity);
|
---|
222 | db.SubmitChanges();
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | public void DeletePlugin(Guid id) {
|
---|
227 | using (var db = CreateContext()) {
|
---|
228 | var entity = db.Plugins.FirstOrDefault(x => x.PluginId == id);
|
---|
229 | if (entity != null) db.Plugins.DeleteOnSubmit(entity);
|
---|
230 | db.SubmitChanges();
|
---|
231 | }
|
---|
232 | }
|
---|
233 | #endregion
|
---|
234 |
|
---|
235 | #region PluginData Methods
|
---|
236 |
|
---|
237 | public DT.PluginData GetPluginData(Guid id) {
|
---|
238 | using (var db = CreateContext()) {
|
---|
239 | return Convert.ToDto(db.PluginDatas.SingleOrDefault(x => x.PluginDataId == id));
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | public IEnumerable<DT.PluginData> GetPluginDatas(Expression<Func<PluginData, bool>> predicate) {
|
---|
244 | using (var db = CreateContext()) {
|
---|
245 | return db.PluginDatas.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | public Guid AddPluginData(DT.PluginData dto) {
|
---|
250 | using (var db = CreateContext()) {
|
---|
251 | var entity = Convert.ToEntity(dto);
|
---|
252 | db.PluginDatas.InsertOnSubmit(entity);
|
---|
253 | db.SubmitChanges();
|
---|
254 | return entity.PluginDataId;
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | public void UpdatePluginData(DT.PluginData dto) {
|
---|
259 | using (var db = CreateContext()) {
|
---|
260 | var entity = db.PluginDatas.FirstOrDefault(x => x.PluginId == dto.PluginId);
|
---|
261 | if (entity == null) db.PluginDatas.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
262 | else Convert.ToEntity(dto, entity);
|
---|
263 | db.SubmitChanges();
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | public void DeletePluginData(Guid id) {
|
---|
268 | using (var db = CreateContext()) {
|
---|
269 | var entity = db.PluginDatas.FirstOrDefault(x => x.PluginDataId == id); // todo: check if all the byte[] is loaded into memory here. otherwise work around to delete without loading it
|
---|
270 | if (entity != null) db.PluginDatas.DeleteOnSubmit(entity);
|
---|
271 | db.SubmitChanges();
|
---|
272 | }
|
---|
273 | }
|
---|
274 | #endregion
|
---|
275 |
|
---|
276 | #region Slave Methods
|
---|
277 | public DT.Slave GetSlave(Guid id) {
|
---|
278 | using (var db = CreateContext()) {
|
---|
279 | return Convert.ToDto(db.Resources.OfType<Slave>().SingleOrDefault(x => x.ResourceId == id));
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | public IEnumerable<DT.Slave> GetSlaves(Expression<Func<Slave, bool>> predicate) {
|
---|
284 | using (var db = CreateContext()) {
|
---|
285 | return db.Resources.OfType<Slave>().Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | public Guid AddSlave(DT.Slave dto) {
|
---|
290 | using (var db = CreateContext()) {
|
---|
291 | var entity = Convert.ToEntity(dto);
|
---|
292 | db.Resources.InsertOnSubmit(entity);
|
---|
293 | db.SubmitChanges();
|
---|
294 | return entity.ResourceId;
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | public void UpdateSlave(DT.Slave dto) {
|
---|
299 | using (var db = CreateContext()) {
|
---|
300 | var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == dto.Id);
|
---|
301 | if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
302 | else Convert.ToEntity(dto, entity);
|
---|
303 | db.SubmitChanges();
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | public void DeleteSlave(Guid id) {
|
---|
308 | using (var db = CreateContext()) {
|
---|
309 | var entity = db.Resources.OfType<Slave>().FirstOrDefault(x => x.ResourceId == id);
|
---|
310 | if (entity != null) db.Resources.DeleteOnSubmit(entity);
|
---|
311 | db.SubmitChanges();
|
---|
312 | }
|
---|
313 | }
|
---|
314 | #endregion
|
---|
315 |
|
---|
316 | #region SlaveGroup Methods
|
---|
317 | public DT.SlaveGroup GetSlaveGroup(Guid id) {
|
---|
318 | using (var db = CreateContext()) {
|
---|
319 | return Convert.ToDto(db.Resources.OfType<SlaveGroup>().SingleOrDefault(x => x.ResourceId == id));
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | public IEnumerable<DT.SlaveGroup> GetSlaveGroups(Expression<Func<SlaveGroup, bool>> predicate) {
|
---|
324 | using (var db = CreateContext()) {
|
---|
325 | return db.Resources.OfType<SlaveGroup>().Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | public Guid AddSlaveGroup(DT.SlaveGroup dto) {
|
---|
330 | using (var db = CreateContext()) {
|
---|
331 | if (dto.Id == Guid.Empty)
|
---|
332 | dto.Id = Guid.NewGuid();
|
---|
333 | var entity = Convert.ToEntity(dto);
|
---|
334 | db.Resources.InsertOnSubmit(entity);
|
---|
335 | db.SubmitChanges();
|
---|
336 | return entity.ResourceId;
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | public void UpdateSlaveGroup(DT.SlaveGroup dto) {
|
---|
341 | using (var db = CreateContext()) {
|
---|
342 | var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == dto.Id);
|
---|
343 | if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
344 | else Convert.ToEntity(dto, entity);
|
---|
345 | db.SubmitChanges();
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | public void DeleteSlaveGroup(Guid id) {
|
---|
350 | using (var db = CreateContext()) {
|
---|
351 | var entity = db.Resources.OfType<SlaveGroup>().FirstOrDefault(x => x.ResourceId == id);
|
---|
352 | if (entity != null) db.Resources.DeleteOnSubmit(entity);
|
---|
353 | db.SubmitChanges();
|
---|
354 | }
|
---|
355 | }
|
---|
356 | #endregion
|
---|
357 |
|
---|
358 | #region Resource Methods
|
---|
359 | public DT.Resource GetResource(Guid id) {
|
---|
360 | using (var db = CreateContext()) {
|
---|
361 | return Convert.ToDto(db.Resources.SingleOrDefault(x => x.ResourceId == id));
|
---|
362 | }
|
---|
363 | }
|
---|
364 |
|
---|
365 | public IEnumerable<DT.Resource> GetResources(Expression<Func<Resource, bool>> predicate) {
|
---|
366 | using (var db = CreateContext()) {
|
---|
367 | return db.Resources.Where(predicate).Select(x => Convert.ToDto(x)).ToArray();
|
---|
368 | }
|
---|
369 | }
|
---|
370 |
|
---|
371 | public Guid AddResource(DT.Resource dto) {
|
---|
372 | using (var db = CreateContext()) {
|
---|
373 | var entity = Convert.ToEntity(dto);
|
---|
374 | db.Resources.InsertOnSubmit(entity);
|
---|
375 | db.SubmitChanges();
|
---|
376 | return entity.ResourceId;
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | public void UpdateResource(DT.Resource dto) {
|
---|
381 | using (var db = CreateContext()) {
|
---|
382 | var entity = db.Resources.FirstOrDefault(x => x.ResourceId == dto.Id);
|
---|
383 | if (entity == null) db.Resources.InsertOnSubmit(Convert.ToEntity(dto));
|
---|
384 | else Convert.ToEntity(dto, entity);
|
---|
385 | db.SubmitChanges();
|
---|
386 | }
|
---|
387 | }
|
---|
388 |
|
---|
389 | public void DeleteResource(Guid id) {
|
---|
390 | using (var db = CreateContext()) {
|
---|
391 | var entity = db.Resources.FirstOrDefault(x => x.ResourceId == id);
|
---|
392 | if (entity != null) db.Resources.DeleteOnSubmit(entity);
|
---|
393 | db.SubmitChanges();
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | public void AssignJobToResource(Guid jobId, Guid resourceId) {
|
---|
398 | using (var db = CreateContext()) {
|
---|
399 | var job = db.Jobs.Where(x => x.JobId == jobId).Single();
|
---|
400 | job.AssignedResources.Add(new AssignedResource() { JobId = jobId, ResourceId = resourceId });
|
---|
401 | db.SubmitChanges();
|
---|
402 | }
|
---|
403 | }
|
---|
404 |
|
---|
405 | public IEnumerable<DT.Resource> GetAssignedResources(Guid jobId) {
|
---|
406 | using (var db = CreateContext()) {
|
---|
407 | var job = db.Jobs.Where(x => x.JobId == jobId).Single();
|
---|
408 | return job.AssignedResources.Select(x => Convert.ToDto(x.Resource)).ToArray();
|
---|
409 | }
|
---|
410 | }
|
---|
411 | #endregion
|
---|
412 |
|
---|
413 | #region Authorization Methods
|
---|
414 | public bool IsUserAuthorizedForJobs(Guid userId, params Guid[] jobIds) {
|
---|
415 | using (var db = CreateContext()) {
|
---|
416 | var userIds = from job in db.Jobs // this needs to be fast!
|
---|
417 | where jobIds.Contains(job.JobId)
|
---|
418 | select job.UserId;
|
---|
419 | return userIds.All(x => x == userId);
|
---|
420 | }
|
---|
421 | }
|
---|
422 | #endregion
|
---|
423 | }
|
---|
424 | }
|
---|