/
[PA 2]WebService.asmx.cs
114 lines (104 loc) · 3.83 KB
/
[PA 2]WebService.asmx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Web.Script.Services;
using System.Web.Services;
namespace WebService2
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService2 : System.Web.Services.WebService
{
static Trie tree;
const string WIKITITLE_FILENAME = "wikititles.txt";
private PerformanceCounter memProcess = new PerformanceCounter("Memory", "Available MBytes");
[WebMethod]
public float GetAvailableMBytes()
{
float memUsage = memProcess.NextValue();
return memUsage;
}
private void DownloadFileFromBlobToDisk()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
ConfigurationManager.AppSettings["kcinfo344"]);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("lecture8");
BlobRequestOptions options = new BlobRequestOptions();
options.MaximumExecutionTime = new TimeSpan(0, 100, 0);
options.ServerTimeout = new TimeSpan(0, 100, 0);
if (container.Exists())
{
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
if (item.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob blob = (CloudBlockBlob)item;
LocalResource myStorage = RoleEnvironment.GetLocalResource("lessonStorage");
string filePath = Path.Combine(myStorage.RootPath, "wiki.txt");
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
blob.DownloadToStream(fs, null, options);
}
}
}
}
}
private void ReadFromDisk()
{
string line;
int counter = 0;
float memLeft = 0;
LocalResource myStorage = RoleEnvironment.GetLocalResource("lessonStorage");
System.IO.StreamReader file = new System.IO.StreamReader(Path.Combine(myStorage.RootPath, "wiki.txt"));
while ((line = file.ReadLine()) != null)
{
counter++;
if (counter % 10 == 0)
{
memLeft = GetAvailableMBytes();
}
if (memLeft > 20)
{
tree.AddTitle(line);
}
}
file.Close();
}
private void LoadFromBlob()
{
try
{
TrieNode poop = tree.root;
}
catch (NullReferenceException)
{
tree = new Trie();
DownloadFileFromBlobToDisk();
ReadFromDisk();
}
}
[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public List<string> Search(string word)
{
LoadFromBlob();
return tree.GetTenResults(word.ToLower().Trim());
}
}
}