Free cookie consent management tool by TermsFeed Policy Generator

source: brances/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/SlaveDao.cs @ 15887

Last change on this file since 15887 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 5.0 KB
Line 
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
23using System;
24using System.Collections.Generic;
25using System.Data.Linq;
26using System.Linq;
27using System.Linq.Expressions;
28using HeuristicLab.Services.Hive.DataAccess.Interfaces;
29
30namespace HeuristicLab.Services.Hive.DataAccess.Daos {
31  public class SlaveDao : IGenericDao<Guid, Slave> {
32
33    private readonly DataContext dataContext;
34    private Table<Resource> Table {
35      get { return dataContext.GetTable<Resource>(); }
36    }
37
38    private IQueryable<Slave> Entities {
39      get { return Table.OfType<Slave>(); }
40    }
41
42    public SlaveDao(DataContext dataContext) {
43      this.dataContext = dataContext;
44    }
45
46    #region IGenericDao<Guid,Slave> Members
47    public Slave GetById(Guid id) {
48      return GetByIdQuery(dataContext, id);
49    }
50
51    public IQueryable<Slave> Get(Expression<Func<Slave, bool>> predicate) {
52      return Entities.Where(predicate);
53    }
54
55    public IQueryable<Slave> GetAll() {
56      return Entities;
57    }
58
59    public IQueryable<Slave> GetOnlineSlaves() {
60      return Entities.Where(x => x.SlaveState != SlaveState.Offline);
61    }
62
63    public Slave Save(Slave entity) {
64      Table.InsertOnSubmit(entity);
65      return entity;
66    }
67
68    public IEnumerable<Slave> Save(params Slave[] entities) {
69      return entities.Select(Save).ToList();
70    }
71
72    public IEnumerable<Slave> Save(IEnumerable<Slave> entities) {
73      return entities.Select(Save).ToList();
74    }
75
76    public Slave SaveOrAttach(Slave entity) {
77      if (Table.Contains(entity)) {
78        if (Table.GetOriginalEntityState(entity) == null) {
79          Table.Attach(entity);
80        }
81      } else {
82        Table.InsertOnSubmit(entity);
83      }
84      return entity;
85    }
86
87    public IEnumerable<Slave> SaveOrAttach(params Slave[] entities) {
88      return entities.Select(SaveOrAttach).ToList();
89    }
90
91    public IEnumerable<Slave> SaveOrAttach(IEnumerable<Slave> entities) {
92      return entities.Select(SaveOrAttach).ToList();
93    }
94
95    public void Delete(Guid id) {
96      Slave entity = GetById(id);
97      if (entity != null) {
98        Table.DeleteOnSubmit(entity);
99      }
100    }
101
102    public void Delete(IEnumerable<Guid> ids) {
103      foreach (var id in ids) {
104        Delete(id);
105      }
106    }
107
108    public void Delete(Slave entity) {
109      Table.DeleteOnSubmit(entity);
110    }
111
112    public void Delete(IEnumerable<Slave> entities) {
113      foreach (var entity in entities) {
114        Delete(entity);
115      }
116    }
117
118    public bool Exists(Slave entity) {
119      return ExistsQuery(dataContext, entity);
120    }
121
122    public bool Exists(Guid id) {
123      return GetById(id) != null;
124    }
125    #endregion
126
127    public bool SlaveHasToShutdownComputer(Guid slaveId) {
128      return dataContext.ExecuteQuery<int>(DowntimeQueryString, slaveId, DateTime.Now, DowntimeType.Shutdown.ToString()).FirstOrDefault() > 0;
129    }
130
131    public bool SlaveIsAllowedToCalculate(Guid slaveId) {
132      return dataContext.ExecuteQuery<int>(DowntimeQueryString, slaveId, DateTime.Now, DowntimeType.Offline.ToString()).FirstOrDefault() == 0;
133    }
134
135    #region Compiled queries
136    private static readonly Func<DataContext, Guid, Slave> GetByIdQuery =
137      CompiledQuery.Compile((DataContext db, Guid slaveId) =>
138        (from slave in db.GetTable<Resource>().OfType<Slave>()
139         where slave.ResourceId == slaveId
140         select slave).SingleOrDefault());
141
142    private static readonly Func<DataContext, Slave, bool> ExistsQuery =
143      CompiledQuery.Compile((DataContext db, Slave entity) =>
144        db.GetTable<Resource>().OfType<Slave>().Contains(entity));
145    #endregion
146
147    #region String queries
148    private const string DowntimeQueryString = @"
149      WITH pr AS (
150        SELECT ResourceId, ParentResourceId
151        FROM [Resource]
152        WHERE ResourceId = {0}
153        UNION ALL
154        SELECT r.ResourceId, r.ParentResourceId
155        FROM [Resource] r JOIN pr ON r.ResourceId = pr.ParentResourceId
156      )
157      SELECT COUNT(dt.DowntimeId)
158      FROM pr JOIN [Downtime] dt ON pr.ResourceId = dt.ResourceId
159      WHERE {1} BETWEEN dt.StartDate AND dt.EndDate
160        AND dt.DowntimeType = {2}
161    ";
162    #endregion
163  }
164}
Note: See TracBrowser for help on using the repository browser.