Free cookie consent management tool by TermsFeed Policy Generator

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

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

Improved memory consumption, fixed bug that already calculated jobs where reset (#372)

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