Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Controller/Azure/DAL.cs @ 9166

Last change on this file since 9166 was 9166, checked in by fschoepp, 11 years ago

#1888:

  • Model: OptimizationScenario may be a tree of algorithms (and problems)
  • Model: Renamed InputParameters to ProblemParameters (as they are the parameters of a problem)
  • Model: Added JobExecutionDetails which contain Repetitions + Group (resource to use)
  • ScenarioParser parses the new XML scenario files
  • Website + Model: You are now able to add/remove rows from a table (no JavaScript involved yet)
  • Website + Controller: Added repetitions (enables batch jobs) and group (resource to use) to OaaS which will be used by the controller to schedule the job
  • Website: Updated templates to use new model structure
  • Website + Scenarios: Added the new algorithm Benchmark Algorithm
  • Controller: Added a singleton to make the (Azure/Mockup)-DAL exchangeable
  • Controller: Added mockup classes for DAL + IScenarioManager
  • Website/Result Page: Line Diagrams will be added via JavaScript, crawling their data using AJAX
  • Website: Most configuration parameters can be set in the ServiceDefinition directly
  • Added a mockup for the Membership classes: These can be used if no network connection is available or if other parts of the app shall be tested
  • Scenarios: Updated TSP mappings to new xsd
File size: 5.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Services.Optimization.ControllerService.Interfaces;
6using Microsoft.WindowsAzure;
7using Microsoft.WindowsAzure.StorageClient;
8using Microsoft.WindowsAzure.ServiceRuntime;
9using System.Diagnostics;
10
11namespace HeuristicLab.Services.Optimization.ControllerService.Azure {
12  public static class AzureConstants {
13    public static readonly string SCENARIO_TABLE = "Scenario";
14    public static readonly string SCENARIO_BLOB_CONTAINER = "scenario";
15    public static readonly string CLOUD_SETTINGS_KEY = "Cloudia.WindowsAzure.Storage";
16  }
17
18  public class ScenarioDao : IScenarioDao {   
19    public CloudTableClient TableClient { get; set; }
20
21    public bool Add(ScenarioEntity entity) {
22      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
23      TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
24      ScenarioEntity dbEntity = (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
25                               where e.RowKey == entity.RowKey
26                               select e).FirstOrDefault();
27      if (dbEntity != null)
28        return false;
29
30      serviceContext.AddObject(AzureConstants.SCENARIO_TABLE, entity);
31      serviceContext.SaveChanges();
32      return true;
33    }
34
35    public bool DeleteByName(string scenarioName) {
36      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
37      TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
38      ScenarioEntity entity = (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
39                               where e.RowKey == scenarioName
40                               select e).FirstOrDefault();
41      if (entity == null)
42        return false;
43
44      serviceContext.DeleteObject(entity);
45      serviceContext.SaveChangesWithRetries();
46      return true;
47    }
48
49    public ScenarioEntity FindByName(string scenarioName) {
50      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
51      TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
52      ScenarioEntity entity = (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
53                               where e.RowKey == scenarioName
54                               select e).FirstOrDefault();
55      return entity;
56    }
57
58
59    public IEnumerable<ScenarioEntity> GetAllEntities() {
60      TableServiceContext serviceContext = TableClient.GetDataServiceContext();
61      TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
62      return (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
63             select e).AsEnumerable();
64    }
65  }
66
67  public class BlobDao : IBlobDao {
68    public CloudBlobClient BlobClient { get; set; }
69
70    public bool Add(StringEntry entry) {
71      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.SCENARIO_BLOB_CONTAINER);
72      container.CreateIfNotExist();
73      var blob = container.GetBlobReference(entry.Key);
74      blob.UploadText(entry.Text);
75      return true;
76    }
77
78    public bool DeleteByKey(string entryKey) {
79      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.SCENARIO_BLOB_CONTAINER);
80      container.CreateIfNotExist();
81      var blob = container.GetBlobReference(entryKey);
82      return blob.DeleteIfExists();
83    }
84
85    public StringEntry FindByKey(string entryKey) {
86      CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.SCENARIO_BLOB_CONTAINER);
87      container.CreateIfNotExist();
88      var blob = container.GetBlobReference(entryKey);
89      return new StringEntry() { Key = entryKey, Text = blob.DownloadText() };     
90    }
91  }
92
93  public class AzureDataAccessLayer : IDataAccessLayer {
94    private IScenarioDao scenarioDao;
95    private IBlobDao blobDao;
96
97    private CloudStorageAccount storageAccount;
98
99    private CloudStorageAccount StorageAccount {
100      get {
101        if (storageAccount == null) {
102          try {
103            storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(AzureConstants.CLOUD_SETTINGS_KEY));
104          }
105          catch (Exception ex) {
106            Trace.WriteLine(ex.Message);
107            storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=optimizationstorage1;AccountKey=n7Leom8ZFWkof/VQ2a4aRSvwOlX+Gwr3uojQF9CFJw1osmGCV0WwaNC8s7nkZ+qteLduAgW2l75WFpbXrkvG4Q==");
108          }
109        }
110        return storageAccount;
111      }     
112    }
113
114    public IScenarioDao ScenarioDao {
115      get {
116        if (scenarioDao == null) {
117          scenarioDao = new ScenarioDao();
118        }
119        return scenarioDao;
120      }
121    }
122
123    public IBlobDao BlobDao {
124      get {
125        if (blobDao == null) {
126          blobDao = new BlobDao();
127        }
128        return blobDao;
129      }
130    }
131
132    public IScenarioDao CreateScenarioDao() {
133      return new ScenarioDao() { TableClient = StorageAccount.CreateCloudTableClient() };
134    }
135
136    public IBlobDao CreateBlobDao() {
137      return new BlobDao() { BlobClient = StorageAccount.CreateCloudBlobClient() };
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.