Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/ResourceAdapter.cs @ 1134

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

fixed race condition issues, improved performance (#372)

File size: 4.7 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.Runtime.CompilerServices;
29
30namespace HeuristicLab.Hive.Server.ADODataAccess {
31  class ResourceAdapter:
32    CachedDataAdapter<
33      dsHiveServerTableAdapters.ResourceTableAdapter,
34      Resource,
35      dsHiveServer.ResourceRow,
36      dsHiveServer.ResourceDataTable>, 
37    IResourceAdapter {
38    #region Fields
39    dsHiveServer.ResourceDataTable data =
40        new dsHiveServer.ResourceDataTable();
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    #endregion
53
54    #region Overrides
55    protected override Resource ConvertRow(dsHiveServer.ResourceRow row,
56      Resource resource) {
57      if (row != null && resource != null) {
58        resource.Id = row.ResourceId;
59        if (!row.IsNameNull())
60          resource.Name = row.Name;
61        else
62          resource.Name = String.Empty;
63
64        return resource;
65      } else
66        return null;
67    }
68
69    protected override dsHiveServer.ResourceRow ConvertObj(Resource resource,
70      dsHiveServer.ResourceRow row) {
71      if (resource != null && row != null) {
72        row.Name = resource.Name;
73
74        return row;
75      } else
76        return null;
77    }
78
79    protected override void UpdateRow(dsHiveServer.ResourceRow row) {
80      Adapter.Update(row);
81    }
82
83    protected override dsHiveServer.ResourceRow
84      InsertNewRow(Resource resource) {
85      dsHiveServer.ResourceRow row = data.NewResourceRow();
86      data.AddResourceRow(row);
87
88      return row;
89    }
90
91    protected override dsHiveServer.ResourceRow
92      InsertNewRowInCache(Resource resource) {
93      dsHiveServer.ResourceRow row = cache.NewResourceRow();
94      cache.AddResourceRow(row);
95
96      return row;
97    }
98
99    protected override void FillCache() {
100      Adapter.FillByActive(cache);
101    }
102
103    public override void SyncWithDb() {
104      Adapter.Update(cache);
105    }
106
107    protected override bool PutInCache(Resource obj) {
108      return (obj is ClientInfo &&
109        (obj as ClientInfo).State != State.offline);
110    }
111
112    protected override IEnumerable<dsHiveServer.ResourceRow>
113      FindById(long id) {
114      return Adapter.GetDataById(id);
115    }
116
117    protected override dsHiveServer.ResourceRow
118      FindCachedById(long id) {
119      return cache.FindByResourceId(id);
120    }
121
122    protected override IEnumerable<dsHiveServer.ResourceRow>
123      FindAll() {
124      return FindMultipleRows(
125        new Selector(Adapter.GetData),
126        new Selector(cache.AsEnumerable<dsHiveServer.ResourceRow>));
127    }
128
129    #endregion
130
131    #region IResourceAdapter Members
132    public bool GetById(Resource resource) {
133      if (resource != null) {
134        dsHiveServer.ResourceRow row =
135          GetRowById(resource.Id);
136
137        if (row != null) {
138          Convert(row, resource);
139
140          return true;
141        }
142      }
143
144      return false;
145    }
146
147    public Resource GetByName(string name) {
148      dsHiveServer.ResourceRow row =
149        base.FindSingleRow(
150          delegate() {
151            return Adapter.GetDataByName(name);
152          },
153          delegate() {
154            return from r in
155                     cache.AsEnumerable<dsHiveServer.ResourceRow>()
156                   where !r.IsNameNull() &&
157                          r.Name == name
158                   select r;
159          });
160
161      if (row != null) {
162        Resource res = new Resource();
163        res = Convert(row, res);
164
165        return res;
166      } else {
167        return null;
168      }
169    }
170
171    #endregion
172  }
173}
Note: See TracBrowser for help on using the repository browser.