Revision ab2f6f79 trunk/Pithos.Core/Agents/StatusAgent.cs

b/trunk/Pithos.Core/Agents/StatusAgent.cs
143 143
                    //This file was deleted while we were down. We should mark it as deleted
144 144
                    //We have to go through UpdateStatus here because the state object we are using
145 145
                    //was created by a different ORM session.
146
                    UpdateStatus(fileState.Id,state=> state.FileStatus = FileStatus.Deleted);                    
146
                    FileState.UpdateStatus(fileState.Id,FileStatus.Deleted);                    
147 147
                }
148 148
                else
149 149
                {
......
152 152
                    //If the hashes don't match the file was changed
153 153
                    if (fileState.Checksum != hashString)
154 154
                    {
155
                        UpdateStatus(fileState.Id, state => state.FileStatus = FileStatus.Modified);
155
                        FileState.UpdateStatus(fileState.Id, FileStatus.Modified);
156 156
                    }                    
157 157
                }
158 158
            });            
......
195 195

  
196 196

  
197 197
        private string _pithosDataPath;
198

  
199
        public T GetStatus<T>(string path,Func<FileState,T> getter,T defaultValue )
200
        {
201
            if (String.IsNullOrWhiteSpace(path))
202
                throw new ArgumentNullException("path");
203
            if (!Path.IsPathRooted(path))
204
                throw new ArgumentException("path must be a rooted path", "path");
205
            if (getter == null)
206
                throw new ArgumentNullException("getter");
207
            Contract.EndContractBlock();
208

  
209

  
210
            try
211
            {                
212
                var state = FileState.FindByFilePath(path);
213
                return state == null ? defaultValue : getter(state);
214
            }
215
            catch (Exception exc)
216
            {
217
                Log.ErrorFormat(exc.ToString());
218
                return defaultValue;
219
            }
220
        }
221

  
222
        /// <summary>
223
        /// Sets the status of a file, creating a new FileState entry if one doesn't already exist.
224
        /// </summary>
225
        /// <param name="path"></param>
226
        /// <param name="setter"></param>
227
        public void SetStatus(string path,Action<FileState> setter)
228
        {
229
            if (String.IsNullOrWhiteSpace(path))
230
                throw new ArgumentNullException("path", "path can't be empty");
231
            if (setter==null)
232
                throw new ArgumentNullException("setter", "setter can't be empty");
233
            Contract.EndContractBlock();
234

  
235
            _persistenceAgent.Post(() =>
236
            {
237
                using (new SessionScope())
238
                {
239
                    var filePath = path.ToLower();
240
                    var state = FileState.FindByFilePath(filePath);
241
                    if (state != null)
242
                    {
243
                        setter(state);
244
                        state.Save();
245
                    }
246
                    else
247
                    {
248
                        state = new FileState {FilePath = filePath};
249
                        setter(state);
250
                        state.Save();
251
                    }                    
252
                }
253
            });
254
        }
255

  
256
        /// <summary>
257
        /// Sets the status of a file only if the file already exists
258
        /// </summary>
259
        /// <param name="path"></param>
260
        /// <param name="setter"></param>
261
        private void UpdateStatus(string path, Action<FileState> setter)
262
        {
263
            if (String.IsNullOrWhiteSpace(path))
264
                throw new ArgumentNullException("path");
265
            if (!Path.IsPathRooted(path))
266
                throw new ArgumentException("The path must be rooted", "path");
267
            if (setter == null)
268
                throw new ArgumentNullException("setter");
269
            Contract.EndContractBlock();
270

  
271
            Debug.Assert(!path.Contains(FolderConstants.CacheFolder));
272
            Debug.Assert(!path.EndsWith(".ignore"));
273

  
274
            if (String.IsNullOrWhiteSpace(path))
275
                throw new ArgumentNullException("path", "path can't be empty");
276

  
277
            if (setter == null)
278
                throw new ArgumentNullException("setter", "setter can't be empty");
279

  
280
            _persistenceAgent.Post(() =>
281
            {
282
                using (new SessionScope())
283
                {
284
                    var filePath = path.ToLower();
285

  
286
                    var state = FileState.FindByFilePath(filePath);
287
                    if (state == null)
288
                    {
289
                        Log.WarnFormat("[NOFILE] Unable to set status for {0}.", filePath);
290
                        return;
291
                    }
292
                    setter(state);
293
                    state.Save();
294
                }
295
                
296
            });
297
        }
298
        
299
        /// <summary>
300
        /// Sets the status of a specific state
301
        /// </summary>
302
        /// <param name="path"></param>
303
        /// <param name="setter"></param>
304
        private void UpdateStatus(Guid stateID, Action<FileState> setter)
305
        {
306
            if (setter == null)
307
                throw new ArgumentNullException("setter");
308
            Contract.EndContractBlock();
309

  
310

  
311
            _persistenceAgent.Post(() =>
312
            {
313
                using (new SessionScope())
314
                {
315
                    var state = FileState.Find(stateID);
316
                    if (state == null)
317
                    {
318
                        Log.WarnFormat("[NOFILE] Unable to set status for {0}.", stateID);
319
                        return;
320
                    }
321
                    setter(state);
322
                    state.Save();
323
                }
324
                
325
            });
326
        }
198
       
327 199

  
328 200
        public FileOverlayStatus GetFileOverlayStatus(string path)
329 201
        {
......
335 207

  
336 208
            try
337 209
            {
338
                var state = FileState.FindByFilePath(path);
339
                return state == null ? FileOverlayStatus.Unversioned : state.OverlayStatus;
210
                var status = from state in  FileState.Queryable 
211
                                 where state.FilePath ==path.ToLower()
212
                                 select state.OverlayStatus;
213
                return status.Any()? status.First():FileOverlayStatus.Unversioned;
340 214
            }
341 215
            catch (Exception exc)
342 216
            {
......
353 227
                throw new ArgumentException("The path must be rooted","path");
354 228
            Contract.EndContractBlock();
355 229

  
356
            SetStatus(path.ToLower(),s=>s.OverlayStatus=overlayStatus);
230
            _persistenceAgent.Post(() => FileState.StoreOverlayStatus(path.ToLower(),overlayStatus));
357 231
        }
358 232

  
359 233
        /*public void RemoveFileOverlayStatus(string path)
......
391 265
                throw new ArgumentException("The newPath must be rooted", "newPath");
392 266
            Contract.EndContractBlock();
393 267

  
394
            _persistenceAgent.Post(() =>
395
                InnerRenameFileOverlayStatus(oldPath, newPath));
396
        }
397

  
398
        private static void InnerRenameFileOverlayStatus(string oldPath, string newPath)
399
        {
400
            if (String.IsNullOrWhiteSpace(oldPath))
401
                throw new ArgumentNullException("oldPath");
402
            if (!Path.IsPathRooted(oldPath))
403
                throw new ArgumentException("The oldPath must be rooted", "oldPath");
404
            if (String.IsNullOrWhiteSpace(newPath))
405
                throw new ArgumentNullException("newPath");
406
            if (!Path.IsPathRooted(newPath))
407
                throw new ArgumentException("The newPath must be rooted", "newPath");
408
            Contract.EndContractBlock();
409

  
410
            var state = FileState.FindByFilePath(oldPath);
411

  
412
            if (state == null)
413
            {
414
                Log.WarnFormat("[NOFILE] Unable to set status for {0}.", oldPath);
415
                return;
416
            }
417
            //NOTE: This will cause problems if path is used as a key in relationships
418
            state.FilePath = newPath;
419
            state.Update();
268
            _persistenceAgent.Post(() =>FileState.RenameState(oldPath, newPath));
420 269
        }
421 270

  
422 271
        public void SetFileState(string path, FileStatus fileStatus, FileOverlayStatus overlayStatus)
......
427 276
                throw new ArgumentException("The path must be rooted", "path");
428 277
            Contract.EndContractBlock();
429 278

  
430
            UpdateStatus(path.ToLower(),state=>
431
                                  {
432
                                      state.FileStatus = fileStatus;
433
                                      state.OverlayStatus = overlayStatus;
434
                                  });            
279
            Debug.Assert(!path.Contains(FolderConstants.CacheFolder));
280
            Debug.Assert(!path.EndsWith(".ignore"));
281

  
282
            _persistenceAgent.Post(() => FileState.UpdateStatus(path.ToLower(), fileStatus, overlayStatus));
435 283
        }
436 284

  
437 285
        public void StoreInfo(string path,ObjectInfo objectInfo)
......
453 301
                    //Forgetting to use a sessionscope results in two sessions being created, one by 
454 302
                    //FirstOrDefault and one by Save()
455 303
                    var state =FileState.FindByFilePath(filePath);
304
                    
456 305
                    //Create a new empty state object if this is a new file
457 306
                    state = state ?? new FileState();
458 307

  
......
485 334

  
486 335
        
487 336
        public void SetFileStatus(string path, FileStatus status)
488
        {            
489
            UpdateStatus(path.ToLower(), state=>state.FileStatus = status);
337
        {
338
            if (String.IsNullOrWhiteSpace(path))
339
                throw new ArgumentNullException("path");
340
            if (!Path.IsPathRooted(path))
341
                throw new ArgumentException("The path must be rooted", "path");
342
            Contract.EndContractBlock();
343

  
344
            _persistenceAgent.Post(() => FileState.UpdateStatus(path.ToLower(), status));
490 345
        }
491 346

  
492 347
        public FileStatus GetFileStatus(string path)
......
497 352
                throw new ArgumentException("The path must be rooted", "path");
498 353
            Contract.EndContractBlock();
499 354

  
500
            var state = FileState.FindByFilePath(path);
501
            return (state==null)?FileStatus.Missing:state.FileStatus ;
355
            var status = from r in FileState.Queryable
356
                     where r.FilePath == path.ToLower()
357
                     select r.FileStatus;                        
358
            return status.Any()?status.First(): FileStatus.Missing;
502 359
        }
503 360

  
504 361
        public void ClearFileStatus(string path)
......
527 384
                throw new ArgumentException("The path must be rooted", "path");            
528 385
            Contract.EndContractBlock();
529 386

  
530
            _persistenceAgent.Post(() =>
531
            {
532
                using (new SessionScope())
533
                {
534
                    var state = FileState.FindByFilePath(path);
535
                    if (state == null)
536
                    {
537
                        Log.WarnFormat("[NOFILE] Unable to set checkesum for {0}.", path);
538
                        return;
539
                    }
540
                    state.Checksum = checksum;
541
                    state.Update();
542
                }
543
            });
387
            _persistenceAgent.Post(() => FileState.UpdateChecksum(path.ToLower(), checksum));
544 388
        }
545 389

  
546 390
    }

Also available in: Unified diff