Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveProjectManagement/HeuristicLab.Services.Hive.DataAccess/3.3/Manager/PersistenceManager.cs @ 15632

Last change on this file since 15632 was 15552, checked in by jzenisek, 7 years ago

#2839 worked on permission checks in listing methods

File size: 9.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
22using System;
23using System.Data.Linq;
24using System.Linq;
25using System.Transactions;
26using HeuristicLab.Services.Hive.DataAccess.Daos;
27using HeuristicLab.Services.Hive.DataAccess.Daos.HiveStatistics;
28using HeuristicLab.Services.Hive.DataAccess.Data;
29using HeuristicLab.Services.Hive.DataAccess.Interfaces;
30
31namespace HeuristicLab.Services.Hive.DataAccess.Manager {
32  public class PersistenceManager : IPersistenceManager {
33    private readonly DataContext dataContext;
34    public DataContext DataContext {
35      get { return dataContext; }
36    }
37
38    #region Hive daos
39
40    private AssignedTaskResourceDao assignedTaskResourceDao;
41    public AssignedTaskResourceDao AssignedTaskResourceDao {
42      get { return assignedTaskResourceDao ?? (assignedTaskResourceDao = new AssignedTaskResourceDao(dataContext)); }
43    }
44
45    private AssignedJobResourceDao assignedJobResourceDao;
46    public AssignedJobResourceDao AssignedJobResourceDao {
47      get { return assignedJobResourceDao ?? (assignedJobResourceDao = new AssignedJobResourceDao(dataContext)); }
48    }
49
50    private AssignedProjectResourceDao assignedProjectResourceDao;
51    public AssignedProjectResourceDao AssignedProjectResourceDao {
52      get { return assignedProjectResourceDao ?? (assignedProjectResourceDao = new AssignedProjectResourceDao(dataContext)); }
53    }
54
55    private DowntimeDao downtimeDao;
56    public DowntimeDao DowntimeDao {
57      get { return downtimeDao ?? (downtimeDao = new DowntimeDao(dataContext)); }
58    }
59
60    private JobDao jobDao;
61    public JobDao JobDao {
62      get { return jobDao ?? (jobDao = new JobDao(dataContext)); }
63    }
64
65    private JobPermissionDao jobPermissionDao;
66    public JobPermissionDao JobPermissionDao {
67      get { return jobPermissionDao ?? (jobPermissionDao = new JobPermissionDao(dataContext)); }
68    }
69
70    private LifecycleDao lifecycleDao;
71    public LifecycleDao LifecycleDao {
72      get { return lifecycleDao ?? (lifecycleDao = new LifecycleDao(dataContext)); }
73    }
74
75    private PluginDao pluginDao;
76    public PluginDao PluginDao {
77      get { return pluginDao ?? (pluginDao = new PluginDao(dataContext)); }
78    }
79
80    private PluginDataDao pluginDataDao;
81    public PluginDataDao PluginDataDao {
82      get { return pluginDataDao ?? (pluginDataDao = new PluginDataDao(dataContext)); }
83    }
84
85    private ProjectDao projectDao;
86    public ProjectDao ProjectDao {
87      get { return projectDao ?? (projectDao = new ProjectDao(dataContext)); }
88    }
89
90    private ProjectPermissionDao projectPermissionDao;
91    public ProjectPermissionDao ProjectPermissionDao {
92      get { return projectPermissionDao ?? (projectPermissionDao = new ProjectPermissionDao(dataContext)); }
93    }
94
95    private RequiredPluginDao requiredPluginDao;
96    public RequiredPluginDao RequiredPluginDao {
97      get { return requiredPluginDao ?? (requiredPluginDao = new RequiredPluginDao(dataContext)); }
98    }
99
100    private ResourceDao resourceDao;
101    public ResourceDao ResourceDao {
102      get { return resourceDao ?? (resourceDao = new ResourceDao(dataContext)); }
103    }
104
105    private SlaveDao slaveDao;
106    public SlaveDao SlaveDao {
107      get { return slaveDao ?? (slaveDao = new SlaveDao(dataContext)); }
108    }
109
110    private SlaveGroupDao slaveGroupDao;
111    public SlaveGroupDao SlaveGroupDao {
112      get { return slaveGroupDao ?? (slaveGroupDao = new SlaveGroupDao(dataContext)); }
113    }
114
115    private StateLogDao stateLogDao;
116    public StateLogDao StateLogDao {
117      get { return stateLogDao ?? (stateLogDao = new StateLogDao(dataContext)); }
118    }
119
120    private TaskDao taskDao;
121    public TaskDao TaskDao {
122      get { return taskDao ?? (taskDao = new TaskDao(dataContext)); }
123    }
124
125    private TaskDataDao taskDataDao;
126    public TaskDataDao TaskDataDao {
127      get { return taskDataDao ?? (taskDataDao = new TaskDataDao(dataContext)); }
128    }
129
130    private UserPriorityDao userPriorityDao;
131    public UserPriorityDao UserPriorityDao {
132      get { return userPriorityDao ?? (userPriorityDao = new UserPriorityDao(dataContext)); }
133    }
134    #endregion
135
136    #region HiveStatistics daos
137
138    private DimClientDao dimClientDao;
139    public DimClientDao DimClientDao {
140      get { return dimClientDao ?? (dimClientDao = new DimClientDao(dataContext)); }
141    }
142
143    private DimJobDao dimJobDao;
144    public DimJobDao DimJobDao {
145      get { return dimJobDao ?? (dimJobDao = new DimJobDao(dataContext)); }
146    }
147
148    private DimTimeDao dimTimeDao;
149    public DimTimeDao DimTimeDao {
150      get { return dimTimeDao ?? (dimTimeDao = new DimTimeDao(dataContext)); }
151    }
152
153    private DimUserDao dimUserDao;
154    public DimUserDao DimUserDao {
155      get { return dimUserDao ?? (dimUserDao = new DimUserDao(dataContext)); }
156    }
157
158    private FactClientInfoDao factClientInfoDao;
159    public FactClientInfoDao FactClientInfoDao {
160      get { return factClientInfoDao ?? (factClientInfoDao = new FactClientInfoDao(dataContext)); }
161    }
162
163    private FactTaskDao factTaskDao;
164    public FactTaskDao FactTaskDao {
165      get { return factTaskDao ?? (factTaskDao = new FactTaskDao(dataContext)); }
166    }
167    #endregion
168
169    public PersistenceManager(bool longRunning = false) {
170      var context = new HiveDataContext(Settings.Default.HeuristicLab_Hive_LinqConnectionString);
171      if (longRunning) context.CommandTimeout = (int)Settings.Default.LongRunningDatabaseCommandTimeout.TotalSeconds;
172      dataContext = context;
173    }
174
175    public PersistenceManager(DataContext dataContext) {
176      this.dataContext = dataContext;
177    }
178
179    #region Transaction management
180    public void UseTransaction(Action call, bool repeatableRead = false, bool longRunning = false) {
181      UseTransaction<object>(() => {
182        call();
183        return null;
184      });
185    }
186
187    public T UseTransaction<T>(Func<T> call, bool repeatableRead = false, bool longRunning = false) {
188      int n = 10;
189      while (n > 0) {
190        TransactionScope transaction = CreateTransaction(repeatableRead, longRunning);
191        try {
192          T result = call();
193          transaction.Complete();
194          return result;
195        }
196        catch (System.Data.SqlClient.SqlException e) {
197          n--; // probably deadlock situation, let it roll back and repeat the transaction n times
198          LogFactory.GetLogger(typeof(TransactionManager).Namespace).Log(string.Format("Exception occured, repeating transaction {0} more times. Details: {1}", n, e.ToString()));
199          if (n <= 0) throw;
200        }
201        finally {
202          transaction.Dispose();
203        }
204      }
205      throw new Exception("Transaction couldn't be completed.");
206    }
207
208    private static TransactionScope CreateTransaction(bool repeatableRead, bool longRunning) {
209      var options = new TransactionOptions {
210        IsolationLevel = repeatableRead ? IsolationLevel.RepeatableRead : IsolationLevel.ReadUncommitted
211      };
212      if (longRunning) {
213        options.Timeout = Settings.Default.LongRunningDatabaseCommandTimeout;
214      }
215      return new TransactionScope(TransactionScopeOption.Required, options);
216    }
217    #endregion
218
219    public TableInformation GetTableInformation(string table) {
220      string query = string.Format("sp_spaceused '{0}'", table);
221      var result = dataContext.ExecuteQuery<SqlServerTableInformation>(query).FirstOrDefault();
222      if (result == null) return null;
223      return new TableInformation {
224        Name = result.Name,
225        Rows = int.Parse(result.Rows.Remove(result.Rows.IndexOf(' '))),
226        Reserved = int.Parse(result.Reserved.Remove(result.Reserved.IndexOf(' '))),
227        Data = int.Parse(result.Data.Remove(result.Data.IndexOf(' '))),
228        IndexSize = int.Parse(result.Index_Size.Remove(result.Index_Size.IndexOf(' '))),
229        Unused = int.Parse(result.Unused.Remove(result.Unused.IndexOf(' ')))
230      };
231    }
232
233    public void SubmitChanges() {
234      if (dataContext != null) {
235        dataContext.SubmitChanges();
236      }
237    }
238
239    public void Dispose() {
240      if (dataContext != null) {
241        dataContext.Dispose();
242      }
243    }
244
245    private class SqlServerTableInformation {
246      public string Name { get; set; }
247      public string Rows { get; set; }
248      public string Reserved { get; set; }
249      public string Data { get; set; }
250      public string Index_Size { get; set; } // naming of sp_spaceused...
251      public string Unused { get; set; }
252    }
253  }
254}
Note: See TracBrowser for help on using the repository browser.