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

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

fixed race condition issues, improved performance (#372)

File size: 8.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.Core.InternalInterfaces.DataAccess;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using System.Linq.Expressions;
29using System.Runtime.CompilerServices;
30
31namespace HeuristicLab.Hive.Server.ADODataAccess {
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();
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    }
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    }
76    #endregion
77
78    #region Overrides
79    protected override Job ConvertRow(dsHiveServer.JobRow row,
80      Job job) {
81      if (row != null && job != null) {
82        job.Id = row.JobId;
83
84        if (!row.IsParentJobIdNull())
85          job.ParentJob = GetById(row.ParentJobId);
86        else
87          job.ParentJob = null;
88
89        if (!row.IsResourceIdNull())
90          job.Client = ClientAdapter.GetById(row.ResourceId);
91        else
92          job.Client = null;
93       
94        if (!row.IsJobStateNull())
95          job.State = (State)Enum.Parse(job.State.GetType(), row.JobState);
96        else
97          job.State = State.nullState;
98
99        if (!row.IsPercentageNull())
100          job.Percentage = row.Percentage;
101        else
102          job.Percentage = 0.0;
103
104        if (!row.IsSerializedJobNull())
105          job.SerializedJob = row.SerializedJob;
106        else
107          job.SerializedJob = null;
108
109        return job;
110      } else
111        return null;
112    }
113
114    protected override dsHiveServer.JobRow ConvertObj(Job job,
115      dsHiveServer.JobRow row) {
116      if (job != null && row != null) {
117        if (job.Client != null) {
118          if (row.IsResourceIdNull() ||
119            row.ResourceId != job.Client.Id) {
120            ClientAdapter.Update(job.Client);
121            row.ResourceId = job.Client.Id;
122          }
123        } else
124          row.SetResourceIdNull();
125
126        if (job.ParentJob != null) {
127          if (row.IsParentJobIdNull() ||
128            row.ParentJobId != job.ParentJob.Id) {
129            Update(job.ParentJob);
130            row.ParentJobId = job.ParentJob.Id;
131          }
132        } else
133          row.SetParentJobIdNull();
134
135        if (job.State != State.nullState)
136          row.JobState = job.State.ToString();
137        else
138          row.SetJobStateNull();
139
140        row.Percentage = job.Percentage;
141
142        row.SerializedJob = job.SerializedJob;
143      }
144
145      return row;
146    }
147
148    protected override void UpdateRow(dsHiveServer.JobRow row) {
149      Adapter.Update(row);
150    }
151
152    protected override dsHiveServer.JobRow
153      InsertNewRow(Job job) {     
154      dsHiveServer.JobRow row = data.NewJobRow();
155      data.AddJobRow(row);
156
157      return row;
158    }
159
160    protected override dsHiveServer.JobRow
161      InsertNewRowInCache(Job job) {
162      dsHiveServer.JobRow row = data.NewJobRow();
163      cache.AddJobRow(row);
164
165      return row;
166    }
167
168    protected override void FillCache() {
169      Adapter.FillByActive(cache);
170    }
171
172    public override void SyncWithDb() {
173      this.Adapter.Update(cache);
174    }
175
176    protected override bool PutInCache(Job job) {
177      return job != null
178        && (job.State == State.calculating
179            || job.State == State.idle);
180    }
181
182    protected override IEnumerable<dsHiveServer.JobRow>
183      FindById(long id) {
184      return Adapter.GetDataById(id);
185    }
186
187    protected override dsHiveServer.JobRow
188      FindCachedById(long id) {
189      return cache.FindByJobId(id);
190    }
191
192    protected override IEnumerable<dsHiveServer.JobRow>
193      FindAll() {
194      return FindMultipleRows(
195        new Selector(Adapter.GetData),
196        new Selector(cache.AsEnumerable<dsHiveServer.JobRow>));
197    }
198
199    #endregion
200
201    #region IJobAdapter Members
202    public ICollection<Job> GetAllSubjobs(Job job) {
203      if (job != null) {
204        return
205          base.FindMultiple(
206            delegate() {
207              return Adapter.GetDataBySubjobs(job.Id);
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            });
216      }
217
218      return null;
219    }
220
221    public ICollection<Job> GetJobsByState(State state) {
222      return
223         base.FindMultiple(
224           delegate() {
225             return Adapter.GetDataByState(state.ToString());
226           },
227           delegate() {
228             return from job in
229                      cache.AsEnumerable<dsHiveServer.JobRow>()
230                    where !job.IsJobStateNull() &&
231                           job.JobState == state.ToString()
232                    select job;
233           });
234    }
235
236    public ICollection<Job> GetJobsOf(ClientInfo client) {
237      if (client != null) {
238        return
239          base.FindMultiple(
240            delegate() {
241              return Adapter.GetDataByClient(client.Id);
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            });
250      }
251
252      return null;
253    }
254
255    public ICollection<Job> GetJobsOf(User user) {
256      if (user != null) {
257        return
258          base.FindMultiple(
259            delegate() {
260              return Adapter.GetDataByUser(user.Id);
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            });
269      }
270
271      return null;
272    }
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    }
295    #endregion
296  }
297}
Note: See TracBrowser for help on using the repository browser.