source: branches/2931_OR-Tools_LP_MIP/HeuristicLab.Services.Hive.DataAccess/3.3/Manager/PersistenceManager.cs @ 16139

Last change on this file since 16139 was 16139, checked in by ddorfmei, 12 months ago

#2931: Merged [16046-16138/trunk] into branch

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