1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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.Data.Linq;
|
---|
25 | using System.Linq;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Services.Hive.DataAccess.Daos {
|
---|
28 | public class JobDao : GenericDao<Guid, Job> {
|
---|
29 | public JobDao(DataContext dataContext) : base(dataContext) { }
|
---|
30 |
|
---|
31 | public override Job GetById(Guid id) {
|
---|
32 | return GetByIdQuery(DataContext, id);
|
---|
33 | }
|
---|
34 |
|
---|
35 | public void DeleteByState(JobState state) {
|
---|
36 | DataContext.ExecuteCommand(DeleteByStateQueryString, Enum.GetName(typeof(JobState), state));
|
---|
37 | }
|
---|
38 |
|
---|
39 | public IEnumerable<Job> GetByProjectId(Guid id) {
|
---|
40 | return GetByProjectIdQuery(DataContext, id);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public IEnumerable<Job> GetByProjectIds(IEnumerable<Guid> projectIds) {
|
---|
44 | string paramProjectIds = string.Join(",", projectIds.ToList().Select(x => string.Format("'{0}'", x)));
|
---|
45 | if(!string.IsNullOrWhiteSpace(paramProjectIds)) {
|
---|
46 | string queryString = string.Format(GetByProjectIdsQueryString, paramProjectIds);
|
---|
47 | return DataContext.ExecuteQuery<Job>(queryString);
|
---|
48 | }
|
---|
49 | return Enumerable.Empty<Job>();
|
---|
50 | }
|
---|
51 |
|
---|
52 | public IEnumerable<Job> GetByState(JobState state) {
|
---|
53 | return GetByStateQuery(DataContext, state);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public IEnumerable<Guid> GetJobIdsByState(JobState state) {
|
---|
57 | return GetJobIdsByStateQuery(DataContext, state);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public IEnumerable<Job> GetJobsReadyForDeletion() {
|
---|
61 | return GetJobsReadyForDeletionQuery(DataContext);
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | #region Compiled queries
|
---|
66 | private static readonly Func<DataContext, Guid, Job> GetByIdQuery =
|
---|
67 | CompiledQuery.Compile((DataContext db, Guid jobId) =>
|
---|
68 | (from job in db.GetTable<Job>()
|
---|
69 | where job.JobId == jobId
|
---|
70 | select job).SingleOrDefault());
|
---|
71 | private static readonly Func<DataContext, Guid, IEnumerable<Job>> GetByProjectIdQuery =
|
---|
72 | CompiledQuery.Compile((DataContext db, Guid projectId) =>
|
---|
73 | (from job in db.GetTable<Job>()
|
---|
74 | where job.ProjectId == projectId
|
---|
75 | select job));
|
---|
76 | private static readonly Func<DataContext, JobState, IEnumerable<Job>> GetByStateQuery =
|
---|
77 | CompiledQuery.Compile((DataContext db, JobState jobState) =>
|
---|
78 | (from job in db.GetTable<Job>()
|
---|
79 | where job.State == jobState
|
---|
80 | select job));
|
---|
81 | private static readonly Func<DataContext, JobState, IEnumerable<Guid>> GetJobIdsByStateQuery =
|
---|
82 | CompiledQuery.Compile((DataContext db, JobState jobState) =>
|
---|
83 | (from job in db.GetTable<Job>()
|
---|
84 | where job.State == jobState
|
---|
85 | select job.JobId));
|
---|
86 | private static readonly Func<DataContext, IEnumerable<Job>> GetJobsReadyForDeletionQuery =
|
---|
87 | CompiledQuery.Compile((DataContext db) =>
|
---|
88 | (from job in db.GetTable<Job>()
|
---|
89 | where job.State == JobState.StatisticsPending
|
---|
90 | && (from task in db.GetTable<Task>()
|
---|
91 | where task.JobId == job.JobId
|
---|
92 | select task).All(x => x.State == TaskState.Finished
|
---|
93 | || x.State == TaskState.Aborted
|
---|
94 | || x.State == TaskState.Failed)
|
---|
95 | select job));
|
---|
96 | #endregion
|
---|
97 |
|
---|
98 | #region String queries
|
---|
99 | private const string DeleteByStateQueryString = @"
|
---|
100 | DELETE FROM [Job]
|
---|
101 | WHERE JobState = {0}
|
---|
102 | ";
|
---|
103 | private const string GetStatisticsPendingJobs = @"
|
---|
104 | SELECT DISTINCT j.*
|
---|
105 | FROM [Job] j
|
---|
106 | WHERE j.JobState = 'StatisticsPending'
|
---|
107 | AND 'Calculating' NOT IN (
|
---|
108 | SELECT t.TaskState
|
---|
109 | FROM [Task] t
|
---|
110 | WHERE t.JobId = j.JobId)
|
---|
111 | ";
|
---|
112 | private const string GetByProjectIdsQueryString = @"
|
---|
113 | SELECT DISTINCT j.*
|
---|
114 | FROM [Job] j
|
---|
115 | WHERE j.ProjectId IN ({0})
|
---|
116 | ";
|
---|
117 | #endregion
|
---|
118 | }
|
---|
119 | }
|
---|