- Timestamp:
- 04/19/13 17:16:49 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HivePerformance/sources/HeuristicLab.Services.Hive/3.3/HiveDao.cs
r9381 r9385 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Data.Linq; 24 25 using System.Linq; 25 26 using System.Linq.Expressions; … … 45 46 public Task GetTaskDA(Guid id) { 46 47 var db = HiveOperationContext.Current.DataContext; 47 return db.Tasks.SingleOrDefault(x => x.TaskId == id); 48 } 48 return GetTaskByIdQuery(db, id).SingleOrDefault(); 49 } 50 51 private static Func<HiveDataContext, Guid, IQueryable<Task>> GetTaskByIdQuery = CompiledQuery.Compile((HiveDataContext db, Guid id) => 52 from t in db.Tasks 53 where t.TaskId == id 54 select t 55 ); 49 56 50 57 public IEnumerable<DT.Task> GetTasks(Expression<Func<Task, bool>> predicate) { … … 205 212 } 206 213 207 public I Enumerable<TaskInfoForScheduler> GetWaitingTasksDA(Slave slave) {214 public IQueryable<TaskInfoForScheduler> GetWaitingTasksDA(Slave slave) { 208 215 var db = HiveOperationContext.Current.DataContext; 216 209 217 var parentResources = GetParentResourcesDA(slave.ResourceId); 210 218 var resourceIds = parentResources.Select(x => x.ResourceId); … … 213 221 //we skip this step because it's wasted runtime 214 222 215 var query = from ar in db.AssignedResources 216 where resourceIds.Contains(ar.ResourceId) 217 && !(ar.Task.IsParentTask && ar.Task.FinishWhenChildJobsFinished) 218 && ar.Task.State == TaskState.Waiting 219 && ar.Task.CoresNeeded <= slave.FreeCores 220 && ar.Task.MemoryNeeded <= slave.FreeMemory 221 select new TaskInfoForScheduler() { TaskId = ar.Task.TaskId, JobId = ar.Task.JobId, Priority = ar.Task.Priority }; 222 var waitingTasks = query.ToArray(); 223 return waitingTasks; 224 } 223 return from ar in db.AssignedResources 224 where resourceIds.Contains(ar.ResourceId) 225 && !(ar.Task.IsParentTask && ar.Task.FinishWhenChildJobsFinished) 226 && ar.Task.State == TaskState.Waiting 227 && ar.Task.CoresNeeded <= slave.FreeCores 228 && ar.Task.MemoryNeeded <= slave.FreeMemory 229 select new TaskInfoForScheduler() { TaskId = ar.Task.TaskId, JobId = ar.Task.JobId, Priority = ar.Task.Priority }; 230 } 231 232 /*private static Func<HiveDataContext, Guid, Slave, IQueryable<TaskInfoForScheduler>> GetWaitingTasksQuery = CompiledQuery.Compile((HiveDataContext db, Guid id, Slave slave) => 233 from ar in db.AssignedResources 234 where ar.ResourceId == id 235 && !(ar.Task.IsParentTask && ar.Task.FinishWhenChildJobsFinished) 236 && ar.Task.State == TaskState.Waiting 237 && ar.Task.CoresNeeded <= slave.FreeCores 238 && ar.Task.MemoryNeeded <= slave.FreeMemory 239 select new TaskInfoForScheduler() { TaskId = ar.Task.TaskId, JobId = ar.Task.JobId, Priority = ar.Task.Priority } 240 );*/ 225 241 226 242 public DT.Task UpdateTaskState(Guid taskId, TaskState taskState, Guid? slaveId, Guid? userId, string exception) { … … 265 281 }); 266 282 267 var task = db.Tasks.SingleOrDefault(x => x.TaskId ==taskId);283 var task = GetTaskDA(taskId); 268 284 task.State = taskState; 269 285 … … 567 583 public Slave GetSlaveDA(Guid id) { 568 584 var db = HiveOperationContext.Current.DataContext; 569 return db.Resources.OfType<Slave>().SingleOrDefault(x => x.ResourceId == id); 570 } 585 return GetSlaveByIdQuery(db, id).SingleOrDefault(); 586 } 587 588 private static Func<HiveDataContext, Guid, IQueryable<Slave>> GetSlaveByIdQuery = CompiledQuery.Compile((HiveDataContext db, Guid slaveId) => 589 from s in db.Resources.OfType<Slave>() 590 where s.ResourceId == slaveId 591 select s 592 ); 571 593 572 594 public IEnumerable<DT.Slave> GetSlaves(Expression<Func<Slave, bool>> predicate) { … … 714 736 } 715 737 738 public IQueryable<Guid> GetAssignedResourcesIdsDA(Guid taskId) { 739 var db = HiveOperationContext.Current.DataContext; 740 return GetAssignedResourcesIdQuery(db, taskId); 741 } 742 743 private static Func<HiveDataContext, Guid, IQueryable<Guid>> GetAssignedResourcesIdQuery = CompiledQuery.Compile((HiveDataContext db, Guid taskId) => 744 from ar in db.AssignedResources 745 where ar.TaskId == taskId 746 select ar.ResourceId 747 ); 748 716 749 /// <summary> 717 750 /// Returns all parent resources of a resource (the given resource is also added) … … 727 760 public IEnumerable<Resource> GetParentResourcesDA(Guid resourceId) { 728 761 var db = HiveOperationContext.Current.DataContext; 729 var resources = new List<Resource>(); 730 CollectParentResources(resources, db.Resources.Where(r => r.ResourceId == resourceId).Single()); 731 return resources; 732 } 733 734 private void CollectParentResources(List<Resource> resources, Resource resource) { 762 var child = db.Resources.Single(r => r.ResourceId == resourceId); 763 764 yield return child; 765 while (child.ParentResource != null) { 766 child = child.ParentResource; 767 yield return child; 768 } 769 } 770 771 public IEnumerable<Guid> GetParentResourcesIDsDA(Guid resourceId) { 772 var db = HiveOperationContext.Current.DataContext; 773 var child = db.Resources.Single(r => r.ResourceId == resourceId); 774 775 yield return resourceId; 776 while (child.ParentResource != null) { 777 child = child.ParentResource; 778 yield return child.ResourceId; 779 } 780 } 781 782 public IQueryable<int> GetNumberOfDowntimesFromParentResourcesAtCurrentTimeDA(Guid resourceId, DowntimeType type) { 783 var db = HiveOperationContext.Current.DataContext; 784 785 var ids = GetParentResourcesIDsDA(resourceId).ToArray(); 786 787 return from r in db.Resources 788 where ids.Contains(r.ResourceId) 789 select (from d in db.Downtimes 790 where d.ResourceId == r.ResourceId && d.DowntimeType == type && (DateTime.Now >= d.StartDate) && (DateTime.Now <= d.EndDate) 791 select d).Count(); 792 } 793 794 /*private static Func<HiveDataContext, Guid, DowntimeType, int> GetNumberOfDowntimesAtCurrentTimeQuery = 795 CompiledQuery.Compile((HiveDataContext db, Guid ids, DowntimeType type) => 796 (from d in db.Downtimes 797 where d.ResourceId == ids && d.DowntimeType == type && (DateTime.Now >= d.StartDate) && (DateTime.Now <= d.EndDate) 798 select d).Count() 799 );*/ 800 801 private static void CollectParentResources(ICollection<Resource> resources, Resource resource) { 735 802 if (resource == null) return; 736 803 resources.Add(resource);
Note: See TracChangeset
for help on using the changeset viewer.