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