protected override CreatedConflict CreateConflict(string id, string newDocumentConflictId,
                                                          string existingDocumentConflictId, JsonDocument existingItem, RavenJObject existingMetadata)
        {
            existingMetadata.Add(Constants.RavenReplicationConflict, true);
            Actions.Documents.AddDocument(existingDocumentConflictId, Etag.Empty, existingItem.DataAsJson, existingItem.Metadata);
            var etag = existingMetadata.Value <bool>(Constants.RavenDeleteMarker) ? Etag.Empty : existingItem.Etag;

            Actions.Lists.Remove(Constants.RavenReplicationDocsTombstones, id);
            var conflictsArray = new RavenJArray(existingDocumentConflictId, newDocumentConflictId);
            var addResult      = Actions.Documents.AddDocument(id, etag,
                                                               new RavenJObject
            {
                { "Conflicts", conflictsArray }
            },
                                                               new RavenJObject
            {
                { Constants.RavenReplicationConflict, true },
                { "@Http-Status-Code", 409 },
                { "@Http-Status-Description", "Conflict" }
            });

            return(new CreatedConflict()
            {
                Etag = addResult.Etag,
                ConflictedIds = conflictsArray.Select(x => x.Value <string>()).ToArray()
            });
        }
Example #2
0
        protected override CreatedConflict AppendToCurrentItemConflicts(string id, string newConflictId, RavenJObject existingMetadata, Attachment existingItem)
        {
            var existingConflict = existingItem.Data().ToJObject();

            // just update the current attachment with the new conflict document
            RavenJArray conflictArray;

            existingConflict["Conflicts"] = conflictArray = new RavenJArray(existingConflict.Value <RavenJArray>("Conflicts"));

            var conflictEtag = existingItem.Etag;

            if (conflictArray.Contains(newConflictId) == false)
            {
                conflictArray.Add(newConflictId);

                var memoryStream = new MemoryStream();
                existingConflict.WriteTo(memoryStream);
                memoryStream.Position = 0;

                var newETag = Actions.Attachments.AddAttachment(id, existingItem.Etag, memoryStream, existingItem.Metadata);
                conflictEtag = newETag;
            }

            return(new CreatedConflict
            {
                Etag = conflictEtag,
                ConflictedIds = conflictArray.Select(x => x.Value <string>()).ToArray()
            });
        }
Example #3
0
        protected override CreatedConflict CreateConflict(string id, string newDocumentConflictId, string existingDocumentConflictId, Attachment existingItem, RavenJObject existingMetadata)
        {
            existingItem.Metadata.Add(Constants.RavenReplicationConflict, RavenJToken.FromObject(true));
            Actions.Attachments.AddAttachment(existingDocumentConflictId, null, existingItem.Data(), existingItem.Metadata);
            Actions.Lists.Remove(Constants.RavenReplicationDocsTombstones, id);
            var conflictsArray     = new RavenJArray(existingDocumentConflictId, newDocumentConflictId);
            var conflictAttachment = new RavenJObject
            {
                { "Conflicts", conflictsArray }
            };
            var memoryStream = new MemoryStream();

            conflictAttachment.WriteTo(memoryStream);
            memoryStream.Position = 0;
            var etag    = existingMetadata.Value <bool>(Constants.RavenDeleteMarker) ? null : existingItem.Etag;
            var newEtag = Actions.Attachments.AddAttachment(id, etag,
                                                            memoryStream,
                                                            new RavenJObject
            {
                { Constants.RavenReplicationConflict, true },
                { "@Http-Status-Code", 409 },
                { "@Http-Status-Description", "Conflict" }
            });

            return(new CreatedConflict()
            {
                Etag = newEtag,
                ConflictedIds = conflictsArray.Select(x => x.Value <string>()).ToArray()
            });
        }
Example #4
0
        private bool TryReplicationAttachments(ReplicationStrategy destination, RavenJArray jsonAttachments, out string errorMessage)
        {
            try
            {
                var url = destination.ConnectionStringOptions.Url + "/replication/replicateAttachments?from=" +
                          UrlEncodedServerUrl() + "&dbid=" + docDb.TransactionalStorage.Id;

                var sp      = Stopwatch.StartNew();
                var request = httpRavenRequestFactory.Create(url, "POST", destination.ConnectionStringOptions);

                request.WebRequest.Headers.Add("Attachment-Ids", string.Join(", ", jsonAttachments.Select(x => x.Value <string>("@id"))));

                request.WriteBson(jsonAttachments);
                request.ExecuteRequest();
                log.Info("Replicated {0} attachments to {1} in {2:#,#;;0} ms", jsonAttachments.Length, destination, sp.ElapsedMilliseconds);
                errorMessage = "";
                return(true);
            }
            catch (WebException e)
            {
                var response = e.Response as HttpWebResponse;
                if (response != null)
                {
                    using (var streamReader = new StreamReader(response.GetResponseStreamWithHttpDecompression()))
                    {
                        var error = streamReader.ReadToEnd();
                        try
                        {
                            var ravenJObject = RavenJObject.Parse(error);
                            log.WarnException("Replication to " + destination + " had failed\r\n" + ravenJObject.Value <string>("Error"), e);
                            errorMessage = error;
                            return(false);
                        }
                        catch (Exception)
                        {
                        }

                        log.WarnException("Replication to " + destination + " had failed\r\n" + error, e);
                        errorMessage = error;
                    }
                }
                else
                {
                    log.WarnException("Replication to " + destination + " had failed", e);
                    errorMessage = e.Message;
                }
                return(false);
            }
            catch (Exception e)
            {
                log.WarnException("Replication to " + destination + " had failed", e);
                errorMessage = e.Message;
                return(false);
            }
        }
        protected override CreatedConflict AppendToCurrentItemConflicts(string id, string newConflictId, RavenJObject existingMetadata, JsonDocument existingItem)
        {
            // just update the current doc with the new conflict document
            RavenJArray ravenJArray;

            existingItem.DataAsJson["Conflicts"] =
                ravenJArray = new RavenJArray(existingItem.DataAsJson.Value <RavenJArray>("Conflicts"));
            ravenJArray.Add(RavenJToken.FromObject(newConflictId));
            var addResult = Actions.Documents.AddDocument(id, existingItem.Etag, existingItem.DataAsJson, existingItem.Metadata);

            return(new CreatedConflict()
            {
                Etag = addResult.Etag,
                ConflictedIds = ravenJArray.Select(x => x.Value <string>()).ToArray()
            });
        }
Example #6
0
		private bool TryReplicationAttachments(ReplicationStrategy destination, RavenJArray jsonAttachments, out string errorMessage)
		{
			try
			{
				var url = destination.ConnectionStringOptions.Url + "/replication/replicateAttachments?from=" +
						  UrlEncodedServerUrl() + "&dbid=" + docDb.TransactionalStorage.Id;

				var sp = Stopwatch.StartNew();
				var request = httpRavenRequestFactory.Create(url, "POST", destination.ConnectionStringOptions);

				request.WebRequest.Headers.Add("Attachment-Ids", string.Join(", ", jsonAttachments.Select(x => x.Value<string>("@id"))));

				request.WriteBson(jsonAttachments);
				request.ExecuteRequest();
				log.Info("Replicated {0} attachments to {1} in {2:#,#;;0} ms", jsonAttachments.Length, destination, sp.ElapsedMilliseconds);
				errorMessage = "";
				return true;
			}
			catch (WebException e)
			{
				var response = e.Response as HttpWebResponse;
				if (response != null)
				{
					using (var streamReader = new StreamReader(response.GetResponseStreamWithHttpDecompression()))
					{
						var error = streamReader.ReadToEnd();
						try
						{
							var ravenJObject = RavenJObject.Parse(error);
							log.WarnException("Replication to " + destination + " had failed\r\n" + ravenJObject.Value<string>("Error"), e);
							errorMessage = error;
							return false;
						}
						catch (Exception)
						{
						}

						log.WarnException("Replication to " + destination + " had failed\r\n" + error, e);
						errorMessage = error;
					}
				}
				else
				{
					log.WarnException("Replication to " + destination + " had failed", e);
					errorMessage = e.Message;
				}
				return false;
			}
			catch (Exception e)
			{
				log.WarnException("Replication to " + destination + " had failed", e);
				errorMessage = e.Message;
				return false;
			}
		}
Example #7
0
        /// <summary>
        /// Deserialize a <param name="self"/> to a list of instances of<typeparam name="T"/>
        /// </summary>
        public static T[] JsonDeserialization <T>(this RavenJArray self)
        {
            var serializer = CreateDefaultJsonSerializer();

            return(self.Select(x => (T)serializer.Deserialize(new RavenJTokenReader(x), typeof(T))).ToArray());
        }