Permalink
Browse files

Adding initial work - just making sure that we can write to disk fast…

… enough
  • Loading branch information...
ayende committed Oct 5, 2012
0 parents commit 1d2af78a01eabb8a15466a822faae798585d420a
@@ -0,0 +1,30 @@
+[Bb]in
+[Oo]bj
+*resharper.user
+build/
+[Hh]elp/
+*.suo
+*.sln.cache
+_ReSharper.*/
+*.user
+/Data/Data
+/Data/IndexDefinitions/
+/Data/Index/
+/Data/logs/
+/Data/system/
+/Data/temp/
+*.InstallLog
+/test/storage.raven
+/Raven.Web/Data/
+VERSION
+/packages/
+ClientBin/
+/Raven.Tests.Silverlight.Results.xml
+*.g.3.5.csproj
+*.g.csproj
+*.g.4.csproj
+*.docstates
+StyleCop.Cache
+*.dotCover
+RavenDB*.nupkg
+*.orig
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<configuration>
+ <solution>
+ <add key="disableSourceControlIntegration" value="true" />
+ </solution>
+</configuration>
Binary file not shown.
@@ -0,0 +1,143 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">$(MSBuildProjectDirectory)\..\</SolutionDir>
+
+ <!-- Enable the restore command to run before builds -->
+ <RestorePackages Condition=" '$(RestorePackages)' == '' ">false</RestorePackages>
+
+ <!-- Property that enables building a package from a project -->
+ <BuildPackage Condition=" '$(BuildPackage)' == '' ">false</BuildPackage>
+
+ <!-- Determines if package restore consent is required to restore packages -->
+ <RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>
+
+ <!-- Download NuGet.exe if it does not already exist -->
+ <DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">false</DownloadNuGetExe>
+ </PropertyGroup>
+
+ <ItemGroup Condition=" '$(PackageSources)' == '' ">
+ <!-- Package sources used to restore packages. By default will used the registered sources under %APPDATA%\NuGet\NuGet.Config -->
+ <!--
+ <PackageSource Include="https://nuget.org/api/v2/" />
+ <PackageSource Include="https://my-nuget-source/nuget/" />
+ -->
+ </ItemGroup>
+
+ <PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
+ <!-- Windows specific commands -->
+ <NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
+ <PackagesConfig>$([System.IO.Path]::Combine($(ProjectDir), "packages.config"))</PackagesConfig>
+ <PackagesDir>$([System.IO.Path]::Combine($(SolutionDir), "packages"))</PackagesDir>
+ </PropertyGroup>
+
+ <PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
+ <!-- We need to launch nuget.exe with the mono command if we're not on windows -->
+ <NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
+ <PackagesConfig>packages.config</PackagesConfig>
+ <PackagesDir>$(SolutionDir)packages</PackagesDir>
+ </PropertyGroup>
+
+ <PropertyGroup>
+ <!-- NuGet command -->
+ <NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)\nuget.exe</NuGetExePath>
+ <PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>
+
+ <NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
+ <NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 $(NuGetExePath)</NuGetCommand>
+
+ <PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>
+
+ <RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
+ <!-- Commands -->
+ <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(RequireConsentSwitch) -o "$(PackagesDir)"</RestoreCommand>
+ <BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -p Configuration=$(Configuration) -o "$(PackageOutputDir)" -symbols</BuildCommand>
+
+ <!-- Make the build depend on restore packages -->
+ <BuildDependsOn Condition="$(RestorePackages) == 'true'">
+ RestorePackages;
+ $(BuildDependsOn);
+ </BuildDependsOn>
+
+ <!-- Make the build depend on restore packages -->
+ <BuildDependsOn Condition="$(BuildPackage) == 'true'">
+ $(BuildDependsOn);
+ BuildPackage;
+ </BuildDependsOn>
+ </PropertyGroup>
+
+ <Target Name="CheckPrerequisites">
+ <!-- Raise an error if we're unable to locate nuget.exe -->
+ <Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
+ <SetEnvironmentVariable EnvKey="VisualStudioVersion" EnvValue="$(VisualStudioVersion)" Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' " />
+ <DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
+ </Target>
+
+ <Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
+ <Exec Command="$(RestoreCommand)"
+ Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />
+
+ <Exec Command="$(RestoreCommand)"
+ LogStandardErrorAsError="true"
+ Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
+ </Target>
+
+ <Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
+ <Exec Command="$(BuildCommand)"
+ Condition=" '$(OS)' != 'Windows_NT' " />
+
+ <Exec Command="$(BuildCommand)"
+ LogStandardErrorAsError="true"
+ Condition=" '$(OS)' == 'Windows_NT' " />
+ </Target>
+
+ <UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
+ <ParameterGroup>
+ <OutputFilename ParameterType="System.String" Required="true" />
+ </ParameterGroup>
+ <Task>
+ <Reference Include="System.Core" />
+ <Using Namespace="System" />
+ <Using Namespace="System.IO" />
+ <Using Namespace="System.Net" />
+ <Using Namespace="Microsoft.Build.Framework" />
+ <Using Namespace="Microsoft.Build.Utilities" />
+ <Code Type="Fragment" Language="cs">
+ <![CDATA[
+ try {
+ OutputFilename = Path.GetFullPath(OutputFilename);
+
+ Log.LogMessage("Downloading latest version of NuGet.exe...");
+ WebClient webClient = new WebClient();
+ webClient.DownloadFile("https://nuget.org/nuget.exe", OutputFilename);
+
+ return true;
+ }
+ catch (Exception ex) {
+ Log.LogErrorFromException(ex);
+ return false;
+ }
+ ]]>
+ </Code>
+ </Task>
+ </UsingTask>
+
+ <UsingTask TaskName="SetEnvironmentVariable" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
+ <ParameterGroup>
+ <EnvKey ParameterType="System.String" Required="true" />
+ <EnvValue ParameterType="System.String" Required="true" />
+ </ParameterGroup>
+ <Task>
+ <Using Namespace="System" />
+ <Code Type="Fragment" Language="cs">
+ <![CDATA[
+ try {
+ Environment.SetEnvironmentVariable(EnvKey, EnvValue, System.EnvironmentVariableTarget.Process);
+ }
+ catch {
+ }
+ ]]>
+ </Code>
+ </Task>
+ </UsingTask>
+</Project>
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 2012
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rhino.Events", "Rhino.Events\Rhino.Events.csproj", "{3D9D23F6-1FA7-4E1C-83CB-6AEB620885D7}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {3D9D23F6-1FA7-4E1C-83CB-6AEB620885D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {3D9D23F6-1FA7-4E1C-83CB-6AEB620885D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {3D9D23F6-1FA7-4E1C-83CB-6AEB620885D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {3D9D23F6-1FA7-4E1C-83CB-6AEB620885D7}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
@@ -0,0 +1,27 @@
+using System.IO;
+
+namespace Rhino.Events
+{
+ public class FileStreamSource : IStreamSource
+ {
+ public Stream OpenWrite(string path)
+ {
+ var dir = Path.GetDirectoryName(path);
+ if (Directory.Exists(dir) == false)
+ Directory.CreateDirectory(dir);
+ var fileStream = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
+ fileStream.Seek(0, SeekOrigin.End);
+ return fileStream;
+ }
+
+ public Stream OpenRead(string path)
+ {
+ return File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite);
+ }
+
+ public void Flush(Stream stream)
+ {
+ ((FileStream) stream).Flush(true);
+ }
+ }
+}
@@ -0,0 +1,112 @@
+using System;
+using System.IO;
+using System.Net;
+using System.Threading.Tasks;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
+
+namespace Rhino.Events
+{
+ public class HttpServer : IDisposable
+ {
+ private readonly OnDiskData data;
+ private readonly HttpListener httpListener;
+
+ public HttpServer()
+ {
+ data = new OnDiskData(new FileStreamSource(), "Data");
+
+ httpListener = new HttpListener
+ {
+ Prefixes = {"http://+:8080/"},
+ IgnoreWriteExceptions = true,
+ };
+ httpListener.Start();
+
+ for (int i = 0; i < 30; i++)
+ {
+ ListenAsync();
+ }
+ }
+
+ private async void ListenAsync()
+ {
+ var ctx = await Task.Factory.FromAsync<HttpListenerContext>(httpListener.BeginGetContext, httpListener.EndGetContext, null);
+ try
+ {
+ ListenAsync();
+
+ }
+ catch (ObjectDisposedException)
+ {
+ // disposed, exiting
+ }
+
+ //Console.WriteLine(ctx.Request.HttpMethod + " " + ctx.Request.RawUrl);
+ var streamWriter = new StreamWriter(ctx.Response.OutputStream);
+ try
+ {
+ var id = ctx.Request.QueryString["id"];
+ switch (ctx.Request.HttpMethod)
+ {
+ case "PUT":
+ var value = (JObject)JToken.ReadFrom(new JsonTextReader(new StreamReader(ctx.Request.InputStream)));
+ if(string.IsNullOrWhiteSpace(id))
+ throw new ArgumentException("id query string must have a value");
+
+ await data.Enqueue(id, value);
+
+ break;
+ case "GET":
+ if(string.IsNullOrWhiteSpace(id))
+ throw new ArgumentException("id query string must have a value");
+
+ var stream = data.Read(id);
+ if (stream == null)
+ {
+ ctx.Response.StatusCode = 404;
+ ctx.Response.StatusDescription = "Not Found";
+ return;
+ }
+ var writer = new JsonTextWriter(streamWriter);
+ writer.WriteStartObject();
+ writer.WritePropertyName("Stream");
+ writer.WriteStartArray();
+
+
+ foreach (var item in stream)
+ {
+ item.WriteTo(writer);
+ }
+
+ writer.WriteEndArray();
+ writer.WriteEndObject();
+
+ break;
+
+ default:
+ throw new NotSupportedException("Http Method: " + ctx.Request.HttpMethod);
+ }
+ }
+ catch (Exception e)
+ {
+ Console.WriteLine(e);
+ ctx.Response.StatusCode = 500;
+ using(var wr = streamWriter)
+ {
+ wr.Write(e);
+ }
+ }
+ finally
+ {
+ streamWriter.Flush();
+ ctx.Response.Close();
+ }
+ }
+
+ public void Dispose()
+ {
+ data.Dispose();
+ }
+ }
+}
@@ -0,0 +1,12 @@
+using System.IO;
+
+namespace Rhino.Events
+{
+ public interface IStreamSource
+ {
+ Stream OpenWrite(string path);
+ Stream OpenRead(string path);
+
+ void Flush(Stream stream);
+ }
+}
@@ -0,0 +1,30 @@
+using System;
+using Newtonsoft.Json.Linq;
+
+namespace Rhino.Events
+{
+ public class JsonDataCache : IDisposable
+ {
+ //private readonly MemoryCache cache = new MemoryCache("events");
+
+ public Tuple<JObject,long> Get(long pos)
+ {
+ //var o = cache.Get(pos.ToString(CultureInfo.InvariantCulture)) as Tuple<JObject, long>;
+ //if(o == null)
+ return null;
+ //return Tuple.Create((JObject) o.Item1.DeepClone(), o.Item2);
+ }
+
+ public void Set(long pos, JObject val, long prev)
+ {
+ //cache.Set(new CacheItem(pos.ToString(CultureInfo.InvariantCulture), Tuple.Create(val, prev)), new CacheItemPolicy
+ // {
+ // SlidingExpiration = TimeSpan.FromMilliseconds(1),
+ // });
+ }
+ public void Dispose()
+ {
+ //cache.Dispose();
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 1d2af78

Please sign in to comment.