Skip to content

Commit

Permalink
Modify the DocumentConvention and FileConvention to use same base abs…
Browse files Browse the repository at this point in the history
…tract class
  • Loading branch information
DanielDar committed Dec 12, 2013
1 parent 4fbad12 commit dfc6221
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 208 deletions.
Expand Up @@ -13,7 +13,7 @@ namespace Raven.Client.Connection

public class CreateHttpJsonRequestParams
{
public CreateHttpJsonRequestParams(IHoldProfilingInformation self, string url, string method, RavenJObject metadata, OperationCredentials credentials, DocumentConvention convention)
public CreateHttpJsonRequestParams(IHoldProfilingInformation self, string url, string method, RavenJObject metadata, OperationCredentials credentials, Convention convention)
{
Owner = self;
Url = url;
Expand Down Expand Up @@ -85,7 +85,7 @@ public void UpdateHeaders(NameValueCollection headers)
}
}

public CreateHttpJsonRequestParams(IHoldProfilingInformation self, string url, string method, OperationCredentials credentials, DocumentConvention convention)
public CreateHttpJsonRequestParams(IHoldProfilingInformation self, string url, string method, OperationCredentials credentials, Convention convention)
: this(self, url, method, new RavenJObject(), credentials, convention)
{}

Expand Down Expand Up @@ -121,7 +121,7 @@ private string GenerateUrl()
public RavenJObject Metadata { get; set; }
public OperationCredentials Credentials { get; set; }

public DocumentConvention Convention { get; set; }
public Convention Convention { get; set; }
public bool DisableRequestCompression { get; set; }
}
}
Expand Up @@ -58,7 +58,7 @@ public class HttpJsonRequest
internal CachedRequest CachedRequestDetails;
private readonly HttpJsonRequestFactory factory;
private readonly IHoldProfilingInformation owner;
private readonly DocumentConvention conventions;
private readonly Convention conventions;
private string postedData;
private bool isRequestSentToServer;

Expand Down
123 changes: 123 additions & 0 deletions Raven.Client.Lightweight/Convention.cs
@@ -0,0 +1,123 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using Raven.Abstractions.Connection;
using Raven.Client.Document;

namespace Raven.Client
{
public abstract class Convention
{
protected Dictionary<Type, PropertyInfo> idPropertyCache = new Dictionary<Type, PropertyInfo>();

/// <summary>
/// How should we behave in a replicated environment when we can't
/// reach the primary node and need to failover to secondary node(s).
/// </summary>
public FailoverBehavior FailoverBehavior { get; set; }

public FailoverBehavior FailoverBehaviorWithoutFlags
{
get { return FailoverBehavior & (~FailoverBehavior.ReadFromAllServers); }
}

/// <summary>
/// The maximum amount of time that we will wait before checking
/// that a failed node is still up or not.
/// Default: 5 minutes
/// </summary>
public TimeSpan MaxFailoverCheckPeriod { get; set; }

/// <summary>
/// Enable multipule async operations
/// </summary>
public bool AllowMultipuleAsyncOperations { get; set; }

/// <summary>
/// Gets or sets the function to find the identity property.
/// </summary>
/// <value>The find identity property.</value>
public Func<PropertyInfo, bool> FindIdentityProperty { get; set; }

/// <summary>
/// Gets or sets the identity parts separator used by the HiLo generators
/// </summary>
/// <value>The identity parts separator.</value>
public string IdentityPartsSeparator { get; set; }

/// <summary>
/// Whatever or not RavenDB should cache the request to the specified url.
/// </summary>
public Func<string, bool> ShouldCacheRequest { get; set; }

/// <summary>
/// Gets the identity property.
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public PropertyInfo GetIdentityProperty(Type type)
{
PropertyInfo info;
var currentIdPropertyCache = idPropertyCache;
if (currentIdPropertyCache.TryGetValue(type, out info))
return info;

var identityProperty = GetPropertiesForType(type).FirstOrDefault(FindIdentityProperty);

if (identityProperty != null && identityProperty.DeclaringType != type)
{
var propertyInfo = identityProperty.DeclaringType.GetProperty(identityProperty.Name);
identityProperty = propertyInfo ?? identityProperty;
}

idPropertyCache = new Dictionary<Type, PropertyInfo>(currentIdPropertyCache)
{
{type, identityProperty}
};

return identityProperty;
}

private static IEnumerable<PropertyInfo> GetPropertiesForType(Type type)
{
foreach (var propertyInfo in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic))
{
yield return propertyInfo;
}

foreach (var @interface in type.GetInterfaces())
{
foreach (var propertyInfo in GetPropertiesForType(@interface))
{
yield return propertyInfo;
}
}
}

/// <summary>
/// Handles unauthenticated responses, usually by authenticating against the oauth server
/// </summary>
public Func<HttpWebResponse, OperationCredentials, Action<HttpWebRequest>> HandleUnauthorizedResponse { get; set; }

/// <summary>
/// Handles forbidden responses
/// </summary>
public Func<HttpWebResponse, Action<HttpWebRequest>> HandleForbiddenResponse { get; set; }

/// <summary>
/// Begins handling of unauthenticated responses, usually by authenticating against the oauth server
/// in async manner
/// </summary>
public Func<HttpResponseMessage, OperationCredentials, Task<Action<HttpClient>>> HandleUnauthorizedResponseAsync { get; set; }

/// <summary>
/// Begins handling of forbidden responses
/// in async manner
/// </summary>
public Func<HttpResponseMessage, OperationCredentials, Task<Action<HttpClient>>> HandleForbiddenResponseAsync { get; set; }
}
}
122 changes: 1 addition & 121 deletions Raven.Client.Lightweight/Document/DocumentConvention.cs
Expand Up @@ -40,15 +40,14 @@ namespace Raven.Client.Document
/// The set of conventions used by the <see cref="DocumentStore"/> which allow the users to customize
/// the way the Raven client API behaves
/// </summary>
public class DocumentConvention
public class DocumentConvention : Convention
{
public delegate IEnumerable<object> ApplyReduceFunctionFunc(
Type indexType,
Type resultType,
IEnumerable<object> results,
Func<Func<IEnumerable<object>, IEnumerable>> generateTransformResults);

private Dictionary<Type, PropertyInfo> idPropertyCache = new Dictionary<Type, PropertyInfo>();
private Dictionary<Type, Func<IEnumerable<object>, IEnumerable>> compiledReduceCache = new Dictionary<Type, Func<IEnumerable<object>, IEnumerable>>();

#if !SILVERLIGHT && !NETFX_CORE
Expand Down Expand Up @@ -169,13 +168,6 @@ public string DefaultFindFullDocumentKeyFromNonStringIdentifier(object id, Type
return tag + id;
}


/// <summary>
/// How should we behave in a replicated environment when we can't
/// reach the primary node and need to failover to secondary node(s).
/// </summary>
public FailoverBehavior FailoverBehavior { get; set; }

/// <summary>
/// Register an action to customize the json serializer used by the <see cref="DocumentStore"/>
/// </summary>
Expand All @@ -186,23 +178,12 @@ public string DefaultFindFullDocumentKeyFromNonStringIdentifier(object id, Type
/// </summary>
public bool DisableProfiling { get; set; }

/// <summary>
/// Enable multipule async operations
/// </summary>
public bool AllowMultipuleAsyncOperations { get; set; }

///<summary>
/// A list of type converters that can be used to translate the document key (string)
/// to whatever type it is that is used on the entity, if the type isn't already a string
///</summary>
public List<ITypeConverter> IdentityTypeConvertors { get; set; }

/// <summary>
/// Gets or sets the identity parts separator used by the HiLo generators
/// </summary>
/// <value>The identity parts separator.</value>
public string IdentityPartsSeparator { get; set; }

/// <summary>
/// Gets or sets the default max number of requests per session.
/// </summary>
Expand Down Expand Up @@ -326,61 +307,6 @@ public Task<string> GenerateDocumentKeyAsync(string dbName, IAsyncDatabaseComman
return AsyncDocumentKeyGenerator(dbName, databaseCommands, entity);
}

/// <summary>
/// Gets the identity property.
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public PropertyInfo GetIdentityProperty(Type type)
{
PropertyInfo info;
var currentIdPropertyCache = idPropertyCache;
if (currentIdPropertyCache.TryGetValue(type, out info))
return info;

// we want to ignore nested entities from index creation tasks
if(type.IsNested && type.DeclaringType != null &&
typeof(AbstractIndexCreationTask).IsAssignableFrom(type.DeclaringType))
{
idPropertyCache = new Dictionary<Type, PropertyInfo>(currentIdPropertyCache)
{
{type, null}
};
return null;
}

var identityProperty = GetPropertiesForType(type).FirstOrDefault(FindIdentityProperty);

if (identityProperty != null && identityProperty.DeclaringType != type)
{
var propertyInfo = identityProperty.DeclaringType.GetProperty(identityProperty.Name);
identityProperty = propertyInfo ?? identityProperty;
}

idPropertyCache = new Dictionary<Type, PropertyInfo>(currentIdPropertyCache)
{
{type, identityProperty}
};

return identityProperty;
}

private static IEnumerable<PropertyInfo> GetPropertiesForType(Type type)
{
foreach (var propertyInfo in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic))
{
yield return propertyInfo;
}

foreach (var @interface in type.GetInterfaces())
{
foreach (var propertyInfo in GetPropertiesForType(@interface))
{
yield return propertyInfo;
}
}
}

/// <summary>
/// Gets or sets the function to find the clr type of a document.
/// </summary>
Expand Down Expand Up @@ -421,17 +347,6 @@ private static IEnumerable<PropertyInfo> GetPropertiesForType(Type type)
/// </summary>
public Func<Type, string, string, string, string> FindPropertyNameForDynamicIndex { get; set; }

/// <summary>
/// Whatever or not RavenDB should cache the request to the specified url.
/// </summary>
public Func<string, bool> ShouldCacheRequest { get; set; }

/// <summary>
/// Gets or sets the function to find the identity property.
/// </summary>
/// <value>The find identity property.</value>
public Func<PropertyInfo, bool> FindIdentityProperty { get; set; }

/// <summary>
/// Get or sets the function to get the identity property name from the entity name
/// </summary>
Expand Down Expand Up @@ -592,28 +507,6 @@ public string GetClrTypeName(Type entityType)
return FindClrTypeName(entityType);
}

/// <summary>
/// Handles unauthenticated responses, usually by authenticating against the oauth server
/// </summary>
public Func<HttpWebResponse, OperationCredentials, Action<HttpWebRequest>> HandleUnauthorizedResponse { get; set; }

/// <summary>
/// Handles forbidden responses
/// </summary>
public Func<HttpWebResponse, Action<HttpWebRequest>> HandleForbiddenResponse { get; set; }

/// <summary>
/// Begins handling of unauthenticated responses, usually by authenticating against the oauth server
/// in async manner
/// </summary>
public Func<HttpResponseMessage, OperationCredentials, Task<Action<HttpClient>>> HandleUnauthorizedResponseAsync { get; set; }

/// <summary>
/// Begins handling of forbidden responses
/// in async manner
/// </summary>
public Func<HttpResponseMessage, OperationCredentials, Task<Action<HttpClient>>> HandleForbiddenResponseAsync { get; set; }

/// <summary>
/// When RavenDB needs to convert between a string id to a value type like int or guid, it calls
/// this to perform the actual work
Expand Down Expand Up @@ -656,19 +549,6 @@ public DocumentConvention Clone()
/// </summary>
public Func<string, ReplicationInformer> ReplicationInformerFactory { get; set; }


public FailoverBehavior FailoverBehaviorWithoutFlags
{
get { return FailoverBehavior & (~FailoverBehavior.ReadFromAllServers); }
}

/// <summary>
/// The maximum amount of time that we will wait before checking
/// that a failed node is still up or not.
/// Default: 5 minutes
/// </summary>
public TimeSpan MaxFailoverCheckPeriod { get; set; }

public int IncrementRequestCount()
{
return Interlocked.Increment(ref requestCount);
Expand Down
1 change: 1 addition & 0 deletions Raven.Client.Lightweight/Raven.Client.Lightweight.csproj
Expand Up @@ -100,6 +100,7 @@
<Compile Include="Document\DTC\VolatileOnlyTransactionRecoveryStorage.cs" />
<Compile Include="Document\ReplicationBehavior.cs" />
<Compile Include="Document\SessionOperations\LoadTransformerOperation.cs" />
<Compile Include="Convention.cs" />
<Compile Include="Indexes\RavenMethodAttribute.cs" />
<Compile Include="Linq\AggregationQuery.cs" />
<Compile Include="Linq\DynamicAggregationQuery.cs" />
Expand Down

0 comments on commit dfc6221

Please sign in to comment.