-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebService1.asmx.cs
106 lines (97 loc) · 3.61 KB
/
WebService1.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Web.Script.Serialization;
using System.Diagnostics;
using System.Configuration;
using System.Web.Hosting;
namespace WebApplication1
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class WebService1 : System.Web.Services.WebService
{
public static Trie.Trie dict = new Trie.Trie();
private System.Diagnostics.PerformanceCounter ramCounter;
private static string path = System.IO.Path.GetTempFileName();
[WebMethod]
public string DownloadWiki()
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("titles");
container.CreateIfNotExists();
if (container.Exists())
{
CloudBlockBlob blockBlob = container.GetBlockBlobReference("wikiDumpNew.txt");
try
{
using (var fileStream = File.OpenWrite(path))
{
blockBlob.DownloadToStream(fileStream);
Debug.WriteLine("Download Complete");
fileStream.Close();
}
}
catch (Exception e)
{
Debug.WriteLine(e);
Debug.WriteLine("Download Failed...");
return "FAIL: File was not downloaded";
}
}
else
{
Debug.WriteLine("No Container Found");
return "FAIL: No blob container found";
}
return "Success!";
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Search(string input)
{
List<String> result = dict.Search(input.ToLower());
return new JavaScriptSerializer().Serialize(result.ToArray());
}
[WebMethod]
public String BuildTree()
{
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
try
{
using (StreamReader sr = new StreamReader(path))
{
string cur;
Double availableRam = ramCounter.NextValue();
while (((cur = sr.ReadLine()) != null) && (availableRam > 40))
{
dict.Add(cur.ToLower());
availableRam = ramCounter.NextValue();
}
Debug.WriteLine("Tree has maxed out its RAM capacity...");
return "Memory has been maxed out to its capacity";
}
}
catch (IOException)
{
Debug.WriteLine("File could be used by another process");
return "Failed...";
}
}
}
}