1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Services.Optimization.ControllerService.Interfaces;
|
---|
6 | using Microsoft.WindowsAzure;
|
---|
7 | using Microsoft.WindowsAzure.StorageClient;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Services.Optimization.ControllerService.Azure {
|
---|
10 | public static class AzureConstants {
|
---|
11 | public static readonly string SCENARIO_TABLE = "Scenario";
|
---|
12 | public static readonly string SCENARIO_BLOB_CONTAINER = "scenario";
|
---|
13 | public static readonly string CLOUD_SETTINGS_KEY = "Cloudia.WindowsAzure.Storage";
|
---|
14 | }
|
---|
15 |
|
---|
16 | public class ScenarioDao : IScenarioDao {
|
---|
17 | public CloudTableClient TableClient { get; set; }
|
---|
18 |
|
---|
19 | public bool Add(ScenarioEntity entity) {
|
---|
20 | TableServiceContext serviceContext = TableClient.GetDataServiceContext();
|
---|
21 | TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
|
---|
22 | ScenarioEntity dbEntity = (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
|
---|
23 | where e.RowKey == entity.RowKey
|
---|
24 | select e).FirstOrDefault();
|
---|
25 | if (dbEntity != null)
|
---|
26 | return false;
|
---|
27 |
|
---|
28 | serviceContext.AddObject(AzureConstants.SCENARIO_TABLE, entity);
|
---|
29 | serviceContext.SaveChanges();
|
---|
30 | return true;
|
---|
31 | }
|
---|
32 |
|
---|
33 | public bool DeleteByName(string scenarioName) {
|
---|
34 | TableServiceContext serviceContext = TableClient.GetDataServiceContext();
|
---|
35 | TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
|
---|
36 | ScenarioEntity entity = (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
|
---|
37 | where e.RowKey == scenarioName
|
---|
38 | select e).FirstOrDefault();
|
---|
39 | if (entity == null)
|
---|
40 | return false;
|
---|
41 |
|
---|
42 | serviceContext.DeleteObject(entity);
|
---|
43 | serviceContext.SaveChangesWithRetries();
|
---|
44 | return true;
|
---|
45 | }
|
---|
46 |
|
---|
47 | public ScenarioEntity FindByName(string scenarioName) {
|
---|
48 | TableServiceContext serviceContext = TableClient.GetDataServiceContext();
|
---|
49 | TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
|
---|
50 | ScenarioEntity entity = (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
|
---|
51 | where e.RowKey == scenarioName
|
---|
52 | select e).FirstOrDefault();
|
---|
53 | return entity;
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | public IEnumerable<ScenarioEntity> GetAllEntities() {
|
---|
58 | TableServiceContext serviceContext = TableClient.GetDataServiceContext();
|
---|
59 | TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
|
---|
60 | return (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
|
---|
61 | select e).AsEnumerable();
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public class BlobDao : IBlobDao {
|
---|
66 | public CloudBlobClient BlobClient { get; set; }
|
---|
67 |
|
---|
68 | public bool Add(StringEntry entry) {
|
---|
69 | CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.SCENARIO_BLOB_CONTAINER);
|
---|
70 | container.CreateIfNotExist();
|
---|
71 | var blob = container.GetBlobReference(entry.Key);
|
---|
72 | blob.UploadText(entry.Text);
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public bool DeleteByKey(string entryKey) {
|
---|
77 | CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.SCENARIO_BLOB_CONTAINER);
|
---|
78 | container.CreateIfNotExist();
|
---|
79 | var blob = container.GetBlobReference(entryKey);
|
---|
80 | return blob.DeleteIfExists();
|
---|
81 | }
|
---|
82 |
|
---|
83 | public StringEntry FindByKey(string entryKey) {
|
---|
84 | CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.SCENARIO_BLOB_CONTAINER);
|
---|
85 | container.CreateIfNotExist();
|
---|
86 | var blob = container.GetBlobReference(entryKey);
|
---|
87 | return new StringEntry() { Key = entryKey, Text = blob.DownloadText() };
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public class AzureDataAccessLayer : IDataAccessLayer {
|
---|
92 | private IScenarioDao scenarioDao;
|
---|
93 | private IBlobDao blobDao;
|
---|
94 |
|
---|
95 | private CloudStorageAccount storageAccount;
|
---|
96 |
|
---|
97 | private CloudStorageAccount StorageAccount {
|
---|
98 | get {
|
---|
99 | if (storageAccount == null) {
|
---|
100 | storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(AzureConstants.CLOUD_SETTINGS_KEY));
|
---|
101 | }
|
---|
102 | return storageAccount;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | public IScenarioDao ScenarioDao {
|
---|
107 | get {
|
---|
108 | if (scenarioDao == null) {
|
---|
109 | scenarioDao = new ScenarioDao();
|
---|
110 | }
|
---|
111 | return scenarioDao;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | public IBlobDao BlobDao {
|
---|
116 | get {
|
---|
117 | if (blobDao == null) {
|
---|
118 | blobDao = new BlobDao();
|
---|
119 | }
|
---|
120 | return blobDao;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | public IScenarioDao CreateScenarioDao() {
|
---|
125 | return new ScenarioDao() { TableClient = StorageAccount.CreateCloudTableClient() };
|
---|
126 | }
|
---|
127 |
|
---|
128 | public IBlobDao CreateBlobDao() {
|
---|
129 | return new BlobDao() { BlobClient = StorageAccount.CreateCloudBlobClient() };
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|