Beispiel #1
0
		private static void ModifyResult(RavenJObject result)
		{
			var tags = result.Value<string>("Tags");
			if (tags != null)
			{
				result["Tags"] =
					new RavenJArray(tags.Split(new[] {' ', ',', ';'}, StringSplitOptions.RemoveEmptyEntries));
			}
			else
			{
				result["Tags"] = new RavenJArray();
			}
			var deps = result.Value<string>("Dependencies");
			if (deps != null)
			{
				result["Dependencies"] =
					new RavenJArray(deps.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries)
						                .Select(s =>
							                {
								                var strings = s.Split(':');
								                return RavenJObject.FromObject(new {Package = strings[0], Version = strings[1]});
							                }));
			}
			result["PackageId"] = result["Id"];
			result.Remove("Id");
			result.Remove("__metadata");
			result.Remove("DownloadCount");
		}
Beispiel #2
0
		public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
		{
			if (key.StartsWith("Raven/", StringComparison.InvariantCultureIgnoreCase))
				return;

			if (metadata.Value<string>(VersioningUtil.RavenDocumentRevisionStatus) == "Historical")
				return;

			var versioningConfiguration = Database.GetDocumentVersioningConfiguration(metadata);
			if (versioningConfiguration == null || versioningConfiguration.Exclude)
				return;

			using (Database.DisableAllTriggersForCurrentThread())
			{
				var copyMetadata = new RavenJObject(metadata);
				copyMetadata[VersioningUtil.RavenDocumentRevisionStatus] = RavenJToken.FromObject("Historical");
				copyMetadata[Constants.RavenReadOnly] = true;
				copyMetadata.Remove(VersioningUtil.RavenDocumentRevision);
				var parentRevision = metadata.Value<string>(VersioningUtil.RavenDocumentRevision);
				if (parentRevision != null)
				{
					copyMetadata[VersioningUtil.RavenDocumentParentRevision] = key + "/revisions/" + parentRevision;
					metadata[VersioningUtil.RavenDocumentParentRevision] = key + "/revisions/" + parentRevision;
				}

				PutResult newDoc = Database.Put(key + "/revisions/", null, document, copyMetadata,
				                                transactionInformation);
				int revision = int.Parse(newDoc.Key.Split('/').Last());

				RemoveOldRevisions(key, revision, versioningConfiguration, transactionInformation);

				metadata[VersioningUtil.RavenDocumentRevisionStatus] = RavenJToken.FromObject("Current");
				metadata[VersioningUtil.RavenDocumentRevision] = RavenJToken.FromObject(revision);
			}
		}
Beispiel #3
0
		public bool FilterDocuments(string destinationId, string key, RavenJObject metadata)
		{
			if (key.StartsWith("Raven/", StringComparison.InvariantCultureIgnoreCase)) // don't replicate system docs
			{
				if (key.StartsWith("Raven/Hilo/", StringComparison.InvariantCultureIgnoreCase) == false) // except for hilo documents
					return false;
			}
			if (metadata.ContainsKey(Constants.NotForReplication) && metadata.Value<bool>(Constants.NotForReplication)) // not explicitly marked to skip
				return false;
			if (metadata[Constants.RavenReplicationConflict] != null) // don't replicate conflicted documents, that just propagate the conflict
				return false;

			if (metadata.Value<string>(Constants.RavenReplicationSource) == destinationId) // prevent replicating back to source
				return false;

			switch (ReplicationOptionsBehavior)
			{
				case TransitiveReplicationOptions.None:
					var value = metadata.Value<string>(Constants.RavenReplicationSource);
					var replicateDoc = value == null || (value == CurrentDatabaseId);
					return replicateDoc;
			}
			return true;

		}
		public override void AfterPut(string key, RavenJObject document, RavenJObject metadata, System.Guid etag, TransactionInformation transactionInformation)
		{
			if (key.StartsWith("Raven/"))
			{
				return;
			}

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

			var properties = metadata.Value<RavenJArray>(Constants.EnsureUniqueConstraints);

			if (properties == null || properties.Count() <= 0) 
				return;

			var constraintMetaObject = new RavenJObject { { Constants.IsConstraintDocument, true } };
			foreach (var property in properties)
			{
				var propName = ((RavenJValue)property).Value.ToString();
				Database.Put(
					"UniqueConstraints/" + entityName + propName + "/" + document.Value<string>(propName),
					null,
					RavenJObject.FromObject(new { RelatedId = key }),
					constraintMetaObject,
					transactionInformation);
			}
		}
		public override VetoResult AllowPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
		{
			if (key.StartsWith("Raven/"))
			{
				return VetoResult.Allowed;
			}

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

			if (string.IsNullOrEmpty(entityName))
			{
				return VetoResult.Allowed;
			}
			entityName += "/";

			var properties = metadata.Value<RavenJArray>(Constants.EnsureUniqueConstraints);

			if (properties == null || properties.Length <= 0)
				return VetoResult.Allowed;

			var invalidFields = new StringBuilder();

			foreach (var property in properties)
			{
				var propName = property.Value<string>();

				var prefix = "UniqueConstraints/" + entityName + property + "/";
				var prop = document[propName];

				if (prop == null || prop.Type == JTokenType.Null)
					continue;

				var array = prop as RavenJArray;
				var checkKeys = array != null ? array.Select(p => p.Value<string>()) : new[] { prop.Value<string>() };

				foreach (var checkKey in checkKeys)
				{
					var checkDoc = Database.Get(prefix + Util.EscapeUniqueValue(checkKey), transactionInformation);

					if (checkDoc == null)
						continue;

					var checkId = checkDoc.DataAsJson.Value<string>("RelatedId");

					if (checkId != key)
						invalidFields.Append(property + ", ");
				}
			}

			if (invalidFields.Length > 0)
			{
				invalidFields.Length = invalidFields.Length - 2;
				return VetoResult.Deny("Ensure unique constraint violated for fields: " + invalidFields);
			}

			return VetoResult.Allowed;
		}
Beispiel #6
0
        private static List<HistoryItem> TransformToFullConflictHistory(RavenJObject metadata)
        {
            var version = metadata.Value<long>(SynchronizationConstants.RavenSynchronizationVersion);
            var serverId = metadata.Value<string>(SynchronizationConstants.RavenSynchronizationSource);
            var fullHistory = Historian.DeserializeHistory(metadata);
            fullHistory.Add(new HistoryItem { ServerId = serverId, Version = version });

            return fullHistory;
        }
	    public override VetoResult AllowPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
		{
			if (key.StartsWith("Raven/"))
			{
				return VetoResult.Allowed;
			}

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

			if (string.IsNullOrEmpty(entityName))
			{
				return VetoResult.Allowed;
			}
			entityName += "/";

			var properties = metadata.Value<RavenJArray>(Constants.EnsureUniqueConstraints);

			if (properties == null || properties.Length <= 0)
				return VetoResult.Allowed;

			var invalidFields = new StringBuilder();

			foreach (var property in properties)
			{
			    var constraint = Util.GetConstraint(property);

                var prefix = "UniqueConstraints/" + entityName + constraint.PropName+ "/";
                var prop = document[constraint.PropName];
                
			    string[] uniqueValues;
			    if (!Util.TryGetUniqueValues(prop, out uniqueValues))
                    continue;

				foreach (var uniqueValue in uniqueValues)
				{
                    var escapedUniqueValue = Util.EscapeUniqueValue(uniqueValue, constraint.CaseInsensitive);
				    var checkDocKey = prefix + escapedUniqueValue;
                    var checkDoc = Database.Documents.Get(checkDocKey, transactionInformation);

					if (checkDoc == null)
						continue;

				    var checkId = GetRelatedIdFromUniqueConstraintsDocument(checkDoc, escapedUniqueValue);

					if (!string.IsNullOrEmpty(checkId) && checkId != key)
						invalidFields.Append(constraint.PropName + ", ");
				}
			}

			if (invalidFields.Length > 0)
			{
				invalidFields.Length = invalidFields.Length - 2;
				return VetoResult.Deny("Ensure unique constraint violated for fields: " + invalidFields);
			}

			return VetoResult.Allowed;
		}
		private static DateTimeOffset GetLastModified(RavenJObject metadata)
		{
			if (metadata.ContainsKey(Constants.LastModified))
				return metadata.Value<DateTimeOffset>(Constants.LastModified);

			if (metadata.ContainsKey(Constants.RavenLastModified))
				return metadata.Value<DateTimeOffset>(Constants.RavenLastModified);

			throw new InvalidOperationException("Could not find last modification date in metadata");
		}
Beispiel #9
0
        public override void OnRead(string key, RavenJObject document, RavenJObject metadata, ReadOperation operation, TransactionInformation transactionInformation)
        {
            var linkName = metadata.Value<string>("Raven-Link-Name");
            var link = metadata.Value<string>("Raven-Link");
            if (link == null)
                return;

            var linkedDocument = Database.Get(link, transactionInformation);
            document.Add(linkName, linkedDocument.ToJson());
        }
Beispiel #10
0
		public static ICommandData CreateCommand(RavenJObject jsonCommand, TransactionInformation transactionInformation)
		{
			var key = jsonCommand["Key"].Value<string>();
			switch (jsonCommand.Value<string>("Method"))
			{
				case "PUT":
					return new PutCommandData
					{
						Key = key,
						Etag = GetEtagFromCommand(jsonCommand),
						Document = jsonCommand["Document"] as RavenJObject,
						Metadata = jsonCommand["Metadata"] as RavenJObject,
						TransactionInformation = transactionInformation
					};
				case "DELETE":
					return new DeleteCommandData
					{
						Key = key,
						Etag = GetEtagFromCommand(jsonCommand),
						TransactionInformation = transactionInformation
					};
				case "PATCH":
					return new PatchCommandData
					{
						Key = key,
						Etag = GetEtagFromCommand(jsonCommand),
						TransactionInformation = transactionInformation,
						Metadata = jsonCommand["Metadata"] as RavenJObject,
						Patches = jsonCommand
							.Value<RavenJArray>("Patches")
							.Cast<RavenJObject>()
							.Select(PatchRequest.FromJson)
							.ToArray(),
						PatchesIfMissing = jsonCommand["PatchesIfMissing"] == null ? null : jsonCommand
							.Value<RavenJArray>("PatchesIfMissing")
							.Cast<RavenJObject>()
							.Select(PatchRequest.FromJson)
							.ToArray(),
					};
				case "EVAL":
					var debug = jsonCommand["DebugMode"].Value<bool>();
					return new ScriptedPatchCommandData
					{
						Key = key,
						Metadata = jsonCommand["Metadata"] as RavenJObject,
						Etag = GetEtagFromCommand(jsonCommand),
						TransactionInformation = transactionInformation,
						Patch = ScriptedPatchRequest.FromJson(jsonCommand.Value<RavenJObject>("Patch")),
						PatchIfMissing = jsonCommand["PatchIfMissing"] == null ? null : ScriptedPatchRequest.FromJson(jsonCommand.Value<RavenJObject>("PatchIfMissing")),
						DebugMode = debug
					};
				default:
					throw new ArgumentException("Batching only supports PUT, PATCH, EVAL and DELETE.");
			}
		}
 private void ExtractDataAndDo(RavenJObject result, Action<Employee, string> action)
 {
     var attachmentId = result.Value<string>("AttachmentId");
     var employeeId = result.Value<string>("RelatedEntityId");
     using (var session = DocumentStore.OpenSession())
     {
         var employee = session.Load<Employee>(employeeId);
         action(employee, attachmentId);
         session.SaveChanges();
     }
 }
        public override void AfterPut(string key, RavenJObject document, RavenJObject metadata, Etag etag, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/"))
            {
                return;
            }

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

            var properties = metadata.Value<RavenJArray>(Constants.EnsureUniqueConstraints);

            if (properties == null || properties.Length <= 0)
                return;

            foreach (var property in properties)
            {
                var constraint = Util.GetConstraint(property);
                var prop = document[constraint.PropName];

                string[] uniqueValues;
                if (!Util.TryGetUniqueValues(prop, out uniqueValues))
                    continue;

                var prefix = "UniqueConstraints/" + entityName + constraint.PropName + "/";

                foreach (var uniqueValue in uniqueValues)
                {
                    var escapedUniqueValue = Util.EscapeUniqueValue(uniqueValue, constraint.CaseInsensitive);
                    var uniqueConstraintsDocumentKey = prefix + escapedUniqueValue;
                    var uniqueConstraintsDocument = Database.Documents.Get(uniqueConstraintsDocumentKey, transactionInformation);                    

                    if (uniqueConstraintsDocument != null)
                    {
                        uniqueConstraintsDocument = DeepCloneDocument(uniqueConstraintsDocument);
                        ConvertUniqueConstraintsDocumentIfNecessary(uniqueConstraintsDocument, escapedUniqueValue); // backward compatibility
}
                    else
                        uniqueConstraintsDocument = new JsonDocument();

                    AddConstraintToUniqueConstraintsDocument(uniqueConstraintsDocument, escapedUniqueValue, key);
                    uniqueConstraintsDocument.Metadata[Constants.IsConstraintDocument] = true;

                    Database.Documents.Put(
                        uniqueConstraintsDocumentKey,
                        null,
                        uniqueConstraintsDocument.DataAsJson,
                        uniqueConstraintsDocument.Metadata,
                        transactionInformation);
                }
            }
        }
		public override VetoResult AllowPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
		{
			if (key.StartsWith("Raven/"))
			{
				return VetoResult.Allowed;
			}

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

			if (string.IsNullOrEmpty(entityName))
			{
				return VetoResult.Allowed;
			}
			entityName += "/";

			var properties = metadata.Value<RavenJArray>(Constants.EnsureUniqueConstraints);

			if (properties == null || properties.Length <= 0) 
				return VetoResult.Allowed;

			var invalidFields = new StringBuilder();

			foreach (var property in properties)
			{
				var propName = ((RavenJValue)property).Value.ToString();
				var uniqueValue = document.Value<string>(propName);
				if(uniqueValue == null)
					continue;
				var checkKey = "UniqueConstraints/" + entityName + propName + "/" +
				               Util.EscapeUniqueValue(uniqueValue);
				var checkDoc = Database.Get(checkKey, transactionInformation);
				if (checkDoc == null) 
					continue;

				var checkId = checkDoc.DataAsJson.Value<string>("RelatedId");

				if (checkId != key)
				{
					invalidFields.Append(property + ", ");
				}
			}

			if (invalidFields.Length > 0)
			{
				invalidFields.Length = invalidFields.Length - 2;
				return VetoResult.Deny("Ensure unique constraint violated for fields: " + invalidFields);
			}

			return VetoResult.Allowed;
		}
Beispiel #14
0
        public bool FilterDocuments(string destinationId, string key, RavenJObject metadata, out string reason)
        {
            if (IsSystemDocumentId(key))
            {
                reason = string.Format("Will not replicate document '{0}' to '{1}' because it is a system document", key, destinationId);
                Log.Debug(reason);
                return false;
            }
            if (metadata.ContainsKey(Constants.NotForReplication) && metadata.Value<bool>(Constants.NotForReplication))
                // not explicitly marked to skip
            {
                reason = string.Format("Will not replicate document '{0}' to '{1}' because it was marked as not for replication", key, destinationId);
                Log.Debug(reason); 
                return false;
            }

            if (metadata[Constants.RavenReplicationConflict] != null)
                // don't replicate conflicted documents, that just propagate the conflict
            {
                reason = string.Format("Will not replicate document '{0}' to '{1}' because it a conflict document", key, destinationId);
                Log.Debug(reason); 
                return false;
            }

            if (OriginsFromDestination(destinationId, metadata)) // prevent replicating back to source
            {
                reason = string.Format("Will not replicate document '{0}' to '{1}' because the destination server is the same server it originated from", key, destinationId);
                Log.Debug(reason); 
                return false;
            }

            switch (ReplicationOptionsBehavior)
            {
                case TransitiveReplicationOptions.None:
                    var value = metadata.Value<string>(Constants.RavenReplicationSource);
                    if (value != null &&  (value != CurrentDatabaseId))
                    {
                        reason = string.Format("Will not replicate document '{0}' to '{1}' because it was not created on the current server, and TransitiveReplicationOptions = none", key, destinationId);
                        Log.Debug(reason);
                        return false;
                    }
                    break;
            }
            reason = string.Format("Will replicate '{0}' to '{1}'", key, destinationId);
            Log.Debug(reason);
            return true;

        }
Beispiel #15
0
        public Guid AddDocument(string key, Guid?etag, RavenJObject data, RavenJObject metadata)
        {
            var existingEtag = AssertValidEtag(key, etag, "PUT", null);

            var ms = new MemoryStream();

            metadata.WriteTo(ms);

            using (var stream = documentCodecs.Aggregate <Lazy <AbstractDocumentCodec>, Stream>(ms, (dataStream, codec) => codec.Value.Encode(key, data, metadata, dataStream)))
            {
                data.WriteTo(stream);
                stream.Flush();
            }

            var newEtag = generator.CreateSequentialUuid();

            storage.Documents.Put(new RavenJObject
            {
                { "key", key },
                { "etag", newEtag.ToByteArray() },
                { "modified", SystemTime.UtcNow },
                { "id", GetNextDocumentId() },
                { "entityName", metadata.Value <string>(Constants.RavenEntityName) }
            }, ms.ToArray());

            documentCacher.RemoveCachedDocument(key, existingEtag);

            return(newEtag);
        }
Beispiel #16
0
        public Task PutDocument(RavenJObject document, int size)
        {
            if (document != null)
            {
                var metadata = document.Value <RavenJObject>("@metadata");
                var key      = metadata.Value <string>("@id");
                document.Remove("@metadata");

                bulkInsertBatch.Add(new JsonDocument
                {
                    Key        = key,
                    Metadata   = metadata,
                    DataAsJson = document,
                });

                if (Options.BatchSize > bulkInsertBatch.Count)
                {
                    return(new CompletedTask());
                }
            }

            var batchToSave = new List <IEnumerable <JsonDocument> > {
                bulkInsertBatch
            };

            bulkInsertBatch = new List <JsonDocument>();
            database.Documents.BulkInsert(new BulkInsertOptions {
                BatchSize = Options.BatchSize, OverwriteExisting = true
            }, batchToSave, Guid.NewGuid(), CancellationToken.None);
            return(new CompletedTask());
        }
Beispiel #17
0
        private bool HasSingleValidProperty(RavenJObject result, RavenJObject metadata)
        {
            if (metadata == null && result.Count == 1)
            {
                return(true);// { Foo: val }
            }
            if ((metadata != null && result.Count == 2))
            {
                return(true); // { @metadata: {}, Foo: val }
            }
            if ((metadata != null && result.Count == 3))
            {
                var entityName = metadata.Value <string>(Constants.RavenEntityName);

                var idPropName = sessionOperations.Conventions.FindIdentityPropertyNameFromEntityName(entityName);

                if (result.ContainsKey(idPropName))
                {
                    // when we try to project the id by name
                    var token = result.Value <RavenJToken>(idPropName);

                    if (token == null || token.Type == JTokenType.Null)
                    {
                        return(true); // { @metadata: {}, Foo: val, Id: null }
                    }
                }
            }

            return(false);
        }
Beispiel #18
0
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            VersioningConfiguration versioningConfiguration;

            if (metadata.ContainsKey(Constants.RavenCreateVersion))
            {
                metadata.__ExternalState[Constants.RavenCreateVersion] = metadata[Constants.RavenCreateVersion];
                metadata.Remove(Constants.RavenCreateVersion);
            }

            if (metadata.ContainsKey(Constants.RavenIgnoreVersioning))
            {
                metadata.__ExternalState[Constants.RavenIgnoreVersioning] = metadata[Constants.RavenIgnoreVersioning];
                metadata.Remove(Constants.RavenIgnoreVersioning);
                return;
            }

            if (TryGetVersioningConfiguration(key, metadata, out versioningConfiguration) == false)
            {
                return;
            }

            var revision = GetNextRevisionNumber(key);

            using (Database.DisableAllTriggersForCurrentThread())
            {
                RemoveOldRevisions(key, revision, versioningConfiguration, transactionInformation);
            }
            metadata.__ExternalState["Next-Revision"] = revision;

            metadata.__ExternalState["Parent-Revision"] = metadata.Value <string>(VersioningUtil.RavenDocumentRevision);

            metadata[VersioningUtil.RavenDocumentRevisionStatus] = RavenJToken.FromObject("Current");
            metadata[VersioningUtil.RavenDocumentRevision]       = RavenJToken.FromObject(revision);
        }
Beispiel #19
0
        public bool ExcludeExpired(RavenJObject document, DateTime now)
        {
            var metadata = document.Value <RavenJObject>("@metadata");

            const string RavenExpirationDate = "Raven-Expiration-Date";

            // check for expired documents and exclude them if expired
            if (metadata == null)
            {
                return(false);
            }
            var property = metadata[RavenExpirationDate];

            if (property == null)
            {
                return(false);
            }

            DateTime dateTime;

            try
            {
                dateTime = property.Value <DateTime>();
            }
            catch (FormatException)
            {
                return(false);
            }

            return(dateTime < now);
        }
Beispiel #20
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()
            });
        }
Beispiel #21
0
			public void AfterConversionToEntity(string key, RavenJObject document, RavenJObject metadata, object entity)
			{
				if (entity is Item == false)
					return;

				((Item)entity).Revision = metadata.Value<string>("Raven-Document-Revision");
			}
Beispiel #22
0
        private IEnumerable <ICommandData> MigratePerson1ToPerson2(RavenJObject doc, RavenJObject metadata)
        {
            var name = doc.Value <string>("Name");

            if (!string.IsNullOrEmpty(name))
            {
                doc["FirstName"] = name.Split(' ')[0];
                doc["LastName"]  = name.Split(' ')[1];
            }
            doc.Remove("Name");

            metadata[Constants.RavenClrType] = "RavenMigrations.Tests.Person2, RavenMigrations.Tests";

            var foobaz = new FooBaz
            {
                Id  = 1,
                Bar = "loaded"
            };

            var foobazDoc = RavenJObject.FromObject(foobaz);
            var meta      = new RavenJObject();

            meta[Constants.RavenEntityName] = "FooBazs";
            var cmd = new PutCommandData
            {
                Document = foobazDoc,
                Key      = "foobazs/" + foobaz.Id,
                Metadata = meta
            };

            return(new[] { cmd });
        }
        public override void AfterPut(string key, RavenJObject document, RavenJObject metadata, Etag etag, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/", StringComparison.InvariantCultureIgnoreCase))
            return;

              var status = document.Value<string>("Status");
              if (string.IsNullOrWhiteSpace(status) || !status.Equals("Published", StringComparison.InvariantCultureIgnoreCase))
            return;

              if (metadata.Value<string>(PublishedVersioningConstants.AtisDocumentRevisionStatus) == "Historical")
            return;

              using (Database.DisableAllTriggersForCurrentThread())
              {
            var copyMetadata = new RavenJObject(metadata);
            copyMetadata[PublishedVersioningConstants.AtisDocumentRevisionStatus] = RavenJToken.FromObject("Historical");
            copyMetadata[Constants.RavenReadOnly] = true;
            copyMetadata.Remove(PublishedVersioningConstants.AtisDocumentRevision);

            object value;
            metadata.__ExternalState.TryGetValue("Next-Revision", out value);
            var revisionKey = string.Format("{0}/PublishedRevisions/{1}", key, value);
            Database.Put(revisionKey, null, (RavenJObject)document.CreateSnapshot(), copyMetadata, transactionInformation);
            log.Debug(() => string.Format("Created published revision with key [{0}]", revisionKey));
              }
        }
        public Guid AddDocument(string key, Guid?etag, RavenJObject data, RavenJObject metadata)
        {
            AssertValidEtag(key, etag, "PUT", null);

            var ms = new MemoryStream();

            metadata.WriteTo(ms);

            var bytes = documentCodecs.Aggregate(data.ToBytes(), (current, codec) => codec.Value.Encode(key, data, metadata, current));

            ms.Write(bytes, 0, bytes.Length);

            var newEtag = generator.CreateSequentialUuid();

            storage.Documents.Put(new RavenJObject
            {
                { "key", key },
                { "etag", newEtag.ToByteArray() },
                { "modified", DateTime.UtcNow },
                { "id", GetNextDocumentId() },
                { "entityName", metadata.Value <string>(Constants.RavenEntityName) }
            }, ms.ToArray());

            return(newEtag);
        }
Beispiel #25
0
        public override void OnPut(string name, RavenJObject headers)
        {
            if (headers.ContainsKey(Constants.RavenCreateVersion))
            {
                headers.__ExternalState[Constants.RavenCreateVersion] = headers[Constants.RavenCreateVersion];
                headers.Remove(Constants.RavenCreateVersion);
            }

            FileSystem.Storage.Batch(accessor =>
            {
                VersioningConfiguration versioningConfiguration;
                if (TryGetVersioningConfiguration(name, headers, accessor, out versioningConfiguration) == false)
                {
                    return;
                }

                var revision = GetNextRevisionNumber(name, accessor);

                using (FileSystem.DisableAllTriggersForCurrentThread())
                {
                    RemoveOldRevisions(name, revision, versioningConfiguration);
                }

                headers.__ExternalState["Next-Revision"]   = revision;
                headers.__ExternalState["Parent-Revision"] = headers.Value <string>(VersioningUtil.RavenFileRevision);

                headers[VersioningUtil.RavenFileRevisionStatus] = RavenJToken.FromObject("Current");
                headers[VersioningUtil.RavenFileRevision]       = RavenJToken.FromObject(revision);
            });
        }
Beispiel #26
0
        private static JsonDocument PrepareJsonDocument(RavenJObject doc)
        {
            var metadata = doc.Value <RavenJObject>("@metadata");

            if (metadata == null)
            {
                throw new InvalidOperationException("Could not find metadata for document");
            }

            var id = metadata.Value <string>("@id");

            if (string.IsNullOrEmpty(id))
            {
                throw new InvalidOperationException("Could not get id from metadata");
            }

            if (id.Equals(Constants.BulkImportHeartbeatDocKey, StringComparison.InvariantCultureIgnoreCase))
            {
                return(null); //its just a token document, should not get written into the database
            }
            //the purpose of the heartbeat document is to make sure that the connection doesn't time-out
            //during long pauses in the bulk insert operation.
            // Currently used by smuggler to make sure that the connection doesn't time out if there is a
            //continuation token and lots of document skips

            doc.Remove("@metadata");

            return(new JsonDocument
            {
                Key = id,
                DataAsJson = doc,
                Metadata = metadata
            });
        }
Beispiel #27
0
        public T Get <T>(string key, DateTime expiry, Func <T> getFromRiotFunc)
        {
            using (var session = _documentStore.OpenSession())
            {
                logger.Debug($"Getting {key} from cache");

                var entity = session.Load <T>(key);

                if (entity != null)
                {
                    RavenJObject metadata = session.Advanced.GetMetadataFor(entity);

                    // Get the last modified time stamp, which is known to be of type DateTime
                    DateTime expirationDate = metadata.Value <DateTime>("Raven-Expiration-Date");
                    if (expirationDate < DateTime.UtcNow)
                    {
                        entity = Update(session, entity, key, expiry, getFromRiotFunc);
                    }
                }
                else
                {
                    entity = Store(session, key, expiry, getFromRiotFunc);
                }
                return(entity);
            }
        }
Beispiel #28
0
        public override void OnPut(string key, Stream data, RavenJObject metadata)
        {
            if (key.StartsWith("Raven/"))             // we don't deal with system attachment
            {
                return;
            }
            using (Database.DisableAllTriggersForCurrentThread())
            {
                var attachmentMetadata = GetAttachmentMetadata(key);
                if (attachmentMetadata != null)
                {
                    RavenJArray history = new RavenJArray(metadata.Value <RavenJArray>(Constants.RavenReplicationHistory));
                    metadata[Constants.RavenReplicationHistory] = history;

                    if (attachmentMetadata.ContainsKey(Constants.RavenReplicationVersion) &&
                        attachmentMetadata.ContainsKey(Constants.RavenReplicationSource))
                    {
                        history.Add(new RavenJObject
                        {
                            { Constants.RavenReplicationVersion, attachmentMetadata[Constants.RavenReplicationVersion] },
                            { Constants.RavenReplicationSource, attachmentMetadata[Constants.RavenReplicationSource] }
                        });
                    }

                    if (history.Length > Constants.ChangeHistoryLength)
                    {
                        history.RemoveAt(0);
                    }
                }

                metadata[Constants.RavenReplicationVersion] = RavenJToken.FromObject(HiLo.NextId());
                metadata[Constants.RavenReplicationSource]  = RavenJToken.FromObject(Database.TransactionalStorage.Id);
            }
        }
Beispiel #29
0
        private VersioningConfiguration GetDocumentVersioningConfiguration(RavenJObject metadata)
        {
            JsonDocument doc = null;

            var entityName = metadata.Value <string>("Raven-Entity-Name");

            if (entityName != null)
            {
                doc = Database.Get("Raven/Versioning/" + entityName, null);
            }

            if (doc == null)
            {
                doc = Database.Get("Raven/Versioning/DefaultConfiguration", null);
            }

            if (doc != null)
            {
                return(doc.DataAsJson.JsonDeserialization <VersioningConfiguration>());
            }

            return(new VersioningConfiguration
            {
                MaxRevisions = Int32.MaxValue,
                Exclude = false
            });
        }
Beispiel #30
0
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            VersioningConfiguration versioningConfiguration;

            if (TryGetVersioningConfiguration(key, metadata, out versioningConfiguration) == false)
            {
                return;
            }

            int revision = 0;

            Database.TransactionalStorage.Batch(accessor =>
            {
                revision = (int)accessor.General.GetNextIdentityValue(key + "/revisions/");
            });
            using (Database.DisableAllTriggersForCurrentThread())
            {
                RemoveOldRevisions(key, revision, versioningConfiguration, transactionInformation);
            }
            metadata.__ExteranlState["Next-Revision"] = revision;

            metadata.__ExteranlState["Parent-Revision"] = metadata.Value <string>(VersioningUtil.RavenDocumentRevision);

            metadata[VersioningUtil.RavenDocumentRevisionStatus] = RavenJToken.FromObject("Current");
            metadata[VersioningUtil.RavenDocumentRevision]       = RavenJToken.FromObject(revision);
        }
Beispiel #31
0
		private VersioningConfiguration GetDocumentVersioningConfiguration(RavenJObject metadata)
		{
		    JsonDocument doc = null;

			var entityName = metadata.Value<string>("Raven-Entity-Name");
			if(entityName != null)
			{
				doc = Database.Get("Raven/Versioning/" + entityName, null);
			}

			if(doc == null)
			{
				doc = Database.Get("Raven/Versioning/DefaultConfiguration", null);
			}

		    if (doc != null)
		    {
		        return doc.DataAsJson.JsonDeserialization<VersioningConfiguration>();
		    }

		    return new VersioningConfiguration
		               {
		                   MaxRevisions = Int32.MaxValue,
		                   Exclude = false
		               };
		}
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (!metadata.ContainsKey("IsRoutable") || !metadata.Value<bool>("IsRoutable")) {
                return;
            }

            RavenJToken parentIdToken;
            RavenJToken slugToken;
            if (document.TryGetValue("ParentId", out parentIdToken) && document.TryGetValue("Slug", out slugToken)) {
                var parentId = parentIdToken.Value<string>();
                var slug = slugToken.Value<string>();

                string parentPath = null;
                if (!String.IsNullOrEmpty(parentId)) {
                    var parent = Database.Get(parentId, transactionInformation);
                    parentPath = parent.DataAsJson["Path"].Value<string>();
                }

                if (String.IsNullOrEmpty(parentPath)) {
                    document["Path"] = slug;
                } else {
                    document["Path"] = parentPath + "/" + slug;
                }
            }

            base.OnPut(key, document, metadata, transactionInformation);
        }
        public static bool IsTemporalVersioningEnabled(this DocumentDatabase database, string key, RavenJObject metadata)
        {
            if (key == null)
                return false;

            // Don't ever version raven system documents.
            if (key.StartsWith("Raven/", StringComparison.InvariantCultureIgnoreCase))
                return false;

            // Don't version this one from the test helpers either.
            if (key == "Pls Delete Me")
                return false;

            // Don't version any doc that isn't an entity.
            var entityName = metadata.Value<string>(Constants.RavenEntityName);
            if (entityName == null)
                return false;

            bool enabled;
            var cacheKey = (database.Name ?? "") + ":" + entityName;
            if (ConfigCache.TryGetValue(cacheKey, out enabled))
                return enabled;

            var temporalVersioningConfiguration = database.GetTemporalVersioningConfiguration(entityName);
            enabled = temporalVersioningConfiguration != null && temporalVersioningConfiguration.Enabled;
            ConfigCache.TryAdd(cacheKey, enabled);

            return enabled;
        }
Beispiel #34
0
        private void TestCreatedDate(RavenJObject metadata)
        {
            var createdDate = metadata.Value <DateTime>("CreatedDate");

            Assert.Equal(DateTimeKind.Unspecified, createdDate.Kind);
            Assert.Equal(AuditTrigger.CreatedAtDateTime, createdDate);
        }
		public override ReadVetoResult AllowRead(string name, RavenJObject metadata, ReadOperation operation)
		{
			if (metadata.Value<bool>(SynchronizationConstants.RavenDeleteMarker))
				return ReadVetoResult.Ignore;

			return ReadVetoResult.Allowed;
		}
        public void AttachmentStorage_AttachmentAdded_AttachmentFeched(string requestedStorage)
        {
            using (var storage = NewTransactionalStorage(requestedStorage))
            {
                using (Stream dataStream = new MemoryStream())
                {
                    var data = RavenJObject.FromObject(new { Name = "Bar" });
                    data.WriteTo(dataStream);
                    dataStream.Position = 0;
                    storage.Batch(mutator => mutator.Attachments.AddAttachment("Foo", null, dataStream, new RavenJObject()));

                    Attachment fetchedAttachment = null;
                    storage.Batch(viewer => fetchedAttachment = viewer.Attachments.GetAttachment("Foo"));

                    Assert.NotNull(fetchedAttachment);

                    RavenJObject fetchedAttachmentData = null;
                    Assert.DoesNotThrow(() =>
                                        storage.Batch(viewer =>
                    {
                        using (var fetchedDataStream = fetchedAttachment.Data())
                            fetchedAttachmentData = fetchedDataStream.ToJObject();
                    }));
                    Assert.NotNull(fetchedAttachmentData);

                    Assert.Equal(fetchedAttachmentData.Keys, data.Keys);
                    Assert.Equal(1, fetchedAttachmentData.Count);
                    Assert.Equal(fetchedAttachmentData.Value <string>("Name"), data.Value <string>("Name"));
                }
            }
        }
Beispiel #37
0
        public override VetoResult AllowPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            var isNewReadOnly = metadata.Value <bool>(Constants.RavenReadOnly);

            if (!isNewReadOnly)
            {
                return(VetoResult.Allowed);
            }

            var old = Database.Documents.Get(key, transactionInformation);

            if (old == null)
            {
                return(VetoResult.Allowed);
            }

            var isOldReadOnly = old.Metadata.Value <bool>(Constants.RavenReadOnly);

            if (isOldReadOnly)
            {
                if (Database.IsVersioningDisabledForImport(metadata))
                {
                    return(VetoResult.Allowed);
                }
                return(VetoResult.Deny(string.Format("You cannot update document '{0}' when both of them, new and existing one, are marked as readonly. To update this document change '{1}' flag to 'False' or remove it entirely.", key, Constants.RavenReadOnly)));
            }
            return(VetoResult.Allowed);
        }
		public override void OnPut(string name, RavenJObject headers)
		{
			if (headers.ContainsKey(Constants.RavenCreateVersion))
			{
				headers.__ExternalState[Constants.RavenCreateVersion] = headers[Constants.RavenCreateVersion];
				headers.Remove(Constants.RavenCreateVersion);
			}

			FileSystem.Storage.Batch(accessor =>
			{
				VersioningConfiguration versioningConfiguration;
				if (TryGetVersioningConfiguration(name, headers, accessor, out versioningConfiguration) == false) 
					return;

				var revision = GetNextRevisionNumber(name, accessor);

				using (FileSystem.DisableAllTriggersForCurrentThread())
				{
					RemoveOldRevisions(name, revision, versioningConfiguration);
				}

				headers.__ExternalState["Next-Revision"] = revision;
				headers.__ExternalState["Parent-Revision"] = headers.Value<string>(VersioningUtil.RavenFileRevision);

				headers[VersioningUtil.RavenFileRevisionStatus] = RavenJToken.FromObject("Current");
				headers[VersioningUtil.RavenFileRevision] = RavenJToken.FromObject(revision);
			});
		}
Beispiel #39
0
        protected override void PutDocument(RavenJObject document, SmugglerOptions options, int size)
        {
            if (document != null)
            {
                var metadata = document.Value <RavenJObject>("@metadata");
                var key      = metadata.Value <string>("@id");
                document.Remove("@metadata");

                bulkInsertBatch.Add(new JsonDocument
                {
                    Key        = key,
                    Metadata   = metadata,
                    DataAsJson = document,
                });
                return;
            }

            var batchToSave = new List <IEnumerable <JsonDocument> > {
                bulkInsertBatch
            };

            bulkInsertBatch = new List <JsonDocument>();
            database.Documents.BulkInsert(new BulkInsertOptions {
                BatchSize = options.BatchSize, OverwriteExisting = true
            }, batchToSave, Guid.NewGuid(), CancellationToken.None);
        }
		public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
		{
			VersioningConfiguration versioningConfiguration;

			if (metadata.ContainsKey(Constants.RavenCreateVersion))
			{
				metadata.__ExternalState[Constants.RavenCreateVersion] = metadata[Constants.RavenCreateVersion];
				metadata.Remove(Constants.RavenCreateVersion);
			}

			if (TryGetVersioningConfiguration(key, metadata, out versioningConfiguration) == false)
				return;

			var revision = GetNextRevisionNumber(key);

			using (Database.DisableAllTriggersForCurrentThread())
			{
				RemoveOldRevisions(key, revision, versioningConfiguration, transactionInformation);
			}
			metadata.__ExternalState["Next-Revision"] = revision;

			metadata.__ExternalState["Parent-Revision"] = metadata.Value<string>(VersioningUtil.RavenDocumentRevision);

			metadata[VersioningUtil.RavenDocumentRevisionStatus] = RavenJToken.FromObject("Current");
			metadata[VersioningUtil.RavenDocumentRevision] = RavenJToken.FromObject(revision);
		}
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (!metadata.ContainsKey("IsRoutable") || !metadata.Value <bool>("IsRoutable"))
            {
                return;
            }

            RavenJToken parentIdToken;
            RavenJToken slugToken;

            if (document.TryGetValue("ParentId", out parentIdToken) && document.TryGetValue("Slug", out slugToken))
            {
                var parentId = parentIdToken.Value <string>();
                var slug     = slugToken.Value <string>();

                string parentPath = null;
                if (!String.IsNullOrEmpty(parentId))
                {
                    var parent = Database.Get(parentId, transactionInformation);
                    parentPath = parent.DataAsJson["Path"].Value <string>();
                }

                if (String.IsNullOrEmpty(parentPath))
                {
                    document["Path"] = slug;
                }
                else
                {
                    document["Path"] = parentPath + "/" + slug;
                }
            }


            base.OnPut(key, document, metadata, transactionInformation);
        }
        /// <summary>
        /// Tracks the entity.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">The key.</param>
        /// <param name="document">The document.</param>
        /// <param name="metadata">The metadata.</param>
        /// <returns></returns>
        public T TrackEntity <T>(string key, RavenJObject document, RavenJObject metadata)
        {
            object entity;

            if (entitiesByKey.TryGetValue(key, out entity) == false)
            {
                entity = ConvertToEntity <T>(key, document, metadata);
            }
            else
            {
                // the local instnace may have been changed, we adhere to the current Unit of Work
                // instance, and return that, ignoring anything new.
                return((T)entity);
            }
            var etag = metadata.Value <string>("@etag");

            document.Remove("@metadata");
            entitiesAndMetadata[entity] = new DocumentMetadata
            {
                OriginalValue    = document,
                Metadata         = metadata,
                OriginalMetadata = (RavenJObject)metadata.CloneToken(),
                ETag             = new Guid(etag),
                Key = key
            };
            entitiesByKey[key] = entity;
            return((T)entity);
        }
Beispiel #43
0
        public void ResolveConflict(string id, RavenJObject metadata, TExternal incoming, TInternal existingItem)
        {
            RavenJObject resolvedMetadataToSave;
            TExternal    resolvedItemToSave;

            if (TryResolveConflict(id, metadata, incoming, existingItem, out resolvedMetadataToSave, out resolvedItemToSave))
            {
                if (metadata.ContainsKey("Raven-Remove-Document-Marker") &&
                    metadata.Value <bool>("Raven-Remove-Document-Marker"))
                {
                    if (resolvedMetadataToSave.ContainsKey(Constants.RavenEntityName))
                    {
                        metadata[Constants.RavenEntityName] = resolvedMetadataToSave[Constants.RavenEntityName];
                    }
                    DeleteItem(id, null);
                    MarkAsDeleted(id, metadata);
                }
                else
                {
                    var resolvedItemJObject = resolvedItemToSave as RavenJObject;
                    if (resolvedItemJObject != null)
                    {
                        ExecuteRemoveConflictOnPutTrigger(id, metadata, resolvedItemJObject);
                    }
                    resolvedMetadataToSave.Remove(Constants.RavenReplicationConflict);
                    resolvedMetadataToSave.Remove(Constants.RavenReplicationConflictDocument);
                    AddWithoutConflict(id, null, resolvedMetadataToSave, resolvedItemToSave);
                }
            }
        }
Beispiel #44
0
        public override void AfterPut(string key, RavenJObject document, RavenJObject metadata, Etag etag, TransactionInformation transactionInformation)
        {
            // leave raven system docs alone
            if (key.StartsWith("Raven/"))
            {
                return;
            }

            // when there's already a Created date written, this is not the original insert
            if (metadata.ContainsKey("Created"))
            {
                return;
            }

            // get the timestamp set for the last-modified date
            var timestamp = metadata.Value <DateTime>(Constants.LastModified);

            // copy the metadata and add the timestamp
            var newMetadata = (RavenJObject)metadata.CreateSnapshot();

            newMetadata.Add("Created", timestamp);

            // update the metadata in the document
            using (Database.DisableAllTriggersForCurrentThread())
                Database.PutDocumentMetadata(key, newMetadata);
        }
Beispiel #45
0
        public override void AfterPut(string key, RavenJObject document, RavenJObject metadata, System.Guid etag, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/"))
            {
                return;
            }

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

            var properties = metadata.Value <RavenJArray>(Constants.EnsureUniqueConstraints);

            if (properties == null || properties.Count() <= 0)
            {
                return;
            }

            var constraintMetaObject = new RavenJObject {
                { Constants.IsConstraintDocument, true }
            };

            foreach (var property in properties)
            {
                var propName = ((RavenJValue)property).Value.ToString();
                Database.Put(
                    "UniqueConstraints/" + entityName + propName + "/" + document.Value <string>(propName),
                    null,
                    RavenJObject.FromObject(new { RelatedId = key }),
                    constraintMetaObject,
                    transactionInformation);
            }
        }
        public void UpdateFileMetadata(string filename, RavenJObject metadata)
        {
            Api.JetSetCurrentIndex(session, Files, "by_name");
            Api.MakeKey(session, Files, filename, Encoding.Unicode, MakeKeyGrbit.NewKey);
            if (Api.TrySeek(session, Files, SeekGrbit.SeekEQ) == false)
            {
                throw new FileNotFoundException(filename);
            }

            using (var update = new Update(session, Files, JET_prep.Replace))
            {
                if (!metadata.ContainsKey("ETag"))
                {
                    throw new InvalidOperationException("Metadata of file {0} does not contain 'ETag' key " + filename);
                }

                var innerEsentMetadata = new RavenJObject(metadata);
                var etag = innerEsentMetadata.Value <Guid>("ETag");
                innerEsentMetadata.Remove("ETag");

                var existingMetadata = RetrieveMetadata();

                if (existingMetadata.ContainsKey("Content-MD5"))
                {
                    innerEsentMetadata["Content-MD5"] = existingMetadata["Content-MD5"];
                }

                Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["etag"], etag.TransformToValueForEsentSorting());
                Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["metadata"], ToQueryString(innerEsentMetadata), Encoding.Unicode);

                update.Save();
            }
        }
		public override void OnPut(string key, Stream data, RavenJObject metadata)
		{
			if (key.StartsWith("Raven/")) // we don't deal with system attachment
				return;
			using (Database.DisableAllTriggersForCurrentThread())
			{
				var attachmentMetadata = GetAttachmentMetadata(key);
				if (attachmentMetadata != null)
				{
					RavenJArray history = new RavenJArray(metadata.Value<RavenJArray>(Constants.RavenReplicationHistory));
					metadata[Constants.RavenReplicationHistory] = history;

					if (attachmentMetadata.ContainsKey(Constants.RavenReplicationVersion) &&
						attachmentMetadata.ContainsKey(Constants.RavenReplicationSource))
					{
						history.Add(new RavenJObject
						{
							{Constants.RavenReplicationVersion, attachmentMetadata[Constants.RavenReplicationVersion]},
							{Constants.RavenReplicationSource, attachmentMetadata[Constants.RavenReplicationSource]}
						});
					}

					if (history.Length > Constants.ChangeHistoryLength)
					{
						history.RemoveAt(0);
					}
				}

				metadata[Constants.RavenReplicationVersion] = RavenJToken.FromObject(HiLo.NextId());
				metadata[Constants.RavenReplicationSource] = RavenJToken.FromObject(Database.TransactionalStorage.Id);
			}
		}
Beispiel #48
0
        protected override async Task PutDocument(RavenJObject document)
        {
            if (document == null)
            {
                return;
            }

            var metadata = document.Value <RavenJObject>("@metadata");
            var id       = metadata.Value <string>("@id");

            document.Remove("@metadata");

            operation.Store(document, metadata, id);
            storedDocumentCountInBatch++;
            if (storedDocumentCountInBatch >= currentBatchSize && currentBatchSize > 0)
            {
                storedDocumentCountInBatch = 0;
                await operation.DisposeAsync();

                operation = store.BulkInsert(options: new BulkInsertOptions
                {
                    BatchSize       = currentBatchSize,
                    CheckForUpdates = true
                });

                operation.Report += text => ShowProgress(text);
            }
        }
Beispiel #49
0
        public override void AfterCommit(string key, Stream data, RavenJObject metadata, Guid etag)
        {
            // Make sure we have a filename
            var filename = metadata.Value<string>("Raven-Attachment-Filename");
            if (string.IsNullOrEmpty(filename))
                return;

            RavenJObject doc;
            try
            {
                // Extract the text in the attachment as a json document
                var extension = Path.GetExtension(filename);
                doc = Extractor.GetJson(data, extension);
            }
            catch (InvalidOperationException ex)
            {
                // If there is no ifilter installed, don't do the extraction, but still do everything else.
                if (!ex.Message.Contains("HRESULT: 0x80004005")) throw;

                // Still write a dummmy doc so we get metadata in the index.
                doc = new RavenJObject();
            }

            // Write the results to a document.  Include all of the attachment's metadata, and a reference back to the attachment key.
            var md = new RavenJObject(metadata) { { "Raven-Attachment-Key", key } };
            Database.Put(key + "/text", null, doc, md, null);
        }
Beispiel #50
0
		public override void AfterPut(string key, RavenJObject document, RavenJObject metadata, System.Guid etag, TransactionInformation transactionInformation)
		{
			if (key.StartsWith("Raven/"))
			{
				return;
			}

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

			var properties = metadata.Value<RavenJArray>(Constants.EnsureUniqueConstraints);

			if (properties == null || properties.Length <= 0) 
				return;

			var constraintMetaObject = new RavenJObject { { Constants.IsConstraintDocument, true } };
			constraintMetaObject.EnsureSnapshot();
			foreach (var property in properties)
			{
				var propName = ((RavenJValue)property).Value.ToString();
				var uniqueValue = document.Value<string>(propName);
				if(uniqueValue == null)
					continue;
				string documentName = "UniqueConstraints/" + entityName + propName + "/" +Util.EscapeUniqueValue(uniqueValue);
				Database.Put(
					documentName,
					null,
					RavenJObject.FromObject(new { RelatedId = key }),
					(RavenJObject)constraintMetaObject.CreateSnapshot(),
					transactionInformation);
			}
		}
        private void RecordDelete(string id, RavenJObject metadata)
        {
            Database.TransactionalStorage.Batch(accessor =>
            {
                bool hasChanges = false;
                foreach (var config in replicationConfigs)
                {
                    if (string.Equals(config.RavenEntityName, metadata.Value <string>(Constants.RavenEntityName), StringComparison.InvariantCultureIgnoreCase) == false)
                    {
                        continue;
                    }

                    hasChanges = true;
                    accessor.Lists.Set(GetSqlReplicationDeletionName(config), id, metadata, UuidType.Documents);
                }
                if (hasChanges)
                {
                    Database.WorkContext.NotifyAboutWork();
                }
            });
            if (log.IsDebugEnabled)
            {
                log.Debug(() => "recorded a deleted document " + id);
            }
        }
        public SynchronizationWorkItem DetermineWork(string file, RavenJObject localMetadata, RavenJObject destinationMetadata, string localServerUrl, out NoSyncReason reason)
        {
            reason = NoSyncReason.Unknown;

            if (localMetadata == null)
            {
                reason = NoSyncReason.SourceFileNotExist;
                return null;
            }

            if (destinationMetadata != null && destinationMetadata[SynchronizationConstants.RavenSynchronizationConflict] != null && destinationMetadata[SynchronizationConstants.RavenSynchronizationConflictResolution] == null)
            {
                reason = NoSyncReason.DestinationFileConflicted;
                return null;
            }

            if (localMetadata[SynchronizationConstants.RavenSynchronizationConflict] != null)
            {
                reason = NoSyncReason.SourceFileConflicted;
                return null;
            }

            if (localMetadata[SynchronizationConstants.RavenDeleteMarker] != null)
            {
                if (localMetadata.ContainsKey(SynchronizationConstants.RavenRenameFile))
                {
                    var rename = localMetadata.Value<string>(SynchronizationConstants.RavenRenameFile);

                    if (destinationMetadata != null)
                        return new RenameWorkItem(file, rename, localServerUrl, storage);

                    return new ContentUpdateWorkItem(rename, localServerUrl, storage, sigGenerator);
                    // we have a rename tombstone but file does not exists on destination
                }
                return new DeleteWorkItem(file, localServerUrl, storage);
            }

            if (destinationMetadata != null && Historian.IsDirectChildOfCurrent(localMetadata, destinationMetadata))
            {
                reason = NoSyncReason.ContainedInDestinationHistory;
                return null;
            }

            // file exists on dest and has the same content
            if (destinationMetadata != null && localMetadata.Value<string>("Content-MD5") == destinationMetadata.Value<string>("Content-MD5"))
            {
                // check metadata to detect if any synchronization is needed
                if (localMetadata.Keys.Except(new[] { Constants.MetadataEtagField, Constants.RavenLastModified, Constants.LastModified })
                                 .Any(key => !destinationMetadata.ContainsKey(key) || localMetadata[key] != destinationMetadata[key]))
                {
                    return new MetadataUpdateWorkItem(file, localServerUrl, destinationMetadata, storage);
                }

                reason = NoSyncReason.SameContentAndMetadata;

                return null; // the same content and metadata - no need to synchronize
            }

            return new ContentUpdateWorkItem(file, localServerUrl, storage, sigGenerator);
        }
Beispiel #53
0
        private Etag EnsureDocumentEtagMatch(string key, Etag etag, RavenJObject file)
        {
            var existingEtag = Etag.Parse(file.Value <byte[]>("etag"));

            if (etag != null)
            {
                if (existingEtag != etag)
                {
                    if (etag == Etag.Empty)
                    {
                        var metadata = (RavenJObject)file["metadata"];

                        if (metadata.ContainsKey(RavenConstants.RavenDeleteMarker) &&
                            metadata.Value <bool>(RavenConstants.RavenDeleteMarker))
                        {
                            return(existingEtag);
                        }
                    }

                    throw new ConcurrencyException("Operation attempted on file '" + key +
                                                   "' using a non current etag")
                          {
                              ActualETag   = existingEtag,
                              ExpectedETag = etag
                          };
                }
            }

            return(existingEtag);
        }
Beispiel #54
0
        public bool FilterDocuments(string destinationId, string key, RavenJObject metadata, out string reason)
        {
            if (IsSystemDocumentId(key))
            {
                reason = string.Format("Will not replicate document '{0}' to '{1}' because it is a system document", key, destinationId);
                Log.Debug(reason);
                return(false);
            }
            if (metadata.ContainsKey(Constants.NotForReplication) && metadata.Value <bool>(Constants.NotForReplication))
            // not explicitly marked to skip
            {
                reason = string.Format("Will not replicate document '{0}' to '{1}' because it was marked as not for replication", key, destinationId);
                Log.Debug(reason);
                return(false);
            }

            if (metadata[Constants.RavenReplicationConflict] != null)
            // don't replicate conflicted documents, that just propagate the conflict
            {
                reason = string.Format("Will not replicate document '{0}' to '{1}' because it a conflict document", key, destinationId);
                Log.Debug(reason);
                return(false);
            }

            if (OriginsFromDestination(destinationId, metadata))             // prevent replicating back to source
            {
                reason = string.Format("Will not replicate document '{0}' to '{1}' because the destination server is the same server it originated from", key, destinationId);
                Log.Debug(reason);
                return(false);
            }

            switch (ReplicationOptionsBehavior)
            {
            case TransitiveReplicationOptions.None:
                var value = metadata.Value <string>(Constants.RavenReplicationSource);
                if (value != null && (value != CurrentDatabaseId))
                {
                    reason = string.Format("Will not replicate document '{0}' to '{1}' because it was not created on the current server, and TransitiveReplicationOptions = none", key, destinationId);
                    Log.Debug(reason);
                    return(false);
                }
                break;
            }
            reason = string.Format("Will replicate '{0}' to '{1}'", key, destinationId);
            Log.Debug(reason);
            return(true);
        }
Beispiel #55
0
        private void SendRequestToServer(Action <WebResponse> action)
        {
            int retries = 0;

            while (true)
            {
                try
                {
                    using (var res = WebRequest.GetResponse())
                    {
                        action(res);
                    }
                    return;
                }
                catch (WebException e)
                {
                    if (++retries >= 3)
                    {
                        throw;
                    }

                    var response = e.Response as HttpWebResponse;
                    if (response == null)
                    {
                        throw;
                    }

                    if (response.StatusCode != HttpStatusCode.Unauthorized &&
                        response.StatusCode != HttpStatusCode.PreconditionFailed)
                    {
                        using (var streamReader = new StreamReader(response.GetResponseStreamWithHttpDecompression()))
                        {
                            var          error        = streamReader.ReadToEnd();
                            RavenJObject ravenJObject = null;
                            try
                            {
                                ravenJObject = RavenJObject.Parse(error);
                            }
                            catch { }

                            if (ravenJObject == null)
                            {
                                throw;
                            }
                            throw new WebException("Error: " + ravenJObject.Value <string>("Error"), e);
                        }
                    }

                    if (handleUnauthorizedResponse != null && handleUnauthorizedResponse(connectionStringOptions, e.Response))
                    {
                        RecreateWebRequest();
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
 public void AfterStore(string key, object entityInstance, RavenJObject metadata)
 {
     var schemaEntityWithETag = entityInstance as IHasETag;
     if (schemaEntityWithETag != null)
     {
         schemaEntityWithETag.Etag = metadata.Value<Guid>("@etag");
     }
 }
        public override void AfterCommit(string key, RavenJObject document, RavenJObject metadata, Etag etag)
        {
            var entityName = metadata.Value<string>(Constants.RavenEntityName);

            if (entityName == null)
                return;

            Database.LastCollectionEtags.Update(entityName, etag);
        }
Beispiel #58
0
		/// <summary>
		/// Create an instance from a json object
		/// </summary>
		/// <param name="patchRequestJson">The patch request json.</param>
		public static PatchRequest FromJson(RavenJObject patchRequestJson)
		{
			PatchRequest[] nested = null;
			var nestedJson = patchRequestJson.Value<RavenJToken>("Nested");
            if (nestedJson != null && nestedJson.Type != JTokenType.Null)
                nested = patchRequestJson.Value<RavenJArray>("Nested").Cast<RavenJObject>().Select(FromJson).ToArray();

			return new PatchRequest
			{
				Type = (PatchCommandType)Enum.Parse(typeof(PatchCommandType), patchRequestJson.Value<string>("Type"), true),
				Name = patchRequestJson.Value<string>("Name"),
				Nested = nested,
				Position = patchRequestJson.Value<int?>("Position"),
                AllPositions = patchRequestJson.Value<bool?>("AllPositions"),
				PrevVal = patchRequestJson["PrevVal"],
				Value = patchRequestJson["Value"],
			};
		}
        public override void AfterCommit(string key, RavenJObject document, RavenJObject metadata, Guid etag)
        {
            base.AfterCommit(key, document, metadata, etag);
            var entityName = metadata.Value<string>(Constants.RavenEntityName);
            if (entityName != UpdateCascadeOperation.EntityName) return;
            if (document.Value<string>("Status") != "Pending") return;
            log.Trace("A new operation with id {0} has been put", key);
            var operation = document.JsonDeserialization<UpdateCascadeOperation>();
            var referencedDocId = document.Value<string>("ReferencedDocId");
            var referencedDoc = this.Database.Get(referencedDocId, null);

            if (referencedDoc == null) return;

            if (services.RunningOperationsCoordinator != null)
            {
                services.RunningOperationsCoordinator.TryStartOperation(operation, referencedDoc);
            }
        }