- Timestamp:
- 01/15/09 15:04:50 (16 years ago)
- File:
-
- 1 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 }
Note: See TracChangeset
for help on using the changeset viewer.