Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 719 lines (626 sloc) 25.783 kb
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
//-----------------------------------------------------------------------
// <copyright file="DocumentConvention.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Runtime.Serialization.Formatters;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Raven.Abstractions.Indexing;
using Raven.Client.Connection.Async;
using Raven.Client.Indexes;
using Raven.Client.Linq;
using Raven.Imports.Newtonsoft.Json;
using Raven.Imports.Newtonsoft.Json.Serialization;
using Raven.Imports.Newtonsoft.Json.Utilities;
using Raven.Abstractions;
using Raven.Abstractions.Json;
using Raven.Client.Connection;
using Raven.Client.Converters;
using Raven.Client.Util;
using Raven.Json.Linq;
#if NETFX_CORE
using Raven.Client.WinRT.MissingFromWinRT;
#endif

namespace Raven.Client.Document
{
using Raven.Abstractions.Connection;

/// <summary>
/// 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 : Convention
{
public delegate IEnumerable<object> ApplyReduceFunctionFunc(
Type indexType,
Type resultType,
IEnumerable<object> results,
Func<Func<IEnumerable<object>, IEnumerable>> generateTransformResults);

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

#if !NETFX_CORE
private readonly IList<Tuple<Type, Func<string, IDatabaseCommands, object, string>>> listOfRegisteredIdConventions =
new List<Tuple<Type, Func<string, IDatabaseCommands, object, string>>>();
#endif

private readonly IList<Tuple<Type, Func<string, IAsyncDatabaseCommands, object, Task<string>>>> listOfRegisteredIdConventionsAsync =
new List<Tuple<Type, Func<string, IAsyncDatabaseCommands, object, Task<string>>>>();

/// <summary>
/// Initializes a new instance of the <see cref="DocumentConvention"/> class.
/// </summary>
public DocumentConvention()
{
IdentityTypeConvertors = new List<ITypeConverter>
{
new GuidConverter(),
new Int32Converter(),
new Int64Converter(),
};
MaxFailoverCheckPeriod = TimeSpan.FromMinutes(5);
DisableProfiling = true;
EnlistInDistributedTransactions = true;
UseParallelMultiGet = true;
DefaultQueryingConsistency = ConsistencyOptions.None;
FailoverBehavior = FailoverBehavior.AllowReadsFromSecondaries;
ShouldCacheRequest = url => true;
FindIdentityProperty = q => q.Name == "Id";
FindClrType = (id, doc, metadata) => metadata.Value<string>(Abstractions.Data.Constants.RavenClrType);

FindClrTypeName = ReflectionUtil.GetFullNameWithoutVersionInformation;
TransformTypeTagNameToDocumentKeyPrefix = DefaultTransformTypeTagNameToDocumentKeyPrefix;
FindFullDocumentKeyFromNonStringIdentifier = DefaultFindFullDocumentKeyFromNonStringIdentifier;
FindIdentityPropertyNameFromEntityName = entityName => "Id";
FindTypeTagName = DefaultTypeTagName;
FindPropertyNameForIndex = (indexedType, indexedName, path, prop) => (path + prop).Replace(",", "_").Replace(".", "_");
FindPropertyNameForDynamicIndex = (indexedType, indexedName, path, prop) => path + prop;
IdentityPartsSeparator = "/";
JsonContractResolver = new DefaultRavenContractResolver(shareCache: true)
{
#if !NETFX_CORE
DefaultMembersSearchFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
#endif
};
MaxNumberOfRequestsPerSession = 30;
ApplyReduceFunction = DefaultApplyReduceFunction;
ReplicationInformerFactory = url => new ReplicationInformer(this);
CustomizeJsonSerializer = serializer => { };
FindIdValuePartForValueTypeConversion = (entity, id) => id.Split(new[] { IdentityPartsSeparator }, StringSplitOptions.RemoveEmptyEntries).Last();
ShouldAggressiveCacheTrackChanges = true;
ShouldSaveChangesForceAggressiveCacheCheck = true;
}

private IEnumerable<object> DefaultApplyReduceFunction(
Type indexType,
Type resultType,
IEnumerable<object> results,
Func<Func<IEnumerable<object>, IEnumerable>> generateTransformResults)
{
Func<IEnumerable<object>, IEnumerable> compile;
if (compiledReduceCache.TryGetValue(indexType, out compile) == false)
{
compile = generateTransformResults();
compiledReduceCache = new Dictionary<Type, Func<IEnumerable<object>, IEnumerable>>(compiledReduceCache)
{
{indexType, compile}
};
}
return compile(results).Cast<object>()
.Select(result =>
{
// we got an anonymous object and we need to get the reduce results
var ravenJTokenWriter = new RavenJTokenWriter();
var jsonSerializer = CreateSerializer();
jsonSerializer.Serialize(ravenJTokenWriter, result);
return jsonSerializer.Deserialize(new RavenJTokenReader(ravenJTokenWriter.Token), resultType);
});
}

public static string DefaultTransformTypeTagNameToDocumentKeyPrefix(string typeTagName)
{
#if NETFX_CORE
var count = typeTagName.ToCharArray().Count(char.IsUpper);
#else
var count = typeTagName.Count(char.IsUpper);
#endif

if (count <= 1) // simple name, just lower case it
return typeTagName.ToLowerInvariant();

// multiple capital letters, so probably something that we want to preserve caps on.
return typeTagName;
}

///<summary>
/// Find the full document name assuming that we are using the standard conventions
/// for generating a document key
///</summary>
///<returns></returns>
public string DefaultFindFullDocumentKeyFromNonStringIdentifier(object id, Type type, bool allowNull)
{
var converter = IdentityTypeConvertors.FirstOrDefault(x => x.CanConvertFrom(id.GetType()));
var tag = GetTypeTagName(type);
if (tag != null)
{
tag = TransformTypeTagNameToDocumentKeyPrefix(tag);
tag += IdentityPartsSeparator;
}
if (converter != null)
{
return converter.ConvertFrom(tag, id, allowNull);
}
return tag + id;
}

/// <summary>
/// Register an action to customize the json serializer used by the <see cref="DocumentStore"/>
/// </summary>
public Action<JsonSerializer> CustomizeJsonSerializer { get; set; }

/// <summary>
/// Disable all profiling support
/// </summary>
public bool DisableProfiling { 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 default max number of requests per session.
/// </summary>
/// <value>The max number of requests per session.</value>
public int MaxNumberOfRequestsPerSession { get; set; }

/// <summary>
/// Whatever to allow queries on document id.
/// By default, queries on id are disabled, because it is far more efficient
/// to do a Load() than a Query() if you already know the id.
/// This is NOT recommended and provided for backward compatibility purposes only.
/// </summary>
public bool AllowQueriesOnId { get; set; }

/// <summary>
/// The consistency options used when querying the database by default
/// Note that this option impact only queries, since we have Strong Consistency model for the documents
/// </summary>
public ConsistencyOptions DefaultQueryingConsistency { get; set; }

/// <summary>
/// Generates the document key using identity.
/// </summary>
/// <param name="conventions">The conventions.</param>
/// <param name="entity">The entity.</param>
/// <returns></returns>
public static string GenerateDocumentKeyUsingIdentity(DocumentConvention conventions, object entity)
{
return conventions.FindTypeTagName(entity.GetType()) + "/";
}

private static IDictionary<Type, string> cachedDefaultTypeTagNames = new Dictionary<Type, string>();
private int requestCount;

/// <summary>
/// Get the default tag name for the specified type.
/// </summary>
public static string DefaultTypeTagName(Type t)
{
string result;
if (cachedDefaultTypeTagNames.TryGetValue(t, out result))
return result;

if (t.Name.Contains("<>"))
return null;
if (t.IsGenericType())
{
var name = t.GetGenericTypeDefinition().Name;
if (name.Contains('`'))
{
name = name.Substring(0, name.IndexOf('`'));
}
var sb = new StringBuilder(Inflector.Pluralize(name));
foreach (var argument in t.GetGenericArguments())
{
sb.Append("Of")
.Append(DefaultTypeTagName(argument));
}
result = sb.ToString();
}
else
{
result = Inflector.Pluralize(t.Name);
}
var temp = new Dictionary<Type, string>(cachedDefaultTypeTagNames);
temp[t] = result;
cachedDefaultTypeTagNames = temp;
return result;
}

/// <summary>
/// Gets the name of the type tag.
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public string GetTypeTagName(Type type)
{
return FindTypeTagName(type) ?? DefaultTypeTagName(type);
}

#if !NETFX_CORE
/// <summary>
/// Generates the document key.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns></returns>
public string GenerateDocumentKey(string dbName, IDatabaseCommands databaseCommands, object entity)
{
var type = entity.GetType();
foreach (var typeToRegisteredIdConvention in listOfRegisteredIdConventions
.Where(typeToRegisteredIdConvention => typeToRegisteredIdConvention.Item1.IsAssignableFrom(type)))
{
return typeToRegisteredIdConvention.Item2(dbName, databaseCommands, entity);
}

if (listOfRegisteredIdConventionsAsync.Any(x => x.Item1.IsAssignableFrom(type)))
{
throw new InvalidOperationException("Id convention for synchronous operation was not found for entity " + type.FullName + ", but convention for asynchronous operation exists.");
}

return DocumentKeyGenerator(dbName, databaseCommands, entity);
}
#endif

public Task<string> GenerateDocumentKeyAsync(string dbName, IAsyncDatabaseCommands databaseCommands, object entity)
{
var type = entity.GetType();
foreach (var typeToRegisteredIdConvention in listOfRegisteredIdConventionsAsync
.Where(typeToRegisteredIdConvention => typeToRegisteredIdConvention.Item1.IsAssignableFrom(type)))
{
return typeToRegisteredIdConvention.Item2(dbName, databaseCommands, entity);
}

#if !NETFX_CORE
if (listOfRegisteredIdConventions.Any(x => x.Item1.IsAssignableFrom(type)))
{
throw new InvalidOperationException("Id convention for asynchronous operation was not found for entity " + type.FullName + ", but convention for synchronous operation exists.");
}
#endif

return AsyncDocumentKeyGenerator(dbName, databaseCommands, entity);
}

/// <summary>
/// Gets or sets the function to find the clr type of a document.
/// </summary>
public Func<string, RavenJObject, RavenJObject, string> FindClrType { get; set; }

/// <summary>
/// Gets or sets the function to find the clr type name from a clr type
/// </summary>
public Func<Type, string> FindClrTypeName { get; set; }

/// <summary>
/// Gets or sets the function to find the full document key based on the type of a document
/// and the value type identifier (just the numeric part of the id).
/// </summary>
public Func<object, Type, bool, string> FindFullDocumentKeyFromNonStringIdentifier { get; set; }

/// <summary>
/// Gets or sets the json contract resolver.
/// </summary>
/// <value>The json contract resolver.</value>
public IContractResolver JsonContractResolver { get; set; }

/// <summary>
/// Gets or sets the function to find the type tag.
/// </summary>
/// <value>The name of the find type tag.</value>
public Func<Type, string> FindTypeTagName { get; set; }

/// <summary>
/// Gets or sets the function to find the indexed property name
/// given the indexed document type, the index name, the current path and the property path.
/// </summary>
public Func<Type, string, string, string, string> FindPropertyNameForIndex { get; set; }

/// <summary>
/// Gets or sets the function to find the indexed property name
/// given the indexed document type, the index name, the current path and the property path.
/// </summary>
public Func<Type, string, string, string, string> FindPropertyNameForDynamicIndex { get; set; }

/// <summary>
/// Get or sets the function to get the identity property name from the entity name
/// </summary>
public Func<string, string> FindIdentityPropertyNameFromEntityName { get; set; }

        /// <summary>
/// Gets or sets the document key generator.
/// </summary>
/// <value>The document key generator.</value>
public Func<string, IDatabaseCommands, object, string> DocumentKeyGenerator { get; set; }

/// <summary>
/// Gets or sets the document key generator.
/// </summary>
/// <value>The document key generator.</value>
public Func<string, IAsyncDatabaseCommands, object, Task<string>> AsyncDocumentKeyGenerator { get; set; }

/// <summary>
/// Instruct RavenDB to parallel Multi Get processing
/// when handling lazy requests
/// </summary>
public bool UseParallelMultiGet { get; set; }

/// <summary>
/// Whatever or not RavenDB should in the aggressive cache mode use Changes API to track
/// changes and rebuild the cache. This will make that outdated data will be revalidated
/// to make the cache more updated, however it is still possible to get a state result because of the time
/// needed to receive the notification and forcing to check for cached data.
/// </summary>
public bool ShouldAggressiveCacheTrackChanges { get; set; }

/// <summary>
/// Whatever or not RavenDB should in the aggressive cache mode should force the aggressive cache
/// to check with the server after we called SaveChanges() on a non empty data set.
/// This will make any outdated data revalidated, and will work nicely as long as you have just a
/// single client. For multiple clients, <see cref="ShouldAggressiveCacheTrackChanges"/>.
/// </summary>
public bool ShouldSaveChangesForceAggressiveCacheCheck { get; set; }


#if !NETFX_CORE
/// <summary>
/// Register an id convention for a single type (and all of its derived types.
/// Note that you can still fall back to the DocumentKeyGenerator if you want.
/// </summary>
public DocumentConvention RegisterIdConvention<TEntity>(Func<string, IDatabaseCommands, TEntity, string> func)
{
var type = typeof(TEntity);
var entryToRemove = listOfRegisteredIdConventions.FirstOrDefault(x => x.Item1 == type);
if (entryToRemove != null)
{
listOfRegisteredIdConventions.Remove(entryToRemove);
}

int index;
for (index = 0; index < listOfRegisteredIdConventions.Count; index++)
{
var entry = listOfRegisteredIdConventions[index];
if (entry.Item1.IsAssignableFrom(type))
{
break;
}
}

var item = new Tuple<Type, Func<string, IDatabaseCommands, object, string>>(typeof(TEntity), (dbName, commands, o) => func(dbName, commands, (TEntity)o));
listOfRegisteredIdConventions.Insert(index, item);

return this;
}
#endif

/// <summary>
/// Register an async id convention for a single type (and all of its derived types.
/// Note that you can still fall back to the DocumentKeyGenerator if you want.
/// </summary>
public DocumentConvention RegisterAsyncIdConvention<TEntity>(Func<string, IAsyncDatabaseCommands, TEntity, Task<string>> func)
{
var type = typeof(TEntity);
var entryToRemove = listOfRegisteredIdConventionsAsync.FirstOrDefault(x => x.Item1 == type);
if (entryToRemove != null)
{
listOfRegisteredIdConventionsAsync.Remove(entryToRemove);
}

int index;
for (index = 0; index < listOfRegisteredIdConventionsAsync.Count; index++)
{
var entry = listOfRegisteredIdConventionsAsync[index];
if (entry.Item1.IsAssignableFrom(type))
{
break;
}
}

var item = new Tuple<Type, Func<string, IAsyncDatabaseCommands, object, Task<string>>>(typeof(TEntity), (dbName, commands, o) => func(dbName, commands, (TEntity)o));
listOfRegisteredIdConventionsAsync.Insert(index, item);

return this;
}

/// <summary>
/// Creates the serializer.
/// </summary>
/// <returns></returns>
public JsonSerializer CreateSerializer()
{
var jsonSerializer = new JsonSerializer
{
DateParseHandling = DateParseHandling.None,
ObjectCreationHandling = ObjectCreationHandling.Replace,
ContractResolver = JsonContractResolver,
TypeNameHandling = TypeNameHandling.Auto,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple,
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
                FloatParseHandling = FloatParseHandling.Decimal,
Converters =
{
new JsonLuceneDateTimeConverter(),
new JsonNumericConverter<int>(int.TryParse),
new JsonNumericConverter<long>(long.TryParse),
new JsonNumericConverter<decimal>(decimal.TryParse),
new JsonNumericConverter<double>(double.TryParse),
new JsonNumericConverter<short>(short.TryParse),
new JsonMultiDimensionalArrayConverter(),
new JsonDynamicConverter()
}
};

for (var i = Default.Converters.Length - 1; i >= 0; i--)
{
jsonSerializer.Converters.Insert(0, Default.Converters[i]);
}

if (SaveEnumsAsIntegers)
{
var converter = jsonSerializer.Converters.FirstOrDefault(x => x is JsonEnumConverter);
if (converter != null)
jsonSerializer.Converters.Remove(converter);
}

CustomizeJsonSerializer(jsonSerializer);
return jsonSerializer;
}

/// <summary>
/// Get the CLR type (if exists) from the document
/// </summary>
public string GetClrType(string id, RavenJObject document, RavenJObject metadata)
{
return FindClrType(id, document, metadata);
}

/// <summary>
/// Get the CLR type name to be stored in the entity metadata
/// </summary>
public string GetClrTypeName(Type entityType)
{
return FindClrTypeName(entityType);
}

/// <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
/// </summary>
public Func<object, string, string> FindIdValuePartForValueTypeConversion { get; set; }

/// <summary>
/// Saves Enums as integers and instruct the Linq provider to query enums as integer values.
/// </summary>
public bool SaveEnumsAsIntegers { get; set; }

/// <summary>
/// Translate the type tag name to the document key prefix
/// </summary>
public Func<string, string> TransformTypeTagNameToDocumentKeyPrefix { get; set; }

///<summary>
/// Whatever or not RavenDB will automatically enlist in distributed transactions
///</summary>
public bool EnlistInDistributedTransactions { get; set; }

/// <summary>
/// Clone the current conventions to a new instance
/// </summary>
public DocumentConvention Clone()
{
return (DocumentConvention)MemberwiseClone();
}

/// <summary>
/// This is called in order to ensure that reduce function in a sharded environment is run
/// over the merged results
/// </summary>
public ApplyReduceFunctionFunc ApplyReduceFunction { get; set; }


/// <summary>
/// This is called to provide replication behavior for the client. You can customize
/// this to inject your own replication / failover logic.
/// </summary>
public Func<string, IDocumentStoreReplicationInformer> ReplicationInformerFactory { get; set; }

public int IncrementRequestCount()
{
return Interlocked.Increment(ref requestCount);
}

public delegate bool TryConvertValueForQueryDelegate<in T>(string fieldName, T value, QueryValueConvertionType convertionType, out string strValue);

private readonly List<Tuple<Type, TryConvertValueForQueryDelegate<object>>> listOfQueryValueConverters = new List<Tuple<Type, TryConvertValueForQueryDelegate<object>>>();
private readonly Dictionary<string, SortOptions> customDefaultSortOptions = new Dictionary<string, SortOptions>();
private readonly List<Type> customRangeTypes = new List<Type>();

public void RegisterQueryValueConverter<T>(TryConvertValueForQueryDelegate<T> converter, SortOptions defaultSortOption = SortOptions.String, bool usesRangeField = false)
{
TryConvertValueForQueryDelegate<object> actual = (string name, object value, QueryValueConvertionType convertionType, out string strValue) =>
{
if (value is T)
return converter(name, (T)value, convertionType, out strValue);
strValue = null;
return false;
};

int index;
for (index = 0; index < listOfQueryValueConverters.Count; index++)
{
var entry = listOfQueryValueConverters[index];
if (entry.Item1.IsAssignableFrom(typeof(T)))
{
break;
}
}

listOfQueryValueConverters.Insert(index, Tuple.Create(typeof(T), actual));

if (defaultSortOption != SortOptions.String)
customDefaultSortOptions.Add(typeof(T).Name, defaultSortOption);

if (usesRangeField)
customRangeTypes.Add(typeof(T));
}


public bool TryConvertValueForQuery(string fieldName, object value, QueryValueConvertionType convertionType, out string strValue)
{
foreach (var queryValueConverterTuple in listOfQueryValueConverters
.Where(tuple => tuple.Item1.IsInstanceOfType(value)))
{
return queryValueConverterTuple.Item2(fieldName, value, convertionType, out strValue);
}
strValue = null;
return false;
}

public SortOptions GetDefaultSortOption(string typeName)
{
switch (typeName)
{
case "Int16":
return SortOptions.Short;
case "Int32":
return SortOptions.Int;
case "Int64":
case "TimeSpan":
return SortOptions.Long;
case "Double":
case "Decimal":
return SortOptions.Double;
case "Single":
return SortOptions.Float;
case "String":
return SortOptions.String;
default:
SortOptions value;
return customDefaultSortOptions.TryGetValue(typeName, out value)
? value
: SortOptions.String;
}
}

public bool UsesRangeType(object o)
{
if (o == null)
return false;
var type = o as Type ?? o.GetType();
var nonNullable = Nullable.GetUnderlyingType(type);
if (nonNullable != null)
type = nonNullable;

if (type == typeof (int) || type == typeof (long) || type == typeof (double) || type == typeof (float) ||
type == typeof (decimal) || type == typeof (TimeSpan) || type == typeof(short))
return true;

return customRangeTypes.Contains(type);
}

public delegate LinqPathProvider.Result CustomQueryTranslator(LinqPathProvider provider, Expression expression);

private readonly Dictionary<MemberInfo, CustomQueryTranslator> customQueryTranslators = new Dictionary<MemberInfo, CustomQueryTranslator>();

public void RegisterCustomQueryTranslator<T>(Expression<Func<T, object>> member, CustomQueryTranslator translator)
{
var body = member.Body as UnaryExpression;
if (body == null)
throw new NotSupportedException("A custom query translator can only be used to evaluate a simple member access or method call.");

var info = GetMemberInfoFromExpression(body.Operand);

if (!customQueryTranslators.ContainsKey(info))
customQueryTranslators.Add(info, translator);
}

internal LinqPathProvider.Result TranslateCustomQueryExpression(LinqPathProvider provider, Expression expression)
{
var member = GetMemberInfoFromExpression(expression);

CustomQueryTranslator translator;
if (!customQueryTranslators.TryGetValue(member, out translator))
return null;

return translator.Invoke(provider, expression);
}

private static MemberInfo GetMemberInfoFromExpression(Expression expression)
{
var callExpression = expression as MethodCallExpression;
if (callExpression != null)
return callExpression.Method;

var memberExpression = expression as MemberExpression;
if (memberExpression != null)
return memberExpression.Member;

throw new NotSupportedException("A custom query translator can only be used to evaluate a simple member access or method call.");
}
}

public enum QueryValueConvertionType
{
Equality,
Range
}

/// <summary>
/// The consistency options for all queries, fore more details about the consistency options, see:
/// http://www.allthingsdistributed.com/2008/12/eventually_consistent.html
///
/// Note that this option impact only queries, since we have Strong Consistency model for the documents
/// </summary>
public enum ConsistencyOptions
{
/// <summary>
/// Ensures that after querying an index at time T, you will never see the results
/// of the index at a time prior to T.
/// This is ensured by the server, and require no action from the client
/// </summary>
None,
/// <summary>
/// After updating a documents, will only accept queries which already indexed the updated value.
/// </summary>
AlwaysWaitForNonStaleResultsAsOfLastWrite,
/// <summary>
/// Use AlwaysWaitForNonStaleResultsAsOfLastWrite, instead
/// </summary>
[Obsolete("Use AlwaysWaitForNonStaleResultsAsOfLastWrite, instead")]
QueryYourWrites = AlwaysWaitForNonStaleResultsAsOfLastWrite,
/// <summary>
/// Use None, instead
/// </summary>
[Obsolete("Use None, instead")]
MonotonicRead = None
}
}
Something went wrong with that request. Please try again.