Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.Hive.Server.ADODataAccess/3.2/JobAdapter.cs @ 6355

Last change on this file since 6355 was 2099, checked in by svonolfe, 15 years ago

Further avoided out of memory exceptions by updating the JobResult DAO (#372)

File size: 13.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Hive.Server.DataAccess;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using System.Linq.Expressions;
29using HeuristicLab.DataAccess.ADOHelper;
30using HeuristicLab.Hive.Server.ADODataAccess.dsHiveServerTableAdapters;
31using System.Data.Common;
32using System.Data.SqlClient;
33using HeuristicLab.Hive.Server.ADODataAccess.TableAdapterWrapper;
34
35namespace HeuristicLab.Hive.Server.ADODataAccess {
36  class JobAdapter :
37    DataAdapterBase<dsHiveServerTableAdapters.JobTableAdapter,
38                      Job,
39                      dsHiveServer.JobRow>,
40    IJobAdapter {
41    #region Fields
42    private ManyToManyRelationHelper<
43      dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
44      dsHiveServer.RequiredPluginsRow> pluginsManyToManyRelationHelper = null;
45
46    private ManyToManyRelationHelper<
47      dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
48      dsHiveServer.RequiredPluginsRow> PluginsManyToManyRelationHelper {
49      get {
50        if (pluginsManyToManyRelationHelper == null) {
51          pluginsManyToManyRelationHelper =
52            new ManyToManyRelationHelper<dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
53              dsHiveServer.RequiredPluginsRow>(new RequiredPluginsAdapterWrapper(), 1);
54        }
55
56        pluginsManyToManyRelationHelper.Session = Session as Session;
57
58        return pluginsManyToManyRelationHelper;
59      }
60    }
61
62    private ManyToManyRelationHelper<
63      dsHiveServerTableAdapters.AssignedResourcesTableAdapter,
64      dsHiveServer.AssignedResourcesRow> assignedManyToManyRelationHelper = null;
65
66    private ManyToManyRelationHelper<
67      dsHiveServerTableAdapters.AssignedResourcesTableAdapter,
68      dsHiveServer.AssignedResourcesRow> AssignedManyToManyRelationHelper {
69      get {
70        if (assignedManyToManyRelationHelper == null) {
71          assignedManyToManyRelationHelper =
72            new ManyToManyRelationHelper<dsHiveServerTableAdapters.AssignedResourcesTableAdapter,
73              dsHiveServer.AssignedResourcesRow>(new AssignedResourcesAdapterWrapper(), 0);
74        }
75
76        assignedManyToManyRelationHelper.Session = Session as Session;
77
78        return assignedManyToManyRelationHelper;
79      }
80    }
81
82    private IClientAdapter clientAdapter = null;
83
84    private IClientAdapter ClientAdapter {
85      get {
86        if (clientAdapter == null)
87          clientAdapter =
88            this.Session.GetDataAdapter<ClientInfo, IClientAdapter>();
89
90        return clientAdapter;
91      }
92    }
93
94    private IJobResultsAdapter resultsAdapter = null;
95
96    private IJobResultsAdapter ResultsAdapter {
97      get {
98        if (resultsAdapter == null) {
99          resultsAdapter =
100            this.Session.GetDataAdapter<JobResult, IJobResultsAdapter>();
101        }
102
103        return resultsAdapter;
104      }
105    }
106
107    private IPluginInfoAdapter pluginInfoAdapter = null;
108
109    private IPluginInfoAdapter PluginInfoAdapter {
110      get {
111        if (pluginInfoAdapter == null) {
112          pluginInfoAdapter =
113            this.Session.GetDataAdapter<HivePluginInfo, IPluginInfoAdapter>();
114        }
115
116        return pluginInfoAdapter;
117      }
118    }
119
120    private IProjectAdapter projectAdapter = null;
121
122    private IProjectAdapter ProjectAdapter {
123      get {
124        if (projectAdapter == null) {
125          projectAdapter =
126            this.Session.GetDataAdapter<Project, IProjectAdapter>();
127        }
128
129        return projectAdapter;
130      }
131    }
132    #endregion
133
134    public JobAdapter(): base(new JobAdapterWrapper()) {
135    }
136
137    #region Overrides
138    protected override Job ConvertRow(dsHiveServer.JobRow row,
139      Job job) {
140      if (row != null && job != null) {
141        job.Id = row.JobId;
142
143        if (!row.IsParentJobIdNull())
144          job.ParentJob = GetById(row.ParentJobId);
145        else
146          job.ParentJob = null;
147
148        if (!row.IsResourceIdNull())
149          job.Client = ClientAdapter.GetById(row.ResourceId);
150        else
151          job.Client = null;
152
153        if (!row.IsUserIdNull())
154          job.UserId = Guid.Empty;
155        else
156          job.UserId = Guid.Empty;
157       
158        if (!row.IsJobStateNull())
159          job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
160        else
161          job.State = State.nullState;
162
163        if (!row.IsPercentageNull())
164          job.Percentage = row.Percentage;
165        else
166          job.Percentage = 0.0;
167
168        if (!row.IsDateCreatedNull())
169          job.DateCreated = row.DateCreated;
170        else
171          job.DateCreated = DateTime.MinValue;
172
173        if (!row.IsDateCalculatedNull())
174          job.DateCalculated = row.DateCalculated;
175        else
176          job.DateCalculated = DateTime.MinValue;
177
178        if (!row.IsPriorityNull())
179          job.Priority = row.Priority;
180        else
181          job.Priority = default(int);
182
183        if (!row.IsCoresNeededNull())
184          job.CoresNeeded = row.CoresNeeded;
185        else
186          job.CoresNeeded = default(int);
187
188        if (!row.IsMemoryNeededNull())
189          job.MemoryNeeded = row.MemoryNeeded;
190        else
191          job.MemoryNeeded = default(int);
192
193        if (!row.IsProjectIdNull())
194          job.Project = ProjectAdapter.GetById(
195            row.ProjectId);
196        else
197          job.Project = null;
198
199        ICollection<Guid> requiredPlugins =
200          PluginsManyToManyRelationHelper.GetRelationships(job.Id);
201       
202        job.PluginsNeeded.Clear();
203        foreach (Guid requiredPlugin in requiredPlugins) {
204          HivePluginInfo pluginInfo =
205            PluginInfoAdapter.GetById(requiredPlugin);
206
207          job.PluginsNeeded.Add(pluginInfo);
208        }
209
210        job.AssignedResourceIds =
211          new List<Guid>(
212            AssignedManyToManyRelationHelper.GetRelationships(job.Id));
213
214        return job;
215      } else
216        return null;
217    }
218
219    protected override dsHiveServer.JobRow ConvertObj(Job job,
220      dsHiveServer.JobRow row) {
221      if (job != null && row != null) {
222        row.JobId = job.Id;
223       
224        if (job.Client != null) {
225          if (row.IsResourceIdNull()) {
226            row.ResourceId = job.Client.Id;
227          }
228        } else
229          row.SetResourceIdNull();
230
231        if (job.ParentJob != null) {
232          if (row.IsParentJobIdNull()) {
233            row.ParentJobId = job.ParentJob.Id;
234          }
235        } else
236          row.SetParentJobIdNull();
237
238        if (job.UserId != Guid.Empty) {
239          if (row.IsUserIdNull() ||
240           row.UserId != Guid.Empty) {
241            row.UserId = Guid.Empty;
242          }
243        } else
244          row.SetUserIdNull();
245
246        if (job.State != State.nullState)
247          row.JobState = job.State.ToString();
248        else
249          row.SetJobStateNull();
250
251        row.Percentage = job.Percentage;
252
253        if (job.DateCreated != DateTime.MinValue)
254          row.DateCreated = job.DateCreated;
255        else
256          row.SetDateCreatedNull();
257
258        if (job.DateCalculated != DateTime.MinValue)
259          row.DateCalculated = job.DateCalculated;
260        else
261          row.SetDateCalculatedNull();
262
263        row.Priority = job.Priority;
264
265        row.CoresNeeded = job.CoresNeeded;
266
267        row.MemoryNeeded = job.MemoryNeeded;
268
269        if (job.Project != null)
270          row.ProjectId = job.Project.Id;
271        else
272          row.SetProjectIdNull();
273      }
274
275      return row;
276    }
277    #endregion
278
279    #region IJobAdapter Members
280    public ICollection<Job> GetAllSubjobs(Job job) {
281      if (job != null) {
282        return
283          base.FindMultiple(
284            delegate() {
285              return Adapter.GetDataByParentJob(job.Id);
286            });
287      }
288
289      return null;
290    }
291
292    public ICollection<Job> GetJobsByState(State state) {
293      return
294         base.FindMultiple(
295           delegate() {
296             return Adapter.GetDataByState(state.ToString());
297           });
298    }
299
300    public ICollection<Job> GetJobsOf(ClientInfo client) {
301      if (client != null) {
302        return
303          base.FindMultiple(
304            delegate() {
305              return Adapter.GetDataByClient(client.Id);
306            });
307      }
308
309      return null;
310    }
311
312    public ICollection<Job> GetActiveJobsOf(ClientInfo client) {
313
314      if (client != null) {
315        return
316          base.FindMultiple(
317            delegate() {
318              return Adapter.GetDataByCalculatingClient(client.Id);
319            });
320      }
321
322      return null;
323    }
324
325    public ICollection<Job> GetJobsOf(Guid userId) {     
326      return
327          base.FindMultiple(
328            delegate() {
329              return Adapter.GetDataByUser(userId);
330            });
331    }
332
333    public ICollection<Job> FindJobs(State state, int cores, int memory,
334      Guid resourceId) {
335      return
336         base.FindMultiple(
337           delegate() {
338             return Adapter.GetDataByStateCoresMemoryResource(
339               state.ToString(),
340               cores,
341               memory,
342               resourceId);
343           });
344    }
345
346    public ICollection<Job> GetJobsByProject(Guid projectId) {
347      return
348         base.FindMultiple(
349           delegate() {
350             return Adapter.GetDataByProjectId(projectId);
351           });
352    }
353
354    protected override void doUpdate(Job obj) {
355      if (obj != null) {
356        ProjectAdapter.Update(obj.Project);
357        ClientAdapter.Update(obj.Client);
358        Update(obj.ParentJob);
359
360        base.doUpdate(obj);
361
362        //update relationships
363        List<Guid> relationships =
364          new List<Guid>();
365        foreach (HivePluginInfo pluginInfo in obj.PluginsNeeded) {
366          //first check if pluginInfo already exists in the db
367          HivePluginInfo found = PluginInfoAdapter.GetByNameVersionBuilddate(
368            pluginInfo.Name, pluginInfo.Version, pluginInfo.BuildDate);
369          if (found != null) {
370            pluginInfo.Id = found.Id;
371          }
372
373          PluginInfoAdapter.Update(pluginInfo);
374          relationships.Add(pluginInfo.Id);
375        }
376
377        PluginsManyToManyRelationHelper.UpdateRelationships(
378          obj.Id, relationships);
379
380        AssignedManyToManyRelationHelper.UpdateRelationships(
381          obj.Id, obj.AssignedResourceIds);
382      }
383    }
384
385    protected override bool doDelete(Job job) {
386      if (job != null) {
387        dsHiveServer.JobRow row =
388          GetRowById(job.Id);
389
390        if (row != null) {
391          //Referential integrity with job results
392          ICollection<JobResult> results =
393            ResultsAdapter.GetResultsOf(job.Id);
394
395          foreach (JobResult result in results) {
396            ResultsAdapter.Delete(result);
397          }
398
399          //delete all relationships
400          PluginsManyToManyRelationHelper.UpdateRelationships(job.Id,
401            new List<Guid>());
402
403          //delete orphaned pluginInfos
404          ICollection<HivePluginInfo> orphanedPluginInfos =
405             PluginInfoAdapter.GetOrphanedPluginInfos();
406          foreach(HivePluginInfo orphanedPlugin in orphanedPluginInfos) {
407            PluginInfoAdapter.Delete(orphanedPlugin);
408          }
409
410          return base.doDelete(job);
411        }
412      }
413
414      return false;
415    }
416
417    /// <summary>
418    /// Gets the computable job with the secified jobId
419    /// </summary>
420    /// <param name="jobId"></param>
421    /// <returns></returns>
422    public SerializedJob GetSerializedJob(Guid jobId) {
423      return (SerializedJob)base.doInTransaction(
424        delegate() {
425          SerializedJob job =
426            new SerializedJob();
427
428          job.JobInfo = GetById(jobId);
429          if (job.JobInfo != null) {
430            job.SerializedJobData =
431              base.Adapter.GetSerializedJobById(jobId);
432
433            return job;
434          } else {
435            return null;
436          }
437        });
438    }
439
440    /// <summary>
441    /// Saves or update the computable job
442    /// </summary>
443    /// <param name="jobId"></param>
444    public void UpdateSerializedJob(SerializedJob job) {
445      if(job != null &&
446        job.JobInfo != null) {
447        base.doInTransaction(
448          delegate() {
449             Update(job.JobInfo);
450             return base.Adapter.UpdateSerializedJob(
451               job.SerializedJobData, job.JobInfo.Id);
452          });
453      }
454     
455    }
456
457    #endregion
458  }
459}
Note: See TracBrowser for help on using the repository browser.