Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/3.2/JobAdapter.cs @ 2527

Last change on this file since 2527 was 2117, checked in by svonolfe, 15 years ago

Streaming of Jobs and JobsResults directly from/to the DB (#680)

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