Permalink
Browse files

Merge branch 'master' of https://github.com/colinbull/ravendb

Conflicts:
	RavenDB.sln
  • Loading branch information...
2 parents 4867233 + c5aa16f commit 5b03b1be7654085f556734a9c3af6a40c7951cad @ayende ayende committed Mar 2, 2012
Showing with 3,795 additions and 0 deletions.
  1. +19 −0 Raven.Client.Lightweight.FSharp/DocumentStore.fs
  2. +72 −0 Raven.Client.Lightweight.FSharp/Helpers.fs
  3. +78 −0 Raven.Client.Lightweight.FSharp/Raven.Client.Lightweight.FSharp.fsproj
  4. +133 −0 Raven.Client.Lightweight.FSharp/Raven.fs
  5. +7 −0 Raven.Client.Lightweight.FSharp/Script.fsx
  6. +5 −0 Raven.Client.Lightweight.FSharp/packages.config
  7. +121 −0 Raven.Tests.FSharp/Raven.Tests.FSharp.fsproj
  8. +263 −0 Raven.Tests.FSharp/RavenTests.fs
  9. +37 −0 Raven.Tests.FSharp/TestData.fs
  10. +5 −0 Raven.Tests.FSharp/packages.config
  11. +24 −0 RavenDB.sln
  12. +3 −0 Samples/Raven.Sample.FSharp/Program.fs
  13. +5 −0 Samples/Raven.Sample.FSharp/Raven.Sample.FSharp.fsproj
  14. +25 −0 Samples/Raven.Samples.sln
  15. BIN packages/FsLinqFixed.2.1.1/FsLinqFixed.2.1.1.nupkg
  16. BIN packages/FsLinqFixed.2.1.1/lib/net40/FSharp.PowerPack.Linq.Fixed.dll
  17. BIN packages/FsLinqFixed.2.1.1/lib/net40/FSharp.PowerPack.Linq.Fixed.pdb
  18. +367 −0 packages/FsLinqFixed.2.1.1/lib/net40/FSharp.PowerPack.Linq.Fixed.xml
  19. BIN packages/FsLinqFixed.2.1.1/tools/install.ps
  20. +22 −0 packages/FsUnit.xUnit.1.0.0.4/Content/FsUnitSample.fs.pp
  21. BIN packages/FsUnit.xUnit.1.0.0.4/FsUnit.xUnit.1.0.0.4.nupkg
  22. BIN packages/FsUnit.xUnit.1.0.0.4/Lib/Net20/FsUnit.CustomMatchers.dll
  23. BIN packages/FsUnit.xUnit.1.0.0.4/Lib/Net20/FsUnit.Xunit.dll
  24. +78 −0 packages/FsUnit.xUnit.1.0.0.4/Lib/Net20/FsUnit.Xunit.xml
  25. BIN packages/FsUnit.xUnit.1.0.0.4/Lib/Net20/NHamcrest.dll
  26. BIN packages/FsUnit.xUnit.1.0.0.4/Lib/Net40/FsUnit.CustomMatchers.dll
  27. BIN packages/FsUnit.xUnit.1.0.0.4/Lib/Net40/FsUnit.Xunit.dll
  28. +78 −0 packages/FsUnit.xUnit.1.0.0.4/Lib/Net40/FsUnit.Xunit.xml
  29. BIN packages/FsUnit.xUnit.1.0.0.4/Lib/Net40/NHamcrest.dll
  30. +2 −0 packages/repositories.config
  31. BIN packages/xunit.1.8.0.1549/lib/xunit.dll
  32. +5 −0 packages/xunit.1.8.0.1549/lib/xunit.dll.tdnet
  33. BIN packages/xunit.1.8.0.1549/lib/xunit.runner.tdnet.dll
  34. BIN packages/xunit.1.8.0.1549/lib/xunit.runner.utility.dll
  35. +2,446 −0 packages/xunit.1.8.0.1549/lib/xunit.xml
  36. BIN packages/xunit.1.8.0.1549/xunit.1.8.0.1549.nupkg
@@ -0,0 +1,19 @@
+namespace Raven.Client
+
+open System
+open Newtonsoft.Json
+open System.Configuration
+open Raven.Client.Document
+
+[<AutoOpen>]
+module DocumentStoreExt =
+
+ type Raven.Client.Document.DocumentStore with
+
+ static member OpenInitializedStore(name) =
+ let store = new DocumentStore (ConnectionStringName = name)
+ store.Conventions.CustomizeJsonSerializer <- (fun s -> s.Converters.Add(new UnionTypeConverter()))
+ store.Initialize() |> ignore
+ store
+
+
@@ -0,0 +1,72 @@
+namespace Raven.Client
+
+open System
+open System.Linq
+open System.Linq.Expressions
+open Newtonsoft.Json
+open Microsoft.FSharp.Reflection
+open Microsoft.FSharp.Quotations
+open Microsoft.FSharp.Linq
+
+type UnionTypeConverter() =
+ inherit JsonConverter()
+
+ let doRead pos (reader: JsonReader) =
+ reader.Read() |> ignore
+
+ override x.CanConvert(typ:Type) =
+ let result =
+ ((typ.GetInterface(typeof<System.Collections.IEnumerable>.FullName) = null)
+ && FSharpType.IsUnion typ)
+ result
+
+ override x.WriteJson(writer: JsonWriter, value: obj, serializer: JsonSerializer) =
+ let t = value.GetType()
+ let write (name : string) (fields : obj []) =
+ writer.WriteStartObject()
+ writer.WritePropertyName("case")
+ writer.WriteValue(name)
+ writer.WritePropertyName("values")
+ serializer.Serialize(writer, fields)
+ writer.WriteEndObject()
+
+ let (info, fields) = FSharpValue.GetUnionFields(value, t)
+ write info.Name fields
+
+ override x.ReadJson(reader: JsonReader, objectType: Type, existingValue: obj, serializer: JsonSerializer) =
+ let cases = FSharpType.GetUnionCases(objectType)
+ if reader.TokenType <> JsonToken.Null
+ then
+ doRead "1" reader
+ doRead "2" reader
+ let case = cases |> Array.find(fun x -> x.Name = if reader.Value = null then "None" else reader.Value.ToString())
+ doRead "3" reader
+ doRead "4" reader
+ doRead "5" reader
+ let fields = [|
+ for field in case.GetFields() do
+ let result = serializer.Deserialize(reader, field.PropertyType)
+ reader.Read() |> ignore
+ yield result
+ |]
+ let result = FSharpValue.MakeUnion(case, fields)
+ while reader.TokenType <> JsonToken.EndObject do
+ doRead "6" reader
+ result
+ else
+ FSharpValue.MakeUnion(cases.[0], [||])
+
+module Linq =
+
+ ///From http://stackoverflow.com/questions/2682475/converting-f-quotations-into-linq-expressions
+ let toLinqExpression (f : ParameterExpression list -> Expression -> Expression<'a>) p =
+ let rec translateExpr (linq:Expression) =
+ match linq with
+ | :? MethodCallExpression as mc ->
+ let le = mc.Arguments.[0] :?> LambdaExpression
+ let args, body = translateExpr le.Body
+ le.Parameters.[0] :: args, body
+ | _ -> [], linq
+ let args,body = translateExpr (QuotationEvaluator.ToLinqExpression p)
+ f args body
+
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{6996133d-ee21-4cc8-b29b-f6222ee476bb}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <RootNamespace>Raven.Client.Lightweight.FSharp</RootNamespace>
+ <AssemblyName>Raven.Client.Lightweight.FSharp</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <Name>Raven.Client.Lightweight.FSharp</Name>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <Tailcalls>false</Tailcalls>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <WarningLevel>3</WarningLevel>
+ <DocumentationFile>bin\Debug\Raven.Client.Lightweight.FSharp.XML</DocumentationFile>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <Tailcalls>true</Tailcalls>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <WarningLevel>3</WarningLevel>
+ <DocumentationFile>bin\Release\Raven.Client.Lightweight.FSharp.XML</DocumentationFile>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="FSharp.PowerPack.Linq.Fixed">
+ <HintPath>..\packages\FsLinqFixed.2.1.1\lib\net40\FSharp.PowerPack.Linq.Fixed.dll</HintPath>
+ <Private>True</Private>
+ </Reference>
+ <Reference Include="mscorlib" />
+ <Reference Include="FSharp.Core" />
+ <Reference Include="Newtonsoft.Json">
+ <HintPath>..\packages\Newtonsoft.Json.4.0.5\lib\net40\Newtonsoft.Json.dll</HintPath>
+ <Private>True</Private>
+ </Reference>
+ <Reference Include="System" />
+ <Reference Include="System.ComponentModel.Composition" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Numerics" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Helpers.fs" />
+ <Compile Include="DocumentStore.fs" />
+ <Compile Include="Raven.fs" />
+ <None Include="Script.fsx" />
+ <None Include="packages.config" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\Raven.Abstractions\Raven.Abstractions.csproj">
+ <Name>Raven.Abstractions</Name>
+ <Project>{41ac479e-1eb2-4d23-aaf2-e4c8df1bc2ba}</Project>
+ <Private>True</Private>
+ </ProjectReference>
+ <ProjectReference Include="..\Raven.Client.Lightweight\Raven.Client.Lightweight.csproj">
+ <Name>Raven.Client.Lightweight</Name>
+ <Project>{4e087ecb-e7ca-4891-ac3c-3c76702715b6}</Project>
+ <Private>True</Private>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(MSBuildExtensionsPath32)\FSharp\1.0\Microsoft.FSharp.Targets" Condition="!Exists('$(MSBuildBinPath)\Microsoft.Build.Tasks.v4.0.dll')" />
+ <Import Project="$(MSBuildExtensionsPath32)\..\Microsoft F#\v4.0\Microsoft.FSharp.Targets" Condition=" Exists('$(MSBuildBinPath)\Microsoft.Build.Tasks.v4.0.dll')" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
@@ -0,0 +1,133 @@
+namespace Raven.Client
+
+open System
+open System.Linq
+open System.Linq.Expressions
+open System.ComponentModel.Composition
+
+open Raven.Client
+open Raven.Client.Linq
+open Raven.Client.Document
+open Raven.Abstractions.Extensions
+open Microsoft.FSharp.Quotations
+open Microsoft.FSharp.Linq
+open Newtonsoft.Json
+
+ [<AutoOpen>]
+ module Operators =
+
+ let query (f : (IRavenQueryable<'a> -> #IQueryable<'b>)) (a : IDocumentSession) =
+ f(a.Query<'a>()).AsEnumerable()
+
+ let luceneQuery<'a, 'b> (f : (IDocumentQuery<'a> -> IDocumentQuery<'b>)) (a : IDocumentSession) =
+ f(a.Advanced.LuceneQuery<'a>()).AsEnumerable()
+
+ let where (p : Expr<'a -> bool>) (a : IRavenQueryable<'a>) =
+ let expr = Linq.toLinqExpression (fun b a -> Expression.Lambda<Func<'a,bool>>(a, b |> Array.ofSeq)) p
+ a.Where(expr)
+
+ let orderBy descending (p : Expr<'a -> 'b>) (a : IRavenQueryable<'a>) =
+ let expr = Linq.toLinqExpression (fun b a -> Expression.Lambda<Func<'a,'b>>(a, b |> Array.ofSeq)) p
+ if descending
+ then a.OrderByDescending(expr)
+ else a.OrderBy(expr)
+
+ let including (p : Expr<'a -> 'b>) (loader : ILoaderWithInclude<'a> -> 'c) (a : IDocumentSession) =
+ let expr = Linq.toLinqExpression (fun b a -> Expression.Lambda<Func<'a,obj>>(a, b |> Array.ofSeq)) p
+ loader(a.Include<'a>(expr))
+
+ let putAttachment documentId etag data metaData (a : IDocumentSession) =
+ a.Advanced.DatabaseCommands.PutAttachment(documentId, etag, data, Raven.Json.Linq.RavenJObject.FromObject(metaData))
+
+ let createAttachment documentId data metaData (a : IDocumentSession) =
+ putAttachment documentId (Nullable()) data metaData a
+
+ let getAttachmentAsStream<'a> documentId (stream : IO.Stream) (a : IDocumentSession) =
+ let attachment = a.Advanced.DatabaseCommands.GetAttachment(documentId)
+ let attachmentBody = attachment.Data.Invoke().CopyTo(stream)
+ (attachment.Metadata.JsonDeserialization<'a>(), attachment.Etag)
+
+ let getAttachmentAsBytes<'a> documentId (a : IDocumentSession) =
+ let attachment = a.Advanced.DatabaseCommands.GetAttachment(documentId)
+ let attachmentBody = attachment.Data.Invoke().ReadData()
+ (attachment.Metadata.JsonDeserialization<'a>(), attachment.Etag, attachmentBody)
+
+ let load<'a> (id : seq<string>) (a : IDocumentSession) =
+ a.Load(id).Cast<'a>()
+
+ let store input =
+ (fun (s : IDocumentSession) -> s.Store(input); input)
+
+ let delete (input : string) =
+ (fun (session : IDocumentSession) ->
+ session.Advanced.DatabaseCommands.Delete(input, Nullable()))
+
+ let saveChanges (s : IDocumentSession) =
+ s.SaveChanges()
+
+[<AutoOpen>]
+module Raven =
+
+ let run (session : IDocumentSession) f =
+ let a = f(session)
+ session.SaveChanges()
+ a
+
+ type RavenFunc<'a> = (IDocumentSession -> 'a)
+
+ type RavenBuilder() =
+
+ let bind sessionOp rest : RavenFunc<_> =
+ (fun (s : IDocumentSession) -> rest (sessionOp s) s)
+
+ let ret a : RavenFunc<_> = (fun _ -> a)
+
+ let delay f : RavenFunc<_> = bind (ret ()) f
+
+ member x.Bind(sessionOp, rest) = bind sessionOp rest
+
+ member x.Return(a) = ret a
+
+ member x.ReturnFrom(a : RavenFunc<_>) = a
+
+ member x.Yield(a) = ret a
+
+ member x.YieldFrom(a : RavenFunc<_>) = a
+
+ member x.Delay(f) = delay f
+
+ member x.Zero() = x.Return ()
+
+ member x.Combine(a, b) =
+ bind a (fun () -> b)
+
+ member x.TryWith(sessionOp : IDocumentSession -> 'a, handler) =
+ (fun s -> try
+ sessionOp s
+ with e ->
+ (handler e s))
+
+ member x.TryFinally(sessionOp: IDocumentSession -> 'a, compensation) =
+ (fun s -> try
+ sessionOp s
+ finally
+ compensation())
+
+ member x.While(guard, sessionOp) =
+ if guard() then
+ bind sessionOp (fun () -> x.While(guard,sessionOp))
+ else
+ x.Zero()
+
+ member x.Using(resource:#IDisposable, f) =
+ x.TryFinally(f resource, (fun () -> match resource with null -> () | disp -> disp.Dispose()))
+
+ member x.For(sequence : seq<_>, f) =
+ x.Using(sequence.GetEnumerator(),
+ (fun enum ->
+ x.While(
+ (fun () -> enum.MoveNext()),
+ x.Delay(fun () -> f enum.Current))))
+
+
+ let raven = RavenBuilder()
@@ -0,0 +1,7 @@
+// This file is a script that can be executed with the F# Interactive.
+// It can be used to explore and test the library project.
+// Note that script files will not be part of the project build.
+
+#load "Module1.fs"
+open Module1
+
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="FsLinqFixed" version="2.1.1" />
+ <package id="Newtonsoft.Json" version="4.0.5" />
+</packages>
Oops, something went wrong.

0 comments on commit 5b03b1b

Please sign in to comment.