Permalink
Browse files

RavenDB-1613 Query should throw a ConflictException if any result is …

…conflicted
  • Loading branch information...
1 parent aed101f commit 7723bba925e31355e438fbb3f63bdcba8e481e02 @arekpalinski arekpalinski committed Jan 30, 2014
@@ -508,7 +508,15 @@ public QueryResult Query(string index, IndexQuery query, string[] includes, bool
var docResults = queryResult.Results.Concat(queryResult.Includes);
return RetryOperationBecauseOfConflict(docResults, queryResult,
- () => Query(index, query, includes, metadataOnly, indexEntriesOnly));
+ () => Query(index, query, includes, metadataOnly, indexEntriesOnly),
+ conflictedResultId =>
+ new ConflictException(
+ "Conflict detected on " +
+ conflictedResultId.Substring(0, conflictedResultId.IndexOf("/conflicts/", StringComparison.InvariantCulture)) +
+ ", conflict must be resolved before the document will be accessible", true)
+ {
+ ConflictedVersionIds = new[] { conflictedResultId }
+ });
}
/// <summary>
@@ -1189,9 +1197,11 @@ public ProfilingInformation ProfilingInformation
get { return profilingInformation; }
}
- private T RetryOperationBecauseOfConflict<T>(IEnumerable<RavenJObject> docResults, T currentResult, Func<T> nextTry)
+ private T RetryOperationBecauseOfConflict<T>(IEnumerable<RavenJObject> docResults, T currentResult, Func<T> nextTry,
+ Func<string, ConflictException> onConflictedQueryResult = null)
{
- bool requiresRetry = docResults.Aggregate(false, (current, docResult) => current | AssertNonConflictedDocumentAndCheckIfNeedToReload(docResult));
+ bool requiresRetry = docResults.Aggregate(false, (current, docResult) =>
+ current | AssertNonConflictedDocumentAndCheckIfNeedToReload(docResult, onConflictedQueryResult));
if (!requiresRetry)
return currentResult;
@@ -1277,7 +1287,7 @@ private bool AssertNonConflictedDocumentAndCheckIfNeedToReload(JsonDocument json
return false;
}
- private bool AssertNonConflictedDocumentAndCheckIfNeedToReload(RavenJObject docResult)
+ private bool AssertNonConflictedDocumentAndCheckIfNeedToReload(RavenJObject docResult, Func<string, ConflictException> onConflictedQueryResult = null)
{
if (docResult == null)
return false;
@@ -1292,6 +1302,10 @@ private bool AssertNonConflictedDocumentAndCheckIfNeedToReload(RavenJObject docR
return true;
throw concurrencyException;
}
+
+ if (metadata.Value<bool>(Constants.RavenReplicationConflict) && onConflictedQueryResult != null)
+ throw onConflictedQueryResult(metadata.Value<string>("@id"));
+
return false;
}
@@ -1263,7 +1263,17 @@ private QueryResult DirectQuery(string index, IndexQuery query, OperationMetadat
var directQuery = SerializationHelper.ToQueryResult(json, request.GetEtagHeader(), request.ResponseHeaders["Temp-Request-Time"]);
var docResults = directQuery.Results.Concat(directQuery.Includes);
return RetryOperationBecauseOfConflict(docResults, directQuery,
- () => DirectQuery(index, query, operationMetadata, includes, metadataOnly, includeEntries));
+ () =>
+ DirectQuery(index, query, operationMetadata, includes, metadataOnly,
+ includeEntries),
+ conflictedResultId =>
+ new ConflictException(
+ "Conflict detected on " +
+ conflictedResultId.Substring(0, conflictedResultId.IndexOf("/conflicts/", StringComparison.InvariantCulture)) +
+ ", conflict must be resolved before the document will be accessible", true)
+ {
+ ConflictedVersionIds = new[] {conflictedResultId}
+ });
}
/// <summary>
@@ -1374,9 +1384,11 @@ public MultiLoadResult DirectGet(string[] ids, OperationMetadata operationMetada
return RetryOperationBecauseOfConflict(docResults, multiLoadResult, () => DirectGet(ids, operationMetadata, includes, transformer, queryInputs, metadataOnly));
}
- private T RetryOperationBecauseOfConflict<T>(IEnumerable<RavenJObject> docResults, T currentResult, Func<T> nextTry)
+ private T RetryOperationBecauseOfConflict<T>(IEnumerable<RavenJObject> docResults, T currentResult, Func<T> nextTry,
+ Func<string, ConflictException> onConflictedQueryResult = null)
{
- bool requiresRetry = docResults.Aggregate(false, (current, docResult) => current | AssertNonConflictedDocumentAndCheckIfNeedToReload(docResult));
+ bool requiresRetry = docResults.Aggregate(false, (current, docResult) =>
+ current | AssertNonConflictedDocumentAndCheckIfNeedToReload(docResult, onConflictedQueryResult));
if (!requiresRetry)
return currentResult;
@@ -1394,7 +1406,7 @@ private T RetryOperationBecauseOfConflict<T>(IEnumerable<RavenJObject> docResult
}
}
- private bool AssertNonConflictedDocumentAndCheckIfNeedToReload(RavenJObject docResult)
+ private bool AssertNonConflictedDocumentAndCheckIfNeedToReload(RavenJObject docResult, Func<string, ConflictException> onConflictedQueryResult = null)
{
if (docResult == null)
return false;
@@ -1409,6 +1421,10 @@ private bool AssertNonConflictedDocumentAndCheckIfNeedToReload(RavenJObject docR
return true;
throw concurrencyException;
}
+
+ if(metadata.Value<bool>(Constants.RavenReplicationConflict) && onConflictedQueryResult != null)
+ throw onConflictedQueryResult(metadata.Value<string>("@id"));
+
return false;
}

0 comments on commit 7723bba

Please sign in to comment.