Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Services.Hive.DataAccess/3.3/Manager/PersistenceManager.cs @ 15962

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

#2640: Updated year of copyrights in license headers

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