Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2608 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
RevLine 
[971]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;
[1377]26using HeuristicLab.Hive.Server.DataAccess;
[971]27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using System.Linq.Expressions;
[1377]29using HeuristicLab.DataAccess.ADOHelper;
[1468]30using HeuristicLab.Hive.Server.ADODataAccess.dsHiveServerTableAdapters;
31using System.Data.Common;
32using System.Data.SqlClient;
[1580]33using HeuristicLab.Hive.Server.ADODataAccess.TableAdapterWrapper;
[2117]34using System.IO;
[971]35
36namespace HeuristicLab.Hive.Server.ADODataAccess {
[995]37  class JobAdapter :
[1468]38    DataAdapterBase<dsHiveServerTableAdapters.JobTableAdapter,
[995]39                      Job,
[1468]40                      dsHiveServer.JobRow>,
[995]41    IJobAdapter {
42    #region Fields
[1580]43    private ManyToManyRelationHelper<
44      dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
[1947]45      dsHiveServer.RequiredPluginsRow> pluginsManyToManyRelationHelper = null;
[1580]46
47    private ManyToManyRelationHelper<
48      dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
[1947]49      dsHiveServer.RequiredPluginsRow> PluginsManyToManyRelationHelper {
[1580]50      get {
[1947]51        if (pluginsManyToManyRelationHelper == null) {
52          pluginsManyToManyRelationHelper =
[1580]53            new ManyToManyRelationHelper<dsHiveServerTableAdapters.RequiredPluginsTableAdapter,
[1758]54              dsHiveServer.RequiredPluginsRow>(new RequiredPluginsAdapterWrapper(), 1);
[1580]55        }
56
[1947]57        pluginsManyToManyRelationHelper.Session = Session as Session;
[1580]58
[1947]59        return pluginsManyToManyRelationHelper;
[1580]60      }
61    }
62
[1947]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
[971]83    private IClientAdapter clientAdapter = null;
84
85    private IClientAdapter ClientAdapter {
86      get {
87        if (clientAdapter == null)
[1468]88          clientAdapter =
89            this.Session.GetDataAdapter<ClientInfo, IClientAdapter>();
[971]90
91        return clientAdapter;
92      }
93    }
94
[1005]95    private IJobResultsAdapter resultsAdapter = null;
96
97    private IJobResultsAdapter ResultsAdapter {
98      get {
99        if (resultsAdapter == null) {
[1468]100          resultsAdapter =
101            this.Session.GetDataAdapter<JobResult, IJobResultsAdapter>();
[1005]102        }
103
104        return resultsAdapter;
105      }
106    }
[1580]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    }
[1947]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    }
[995]133    #endregion
[971]134
[1468]135    public JobAdapter(): base(new JobAdapterWrapper()) {
136    }
137
[995]138    #region Overrides
[1131]139    protected override Job ConvertRow(dsHiveServer.JobRow row,
[971]140      Job job) {
141      if (row != null && job != null) {
[995]142        job.Id = row.JobId;
[971]143
144        if (!row.IsParentJobIdNull())
[995]145          job.ParentJob = GetById(row.ParentJobId);
[971]146        else
[975]147          job.ParentJob = null;
[971]148
149        if (!row.IsResourceIdNull())
[995]150          job.Client = ClientAdapter.GetById(row.ResourceId);
[971]151        else
152          job.Client = null;
[1161]153
[1449]154        if (!row.IsUserIdNull())
[1377]155          job.UserId = Guid.Empty;
[1161]156        else
[1377]157          job.UserId = Guid.Empty;
[971]158       
[1092]159        if (!row.IsJobStateNull())
160          job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
[971]161        else
[995]162          job.State = State.nullState;
[971]163
[1092]164        if (!row.IsPercentageNull())
165          job.Percentage = row.Percentage;
166        else
167          job.Percentage = 0.0;
168
[1169]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
[1175]179        if (!row.IsPriorityNull())
180          job.Priority = row.Priority;
181        else
182          job.Priority = default(int);
[1169]183
[1505]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
[1947]194        if (!row.IsProjectIdNull())
195          job.Project = ProjectAdapter.GetById(
196            row.ProjectId);
197        else
198          job.Project = null;
199
[1580]200        ICollection<Guid> requiredPlugins =
[1947]201          PluginsManyToManyRelationHelper.GetRelationships(job.Id);
[1580]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
[1947]211        job.AssignedResourceIds =
212          new List<Guid>(
213            AssignedManyToManyRelationHelper.GetRelationships(job.Id));
214
[971]215        return job;
216      } else
217        return null;
218    }
219
[1131]220    protected override dsHiveServer.JobRow ConvertObj(Job job,
[971]221      dsHiveServer.JobRow row) {
222      if (job != null && row != null) {
[1449]223        row.JobId = job.Id;
224       
[971]225        if (job.Client != null) {
[1952]226          if (row.IsResourceIdNull()) {
[1021]227            row.ResourceId = job.Client.Id;
228          }
[971]229        } else
230          row.SetResourceIdNull();
231
[975]232        if (job.ParentJob != null) {
[1952]233          if (row.IsParentJobIdNull()) {
[1021]234            row.ParentJobId = job.ParentJob.Id;
235          }
[971]236        } else
237          row.SetParentJobIdNull();
238
[1377]239        if (job.UserId != Guid.Empty) {
[1449]240          if (row.IsUserIdNull() ||
241           row.UserId != Guid.Empty) {
242            row.UserId = Guid.Empty;
[1161]243          }
244        } else
[1449]245          row.SetUserIdNull();
[1161]246
[995]247        if (job.State != State.nullState)
[1092]248          row.JobState = job.State.ToString();
[995]249        else
[1092]250          row.SetJobStateNull();
251
252        row.Percentage = job.Percentage;
[1120]253
[1169]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;
[1505]265
266        row.CoresNeeded = job.CoresNeeded;
267
268        row.MemoryNeeded = job.MemoryNeeded;
[1947]269
270        if (job.Project != null)
271          row.ProjectId = job.Project.Id;
272        else
273          row.SetProjectIdNull();
[971]274      }
275
276      return row;
277    }
[995]278    #endregion
279
280    #region IJobAdapter Members
[971]281    public ICollection<Job> GetAllSubjobs(Job job) {
282      if (job != null) {
[995]283        return
284          base.FindMultiple(
285            delegate() {
[1449]286              return Adapter.GetDataByParentJob(job.Id);
[995]287            });
[971]288      }
289
[995]290      return null;
[971]291    }
292
[998]293    public ICollection<Job> GetJobsByState(State state) {
294      return
295         base.FindMultiple(
296           delegate() {
[1131]297             return Adapter.GetDataByState(state.ToString());
[998]298           });
299    }
300
[971]301    public ICollection<Job> GetJobsOf(ClientInfo client) {
302      if (client != null) {
[995]303        return
304          base.FindMultiple(
305            delegate() {
[1131]306              return Adapter.GetDataByClient(client.Id);
[995]307            });
[971]308      }
309
[995]310      return null;
[971]311    }
312
[1166]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
[1468]326    public ICollection<Job> GetJobsOf(Guid userId) {     
[1377]327      return
[995]328          base.FindMultiple(
329            delegate() {
[1449]330              return Adapter.GetDataByUser(userId);
[995]331            });
[971]332    }
[1005]333
[2066]334    public ICollection<Job> FindJobs(State state, int cores, int memory,
335      Guid resourceId) {
[1508]336      return
337         base.FindMultiple(
338           delegate() {
[2066]339             return Adapter.GetDataByStateCoresMemoryResource(
340               state.ToString(),
341               cores,
342               memory,
343               resourceId);
[1508]344           });
345    }
346
[1944]347    public ICollection<Job> GetJobsByProject(Guid projectId) {
[1947]348      return
349         base.FindMultiple(
350           delegate() {
351             return Adapter.GetDataByProjectId(projectId);
352           });
[1944]353    }
354
[1580]355    protected override void doUpdate(Job obj) {
[1955]356      if (obj != null) {
357        ProjectAdapter.Update(obj.Project);
358        ClientAdapter.Update(obj.Client);
359        Update(obj.ParentJob);
[1952]360
[1955]361        base.doUpdate(obj);
[1580]362
[1955]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);
[1592]376        }
377
[1955]378        PluginsManyToManyRelationHelper.UpdateRelationships(
379          obj.Id, relationships);
380
381        AssignedManyToManyRelationHelper.UpdateRelationships(
382          obj.Id, obj.AssignedResourceIds);
[1580]383      }
384    }
385
[1468]386    protected override bool doDelete(Job job) {
[1005]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 =
[2099]394            ResultsAdapter.GetResultsOf(job.Id);
[1005]395
396          foreach (JobResult result in results) {
397            ResultsAdapter.Delete(result);
398          }
399
[1580]400          //delete all relationships
[1947]401          PluginsManyToManyRelationHelper.UpdateRelationships(job.Id,
[1759]402            new List<Guid>());
[1580]403
[1592]404          //delete orphaned pluginInfos
405          ICollection<HivePluginInfo> orphanedPluginInfos =
406             PluginInfoAdapter.GetOrphanedPluginInfos();
407          foreach(HivePluginInfo orphanedPlugin in orphanedPluginInfos) {
408            PluginInfoAdapter.Delete(orphanedPlugin);
409          }
410
[1468]411          return base.doDelete(job);
[1005]412        }
413      }
414
415      return false;
416    }
[1944]417
[2082]418    /// <summary>
419    /// Gets the computable job with the secified jobId
420    /// </summary>
421    /// <param name="jobId"></param>
422    /// <returns></returns>
[2099]423    public SerializedJob GetSerializedJob(Guid jobId) {
[2092]424      return (SerializedJob)base.doInTransaction(
[2082]425        delegate() {
[2092]426          SerializedJob job =
427            new SerializedJob();
[2082]428
429          job.JobInfo = GetById(jobId);
[2083]430          if (job.JobInfo != null) {
[2092]431            job.SerializedJobData =
[2083]432              base.Adapter.GetSerializedJobById(jobId);
[2082]433
434            return job;
435          } else {
436            return null;
437          }
438        });
439    }
440
[2117]441    public Stream GetSerializedJobStream(Guid jobId, bool useExistingConnection) {
442      return
443        ((JobAdapterWrapper)base.DataAdapterWrapper).
444          GetSerializedJobStream(jobId, useExistingConnection);
445    }
446
[2082]447    /// <summary>
448    /// Saves or update the computable job
449    /// </summary>
450    /// <param name="jobId"></param>
[2099]451    public void UpdateSerializedJob(SerializedJob job) {
[2082]452      if(job != null &&
453        job.JobInfo != null) {
454        base.doInTransaction(
455          delegate() {
[2087]456             Update(job.JobInfo);
457             return base.Adapter.UpdateSerializedJob(
[2092]458               job.SerializedJobData, job.JobInfo.Id);
[2082]459          });
460      }
461     
462    }
463
[971]464    #endregion
465  }
466}
Note: See TracBrowser for help on using the repository browser.