private void ReplicateDocument(DocumentStorageActions actions, string id, JObject metadata, JObject document, string src) { var existingDoc = actions.DocumentByKey(id, null); if (existingDoc == null) { log.DebugFormat("New document {0} replicated successfully from {1}", id, src); actions.AddDocument(id, Guid.Empty, document, metadata); return; } var existingDocumentIsInConflict = existingDoc.Metadata[ReplicationConstants.RavenReplicationConflict] != null; if (existingDocumentIsInConflict == false && // if the current document is not in conflict, we can continue without having to keep conflict semantics (IsDirectChildOfCurrentDocument(existingDoc, metadata))) // this update is direct child of the existing doc, so we are fine with overwriting this { log.DebugFormat("Existing document {0} replicated successfully from {1}", id, src); actions.AddDocument(id, null, document, metadata); return; } var newDocumentConflictId = id + "/conflicts/" + metadata.Value <string>("@etag"); metadata.Add(ReplicationConstants.RavenReplicationConflict, JToken.FromObject(true)); actions.AddDocument(newDocumentConflictId, null, document, metadata); if (existingDocumentIsInConflict) // the existing document is in conflict { log.DebugFormat("Conflicted document {0} has a new version from {1}, adding to conflicted documents", id, src); // just update the current doc with the new conflict document existingDoc.DataAsJson.Value <JArray>("Conflicts").Add(JToken.FromObject(newDocumentConflictId)); actions.AddDocument(id, existingDoc.Etag, existingDoc.DataAsJson, existingDoc.Metadata); return; } log.DebugFormat("Existing document {0} is in conflict with replicated version from {1}, marking document as conflicted", id, src); // we have a new conflict // move the existing doc to a conflict and create a conflict document var existingDocumentConflictId = id + "/conflicts/" + existingDoc.Etag; existingDoc.Metadata.Add(ReplicationConstants.RavenReplicationConflict, JToken.FromObject(true)); actions.AddDocument(existingDocumentConflictId, null, existingDoc.DataAsJson, existingDoc.Metadata); actions.AddDocument(id, null, new JObject( new JProperty("Conflicts", new JArray(existingDocumentConflictId, newDocumentConflictId))), new JObject( new JProperty(ReplicationConstants.RavenReplicationConflict, true), new JProperty("@Http-Status-Code", 409), new JProperty("@Http-Status-Description", "Conflict") )); }
private static JsonDocument RetrieveDocument(DocumentStorageActions actions, IndexQueryResult queryResult, HashSet <string> loadedIds) { if (queryResult.Projection == null) { if (loadedIds.Add(queryResult.Key)) { return(actions.DocumentByKey(queryResult.Key, null)); } return(null); } return(new JsonDocument { Key = queryResult.Key, Projection = queryResult.Projection, }); }
private static JsonDocument RetrieveDocument(DocumentStorageActions actions, IndexQueryResult queryResult, HashSet<string> loadedIds) { if (queryResult.Projection == null) { if (loadedIds.Add(queryResult.Key)) return actions.DocumentByKey(queryResult.Key, null); return null; } return new JsonDocument { Key = queryResult.Key, Projection = queryResult.Projection, }; }