- Timestamp:
- 02/11/13 10:15:52 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OaaS/HeuristicLab.Services.Optimization.Controller/Azure/DAL.cs
r9166 r9215 8 8 using Microsoft.WindowsAzure.ServiceRuntime; 9 9 using System.Diagnostics; 10 using HeuristicLab.Services.Optimization.ControllerService.Model; 10 11 11 12 namespace HeuristicLab.Services.Optimization.ControllerService.Azure { … … 13 14 public static readonly string SCENARIO_TABLE = "Scenario"; 14 15 public static readonly string SCENARIO_BLOB_CONTAINER = "scenario"; 16 public static readonly string EXPERIMENT_TABLE = "Experiment"; 15 17 public static readonly string CLOUD_SETTINGS_KEY = "Cloudia.WindowsAzure.Storage"; 16 18 } … … 91 93 } 92 94 95 internal sealed class ExperimentEntity : TableServiceEntity { 96 public ExperimentEntity() { 97 } 98 99 public ExperimentEntity(string user, Experiment experiment) { 100 PartitionKey = "ScenarioPartition"; 101 RowKey = user + "_" + experiment.Name; 102 var scenarios = ""; 103 foreach (var scen in experiment.Scenarios) { 104 scenarios += scen.Id + ","; 105 } 106 Scenarios = scenarios.Remove(scenarios.Length - 1); 107 User = user; 108 } 109 110 public string User { get; set; } 111 112 public string Scenarios { get; set; } 113 114 } 115 116 public class ExperimentDao : IExperimentDao { 117 public CloudBlobClient BlobClient { get; set; } 118 public CloudTableClient TableClient { get; set; } 119 120 public bool Add(string username, Model.Experiment experiment) { 121 if (FindByName(username, experiment.Name) != null) 122 return false; 123 124 TableServiceContext serviceContext = TableClient.GetDataServiceContext(); 125 TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE); 126 var entity = new ExperimentEntity(username, experiment); 127 serviceContext.AddObject(AzureConstants.EXPERIMENT_TABLE, entity); 128 serviceContext.SaveChangesWithRetries(); 129 return true; 130 } 131 132 public bool DeleteByName(string username, string experiment) { 133 TableServiceContext serviceContext = TableClient.GetDataServiceContext(); 134 TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE); 135 var entity = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE) 136 where e.RowKey == (username + "_" + experiment) 137 select e).FirstOrDefault(); 138 139 if (entity == null) 140 return false; 141 142 serviceContext.DeleteObject(entity); 143 serviceContext.SaveChangesWithRetries(); 144 return true; 145 } 146 147 public Model.Experiment FindByName(string username, string experiment) { 148 TableServiceContext serviceContext = TableClient.GetDataServiceContext(); 149 TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE); 150 var entity = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE) 151 where e.RowKey == (username + "_" + experiment) 152 select e).FirstOrDefault(); 153 154 if (entity == null) { 155 return null; 156 } 157 158 var exp = Convert(entity); 159 return exp; 160 } 161 162 private Experiment Convert(ExperimentEntity entity) { 163 var exp = new Experiment() { Name = entity.RowKey.Split('_')[1] }; 164 foreach (var scenarioName in entity.Scenarios.Split(',')) 165 exp.Scenarios.Add(new OptimizationScenario() { Id = scenarioName }); 166 return exp; 167 } 168 169 public IEnumerable<Model.Experiment> GetExperiments(string user) { 170 TableServiceContext serviceContext = TableClient.GetDataServiceContext(); 171 TableClient.CreateTableIfNotExist(AzureConstants.EXPERIMENT_TABLE); 172 var entites = (from e in serviceContext.CreateQuery<ExperimentEntity>(AzureConstants.EXPERIMENT_TABLE) 173 where e.User == user 174 select e).ToList(); 175 var experiments = new List<Experiment>(); 176 foreach (var entity in entites) { 177 experiments.Add(Convert(entity)); 178 } 179 return experiments; 180 } 181 } 182 93 183 public class AzureDataAccessLayer : IDataAccessLayer { 94 184 private IScenarioDao scenarioDao; 95 185 private IBlobDao blobDao; 186 private IExperimentDao expDao; 96 187 97 188 private CloudStorageAccount storageAccount; … … 115 206 get { 116 207 if (scenarioDao == null) { 117 scenarioDao = new ScenarioDao() ;208 scenarioDao = new ScenarioDao() { TableClient = StorageAccount.CreateCloudTableClient() }; 118 209 } 119 210 return scenarioDao; … … 124 215 get { 125 216 if (blobDao == null) { 126 blobDao = new BlobDao() ;217 blobDao = new BlobDao() { BlobClient = StorageAccount.CreateCloudBlobClient() }; 127 218 } 128 219 return blobDao; … … 130 221 } 131 222 132 public IScenarioDao CreateScenarioDao() { 133 return new ScenarioDao() { TableClient = StorageAccount.CreateCloudTableClient() }; 134 } 135 136 public IBlobDao CreateBlobDao() { 137 return new BlobDao() { BlobClient = StorageAccount.CreateCloudBlobClient() }; 223 public IExperimentDao ExperimentDao { 224 get { 225 if (expDao == null) { 226 expDao = new ExperimentDao() { TableClient = StorageAccount.CreateCloudTableClient() }; 227 } 228 return expDao; 229 } 138 230 } 139 231 }
Note: See TracChangeset
for help on using the changeset viewer.