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