Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 2df28c1433
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 324 lines (281 sloc) 10.13 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using NLog;
using Newtonsoft.Json.Linq;
using Raven.Abstractions.Data;
using Raven.Abstractions.Indexing;
using Raven.Abstractions.Linq;
using Raven.Client.Exceptions;
using Raven.Json.Linq;

namespace Raven.Client.Document.SessionOperations
{
public class QueryOperation
{
private static readonly Logger log = LogManager.GetCurrentClassLogger();
private readonly InMemoryDocumentSessionOperations sessionOperations;
private readonly string indexName;
private readonly IndexQuery indexQuery;
private readonly HashSet<KeyValuePair<string, Type>> sortByHints;
private readonly Action<string, string> setOperationHeaders;
private readonly bool waitForNonStaleResults;
private readonly TimeSpan timeout;
private readonly Func<IndexQuery, IEnumerable<object>, IEnumerable<object>> transformResults;
private QueryResult currentQueryResults;
private readonly string[] projectionFields;
private bool firstRequest = true;

public QueryResult CurrentQueryResults
{
get { return currentQueryResults; }
}

public string IndexName
{
get { return indexName; }
}

public IndexQuery IndexQuery
{
get { return indexQuery; }
}

private Stopwatch sp;

public QueryOperation(InMemoryDocumentSessionOperations sessionOperations,
string indexName,
IndexQuery indexQuery,
string[] projectionFields,
HashSet<KeyValuePair<string, Type>> sortByHints,
bool waitForNonStaleResults,
Action<string, string> setOperationHeaders,
TimeSpan timeout,
Func<IndexQuery, IEnumerable<object>, IEnumerable<object>> transformResults)
{
this.indexQuery = indexQuery;
this.sortByHints = sortByHints;
this.waitForNonStaleResults = waitForNonStaleResults;
this.setOperationHeaders = setOperationHeaders;
this.timeout = timeout;
this.transformResults = transformResults;
this.projectionFields = projectionFields;
this.sessionOperations = sessionOperations;
this.indexName = indexName;


AddOperationHeaders();
}

private void StartTiming()
{
sp = Stopwatch.StartNew();
}

public void LogQuery()
{
log.Debug("Executing query '{0}' on index '{1}' in '{2}'",
indexQuery.Query, indexName, sessionOperations.StoreIdentifier);
}

public IDisposable EnterQueryContext()
{
if (firstRequest)
{
StartTiming();
firstRequest = false;
}
if (waitForNonStaleResults == false)
return null;

return sessionOperations.DocumentStore.DisableAggressiveCaching();
}

public bool ShouldQueryAgain(Exception e)
{
if (e is NonAuthoritativeInformationException == false)
return false;

return sp.Elapsed <= sessionOperations.NonAuthoritativeInformationTimeout;
}

public IList<T> Complete<T>()
{
var queryResult = currentQueryResults.CreateSnapshot();
foreach (var include in queryResult.Includes)
{
var metadata = include.Value<RavenJObject>("@metadata");

sessionOperations.TrackEntity<object>(metadata.Value<string>("@id"),
include,
metadata);
}
var list = queryResult.Results
.Select(Deserialize<T>)
.ToList();

if (transformResults == null)
return list;

return transformResults(indexQuery, list.Cast<object>()).Cast<T>().ToList();
}

private T Deserialize<T>(RavenJObject result)
{
var metadata = result.Value<RavenJObject>("@metadata");
if ((projectionFields == null || projectionFields.Length <= 0) &&
(metadata != null && string.IsNullOrEmpty(metadata.Value<string>("@id")) == false) )
{
return sessionOperations.TrackEntity<T>(metadata.Value<string>("@id"),
result,
metadata);
}

if (typeof(T) == typeof(RavenJObject))
return (T)(object)result;

#if !NET_3_5
if (typeof(T) == typeof(object) && string.IsNullOrEmpty(result.Value<string>("$type")))
{
return (T)(object)new DynamicJsonObject(result);
}
#endif

var documentId = result.Value<string>(Constants.DocumentIdFieldName); //check if the result contain the reserved name

if (!string.IsNullOrEmpty(documentId) && typeof(T) == typeof(string) && // __document_id is present, and result type is a string
projectionFields != null && projectionFields.Length == 1 && // We are projecting one field only (although that could be derived from the
// previous check, one could never be too careful
((metadata != null && result.Count == 2) || (metadata == null && result.Count == 1)) // there are no more props in the result object
)
{
return (T)(object)documentId;
}

HandleInternalMetadata(result);

var deserializedResult = DeserializedResult<T>(result);

if (string.IsNullOrEmpty(documentId) == false)
{
// we need to make an addtional check, since it is possible that a value was explicitly stated
// for the identity property, in which case we don't want to override it.
var identityProperty = sessionOperations.Conventions.GetIdentityProperty(typeof(T));
if (identityProperty == null ||
(result[identityProperty.Name] == null ||
result[identityProperty.Name].Type == JTokenType.Null))
{
sessionOperations.TrySetIdentity(deserializedResult, documentId);
}
}

return deserializedResult;
}


private T DeserializedResult<T>(RavenJObject result)
{
if (projectionFields != null && projectionFields.Length == 1) // we only select a single field
{
var type = typeof(T);
if (type == typeof(string) || typeof(T).IsValueType || typeof(T).IsEnum)
{
return result.Value<T>(projectionFields[0]);
}
}

var jsonSerializer = sessionOperations.Conventions.CreateSerializer();
var ravenJTokenReader = new RavenJTokenReader(result);

var resultTypeString = result.Value<string>("$type");
if(string.IsNullOrEmpty(resultTypeString) )
{
return (T)jsonSerializer.Deserialize(ravenJTokenReader, typeof(T));
}

var resultType = Type.GetType(resultTypeString, false);
if(resultType == null) // couldn't find the type, let us give it our best shot
{
return (T)jsonSerializer.Deserialize(ravenJTokenReader, typeof(T));
}

return (T) jsonSerializer.Deserialize(ravenJTokenReader, resultType);
}

private void HandleInternalMetadata(RavenJObject result)
{
// Implant a property with "id" value ... if not exists
var metadata = result.Value<RavenJObject>("@metadata");
if (metadata == null || string.IsNullOrEmpty(metadata.Value<string>("@id")))
{
// if the item has metadata, then nested items will not have it, so we can skip recursing down
foreach (var nested in result.Select(property => property.Value))
{
var jObject = nested as RavenJObject;
if (jObject != null)
HandleInternalMetadata(jObject);
var jArray = nested as RavenJArray;
if (jArray == null)
continue;
foreach (var item in jArray.OfType<RavenJObject>())
{
HandleInternalMetadata(item);
}
}
return;
}

var entityName = metadata.Value<string>(Constants.RavenEntityName);

var idPropName = sessionOperations.Conventions.FindIdentityPropertyNameFromEntityName(entityName);
if (result.ContainsKey(idPropName))
return;

result[idPropName] = new RavenJValue(metadata.Value<string>("@id"));
}

public void ForceResult(QueryResult result)
{
currentQueryResults = result;
currentQueryResults.EnsureSnapshot();
}

public bool IsAcceptable(QueryResult result)
{
if (sessionOperations.AllowNonAuthoritativeInformation == false &&
result.NonAuthoritativeInformation)
{
if (sp.Elapsed > sessionOperations.NonAuthoritativeInformationTimeout)
{
sp.Stop();
throw new TimeoutException(
string.Format("Waited for {0:#,#;;0}ms for the query to return authoritative result.",
sp.ElapsedMilliseconds));
}
log.Debug(
"Non authoritative query results on authoritative query '{0}' on index '{1}' in '{2}', query will be retried, index etag is: {3}",
indexQuery.Query,
indexName,
sessionOperations.StoreIdentifier,
result.IndexEtag);
return false;
}
if (waitForNonStaleResults && result.IsStale)
{
if (sp.Elapsed > timeout)
{
sp.Stop();
throw new TimeoutException(
string.Format("Waited for {0:#,#;;0}ms for the query to return non stale result.",
sp.ElapsedMilliseconds));
}
log.Debug(
"Stale query results on non stale query '{0}' on index '{1}' in '{2}', query will be retried, index etag is: {3}",
indexQuery.Query,
indexName,
sessionOperations.StoreIdentifier,
result.IndexEtag);
return false;
}
currentQueryResults = result;
currentQueryResults.EnsureSnapshot();
log.Debug("Query returned {0}/{1} {2}results", result.Results.Count,
result.TotalResults, result.IsStale ? "stale " : "");
return true;
}

private void AddOperationHeaders()
{
foreach (var sortByHint in sortByHints)
{
if (sortByHint.Value == null)
continue;

setOperationHeaders(
string.Format("SortHint-{0}", Uri.EscapeDataString(sortByHint.Key.Trim('-'))),
FromPrimitiveTypestring(sortByHint.Value.Name).ToString());
}
}

private static SortOptions FromPrimitiveTypestring(string type)
{
switch (type)
{
case "Int16":
return SortOptions.Short;
case "Int32":
return SortOptions.Int;
case "Int64":
return SortOptions.Long;
case "Double":
case "Decimal":
return SortOptions.Double;
case "Single":
return SortOptions.Float;
case "String":
return SortOptions.String;
default:
return SortOptions.String;
}
}

}
}
Something went wrong with that request. Please try again.