Free cookie consent management tool by TermsFeed Policy Generator

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

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

fixed race condition issues, improved performance (#372)

File size: 8.2 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;
26using HeuristicLab.Hive.Server.Core.InternalInterfaces.DataAccess;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using System.Linq.Expressions;
29using System.Runtime.CompilerServices;
30
31namespace HeuristicLab.Hive.Server.ADODataAccess {
[995]32  class JobAdapter :
33    CachedDataAdapter<dsHiveServerTableAdapters.JobTableAdapter,
34                      Job,
35                      dsHiveServer.JobRow,
36                      dsHiveServer.JobDataTable>,
37    IJobAdapter {
38    #region Fields
39    dsHiveServer.JobDataTable data =
40        new dsHiveServer.JobDataTable();
[971]41
42    private IClientAdapter clientAdapter = null;
43
44    private IClientAdapter ClientAdapter {
45      get {
46        if (clientAdapter == null)
47          clientAdapter = ServiceLocator.GetClientAdapter();
48
49        return clientAdapter;
50      }
51    }
52
53    private IUserAdapter userAdapter = null;
54
55    private IUserAdapter UserAdapter {
56      get {
57        if (userAdapter == null) {
58          userAdapter = ServiceLocator.GetUserAdapter();
59        }
60
61        return userAdapter;
62      }
63    }
[1005]64
65    private IJobResultsAdapter resultsAdapter = null;
66
67    private IJobResultsAdapter ResultsAdapter {
68      get {
69        if (resultsAdapter == null) {
70          resultsAdapter = ServiceLocator.GetJobResultsAdapter();
71        }
72
73        return resultsAdapter;
74      }
75    }
[995]76    #endregion
[971]77
[995]78    #region Overrides
[1131]79    protected override Job ConvertRow(dsHiveServer.JobRow row,
[971]80      Job job) {
81      if (row != null && job != null) {
[995]82        job.Id = row.JobId;
[971]83
84        if (!row.IsParentJobIdNull())
[995]85          job.ParentJob = GetById(row.ParentJobId);
[971]86        else
[975]87          job.ParentJob = null;
[971]88
89        if (!row.IsResourceIdNull())
[995]90          job.Client = ClientAdapter.GetById(row.ResourceId);
[971]91        else
92          job.Client = null;
93       
[1092]94        if (!row.IsJobStateNull())
95          job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
[971]96        else
[995]97          job.State = State.nullState;
[971]98
[1092]99        if (!row.IsPercentageNull())
100          job.Percentage = row.Percentage;
101        else
102          job.Percentage = 0.0;
103
[1120]104        if (!row.IsSerializedJobNull())
105          job.SerializedJob = row.SerializedJob;
106        else
107          job.SerializedJob = null;
108
[971]109        return job;
110      } else
111        return null;
112    }
113
[1131]114    protected override dsHiveServer.JobRow ConvertObj(Job job,
[971]115      dsHiveServer.JobRow row) {
116      if (job != null && row != null) {
117        if (job.Client != null) {
[1021]118          if (row.IsResourceIdNull() ||
119            row.ResourceId != job.Client.Id) {
120            ClientAdapter.Update(job.Client);
121            row.ResourceId = job.Client.Id;
122          }
[971]123        } else
124          row.SetResourceIdNull();
125
[975]126        if (job.ParentJob != null) {
[1021]127          if (row.IsParentJobIdNull() ||
128            row.ParentJobId != job.ParentJob.Id) {
129            Update(job.ParentJob);
130            row.ParentJobId = job.ParentJob.Id;
131          }
[971]132        } else
133          row.SetParentJobIdNull();
134
[995]135        if (job.State != State.nullState)
[1092]136          row.JobState = job.State.ToString();
[995]137        else
[1092]138          row.SetJobStateNull();
139
140        row.Percentage = job.Percentage;
[1120]141
142        row.SerializedJob = job.SerializedJob;
[971]143      }
144
145      return row;
146    }
147
[995]148    protected override void UpdateRow(dsHiveServer.JobRow row) {
[1131]149      Adapter.Update(row);
[995]150    }
[971]151
[995]152    protected override dsHiveServer.JobRow
153      InsertNewRow(Job job) {     
154      dsHiveServer.JobRow row = data.NewJobRow();
155      data.AddJobRow(row);
[971]156
[995]157      return row;
[971]158    }
159
[995]160    protected override dsHiveServer.JobRow
161      InsertNewRowInCache(Job job) {
[1131]162      dsHiveServer.JobRow row = data.NewJobRow();
[995]163      cache.AddJobRow(row);
[971]164
[995]165      return row;
166    }
[971]167
[995]168    protected override void FillCache() {
[1131]169      Adapter.FillByActive(cache);
[995]170    }
[971]171
[1029]172    public override void SyncWithDb() {
[1131]173      this.Adapter.Update(cache);
[971]174    }
175
[995]176    protected override bool PutInCache(Job job) {
177      return job != null
[1029]178        && (job.State == State.calculating
179            || job.State == State.idle);
[995]180    }
[971]181
[995]182    protected override IEnumerable<dsHiveServer.JobRow>
183      FindById(long id) {
[1131]184      return Adapter.GetDataById(id);
[995]185    }
[971]186
[995]187    protected override dsHiveServer.JobRow
188      FindCachedById(long id) {
189      return cache.FindByJobId(id);
[971]190    }
191
[995]192    protected override IEnumerable<dsHiveServer.JobRow>
193      FindAll() {
194      return FindMultipleRows(
[1131]195        new Selector(Adapter.GetData),
[995]196        new Selector(cache.AsEnumerable<dsHiveServer.JobRow>));
197    }
198
199    #endregion
200
201    #region IJobAdapter Members
[971]202    public ICollection<Job> GetAllSubjobs(Job job) {
203      if (job != null) {
[995]204        return
205          base.FindMultiple(
206            delegate() {
[1131]207              return Adapter.GetDataBySubjobs(job.Id);
[995]208            },
209            delegate() {
210              return from j in
211                   cache.AsEnumerable<dsHiveServer.JobRow>()
212                 where  !j.IsParentJobIdNull() &&
213                        j.ParentJobId == job.Id
214                 select j;
215            });
[971]216      }
217
[995]218      return null;
[971]219    }
220
[998]221    public ICollection<Job> GetJobsByState(State state) {
222      return
223         base.FindMultiple(
224           delegate() {
[1131]225             return Adapter.GetDataByState(state.ToString());
[998]226           },
227           delegate() {
228             return from job in
229                      cache.AsEnumerable<dsHiveServer.JobRow>()
[1092]230                    where !job.IsJobStateNull() &&
231                           job.JobState == state.ToString()
[998]232                    select job;
233           });
234    }
235
[971]236    public ICollection<Job> GetJobsOf(ClientInfo client) {
237      if (client != null) {
[995]238        return
239          base.FindMultiple(
240            delegate() {
[1131]241              return Adapter.GetDataByClient(client.Id);
[995]242            },
243            delegate() {
244              return from job in
245                 cache.AsEnumerable<dsHiveServer.JobRow>()
246               where !job.IsResourceIdNull() &&
247                      job.ResourceId == client.Id
248               select job;
249            });
[971]250      }
251
[995]252      return null;
[971]253    }
254
255    public ICollection<Job> GetJobsOf(User user) {
256      if (user != null) {
[995]257        return
258          base.FindMultiple(
259            delegate() {
[1131]260              return Adapter.GetDataByUser(user.Id);
[995]261            },
262            delegate() {
263              return from job in
264                cache.AsEnumerable<dsHiveServer.JobRow>()
265              where !job.IsPermissionOwnerIdNull() &&
266                job.PermissionOwnerId == user.Id
267              select job;
268            });
[971]269      }
270
[995]271      return null;
[971]272    }
[1005]273
274    [MethodImpl(MethodImplOptions.Synchronized)]
275    public override bool Delete(Job job) {
276      if (job != null) {
277        dsHiveServer.JobRow row =
278          GetRowById(job.Id);
279
280        if (row != null) {
281          //Referential integrity with job results
282          ICollection<JobResult> results =
283            ResultsAdapter.GetResultsOf(job);
284
285          foreach (JobResult result in results) {
286            ResultsAdapter.Delete(result);
287          }
288
289          return base.Delete(job);
290        }
291      }
292
293      return false;
294    }
[971]295    #endregion
296  }
297}
Note: See TracBrowser for help on using the repository browser.