Changeset 1134
- Timestamp:
- 01/15/09 15:04:50 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.Hive.Server.ADODataAccess
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/CachedDataAdapter.cs
r1131 r1134 25 25 using System.Text; 26 26 using HeuristicLab.Hive.Contracts.BusinessObjects; 27 using System.Runtime.CompilerServices;28 27 using System.Data; 28 using System.Threading; 29 29 30 30 namespace HeuristicLab.Hive.Server.ADODataAccess { … … 41 41 private static bool cacheFilled = false; 42 42 43 private static ReaderWriterLock cacheLock = 44 new ReaderWriterLock(); 45 43 46 protected DataTable dataTable = 44 47 new DataTable(); … … 47 50 new List<ICachedDataAdapter>(); 48 51 49 [MethodImpl(MethodImplOptions.Synchronized)]50 52 protected CachedDataAdapter() { 53 cacheLock.AcquireWriterLock(Timeout.Infinite); 54 51 55 if (!cacheFilled) { 52 56 FillCache(); … … 54 58 } 55 59 60 cacheLock.ReleaseWriterLock(); 61 56 62 ServiceLocator.GetTransactionManager().OnUpdate += 57 63 new EventHandler(CachedDataAdapter_OnUpdate); … … 60 66 protected virtual RowT FindSingleRow(Selector dbSelector, 61 67 Selector cacheSelector) { 68 cacheLock.AcquireReaderLock(Timeout.Infinite); 69 62 70 RowT row = 63 71 FindSingleRow(cacheSelector); … … 69 77 } 70 78 79 cacheLock.ReleaseReaderLock(); 80 71 81 return row; 72 82 } … … 74 84 protected virtual IEnumerable<RowT> FindMultipleRows(Selector dbSelector, 75 85 Selector cacheSelector) { 86 cacheLock.AcquireReaderLock(Timeout.Infinite); 87 76 88 IList<RowT> result = 77 89 new List<RowT>(cacheSelector()); … … 85 97 } 86 98 } 99 100 cacheLock.ReleaseReaderLock(); 87 101 88 102 return result; … … 105 119 protected virtual ICollection<ObjT> FindMultiple(Selector dbSelector, 106 120 Selector cacheSelector) { 121 cacheLock.AcquireReaderLock(Timeout.Infinite); 122 107 123 ICollection<ObjT> result = 108 124 FindMultiple(cacheSelector); … … 110 126 ICollection<ObjT> resultDb = 111 127 FindMultiple(dbSelector); 128 129 cacheLock.ReleaseReaderLock(); 112 130 113 131 foreach (ObjT obj in resultDb) { … … 119 137 } 120 138 121 [MethodImpl(MethodImplOptions.Synchronized)]122 139 protected abstract RowT InsertNewRowInCache(ObjT obj); 123 140 124 [MethodImpl(MethodImplOptions.Synchronized)]125 141 protected abstract void FillCache(); 126 142 127 [MethodImpl(MethodImplOptions.Synchronized)]128 143 public abstract void SyncWithDb(); 129 144 130 [MethodImpl(MethodImplOptions.Synchronized)]131 145 protected abstract bool PutInCache(ObjT obj); 132 146 133 147 protected abstract RowT FindCachedById(long id); 134 148 135 [MethodImpl(MethodImplOptions.Synchronized)]136 149 void CachedDataAdapter_OnUpdate(object sender, EventArgs e) { 137 150 foreach (ICachedDataAdapter parent in this.parentAdapters) { … … 140 153 141 154 this.SyncWithDb(); 142 }143 144 [MethodImpl(MethodImplOptions.Synchronized)]145 protected virtual void RemoveRowFromCache(RowT row) {146 cache.Rows.Remove(row);147 155 } 148 156 … … 167 175 } 168 176 169 [MethodImpl(MethodImplOptions.Synchronized)] 177 private void AddToCache(RowT row) { 178 cacheLock.AcquireWriterLock(Timeout.Infinite); 179 180 cache.ImportRow(row); 181 row.Table.Rows.Remove(row); 182 183 cacheLock.ReleaseWriterLock(); 184 } 185 186 private RowT AddToCache(ObjT obj) { 187 cacheLock.AcquireWriterLock(Timeout.Infinite); 188 189 RowT row = InsertNewRowInCache(obj); 190 191 cacheLock.ReleaseWriterLock(); 192 193 return row; 194 } 195 196 private void RemoveRowFromCache(RowT row) { 197 cacheLock.AcquireWriterLock(Timeout.Infinite); 198 199 dataTable.ImportRow(row); 200 cache.Rows.Remove(row); 201 202 cacheLock.ReleaseWriterLock(); 203 204 UpdateRow(row); 205 } 206 170 207 public override void Update(ObjT obj) { 171 208 if (obj != null) { … … 175 212 if (row == null) { 176 213 if (PutInCache(obj)) { 177 row = InsertNewRowInCache(obj);214 row = AddToCache(obj); 178 215 } else { 179 216 row = InsertNewRow(obj); … … 184 221 185 222 obj.Id = (long)row[row.Table.PrimaryKey[0]]; 223 LockRow(obj.Id); 186 224 187 225 ConvertObj(obj, row); … … 191 229 192 230 if (IsCached(row) && 193 !PutInCache(obj)) { 194 //remove from cache 195 dataTable.ImportRow(row); 231 !PutInCache(obj)) { 196 232 RemoveRowFromCache(row); 197 198 UpdateRow(row);199 233 } else if (!IsCached(row) && 200 234 PutInCache(obj)) { 201 //add to cache202 cache.ImportRow(row);203 row.Table.Rows.Remove(row); 204 }235 AddToCache(row); 236 } 237 238 UnlockRow(obj.Id); 205 239 } 206 240 } -
trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/ClientAdapter.cs
r1131 r1134 176 176 protected override dsHiveServer.ClientRow 177 177 InsertNewRowInCache(ClientInfo client) { 178 dsHiveServer.ClientRow row = data.NewClientRow();178 dsHiveServer.ClientRow row = cache.NewClientRow(); 179 179 row.ResourceId = client.Id; 180 180 cache.AddClientRow(row); -
trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/DataAdapterBase.cs
r1131 r1134 26 26 using System.Text; 27 27 using HeuristicLab.Hive.Contracts.BusinessObjects; 28 using System. Runtime.CompilerServices;28 using System.Threading; 29 29 30 30 namespace HeuristicLab.Hive.Server.ADODataAccess { … … 33 33 where RowT : System.Data.DataRow 34 34 where ObjT : IHiveObject, new() { 35 36 private static Mutex lockersMutex = 37 new Mutex(); 38 39 private static IDictionary<long, Mutex> lockers = 40 new Dictionary<long, Mutex>(); 41 42 private static IDictionary<long, int> lockCount = 43 new Dictionary<long, int>(); 44 45 protected void LockRow(long id) { 46 Mutex rowLock = null; 47 48 /////begin critical section//// 49 lockersMutex.WaitOne(); 50 51 if (!lockers.ContainsKey(id)) { 52 lockers[id] = new Mutex(); 53 lockCount[id] = 0; 54 } 55 rowLock = lockers[id]; 56 lockCount[id]++; 57 58 lockersMutex.ReleaseMutex(); 59 /////end critical section//// 60 61 rowLock.WaitOne(); 62 } 63 64 protected void UnlockRow(long id) { 65 Mutex rowLock = lockers[id]; 66 rowLock.ReleaseMutex(); 67 68 /////begin critical section//// 69 lockersMutex.WaitOne(); 70 71 lockCount[id]--; 72 if (lockCount[id] == 0) 73 lockers.Remove(id); 74 75 lockersMutex.ReleaseMutex(); 76 /////end critical section//// 77 } 78 35 79 protected AdapterT Adapter { 36 80 get { … … 44 88 protected abstract ObjT ConvertRow(RowT row, ObjT obj); 45 89 46 [MethodImpl(MethodImplOptions.Synchronized)]47 90 protected abstract RowT InsertNewRow(ObjT obj); 48 91 49 [MethodImpl(MethodImplOptions.Synchronized)]50 92 protected abstract void UpdateRow(RowT row); 51 93 … … 119 161 } 120 162 121 [MethodImpl(MethodImplOptions.Synchronized)]122 163 public virtual void Update(ObjT obj) { 123 164 if (obj != null) { … … 127 168 if (row == null) { 128 169 row = InsertNewRow(obj); 170 UpdateRow(row); 129 171 } 130 172 173 obj.Id = (long)row[row.Table.PrimaryKey[0]]; 174 LockRow(obj.Id); 175 131 176 ConvertObj(obj, row); 132 133 177 UpdateRow(row); 134 178 135 obj.Id = (long)row[row.Table.PrimaryKey[0]];179 UnlockRow(obj.Id); 136 180 } 137 181 } … … 149 193 } 150 194 151 [MethodImpl(MethodImplOptions.Synchronized)]152 195 public virtual bool Delete(ObjT obj) { 196 bool success = false; 197 153 198 if (obj != null) { 199 LockRow(obj.Id); 200 154 201 RowT row = 155 202 GetRowById(obj.Id); … … 159 206 UpdateRow(row); 160 207 161 returntrue;208 success = true; 162 209 } 163 } 164 165 return false; 210 211 UnlockRow(obj.Id); 212 } 213 214 return success; 166 215 } 167 216 } -
trunk/sources/HeuristicLab.Hive.Server.ADODataAccess/JobAdapter.cs
r1131 r1134 160 160 protected override dsHiveServer.JobRow 161 161 InsertNewRowInCache(Job job) { 162 dsHiveServer.JobRow row = data.NewJobRow();162 dsHiveServer.JobRow row = cache.NewJobRow(); 163 163 cache.AddJobRow(row); 164 164
Note: See TracChangeset
for help on using the changeset viewer.