Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/AssignedResourceDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/AssignedResourceDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/AssignedResourceDao.cs	(revision 12691)
@@ -35,4 +35,24 @@
       return Table.Where(x => x.TaskId == taskId);
     }
+
+    public bool TaskIsAllowedToBeCalculatedBySlave(Guid taskId, Guid slaveId) {
+      return DataContext.ExecuteQuery<int>(TaskIsAllowedToBeCalculatedBySlaveQueryString, slaveId, taskId).First() > 0;
+    }
+
+    #region String queries
+    private const string TaskIsAllowedToBeCalculatedBySlaveQueryString = @"
+      WITH pr AS (
+        SELECT ResourceId, ParentResourceId
+        FROM [Resource]
+        WHERE ResourceId = {0}
+        UNION ALL
+        SELECT r.ResourceId, r.ParentResourceId
+        FROM [Resource] r JOIN pr ON r.ResourceId = pr.ParentResourceId
+      )
+      SELECT COUNT(ar.TaskId)
+      FROM pr JOIN AssignedResources ar ON pr.ResourceId = ar.ResourceId
+      WHERE ar.TaskId = {1}
+    ";
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/DowntimeDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/DowntimeDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/DowntimeDao.cs	(revision 12691)
@@ -29,6 +29,18 @@
 
     public override Downtime GetById(Guid id) {
-      return Table.SingleOrDefault(x => x.DowntimeId == id);
+      return GetByIdQuery(DataContext, id);
     }
+
+    public IQueryable<Downtime> GetByResourceId(Guid id) {
+      return Table.Where(x => x.ResourceId == id);
+    }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, Downtime> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid downtimeId) =>
+        (from downtime in db.GetTable<Downtime>()
+         where downtime.DowntimeId == downtimeId
+         select downtime).SingleOrDefault());
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/GenericDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/GenericDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/GenericDao.cs	(revision 12691)
@@ -109,5 +109,5 @@
 
     public bool Exists(T entity) {
-      return Table.Contains(entity);
+      return ExistsQuery(DataContext, entity);
     }
 
@@ -116,4 +116,10 @@
     }
     #endregion
+
+    #region Compiled queries
+    private static readonly Func<DataContext, T, bool> ExistsQuery =
+      CompiledQuery.Compile((DataContext db, T entity) =>
+        db.GetTable<T>().Contains(entity));
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/DimClientDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/DimClientDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/DimClientDao.cs	(revision 12691)
@@ -27,14 +27,10 @@
 namespace HeuristicLab.Services.Hive.DataAccess.Daos.HiveStatistics {
   public class DimClientDao : GenericDao<Guid, DimClient> {
-    private const string UpdateExpirationTimeQuery =
-      @"UPDATE [statistics].[DimClient] 
-           SET ExpirationTime = {0} 
-         WHERE Id IN ({1});";
-
     public DimClientDao(DataContext dataContext) : base(dataContext) { }
 
     public override DimClient GetById(Guid id) {
-      return Table.SingleOrDefault(x => x.Id == id);
+      return GetByIdQuery(DataContext, id);
     }
+
     public IQueryable<DimClient> GetActiveClients() {
       return Table.Where(x => x.ExpirationTime == null);
@@ -53,4 +49,19 @@
       return 0;
     }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, DimClient> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid id) =>
+        (from dimClient in db.GetTable<DimClient>()
+         where dimClient.Id == id
+         select dimClient).SingleOrDefault());
+    #endregion
+
+    #region String queries
+    private const string UpdateExpirationTimeQuery =
+      @"UPDATE [statistics].[DimClient] 
+           SET ExpirationTime = {0} 
+         WHERE Id IN ({1});";
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/DimJobDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/DimJobDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/DimJobDao.cs	(revision 12691)
@@ -29,5 +29,5 @@
 
     public override DimJob GetById(Guid id) {
-      return Table.SingleOrDefault(x => x.JobId == id);
+      return GetByIdQuery(DataContext, id);
     }
 
@@ -43,4 +43,12 @@
       return Table.Where(x => x.DateCompleted != null);
     }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, DimJob> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid id) =>
+        (from dimJob in db.GetTable<DimJob>()
+         where dimJob.JobId == id
+         select dimJob).SingleOrDefault());
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/DimTimeDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/DimTimeDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/DimTimeDao.cs	(revision 12691)
@@ -29,9 +29,23 @@
 
     public override DimTime GetById(DateTime id) {
-      return Table.SingleOrDefault(x => x.Time == id);
+      return GetByIdQuery(DataContext, id);
     }
     public DimTime GetLastEntry() {
-      return Table.OrderByDescending(x => x.Time).FirstOrDefault();
+      return GetLastEntryQuery(DataContext);
     }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, DateTime, DimTime> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, DateTime id) =>
+        (from dimTime in db.GetTable<DimTime>()
+         where dimTime.Time == id
+         select dimTime).SingleOrDefault());
+
+    private static readonly Func<DataContext, DimTime> GetLastEntryQuery =
+      CompiledQuery.Compile((DataContext db) =>
+        (from dimTime in db.GetTable<DimTime>()
+         orderby dimTime.Time descending
+         select dimTime).FirstOrDefault());
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/DimUserDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/DimUserDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/DimUserDao.cs	(revision 12691)
@@ -29,6 +29,14 @@
 
     public override DimUser GetById(Guid id) {
-      return Table.SingleOrDefault(x => x.UserId == id);
+      return GetByIdQuery(DataContext, id);
     }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, DimUser> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid id) =>
+        (from dimUser in db.GetTable<DimUser>()
+         where dimUser.UserId == id
+         select dimUser).SingleOrDefault());
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/FactClientInfoDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/FactClientInfoDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/FactClientInfoDao.cs	(revision 12691)
@@ -53,9 +53,10 @@
       return from cf in Table
              join r in DimClientTable on cf.ClientId equals r.Id
-             group cf by r.ResourceId into grpFacts
-             select new LastUpdateTimestamp {
-               ResourceId = grpFacts.Key,
-               Timestamp = grpFacts.Max(x => x.Time),
-             };
+             group cf by r.ResourceId
+               into grpFacts
+               select new LastUpdateTimestamp {
+                 ResourceId = grpFacts.Key,
+                 Timestamp = grpFacts.Max(x => x.Time),
+               };
     }
 
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/FactTaskDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/FactTaskDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/HiveStatistics/FactTaskDao.cs	(revision 12691)
@@ -37,11 +37,8 @@
     }
 
-    private const string BatchDeleteQuery =
-      @"DELETE FROM [statistics].[FactTask]
-         WHERE TaskId IN ({0});";
     public FactTaskDao(DataContext dataContext) : base(dataContext) { }
 
     public override FactTask GetById(Guid id) {
-      return Table.SingleOrDefault(x => x.TaskId == id);
+      return GetByIdQuery(DataContext, id);
     }
 
@@ -55,5 +52,5 @@
 
     public DateTime? GetLastCompletedTaskFromJob(Guid id) {
-      return Table.Where(x => x.JobId == id && x.EndTime != null).Max(x => x.EndTime);
+      return GetLastCompletedTaskFromJobQuery(DataContext, id);
     }
 
@@ -92,4 +89,24 @@
     }
 
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, FactTask> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid id) =>
+        (from factTask in db.GetTable<FactTask>()
+         where factTask.TaskId == id
+         select factTask).SingleOrDefault());
+
+
+    private static readonly Func<DataContext, Guid, DateTime?> GetLastCompletedTaskFromJobQuery =
+      CompiledQuery.Compile((DataContext db, Guid id) =>
+        (from factTask in db.GetTable<FactTask>()
+         where factTask.JobId == id && factTask.EndTime != null
+         select factTask.EndTime).Max());
+    #endregion
+
+    #region String queries
+    private const string BatchDeleteQuery =
+      @"DELETE FROM [statistics].[FactTask]
+         WHERE TaskId IN ({0});";
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/JobDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/JobDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/JobDao.cs	(revision 12691)
@@ -29,6 +29,14 @@
 
     public override Job GetById(Guid id) {
-      return Table.SingleOrDefault(x => x.JobId == id);
+      return GetByIdQuery(DataContext, id);
     }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, Job> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid jobId) =>
+        (from job in db.GetTable<Job>()
+         where job.JobId == jobId
+         select job).SingleOrDefault());
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/JobPermissionDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/JobPermissionDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/JobPermissionDao.cs	(revision 12691)
@@ -33,6 +33,39 @@
 
     public JobPermission GetByJobAndUserId(Guid jobId, Guid userId) {
-      return Table.SingleOrDefault(x => x.JobId == jobId && x.GrantedUserId == userId);
+      return GetByJobAndUserIdQuery(DataContext, jobId, userId);
     }
+
+    public IQueryable<JobPermission> GetByJobId(Guid jobId) {
+      return Table.Where(x => x.JobId == jobId);
+    }
+
+    public void SetJobPermission(Guid jobId, Guid grantedByUserId, Guid grantedUserId, Permission permission) {
+      var jobPermission = GetByJobAndUserId(jobId, grantedUserId);
+      if (jobPermission != null) {
+        if (permission == Permission.NotAllowed) {
+          Delete(jobPermission);
+        } else {
+          jobPermission.Permission = permission;
+          jobPermission.GrantedByUserId = grantedByUserId;
+        }
+      } else {
+        if (permission != Permission.NotAllowed) {
+          Save(new JobPermission {
+            JobId = jobId,
+            GrantedByUserId = grantedByUserId,
+            GrantedUserId = grantedUserId,
+            Permission = permission
+          });
+        }
+      }
+    }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, Guid, JobPermission> GetByJobAndUserIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid jobId, Guid userId) =>
+        (from jobPermission in db.GetTable<JobPermission>()
+         where jobPermission.JobId == jobId && jobPermission.GrantedUserId == userId
+         select jobPermission).SingleOrDefault());
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/LifecycleDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/LifecycleDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/LifecycleDao.cs	(revision 12691)
@@ -20,4 +20,5 @@
 #endregion
 
+using System;
 using System.Data.Linq;
 using System.Linq;
@@ -28,6 +29,35 @@
 
     public override Lifecycle GetById(int id) {
-      return Table.SingleOrDefault(x => x.LifecycleId == id);
+      return GetByIdQuery(DataContext, id);
     }
+
+    public Lifecycle GetLastLifecycle() {
+      return GetLastLifecycleQuery(DataContext);
+    }
+
+    public void UpdateLifecycle() {
+      var entity = GetLastLifecycle();
+      if (entity != null) {
+        entity.LastCleanup = DateTime.Now;
+      } else {
+        Save(new Lifecycle {
+          LifecycleId = 0,
+          LastCleanup = DateTime.Now
+        });
+      }
+    }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, int, Lifecycle> GetByIdQuery =
+     CompiledQuery.Compile((DataContext db, int lifecycleId) =>
+       (from lifecycle in db.GetTable<Lifecycle>()
+        where lifecycle.LifecycleId == lifecycleId
+        select lifecycle).SingleOrDefault());
+
+    private static readonly Func<DataContext, Lifecycle> GetLastLifecycleQuery =
+      CompiledQuery.Compile((DataContext db) =>
+        (from lifecycle in db.GetTable<Lifecycle>()
+         select lifecycle).SingleOrDefault());
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/PluginDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/PluginDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/PluginDao.cs	(revision 12691)
@@ -21,4 +21,5 @@
 
 using System;
+using System.Collections.Generic;
 using System.Data.Linq;
 using System.Linq;
@@ -29,6 +30,24 @@
 
     public override Plugin GetById(Guid id) {
-      return Table.SingleOrDefault(x => x.PluginId == id);
+      return GetByIdQuery(DataContext, id);
     }
+
+    public IEnumerable<Plugin> GetByHash(byte[] hash) {
+      return GetByHashQuery(DataContext).Where(x => x.Hash.SequenceEqual(hash));
+    }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, Plugin> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid pluginId) =>
+        (from plugin in db.GetTable<Plugin>()
+         where plugin.PluginId == pluginId
+         select plugin).SingleOrDefault());
+
+    private static readonly Func<DataContext, IEnumerable<Plugin>> GetByHashQuery =
+      CompiledQuery.Compile((DataContext db) =>
+        (from plugin in db.GetTable<Plugin>()
+         where plugin.Hash != null
+         select plugin));
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/PluginDataDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/PluginDataDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/PluginDataDao.cs	(revision 12691)
@@ -29,6 +29,14 @@
 
     public override PluginData GetById(Guid id) {
-      return Table.SingleOrDefault(x => x.PluginDataId == id);
+      return GetByIdQuery(DataContext, id);
     }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, PluginData> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid pluginDataId) =>
+        (from pluginData in db.GetTable<PluginData>()
+         where pluginData.PluginDataId == pluginDataId
+         select pluginData).SingleOrDefault());
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/RequiredPluginDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/RequiredPluginDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/RequiredPluginDao.cs	(revision 12691)
@@ -29,6 +29,14 @@
 
     public override RequiredPlugin GetById(Guid id) {
-      return Table.SingleOrDefault(x => x.RequiredPluginId == id);
+      return GetByIdQuery(DataContext, id);
     }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, RequiredPlugin> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid requiredPluginId) =>
+        (from requiredPlugin in db.GetTable<RequiredPlugin>()
+         where requiredPlugin.RequiredPluginId == requiredPluginId
+         select requiredPlugin).SingleOrDefault());
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/ResourceDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/ResourceDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/ResourceDao.cs	(revision 12691)
@@ -29,5 +29,9 @@
 
     public override Resource GetById(Guid id) {
-      return Table.SingleOrDefault(x => x.ResourceId == id);
+      return GetByIdQuery(DataContext, id);
+    }
+
+    public Resource GetByName(string name) {
+      return GetByNameQuery(DataContext, name);
     }
 
@@ -35,4 +39,18 @@
       return Table.Where(x => x.OwnerUserId != null);
     }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, Resource> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid resourceId) =>
+        (from resource in db.GetTable<Resource>()
+         where resource.ResourceId == resourceId
+         select resource).SingleOrDefault());
+
+    private static readonly Func<DataContext, string, Resource> GetByNameQuery =
+     CompiledQuery.Compile((DataContext db, string name) =>
+       (from resource in db.GetTable<Resource>()
+        where resource.Name == name
+        select resource).FirstOrDefault());
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/ResourcePermissionDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/ResourcePermissionDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/ResourcePermissionDao.cs	(revision 12691)
@@ -21,5 +21,7 @@
 
 using System;
+using System.Collections.Generic;
 using System.Data.Linq;
+using System.Linq;
 
 namespace HeuristicLab.Services.Hive.DataAccess.Daos {
@@ -30,4 +32,31 @@
       throw new NotImplementedException();
     }
+
+    public IEnumerable<ResourcePermission> GetByResourceId(Guid id) {
+      return GetByResourceIdGetByIdQuery(DataContext, id);
+    }
+
+    public void DeleteByResourceAndGrantedUserId(Guid resourceId, IEnumerable<Guid> grantedUserId) {
+      string paramIds = string.Join(",", grantedUserId.Select(x => string.Format("'{0}'", x)));
+      if (!string.IsNullOrWhiteSpace(paramIds)) {
+        string query = string.Format(DeleteByGrantedUserQuery, resourceId, paramIds);
+        DataContext.ExecuteCommand(query);
+      }
+    }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, IEnumerable<ResourcePermission>> GetByResourceIdGetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid resourceId) =>
+        from resourcePermission in db.GetTable<ResourcePermission>()
+        where resourcePermission.ResourceId == resourceId
+        select resourcePermission);
+    #endregion
+
+    #region String queries
+    private const string DeleteByGrantedUserQuery =
+      @"DELETE FROM [ResourcePermission]
+         WHERE ResourceId = '{0}'
+           AND GrantedUserId IN ({1});";
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/SlaveDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/SlaveDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/SlaveDao.cs	(revision 12691)
@@ -46,5 +46,5 @@
     #region IGenericDao<Guid,Slave> Members
     public Slave GetById(Guid id) {
-      return Entities.SingleOrDefault(x => x.ResourceId == id);
+      return GetByIdQuery(dataContext, id);
     }
 
@@ -117,5 +117,5 @@
 
     public bool Exists(Slave entity) {
-      return Table.Contains(entity);
+      return ExistsQuery(dataContext, entity);
     }
 
@@ -124,4 +124,41 @@
     }
     #endregion
+
+    public bool SlaveHasToShutdownComputer(Guid slaveId) {
+      return dataContext.ExecuteQuery<int>(DowntimeQueryString, slaveId, DateTime.Now, DowntimeType.Shutdown.ToString()).FirstOrDefault() > 0;
+    }
+
+    public bool SlaveIsAllowedToCalculate(Guid slaveId) {
+      return dataContext.ExecuteQuery<int>(DowntimeQueryString, slaveId, DateTime.Now, DowntimeType.Offline.ToString()).FirstOrDefault() == 0;
+    }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, Slave> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid slaveId) =>
+        (from slave in db.GetTable<Resource>().OfType<Slave>()
+         where slave.ResourceId == slaveId
+         select slave).SingleOrDefault());
+
+    private static readonly Func<DataContext, Slave, bool> ExistsQuery =
+      CompiledQuery.Compile((DataContext db, Slave entity) =>
+        db.GetTable<Resource>().OfType<Slave>().Contains(entity));
+    #endregion
+
+    #region String queries
+    private const string DowntimeQueryString = @"
+      WITH pr AS (
+        SELECT ResourceId, ParentResourceId
+        FROM [Resource]
+        WHERE ResourceId = {0}
+        UNION ALL
+        SELECT r.ResourceId, r.ParentResourceId
+        FROM [Resource] r JOIN pr ON r.ResourceId = pr.ParentResourceId
+      )
+      SELECT COUNT(dt.DowntimeId)
+      FROM pr JOIN [Downtime] dt ON pr.ResourceId = dt.ResourceId
+      WHERE {1} BETWEEN dt.StartDate AND dt.EndDate
+        AND dt.DowntimeType = {2}
+    ";
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/SlaveGroupDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/SlaveGroupDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/SlaveGroupDao.cs	(revision 12691)
@@ -45,5 +45,5 @@
     #region IGenericDao<Guid,SlaveGroup> Members
     public SlaveGroup GetById(Guid id) {
-      return Entities.SingleOrDefault(x => x.ResourceId == id);
+      return GetByIdQuery(dataContext, id);
     }
 
@@ -112,5 +112,5 @@
 
     public bool Exists(SlaveGroup entity) {
-      return Table.Contains(entity);
+      return ExistsQuery(dataContext, entity);
     }
 
@@ -119,4 +119,16 @@
     }
     #endregion
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, SlaveGroup> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid slaveGroupId) =>
+        (from slaveGroup in db.GetTable<Resource>().OfType<SlaveGroup>()
+         where slaveGroup.ResourceId == slaveGroupId
+         select slaveGroup).SingleOrDefault());
+
+    private static readonly Func<DataContext, SlaveGroup, bool> ExistsQuery =
+      CompiledQuery.Compile((DataContext db, SlaveGroup slaveGroup) =>
+        db.GetTable<Resource>().OfType<SlaveGroup>().Contains(slaveGroup));
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/StateLogDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/StateLogDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/StateLogDao.cs	(revision 12691)
@@ -26,5 +26,4 @@
 namespace HeuristicLab.Services.Hive.DataAccess.Daos {
   public class StateLogDao : GenericDao<Guid, StateLog> {
-
     private Table<Task> TaskTable {
       get { return DataContext.GetTable<Task>(); }
@@ -34,13 +33,25 @@
 
     public override StateLog GetById(Guid id) {
-      return Table.SingleOrDefault(x => x.StateLogId == id);
+      return GetByIdQuery(DataContext, id);
     }
 
     public StateLog GetLastStateLogFromTask(Task task) {
-      return Table
-        .Where(x => x.TaskId == task.TaskId)
-        .OrderByDescending(x => x.DateTime)
-        .First(x => x.SlaveId != null);
+      return GetLastStateLogFromTaskQuery(DataContext, task);
     }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, StateLog> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid stateLogId) =>
+        (from stateLog in db.GetTable<StateLog>()
+         where stateLog.StateLogId == stateLogId
+         select stateLog).SingleOrDefault());
+
+    private static readonly Func<DataContext, Task, StateLog> GetLastStateLogFromTaskQuery =
+      CompiledQuery.Compile((DataContext db, Task task) =>
+        (from stateLog in db.GetTable<StateLog>()
+         where stateLog.TaskId == task.TaskId
+         orderby stateLog.DateTime descending
+         select stateLog).First(x => x.SlaveId != null));
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/TaskDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/TaskDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/TaskDao.cs	(revision 12691)
@@ -21,4 +21,5 @@
 
 using System;
+using System.Collections.Generic;
 using System.Data.Linq;
 using System.Linq;
@@ -29,5 +30,5 @@
 
     public override Task GetById(Guid id) {
-      return Table.SingleOrDefault(x => x.TaskId == id);
+      return GetByIdQuery(DataContext, id);
     }
 
@@ -39,4 +40,55 @@
       return Table.Where(x => x.JobId == id);
     }
+    public class TaskPriorityInfo {
+      public Guid JobId { get; set; }
+      public Guid TaskId { get; set; }
+      public int Priority { get; set; }
+    }
+
+    public IEnumerable<TaskPriorityInfo> GetWaitingTasks(Slave slave) {
+      //Originally we checked here if there are parent tasks which should be calculated (with GetParentTasks(resourceIds, count, false);).
+      //Because there is at the moment no case where this makes sense (there don't exist parent tasks which need to be calculated), 
+      //we skip this step because it's wasted runtime
+      return DataContext.ExecuteQuery<TaskPriorityInfo>(GetWaitingTasksQueryString, slave.ResourceId, Enum.GetName(typeof(TaskState), TaskState.Waiting), slave.FreeCores, slave.FreeMemory).ToList();
+    }
+
+    public void UpdateExecutionTime(Guid taskId, double executionTime) {
+      DataContext.ExecuteCommand(UpdateExecutionTimeQuery, executionTime, DateTime.Now, taskId);
+    }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, Task> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid taskId) =>
+        (from task in db.GetTable<Task>()
+         where task.TaskId == taskId
+         select task).SingleOrDefault());
+    #endregion
+
+    #region String queries
+    private const string GetWaitingTasksQueryString = @"
+      WITH pr AS (
+        SELECT ResourceId, ParentResourceId
+        FROM [Resource]
+        WHERE ResourceId = {0}
+        UNION ALL
+        SELECT r.ResourceId, r.ParentResourceId
+        FROM [Resource] r JOIN pr ON r.ResourceId = pr.ParentResourceId
+      )
+      SELECT DISTINCT t.TaskId, t.JobId, t.Priority
+      FROM pr JOIN AssignedResources ar ON ar.ResourceId = pr.ResourceId
+          JOIN Task t ON t.TaskId = ar.TaskId
+      WHERE NOT (t.IsParentTask = 1 AND t.FinishWhenChildJobsFinished = 1)
+          AND t.TaskState = {1}
+          AND t.CoresNeeded <= {2}
+          AND t.MemoryNeeded <= {3}
+    ";
+
+    private const string UpdateExecutionTimeQuery = @"
+      UPDATE [Task] 
+         SET ExecutionTimeMs = {0},
+             LastHeartbeat = {1} 
+       WHERE TaskId = {2}
+    ";
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/TaskDataDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/TaskDataDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/TaskDataDao.cs	(revision 12691)
@@ -29,6 +29,14 @@
 
     public override TaskData GetById(Guid id) {
-      return Table.SingleOrDefault(x => x.TaskId == id);
+      return GetByIdQuery(DataContext, id);
     }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, TaskData> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid taskId) =>
+        (from taskData in db.GetTable<TaskData>()
+         where taskData.TaskId == taskId
+         select taskData).SingleOrDefault());
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/UserPriorityDao.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/UserPriorityDao.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/UserPriorityDao.cs	(revision 12691)
@@ -29,6 +29,14 @@
 
     public override UserPriority GetById(Guid id) {
-      return Table.SingleOrDefault(x => x.UserId == id);
+      return GetByIdQuery(DataContext, id);
     }
+
+    #region Compiled queries
+    private static readonly Func<DataContext, Guid, UserPriority> GetByIdQuery =
+      CompiledQuery.Compile((DataContext db, Guid userId) =>
+        (from userPriority in db.GetTable<UserPriority>()
+         where userPriority.UserId == userId
+         select userPriority).SingleOrDefault());
+    #endregion
   }
 }
Index: branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Manager/PersistenceManager.cs
===================================================================
--- branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Manager/PersistenceManager.cs	(revision 12584)
+++ branches/HiveStatistics/sources/HeuristicLab.Services.Hive.DataAccess/3.3/Manager/PersistenceManager.cs	(revision 12691)
@@ -35,92 +35,116 @@
 
     #region Hive daos
+
+    private AssignedResourceDao assignedResourceDao;
     public AssignedResourceDao AssignedResourceDao {
-      get { return new AssignedResourceDao(dataContext); }
-    }
-
+      get { return assignedResourceDao ?? (assignedResourceDao = new AssignedResourceDao(dataContext)); }
+    }
+
+    private DowntimeDao downtimeDao;
     public DowntimeDao DowntimeDao {
-      get { return new DowntimeDao(dataContext); }
-    }
-
+      get { return downtimeDao ?? (downtimeDao = new DowntimeDao(dataContext)); }
+    }
+
+    private JobDao jobDao;
     public JobDao JobDao {
-      get { return new JobDao(dataContext); }
-    }
-
+      get { return jobDao ?? (jobDao = new JobDao(dataContext)); }
+    }
+
+    private JobPermissionDao jobPermissionDao;
     public JobPermissionDao JobPermissionDao {
-      get { return new JobPermissionDao(dataContext); }
-    }
-
+      get { return jobPermissionDao ?? (jobPermissionDao = new JobPermissionDao(dataContext)); }
+    }
+
+    private LifecycleDao lifecycleDao;
     public LifecycleDao LifecycleDao {
-      get { return new LifecycleDao(dataContext); }
-    }
-
+      get { return lifecycleDao ?? (lifecycleDao = new LifecycleDao(dataContext)); }
+    }
+
+    private PluginDao pluginDao;
     public PluginDao PluginDao {
-      get { return new PluginDao(dataContext); }
-    }
-
+      get { return pluginDao ?? (pluginDao = new PluginDao(dataContext)); }
+    }
+
+    private PluginDataDao pluginDataDao;
     public PluginDataDao PluginDataDao {
-      get { return new PluginDataDao(dataContext); }
-    }
-
+      get { return pluginDataDao ?? (pluginDataDao = new PluginDataDao(dataContext)); }
+    }
+
+    private RequiredPluginDao requiredPluginDao;
     public RequiredPluginDao RequiredPluginDao {
-      get { return new RequiredPluginDao(dataContext); }
-    }
-
+      get { return requiredPluginDao ?? (requiredPluginDao = new RequiredPluginDao(dataContext)); }
+    }
+
+    private ResourceDao resourceDao;
     public ResourceDao ResourceDao {
-      get { return new ResourceDao(dataContext); }
-    }
-
+      get { return resourceDao ?? (resourceDao = new ResourceDao(dataContext)); }
+    }
+
+    private ResourcePermissionDao resourcePermissionDao;
     public ResourcePermissionDao ResourcePermissionDao {
-      get { return new ResourcePermissionDao(dataContext); }
-    }
-
+      get { return resourcePermissionDao ?? (resourcePermissionDao = new ResourcePermissionDao(dataContext)); }
+    }
+
+    private SlaveDao slaveDao;
     public SlaveDao SlaveDao {
-      get { return new SlaveDao(dataContext); }
-    }
-
+      get { return slaveDao ?? (slaveDao = new SlaveDao(dataContext)); }
+    }
+
+    private SlaveGroupDao slaveGroupDao;
     public SlaveGroupDao SlaveGroupDao {
-      get { return new SlaveGroupDao(dataContext); }
-    }
-
+      get { return slaveGroupDao ?? (slaveGroupDao = new SlaveGroupDao(dataContext)); }
+    }
+
+    private StateLogDao stateLogDao;
     public StateLogDao StateLogDao {
-      get { return new StateLogDao(dataContext); }
-    }
-
+      get { return stateLogDao ?? (stateLogDao = new StateLogDao(dataContext)); }
+    }
+
+    private TaskDao taskDao;
     public TaskDao TaskDao {
-      get { return new TaskDao(dataContext); }
-    }
-
+      get { return taskDao ?? (taskDao = new TaskDao(dataContext)); }
+    }
+
+    private TaskDataDao taskDataDao;
     public TaskDataDao TaskDataDao {
-      get { return new TaskDataDao(dataContext); }
-    }
-
+      get { return taskDataDao ?? (taskDataDao = new TaskDataDao(dataContext)); }
+    }
+
+    private UserPriorityDao userPriorityDao;
     public UserPriorityDao UserPriorityDao {
-      get { return new UserPriorityDao(dataContext); }
+      get { return userPriorityDao ?? (userPriorityDao = new UserPriorityDao(dataContext)); }
     }
     #endregion
 
     #region HiveStatistics daos
+
+    private DimClientDao dimClientDao;
     public DimClientDao DimClientDao {
-      get { return new DimClientDao(dataContext); }
-    }
-
+      get { return dimClientDao ?? (dimClientDao = new DimClientDao(dataContext)); }
+    }
+
+    private DimJobDao dimJobDao;
     public DimJobDao DimJobDao {
-      get { return new DimJobDao(dataContext); }
-    }
-
+      get { return dimJobDao ?? (dimJobDao = new DimJobDao(dataContext)); }
+    }
+
+    private DimTimeDao dimTimeDao;
     public DimTimeDao DimTimeDao {
-      get { return new DimTimeDao(dataContext); }
-    }
-
+      get { return dimTimeDao ?? (dimTimeDao = new DimTimeDao(dataContext)); }
+    }
+
+    private DimUserDao dimUserDao;
     public DimUserDao DimUserDao {
-      get { return new DimUserDao(dataContext); }
-    }
-
+      get { return dimUserDao ?? (dimUserDao = new DimUserDao(dataContext)); }
+    }
+
+    private FactClientInfoDao factClientInfoDao;
     public FactClientInfoDao FactClientInfoDao {
-      get { return new FactClientInfoDao(dataContext); }
-    }
-
+      get { return factClientInfoDao ?? (factClientInfoDao = new FactClientInfoDao(dataContext)); }
+    }
+
+    private FactTaskDao factTaskDao;
     public FactTaskDao FactTaskDao {
-      get { return new FactTaskDao(dataContext); }
+      get { return factTaskDao ?? (factTaskDao = new FactTaskDao(dataContext)); }
     }
     #endregion
@@ -130,4 +154,8 @@
       if (longRunning) context.CommandTimeout = (int)Settings.Default.LongRunningDatabaseCommandTimeout.TotalSeconds;
       dataContext = context;
+    }
+
+    public PersistenceManager(DataContext dataContext) {
+      this.dataContext = dataContext;
     }
 
@@ -179,7 +207,7 @@
 
     public void Dispose() {
-      if (dataContext != null) {
-        dataContext.Dispose();
-      }
+      //if (dataContext != null) {
+      //  dataContext.Dispose();
+      //}
     }
   }
