예제 #1
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);
        }
예제 #2
0
		public override void AfterPut(string name, long? size, RavenJObject headers)
		{
			FileSystem.Storage.Batch(accessor =>
			{
				VersioningConfiguration versioningConfiguration;
				if (TryGetVersioningConfiguration(name, headers, accessor, out versioningConfiguration) == false) return;

				using (FileSystem.DisableAllTriggersForCurrentThread())
				{
					var copyHeaders = new RavenJObject(headers);
					copyHeaders[VersioningUtil.RavenFileRevisionStatus] = RavenJToken.FromObject("Historical");
					copyHeaders[Constants.RavenReadOnly] = true;
					copyHeaders.Remove(VersioningUtil.RavenFileRevision);
					object parentRevision;
					headers.__ExternalState.TryGetValue("Parent-Revision", out parentRevision);
					if (parentRevision != null)
					{
						copyHeaders[VersioningUtil.RavenFileParentRevision] = name + "/revisions/" + parentRevision;
					}

					object value;
					headers.__ExternalState.TryGetValue("Next-Revision", out value);

					accessor.PutFile(name + "/revisions/" + value, size, copyHeaders);
				}
			});
		}
예제 #3
0
			public void AfterConversionToDocument(string key, object entity, RavenJObject document, RavenJObject metadata)
			{
				if (entity is Item == false)
					return;

				document.Remove("Revision");
			}
예제 #4
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);
			}
		}
        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));
              }
        }
예제 #6
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);
			});
		}
        public override void AfterPut(string key, RavenJObject document, RavenJObject metadata, Etag etag, TransactionInformation transactionInformation)
        {
            if (!metadata.__ExternalState.ContainsKey(RavenDocumentRevision))
                return;

            using (Database.DisableAllTriggersForCurrentThread())
            {
                var revisionMetadata = new RavenJObject(metadata);
                revisionMetadata[RavenDocumentRevisionStatus] = RavenJToken.FromObject("Historical");
                revisionMetadata.Remove(RavenDocumentRevision);

                if (revisionMetadata.ContainsKey(RavenDocumentEnsureUniqueConstraints))
                    revisionMetadata.Remove(RavenDocumentEnsureUniqueConstraints);

                var revisionCopy = new RavenJObject(document);
                var revisionkey = key + RevisionSegment + metadata.__ExternalState[RavenDocumentRevision];
                Database.Documents.Put(revisionkey, null, revisionCopy, revisionMetadata, transactionInformation);
            }
        }
		public override VetoResult AllowPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
		{
		    if (Database.Name != null && Database.Name != Constants.SystemDatabase)
		        return VetoResult.Allowed;

		    if (key.StartsWith(RavenDatabasesPrefix, StringComparison.InvariantCultureIgnoreCase) == false)
		        return VetoResult.Allowed;

		    var tempPermission = metadata[Constants.AllowBundlesChange];

		    if (tempPermission != null)
		        metadata.Remove(Constants.AllowBundlesChange); // this is a temp marker so do not persist this medatada

		    var bundlesChangesAllowed = tempPermission != null &&
		                                tempPermission.Value<string>()
		                                              .Equals("true", StringComparison.InvariantCultureIgnoreCase);

		    if (bundlesChangesAllowed)
		        return VetoResult.Allowed;

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

		    if (existingDbDoc == null)
		        return VetoResult.Allowed;

		    var currentDbDocument = existingDbDoc.DataAsJson.JsonDeserialization<DatabaseDocument>();

		    var currentBundles = new List<string>();
		    string value;
		    if (currentDbDocument.Settings.TryGetValue(Constants.ActiveBundles, out value))
		        currentBundles = value.GetSemicolonSeparatedValues();

		    var newDbDocument = document.JsonDeserialization<DatabaseDocument>();
		    var newBundles = new List<string>();
		    if (newDbDocument.Settings.TryGetValue(Constants.ActiveBundles, out value))
		        newBundles = value.GetSemicolonSeparatedValues();


		    if (currentBundles.Count == newBundles.Count)
		        return VetoResult.Allowed;

		    if (currentBundles.Count == 0)
		        return VetoResult.Allowed;

		    if (currentBundles.TrueForAll(x => newBundles.Contains(x)))
                return VetoResult.Allowed;

		    return VetoResult.Deny(
		        "You should not change 'Raven/ActiveBundles' setting for a database. This setting should be set only once when a database is created. " +
				"If you really need to override it you have to specify {\"" + Constants.AllowBundlesChange +
		        "\": true} in metadata of a database document every time when you send it." + Environment.NewLine +
		        "Current: " + string.Join("; ", currentBundles) + Environment.NewLine +
		        "New: " + string.Join("; '", newBundles));
		}
		public HttpJsonRequest CreateDatabase(DatabaseDocument databaseDocument, out RavenJObject doc)
		{
			if (databaseDocument.Settings.ContainsKey("Raven/DataDir") == false)
				throw new InvalidOperationException("The Raven/DataDir setting is mandatory");
			var dbname = databaseDocument.Id.Replace("Raven/Databases/", "");
            MultiDatabase.AssertValidName(dbname);
			doc = RavenJObject.FromObject(databaseDocument);
			doc.Remove("Id");

			return createRequestForSystemDatabase("/admin/databases/" + Uri.EscapeDataString(dbname), "PUT");
		}
예제 #10
0
        public HttpJsonRequest CreateDatabase(DatabaseDocument databaseDocument, out RavenJObject doc)
        {
            if (databaseDocument.Settings.ContainsKey("Raven/DataDir") == false)
            {
                throw new InvalidOperationException("The Raven/DataDir setting is mandatory");
            }
            var dbname = databaseDocument.Id.Replace("Raven/Databases/", "");

            MultiDatabase.AssertValidName(dbname);
            doc = RavenJObject.FromObject(databaseDocument);
            doc.Remove("Id");

            return(createRequestForSystemDatabase("/admin/databases/" + Uri.EscapeDataString(dbname), "PUT"));
        }
예제 #11
0
        public void InitializeMetadata(string name, RavenJObject metadata)
        {
            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;
            }

            fileSystem.Storage.Batch(accessor =>
            {
                FileVersioningConfiguration versioningConfiguration;
                if (TryGetVersioningConfiguration(name, metadata, accessor, out versioningConfiguration) == false)
                    return;

                long revision;

                if (metadata.__ExternalState.ContainsKey("Synchronization-Next-Revision"))
                    revision = (long) metadata.__ExternalState["Synchronization-Next-Revision"];
                else
                    revision = GetNextRevisionNumber(name, accessor);

                RemoveOldRevisions(name, revision, versioningConfiguration);

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

                metadata[VersioningUtil.RavenFileRevisionStatus] = RavenJToken.FromObject("Current");
                metadata[VersioningUtil.RavenFileRevision] = RavenJToken.FromObject(revision);
            });
        }
예제 #12
0
        public override void OnPut(string key, RavenJObject jsonReplicationDocument, 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(new HashSet <Type> {
                typeof(VirtualDeleteAndRemoveConflictsTrigger)
            }))
            {
                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, Stream data, RavenJObject metadata)
		{
			using (Database.DisableAllTriggersForCurrentThread())
			{
				metadata.Remove(Constants.RavenReplicationConflict);// you can't put conflicts

				var oldVersion = Database.GetStatic(key);
				if (oldVersion == null)
					return;
				if (oldVersion.Metadata[Constants.RavenReplicationConflict] == null)
					return;

				RavenJArray history = metadata.Value<RavenJArray>(Constants.RavenReplicationHistory) ?? new RavenJArray();
				metadata[Constants.RavenReplicationHistory] = history;

				var ravenJTokenEqualityComparer = new RavenJTokenEqualityComparer();
				// this is a conflict document, holding document keys in the 
				// values of the properties
				var conflictData = oldVersion.Data().ToJObject();
				var conflicts = conflictData.Value<RavenJArray>("Conflicts");
				if (conflicts == null)
					return;
				foreach (var prop in conflicts)
				{
					var id = prop.Value<string>();
					Attachment attachment = Database.GetStatic(id);
					if(attachment == null)
						continue;
					Database.DeleteStatic(id, null);

					// add the conflict history to the mix, so we make sure that we mark that we resolved the conflict
					var conflictHistory = attachment.Metadata.Value<RavenJArray>(Constants.RavenReplicationHistory) ?? new RavenJArray();
					conflictHistory.Add(new RavenJObject
					{
						{Constants.RavenReplicationVersion, attachment.Metadata[Constants.RavenReplicationVersion]},
						{Constants.RavenReplicationSource, attachment.Metadata[Constants.RavenReplicationSource]}
					});

					foreach (var item in conflictHistory)
					{
						if (history.Any(x => ravenJTokenEqualityComparer.Equals(x, item)))
							continue;
						history.Add(item);
					}
				}
			}
		}
예제 #14
0
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            using (Database.DisableAllTriggersForCurrentThread())
            {
                metadata.Remove(ReplicationConstants.RavenReplicationConflict);                // you can't put conflicts

                var oldVersion = Database.Get(key, transactionInformation);
                if (oldVersion == null)
                {
                    return;
                }
                if (oldVersion.Metadata[ReplicationConstants.RavenReplicationConflict] == null)
                {
                    return;
                }

                RavenJArray history = metadata.Value <RavenJArray>(ReplicationConstants.RavenReplicationHistory) ?? new RavenJArray();
                metadata[ReplicationConstants.RavenReplicationHistory] = history;

                var ravenJTokenEqualityComparer = new RavenJTokenEqualityComparer();
                // this is a conflict document, holding document keys in the
                // values of the properties
                foreach (var prop in oldVersion.DataAsJson.Value <RavenJArray>("Conflicts"))
                {
                    RavenJObject deletedMetadata;
                    Database.Delete(prop.Value <string>(), null, transactionInformation, out deletedMetadata);

                    // add the conflict history to the mix, so we make sure that we mark that we resolved the conflict
                    var conflictHistory = deletedMetadata.Value <RavenJArray>(ReplicationConstants.RavenReplicationHistory) ?? new RavenJArray();
                    conflictHistory.Add(new RavenJObject
                    {
                        { ReplicationConstants.RavenReplicationVersion, deletedMetadata[ReplicationConstants.RavenReplicationVersion] },
                        { ReplicationConstants.RavenReplicationSource, deletedMetadata[ReplicationConstants.RavenReplicationSource] }
                    });

                    foreach (var item in conflictHistory)
                    {
                        if (history.Any(x => ravenJTokenEqualityComparer.Equals(x, item)))
                        {
                            continue;
                        }
                        history.Add(item);
                    }
                }
            }
        }
예제 #15
0
        public override void AfterRename(string name, string renamed, RavenJObject metadata)
        {
            using (FileSystem.DisableAllTriggersForCurrentThread())
            {
                FileSystem.Storage.Batch(accessor =>
                {
                    FileVersioningConfiguration versioningConfiguration;
                    if (actions.TryGetVersioningConfiguration(name, new RavenJObject(), accessor, out versioningConfiguration) == false)
                    {
                        return;
                    }

                    var revisions = accessor.GetFilesStartingWith(name + "/revisions/", 0, int.MaxValue).Where(file => file != null).ToArray();

                    for (int i = 0; i < revisions.Length; i++)
                    {
                        var file = revisions[i];

                        if (versioningConfiguration.ResetOnRename)
                        {
                            if (i == (revisions.Length - 1))
                            {
                                // reset file revision
                                metadata[VersioningUtil.RavenFileRevision] = RavenJToken.FromObject(1);
                                metadata.Remove(VersioningUtil.RavenFileParentRevision);
                                accessor.UpdateFileMetadata(renamed, metadata, null);

                                // rename last existing revision to [renamed]/revisions/1
                                var revision = file.Name;
                                accessor.RenameFile(string.Format("{0}/revisions/{1}", name, revision), string.Format("{0}/revisions/{1}", renamed, 1), true);
                            }
                            else
                            {
                                FileSystem.Files.IndicateFileToDelete(file.FullPath, null);
                            }
                        }
                        else
                        {
                            var revision = file.Name;

                            accessor.RenameFile(string.Format("{0}/revisions/{1}", name, revision), string.Format("{0}/revisions/{1}", renamed, revision), true);
                        }
                    }
                });
            }
        }
예제 #16
0
        public FileUpdateResult UpdateFileMetadata(string filename, RavenJObject metadata, Etag etag)
        {
            var key      = CreateKey(filename);
            var keySlice = (Slice)key;

            ushort version;
            var    file = LoadJson(storage.Files, keySlice, writeBatch.Value, out version);

            if (file == null)
            {
                throw new FileNotFoundException(filename);
            }

            var existingEtag = EnsureDocumentEtagMatch(filename, etag, file);

            var newEtag = uuidGenerator.CreateSequentialUuid();

            metadata.Remove(RavenConstants.MetadataEtagField);

            var existingMetadata = (RavenJObject)file["metadata"];

            if (!metadata.ContainsKey("Content-MD5") && existingMetadata.ContainsKey("Content-MD5"))
            {
                metadata["Content-MD5"] = existingMetadata["Content-MD5"];
            }
            if (!metadata.ContainsKey(RavenConstants.FileSystem.RavenFsSize) && existingMetadata.ContainsKey(RavenConstants.FileSystem.RavenFsSize))
            {
                metadata[RavenConstants.FileSystem.RavenFsSize] = existingMetadata[RavenConstants.FileSystem.RavenFsSize];
            }

            file["etag"]     = newEtag.ToByteArray();
            file["metadata"] = metadata;

            storage.Files.Add(writeBatch.Value, keySlice, file, version);

            var filesByEtag = storage.Files.GetIndex(Tables.Files.Indices.ByEtag);

            filesByEtag.Delete(writeBatch.Value, CreateKey(existingEtag));
            filesByEtag.Add(writeBatch.Value, (Slice)CreateKey(newEtag), key);

            return(new FileUpdateResult()
            {
                PrevEtag = existingEtag,
                Etag = newEtag
            });
        }
예제 #17
0
        public void PutFile(string filename, long?totalSize, RavenJObject metadata, bool tombstone = false)
        {
            var filesByEtag = storage.Files.GetIndex(Tables.Files.Indices.ByEtag);

            var key = CreateKey(filename);

            if (!metadata.ContainsKey(RavenConstants.MetadataEtagField))
            {
                throw new InvalidOperationException(string.Format("Metadata of file {0} does not contain 'ETag' key", filename));
            }

            ushort version;
            var    existingFile = LoadJson(storage.Files, key, writeBatch.Value, out version);

            var innerMetadata = new RavenJObject(metadata);
            var etag          = innerMetadata.Value <Guid>(RavenConstants.MetadataEtagField);

            innerMetadata.Remove(RavenConstants.MetadataEtagField);

            var file = new RavenJObject
            {
                { "name", filename },
                { "total_size", totalSize },
                { "uploaded_size", 0 },
                { "etag", new RavenJValue(etag) },
                { "metadata", innerMetadata }
            };

            storage.Files.Add(writeBatch.Value, key, file, version);

            if (existingFile != null)
            {
                filesByEtag.Delete(writeBatch.Value, CreateKey(existingFile.Value <Guid>("etag")));
            }

            filesByEtag.Add(writeBatch.Value, CreateKey(etag), key);

            if (tombstone)
            {
                return;
            }

            var fileCount = storage.Files.GetIndex(Tables.Files.Indices.Count);

            fileCount.Add(writeBatch.Value, key, key);
        }
예제 #18
0
        public void UpdateFileMetadata(string filename, RavenJObject metadata)
        {
            var key = CreateKey(filename);

            ushort version;
            var    file = LoadJson(storage.Files, key, writeBatch.Value, out version);

            if (file == null)
            {
                throw new FileNotFoundException(filename);
            }

            if (!metadata.ContainsKey(RavenConstants.MetadataEtagField))
            {
                throw new InvalidOperationException(string.Format("Metadata of file {0} does not contain 'ETag' key", filename));
            }

            var innerMetadata = new RavenJObject(metadata);
            var etag          = innerMetadata.Value <Guid>(RavenConstants.MetadataEtagField);

            innerMetadata.Remove(RavenConstants.MetadataEtagField);

            var existingMetadata = (RavenJObject)file["metadata"];

            if (!innerMetadata.ContainsKey("Content-MD5") && existingMetadata.ContainsKey("Content-MD5"))
            {
                innerMetadata["Content-MD5"] = existingMetadata["Content-MD5"];
            }
            if (!innerMetadata.ContainsKey("RavenFS-Size") && existingMetadata.ContainsKey("RavenFS-Size"))
            {
                innerMetadata["RavenFS-Size"] = existingMetadata["RavenFS-Size"];
            }

            var oldEtag = file.Value <Guid>("etag");

            file["etag"]     = new RavenJValue(etag);
            file["metadata"] = innerMetadata;

            storage.Files.Add(writeBatch.Value, key, file, version);

            var filesByEtag = storage.Files.GetIndex(Tables.Files.Indices.ByEtag);

            filesByEtag.Delete(writeBatch.Value, CreateKey(oldEtag));
            filesByEtag.Add(writeBatch.Value, CreateKey(etag), key);
        }
예제 #19
0
        public override void AfterRename(string name, string renamed, RavenJObject metadata)
        {
            using (FileSystem.DisableAllTriggersForCurrentThread())
            {
                FileSystem.Storage.Batch(accessor =>
                {
                    FileVersioningConfiguration versioningConfiguration;
                    if (actions.TryGetVersioningConfiguration(name, new RavenJObject(), accessor, out versioningConfiguration) == false)
                        return;

                    var revisions = accessor.GetFilesStartingWith(name + "/revisions/", 0, int.MaxValue).Where(file => file != null).ToArray();

                    for (int i = 0; i < revisions.Length; i++)
                    {
                        var file = revisions[i];

                        if (versioningConfiguration.ResetOnRename)
                        {
                            if (i == (revisions.Length - 1))
                            {
                                // reset file revision
                                metadata[VersioningUtil.RavenFileRevision] = RavenJToken.FromObject(1);
                                metadata.Remove(VersioningUtil.RavenFileParentRevision);
                                accessor.UpdateFileMetadata(renamed, metadata, null);

                                // rename last existing revision to [renamed]/revisions/1
                                var revision = file.Name;
                                accessor.RenameFile(string.Format("{0}/revisions/{1}", name, revision), string.Format("{0}/revisions/{1}", renamed, 1), true);
                            }
                            else
                            {
                                FileSystem.Files.IndicateFileToDelete(file.FullPath, null);
                            }
                        }
                        else
                        {
                            var revision = file.Name;

                            accessor.RenameFile(string.Format("{0}/revisions/{1}", name, revision), string.Format("{0}/revisions/{1}", renamed, revision), true);
                        }
                    }
                });
            }
        }
예제 #20
0
		public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
		{
			using (Database.DisableAllTriggersForCurrentThread())
			{
				metadata.Remove(ReplicationConstants.RavenReplicationConflict);// you can't put conflicts

				var oldVersion = Database.Get(key, transactionInformation);
				if (oldVersion == null)
					return;
				if (oldVersion.Metadata[ReplicationConstants.RavenReplicationConflict] == null)
					return;

				RavenJArray history = metadata.Value<RavenJArray>(ReplicationConstants.RavenReplicationHistory) ?? new RavenJArray();
				metadata[ReplicationConstants.RavenReplicationHistory] = history;

				var ravenJTokenEqualityComparer = new RavenJTokenEqualityComparer();
				// this is a conflict document, holding document keys in the 
				// values of the properties
				var conflicts = oldVersion.DataAsJson.Value<RavenJArray>("Conflicts");
				if (conflicts == null)
					return;
				foreach (var prop in conflicts)
				{
					RavenJObject deletedMetadata;
					Database.Delete(prop.Value<string>(), null, transactionInformation, out deletedMetadata);

					// add the conflict history to the mix, so we make sure that we mark that we resolved the conflict
					var conflictHistory = deletedMetadata.Value<RavenJArray>(ReplicationConstants.RavenReplicationHistory) ?? new RavenJArray();
					conflictHistory.Add(new RavenJObject
					{
						{ReplicationConstants.RavenReplicationVersion, deletedMetadata[ReplicationConstants.RavenReplicationVersion]},
						{ReplicationConstants.RavenReplicationSource, deletedMetadata[ReplicationConstants.RavenReplicationSource]}
					});

					foreach (var item in conflictHistory)
					{
						if(history.Any(x=>ravenJTokenEqualityComparer.Equals(x, item)))
							continue;
						history.Add(item);
					}
				}
			}
		}
예제 #21
0
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (key.StartsWith("Raven/", StringComparison.InvariantCultureIgnoreCase))
            {
                return;
            }

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

            var versioningConfiguration = GetDocumentVersioningConfiguration(metadata);

            if (versioningConfiguration.Exclude)
            {
                return;
            }


            using (Database.DisableAllTriggersForCurrentThread())
            {
                var copyMetadata = new RavenJObject(metadata);
                copyMetadata[RavenDocumentRevisionStatus] = RavenJToken.FromObject("Historical");
                copyMetadata[Constants.RavenReadOnly]     = true;
                copyMetadata.Remove(RavenDocumentRevision);
                var parentRevision = metadata.Value <string>(RavenDocumentRevision);
                if (parentRevision != null)
                {
                    copyMetadata[RavenDocumentParentRevision] = key + "/revisions/" + parentRevision;
                    metadata[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[RavenDocumentRevisionStatus] = RavenJToken.FromObject("Current");
                metadata[RavenDocumentRevision]       = RavenJToken.FromObject(revision);
            }
        }
		public override void OnPut(string key, Stream data, RavenJObject metadata)
		{
			using (Database.DisableAllTriggersForCurrentThread())
			{
				metadata.Remove(ReplicationConstants.RavenReplicationConflict);// you can't put conflicts

				var oldVersion = Database.GetStatic(key);
				if (oldVersion == null)
					return;
				if (oldVersion.Metadata[ReplicationConstants.RavenReplicationConflict] == null)
					return;
				// this is a conflict document, holding document keys in the 
				// values of the properties
				foreach (var prop in oldVersion.Metadata.Value<RavenJArray>("Conflicts"))
				{
					Database.DeleteStatic(prop.Value<string>(), null);
				}
			}
		}
예제 #23
0
        public FileUpdateResult PutFile(string filename, long?totalSize, RavenJObject metadata, bool tombstone = false)
        {
            var filesByEtag = storage.Files.GetIndex(Tables.Files.Indices.ByEtag);

            var keyString = CreateKey(filename);
            var keySlice  = (Slice)keyString;

            ushort version;
            var    existingFile = LoadJson(storage.Files, keySlice, writeBatch.Value, out version);

            var newEtag = uuidGenerator.CreateSequentialUuid();

            metadata.Remove(RavenConstants.MetadataEtagField);

            var file = new RavenJObject
            {
                { "name", filename },
                { "total_size", totalSize },
                { "uploaded_size", 0 },
                { "etag", newEtag.ToByteArray() },
                { "metadata", metadata }
            };

            storage.Files.Add(writeBatch.Value, keySlice, file, version);

            if (existingFile != null)
            {
                filesByEtag.Delete(writeBatch.Value, CreateKey(Etag.Parse(existingFile.Value <byte[]>("etag"))));
            }

            filesByEtag.Add(writeBatch.Value, (Slice)CreateKey(newEtag), keyString);

            if (tombstone == false)
            {
                var fileCount = storage.Files.GetIndex(Tables.Files.Indices.Count);
                fileCount.Add(writeBatch.Value, keySlice, keyString);
            }

            return(new FileUpdateResult()
            {
                Etag = newEtag
            });
        }
예제 #24
0
        protected override void PutDocument(RavenJObject document, SmugglerOptions options, int size)
        {
            if (document == null)
            {
                return;
            }


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

            if (String.IsNullOrWhiteSpace(id))
            {
                throw new InvalidDataException("Error while importing document from the dump: \n\r Missing id in the document metadata. This shouldn't be happening, most likely the dump you are importing from is corrupt");
            }

            document.Remove("@metadata");

            operation.Store(document, metadata, id, size);
        }
예제 #25
0
        public FileUpdateResult PutFile(string filename, long?totalSize, RavenJObject metadata, bool tombstone = false)
        {
            FileUpdateResult result;

            using (var update = new Update(session, Files, JET_prep.Insert))
            {
                Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["name"], filename, Encoding.Unicode);
                if (totalSize != null)
                {
                    Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["total_size"], BitConverter.GetBytes(totalSize.Value));
                }

                Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["uploaded_size"], BitConverter.GetBytes(0));

                metadata.Remove(Constants.MetadataEtagField);
                var newEtag = uuidGenerator.CreateSequentialUuid();

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

                update.Save();

                result = new FileUpdateResult
                {
                    PrevEtag = null,
                    Etag     = newEtag
                };
            }

            if (!tombstone)
            {
                if (Api.TryMoveFirst(session, Details) == false)
                {
                    throw new InvalidOperationException("Could not find system metadata row");
                }

                Api.EscrowUpdate(session, Details, tableColumnsCache.DetailsColumns["file_count"], 1);
            }

            return(result);
        }
예제 #26
0
        protected void InsertItems(string tableName, string pkName, IEnumerable <ItemToReplicate> dataForTable)
        {
            tableName = tableName.ToLowerInvariant();         // type names have to be lowercase
            foreach (var itemToReplicate in dataForTable)
            {
                var o = new RavenJObject();

                if (database != null)
                {
                    database.WorkContext.CancellationToken.ThrowIfCancellationRequested();
                }

                foreach (var column in itemToReplicate.Columns.Where(column => column.Key != pkName))
                {
                    o[column.Key] = column.Value;
                }

                if ("_id".Equals(pkName))
                {
                    bulkCommands.Add("{\"index\":{\"_index\":\"" + targetIndexName + "\",\"_type\":\"" + tableName + "\",\"_id\":\"" + itemToReplicate.DocumentId + "\"}}");
                }
                else
                {
                    o[pkName] = itemToReplicate.DocumentId;
                    bulkCommands.Add("{\"index\":{\"_index\":\"" + targetIndexName + "\",\"_type\":\"" + tableName + "\"}}");
                }

                // Setup timestamps, converting from a Javascript notation to an ES/Kibana notation
                if (o.ContainsKey("$timestamp"))
                {
                    o["@timestamp"] = o["$timestamp"];
                    o.Remove("$timestamp");
                }
                else
                {
                    o["@timestamp"] = DateTime.UtcNow;
                }

                bulkCommands.Add(o.ToString(Formatting.None));
            }
        }
예제 #27
0
        public FileUpdateResult UpdateFileMetadata(string filename, RavenJObject metadata, Etag etag)
        {
            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))
            {
                var existingEtag = EnsureFileEtagMatch(filename, etag);
                metadata.Remove(Constants.MetadataEtagField);

                var existingMetadata = RetrieveMetadata();

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

                if (!metadata.ContainsKey(Constants.FileSystem.RavenFsSize) && existingMetadata.ContainsKey(Constants.FileSystem.RavenFsSize))
                {
                    metadata[Constants.FileSystem.RavenFsSize] = existingMetadata[Constants.FileSystem.RavenFsSize];
                }

                var newEtag = uuidGenerator.CreateSequentialUuid();

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

                update.Save();

                return(new FileUpdateResult
                {
                    PrevEtag = existingEtag,
                    Etag = newEtag
                });
            }
        }
예제 #28
0
        public static JsonDocument RavenJObjectToJsonDocument(RavenJObject doc)
        {
            var metadata = (RavenJObject)doc["@metadata"];

            doc.Remove("@metadata");
            var key = Extract(metadata, "@id", string.Empty);

            if (string.IsNullOrEmpty(key))
            {
                // projection, it seems.
                return(new JsonDocument
                {
                    Key = string.Empty,
                    DataAsJson = doc,
                    LastModified = SystemTime.UtcNow,
                });
            }

            var lastModified = GetLastModified(metadata);

            metadata[Constants.LastModified] = lastModified;

            var etag = Extract(metadata, "@etag", Etag.Empty, (string g) => HttpExtensions.EtagHeaderToEtag(g));
            var nai  = Extract(metadata, "Non-Authoritative-Information", false, (string b) => Convert.ToBoolean(b));

            var jsonDocument = new JsonDocument
            {
                Key            = key,
                LastModified   = lastModified,
                Etag           = etag,
                TempIndexScore = metadata == null ? null : metadata.Value <float?>(Constants.TemporaryScoreValue),
                NonAuthoritativeInformation = nai,
                Metadata   = metadata.FilterHeadersToObject(),
                DataAsJson = doc,
            };

            return(jsonDocument);
        }
예제 #29
0
        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(Constants.MetadataEtagField))
                {
                    throw new InvalidOperationException("Metadata of file {0} does not contain 'ETag' key " + filename);
                }

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

                var existingMetadata = RetrieveMetadata();

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

                if (!innerEsentMetadata.ContainsKey("RavenFS-Size") && existingMetadata.ContainsKey("RavenFS-Size"))
                {
                    innerEsentMetadata["RavenFS-Size"] = existingMetadata["RavenFS-Size"];
                }

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

                update.Save();
            }
        }
예제 #30
0
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            using (Database.DisableAllTriggersForCurrentThread())
            {
                metadata.Remove(ReplicationConstants.RavenReplicationConflict);           // you can't put conflicts

                var oldVersion = Database.Get(key, transactionInformation);
                if (oldVersion == null)
                {
                    return;
                }
                if (oldVersion.Metadata[ReplicationConstants.RavenReplicationConflict] == null)
                {
                    return;
                }
                // this is a conflict document, holding document keys in the
                // values of the properties
                foreach (var prop in oldVersion.DataAsJson.Value <RavenJArray>("Conflicts"))
                {
                    Database.Delete(prop.Value <string>(), null, transactionInformation);
                }
            }
        }
예제 #31
0
        public void PutFile(string filename, long?totalSize, RavenJObject metadata, bool tombstone = false)
        {
            using (var update = new Update(session, Files, JET_prep.Insert))
            {
                Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["name"], filename, Encoding.Unicode);
                if (totalSize != null)
                {
                    Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["total_size"], BitConverter.GetBytes(totalSize.Value));
                }

                Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["uploaded_size"], BitConverter.GetBytes(0));

                if (!metadata.ContainsKey(Constants.MetadataEtagField))
                {
                    throw new InvalidOperationException(string.Format("Metadata of file {0} does not contain 'ETag' key", filename));
                }

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

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

                update.Save();
            }

            if (!tombstone)
            {
                if (Api.TryMoveFirst(session, Details) == false)
                {
                    throw new InvalidOperationException("Could not find system metadata row");
                }

                Api.EscrowUpdate(session, Details, tableColumnsCache.DetailsColumns["file_count"], 1);
            }
        }
예제 #32
0
        public Task WriteDocumentAsync(RavenJObject document, CancellationToken cancellationToken)
        {
            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());
                }
            }

            FlushBatch();
            return(new CompletedTask());
        }
예제 #33
0
		public void CloningTestsWorksCorrectly()
		{
			var f = new RavenJObject();
			f["1"] = new RavenJValue(1);
			f["2"] = new RavenJValue(2);

			var f1 = (RavenJObject)f.CloneToken();
			f1["2"] = new RavenJValue(3);

			var val = (RavenJValue) f["2"];
			Assert.Equal(2, val.Value);
			val = (RavenJValue)f1["2"];
			Assert.Equal(3, val.Value);

			var f2 = (RavenJObject)f1.CloneToken();
			val = (RavenJValue)f2["2"];
			Assert.Equal(3, val.Value);

			f["2"] = f2;
			f1 = (RavenJObject) f.CloneToken();
			f.Remove("2");
			Assert.Null(f["2"]);
			Assert.NotNull(f1["2"]);
		}
예제 #34
0
		private static void RemoveReservedProperties(RavenJObject document)
		{
			var toRemove = new HashSet<string>();
			foreach (var propertyName in document.Keys.Where(propertyName => propertyName.StartsWith("@")))
			{
			    toRemove.Add(propertyName);
			}
			foreach (var propertyName in toRemove)
			{
				document.Remove(propertyName);
			}
		}
예제 #35
0
		private void RemoveNonDisplayedMetadata(RavenJObject metaData)
		{
			metaData.Remove("@etag");
			metaData.Remove("@id");
		}
        public override void OnRead(string key, RavenJObject document, RavenJObject metadata, ReadOperation operation,
            TransactionInformation transactionInformation)
        {
            if (key == null)
                return;

            // If we're loading a revision directly, make sure we have set the rev number in the metadata
            var temporal = metadata.GetTemporalMetadata();
            if (key.Contains(TemporalConstants.TemporalKeySeparator))
                temporal.RevisionNumber = int.Parse(key.Split('/').Last());

            // Handle migration from nontemporal document
            if (temporal.Status == TemporalStatus.NonTemporal && _temporalVersioningEnabled.Value)
            {
                // Rewrite the document temporally.  We specifically do NOT disable triggers on this put.
                temporal.Effective = DateTimeOffset.MinValue;
                Database.Put(key, null, new RavenJObject(document), new RavenJObject(metadata), transactionInformation);

                // Fake out the current document for the return of this load.
                temporal.Status = TemporalStatus.Current;
                temporal.RevisionNumber = 1;
                temporal.Effective = _now.Value;
                temporal.EffectiveStart = DateTimeOffset.MinValue;
                temporal.EffectiveUntil = DateTimeOffset.MaxValue;
            }

            // If we didn't get a new effective revision key above, just return
            var effectiveRevisionKey = _effectiveVersionKey.Value;
            if (effectiveRevisionKey == null)
                return;

            _log.Debug("Temporally loading {0} instead of {1}", effectiveRevisionKey, key);

            using (Database.DisableAllTriggersForCurrentThread())
            {
                // Load the effective document
                var effectiveRevision = Database.Get(effectiveRevisionKey, transactionInformation);

                // Replace the resulting document
                foreach (var prop in document.Keys)
                    document.Remove(prop);
                var evDoc = effectiveRevision.DataAsJson;
                foreach (var prop in evDoc.Keys)
                    document.Add(prop, evDoc[prop]);

                // Replace the resulting metadata
                foreach (var prop in metadata.Keys)
                {
                    if (prop != TemporalMetadata.RavenTemporalEffective)
                        metadata.Remove(prop);
                }
                var evMetadata = effectiveRevision.Metadata;
                foreach (var prop in evMetadata.Keys)
                    metadata.Add(prop, evMetadata[prop]);

                // Send back the revision number also
                temporal.RevisionNumber = int.Parse(effectiveRevisionKey.Split('/').Last());
            }
        }
예제 #37
0
        public override VetoResult AllowPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            if (Database.Name != null && Database.Name != Constants.SystemDatabase)
            {
                return(VetoResult.Allowed);
            }

            if (key.StartsWith(RavenDatabasesPrefix, StringComparison.InvariantCultureIgnoreCase) == false)
            {
                return(VetoResult.Allowed);
            }

            var tempPermission = metadata[Constants.AllowBundlesChange];

            if (tempPermission != null)
            {
                metadata.Remove(Constants.AllowBundlesChange); // this is a temp marker so do not persist this medatada
            }
            var bundlesChangesAllowed = tempPermission != null &&
                                        tempPermission.Value <string>()
                                        .Equals("true", StringComparison.InvariantCultureIgnoreCase);

            if (bundlesChangesAllowed)
            {
                return(VetoResult.Allowed);
            }

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

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

            var currentDbDocument = existingDbDoc.DataAsJson.JsonDeserialization <DatabaseDocument>();

            var    currentBundles = new List <string>();
            string value;

            if (currentDbDocument.Settings.TryGetValue(Constants.ActiveBundles, out value))
            {
                currentBundles = value.GetSemicolonSeparatedValues();
            }

            var newDbDocument = document.JsonDeserialization <DatabaseDocument>();
            var newBundles    = new List <string>();

            if (newDbDocument.Settings.TryGetValue(Constants.ActiveBundles, out value))
            {
                newBundles = value.GetSemicolonSeparatedValues();
            }

            if (currentBundles.Count != newBundles.Count || currentBundles.TrueForAll(x => newBundles.Contains(x, StringComparer.InvariantCultureIgnoreCase)) == false)
            {
                return(VetoResult.Deny(
                           "You should not change 'Raven/ActiveBundles' setting for a database. This setting should be set only once when a database is created. " +
                           "If you really need to override it you have to specify {\"" + Constants.AllowBundlesChange +
                           "\": true} in metadata of a database document every time when you send it." + Environment.NewLine +
                           "Current: " + string.Join("; ", currentBundles) + Environment.NewLine +
                           "New: " + string.Join("; ", newBundles)));
            }

            return(VetoResult.Allowed);
        }
예제 #38
0
파일: RavenDb_1977.cs 프로젝트: mow/ravendb
        public void CanDetectDeleteMetadataChanges()
        {
            using (var store = NewDocumentStore())
            {
                using (var session = store.OpenSession())
                {
                    session.Store(new UserData
                    {
                        Id     = 123,
                        Name   = "user1",
                        Salary = 12.5,
                        Date   = DateTime.Now
                    }, "UserDatas/1");
                    session.Store(new UserData
                    {
                        Id     = 1234,
                        Name   = "user2",
                        Salary = 12.51,
                        Date   = new DateTime(2014, 1, 1)
                    }, "UserDatas/2");
                    session.Store(new UserData
                    {
                        Id     = 1235,
                        Name   = "user3",
                        Salary = 12.45,
                        Date   = new DateTime(2014, 1, 2)
                    }, "UserDatas/3");
                    session.SaveChanges();
                }

                const string propName1 = "test1";
                const string propName2 = "test2";

                using (var session = store.OpenSession())
                {
                    var          userdata1 = session.Load <UserData>("UserDatas/1");
                    RavenJObject metadata  = session.Advanced.GetMetadataFor(userdata1);
                    metadata.Add(propName1, null);
                    metadata.Add(propName2, null);
                    session.SaveChanges();
                }

                using (var session = store.OpenSession())
                {
                    var          userdata1 = session.Load <UserData>("UserDatas/1");
                    RavenJObject metadata  = session.Advanced.GetMetadataFor(userdata1);

                    RavenJToken value;
                    metadata.TryGetValue(propName1, out value);
                    Assert.NotNull(value);
                    metadata.TryGetValue(propName2, out value);
                    Assert.NotNull(value);

                    metadata.Remove(propName1);
                    metadata.Remove(propName2);

                    IDictionary <string, DocumentsChanges[]> changes3 = session.Advanced.WhatChanged();
                    Assert.Equal(1, changes3.Count);
                    Assert.True(changes3.ContainsKey("UserDatas/1"));

                    DocumentsChanges[] data3;
                    changes3.TryGetValue("UserDatas/1", out data3);
                    Assert.NotNull(data3);
                    Assert.Equal(2, data3.Length);
                    session.SaveChanges();
                }
            }
        }
        public override void OnRead(string key, RavenJObject document, RavenJObject metadata, ReadOperation operation,
                                    TransactionInformation transactionInformation)
        {
            if (key == null)
            {
                return;
            }

            // If we're loading a revision directly, make sure we have set the rev number in the metadata
            var temporal = metadata.GetTemporalMetadata();

            if (key.Contains(TemporalConstants.TemporalKeySeparator))
            {
                temporal.RevisionNumber = int.Parse(key.Split('/').Last());
            }

            // Handle migration from nontemporal document
            if (temporal.Status == TemporalStatus.NonTemporal && _temporalVersioningEnabled.Value)
            {
                // Rewrite the document temporally.  We specifically do NOT disable triggers on this put.
                temporal.Effective = DateTimeOffset.MinValue;
                Database.Put(key, null, new RavenJObject(document), new RavenJObject(metadata), transactionInformation);

                // Fake out the current document for the return of this load.
                temporal.Status         = TemporalStatus.Current;
                temporal.RevisionNumber = 1;
                temporal.Effective      = _now.Value;
                temporal.EffectiveStart = DateTimeOffset.MinValue;
                temporal.EffectiveUntil = DateTimeOffset.MaxValue;
            }

            // If we didn't get a new effective revision key above, just return
            var effectiveRevisionKey = _effectiveVersionKey.Value;

            if (effectiveRevisionKey == null)
            {
                return;
            }

            _log.Debug("Temporally loading {0} instead of {1}", effectiveRevisionKey, key);

            using (Database.DisableAllTriggersForCurrentThread())
            {
                // Load the effective document
                var effectiveRevision = Database.Get(effectiveRevisionKey, transactionInformation);

                // Replace the resulting document
                foreach (var prop in document.Keys)
                {
                    document.Remove(prop);
                }
                var evDoc = effectiveRevision.DataAsJson;
                foreach (var prop in evDoc.Keys)
                {
                    document.Add(prop, evDoc[prop]);
                }

                // Replace the resulting metadata
                foreach (var prop in metadata.Keys)
                {
                    if (prop != TemporalMetadata.RavenTemporalEffective)
                    {
                        metadata.Remove(prop);
                    }
                }
                var evMetadata = effectiveRevision.Metadata;
                foreach (var prop in evMetadata.Keys)
                {
                    metadata.Add(prop, evMetadata[prop]);
                }

                // Send back the revision number also
                temporal.RevisionNumber = int.Parse(effectiveRevisionKey.Split('/').Last());
            }
        }
예제 #40
0
		private static void RemoveReservedProperties(RavenJObject document)
		{
			var toRemove = document.Keys.Where(propertyName => propertyName.StartsWith("@")).ToList();
			foreach (var propertyName in toRemove)
			{
				document.Remove(propertyName);
			}
		}
예제 #41
0
		protected override Task PutDocument(RavenJObject document)
		{
			if (document != null)
			{
				var metadata = document.Value<RavenJObject>("@metadata");
				var id = metadata.Value<string>("@id");
				document.Remove("@metadata");

				operation.Store(document, metadata, id);
			}

			return new CompletedTask();
		}
        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();
        }
예제 #43
0
		protected override void PutDocument(RavenJObject document, SmugglerOptions options)
		{
			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());
		}
예제 #44
0
		protected override Task PutDocument(RavenJObject document)
		{
			if (document != null)
			{
				var metadata = document.Value<RavenJObject>("@metadata");
				var key = metadata.Value<string>("@id");
				document.Remove("@metadata");

				_database.Put(key, null, document, metadata, null);
			}

			return new CompletedTask();
		}
예제 #45
0
        public override void OnPut(string key, RavenJObject jsonReplicationDocument, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            using (Database.DisableAllTriggersForCurrentThread())
            {
                if (metadata.Remove(Constants.RavenReplicationConflictSkipResolution))
                {
                    if (key.IndexOf("/conflicts/", StringComparison.OrdinalIgnoreCase) == -1)
                    {
                        metadata["@Http-Status-Code"]        = 409;
                        metadata["@Http-Status-Description"] = "Conflict";
                    }

                    return;
                }

                metadata.Remove(Constants.RavenReplicationConflict);// you can't put conflicts
                metadata.Remove(Constants.RavenReplicationConflictDocument);

                var oldVersion = Database.Documents.Get(key, transactionInformation);
                if (oldVersion == null)
                {
                    return;
                }
                if (oldVersion.Metadata[Constants.RavenReplicationConflict] == null)
                {
                    return;
                }

                var history = new RavenJArray();
                metadata[Constants.RavenReplicationHistory] = history;

                // this is a conflict document, holding document keys in the
                // values of the properties
                var conflicts = oldVersion.DataAsJson.Value <RavenJArray>("Conflicts");
                if (conflicts == null)
                {
                    return;
                }

                Dictionary <string, RavenJObject> conflictHistoryAsDictionary = new Dictionary <string, RavenJObject>();

                foreach (var prop in conflicts)
                {
                    RavenJObject deletedMetadata;
                    Database.Documents.Delete(prop.Value <string>(), null, transactionInformation, out deletedMetadata);

                    if (deletedMetadata != null)
                    {
                        var conflictHistory = new RavenJArray(ReplicationData.GetHistory(deletedMetadata));
                        conflictHistory.Add(new RavenJObject
                        {
                            { Constants.RavenReplicationVersion, deletedMetadata[Constants.RavenReplicationVersion] },
                            { Constants.RavenReplicationSource, deletedMetadata[Constants.RavenReplicationSource] }
                        });
                        Historian.MergeSingleHistory(conflictHistory, conflictHistoryAsDictionary);
                    }
                }

                metadata[Constants.RavenReplicationHistory]       = new RavenJArray(conflictHistoryAsDictionary.Values);
                metadata[Constants.RavenReplicationMergedHistory] = true;
            }
        }
예제 #46
0
        public override void OnPut(string key, RavenJObject jsonReplicationDocument, RavenJObject metadata)
        {
            using (Database.DisableAllTriggersForCurrentThread())
            {
                if (metadata.Remove(Constants.RavenReplicationConflictSkipResolution))
                {
                    if (key.IndexOf("/conflicts/", StringComparison.OrdinalIgnoreCase) == -1)
                    {
                        metadata["@Http-Status-Code"]        = 409;
                        metadata["@Http-Status-Description"] = "Conflict";
                    }

                    return;
                }

                metadata.Remove(Constants.RavenReplicationConflict);// you can't put conflicts

                var oldVersion = Database.Documents.Get(key);
                if (oldVersion == null)
                {
                    return;
                }
                if (oldVersion.Metadata[Constants.RavenReplicationConflict] == null)
                {
                    return;
                }

                var history = new RavenJArray();
                metadata[Constants.RavenReplicationHistory] = history;

                // this is a conflict document, holding document keys in the
                // values of the properties
                var conflicts = oldVersion.DataAsJson.Value <RavenJArray>("Conflicts");
                if (conflicts == null)
                {
                    return;
                }

                var list = new List <RavenJArray>
                {
                    new RavenJArray(ReplicationData.GetHistory(metadata)) // first item to interleave
                };

                foreach (var prop in conflicts)
                {
                    RavenJObject deletedMetadata;
                    Database.Documents.Delete(prop.Value <string>(), null, out deletedMetadata);

                    if (deletedMetadata != null)
                    {
                        var conflictHistory = new RavenJArray(ReplicationData.GetHistory(deletedMetadata));
                        conflictHistory.Add(new RavenJObject
                        {
                            { Constants.RavenReplicationVersion, deletedMetadata[Constants.RavenReplicationVersion] },
                            { Constants.RavenReplicationSource, deletedMetadata[Constants.RavenReplicationSource] }
                        });
                        list.Add(conflictHistory);
                    }
                }


                int  index = 0;
                bool added = true;
                while (added) // interleave the history from all conflicts
                {
                    added = false;
                    foreach (var deletedMetadata in list)
                    {
                        // add the conflict history to the mix, so we make sure that we mark that we resolved the conflict
                        if (index < deletedMetadata.Length)
                        {
                            history.Add(deletedMetadata[index]);
                            added = true;
                        }
                    }
                    index++;
                }

                while (history.Length > Constants.ChangeHistoryLength)
                {
                    history.RemoveAt(0);
                }
            }
        }
예제 #47
0
        public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
        {
            using (Database.DisableAllTriggersForCurrentThread())
            {
                metadata.Remove(Constants.RavenReplicationConflict);                // you can't put conflicts

                var oldVersion = Database.Get(key, transactionInformation);
                if (oldVersion == null)
                {
                    return;
                }
                if (oldVersion.Metadata[Constants.RavenReplicationConflict] == null)
                {
                    return;
                }

                var history = new RavenJArray();
                metadata[Constants.RavenReplicationHistory] = history;

                var ravenJTokenEqualityComparer = new RavenJTokenEqualityComparer();
                // this is a conflict document, holding document keys in the
                // values of the properties
                var conflicts = oldVersion.DataAsJson.Value <RavenJArray>("Conflicts");
                if (conflicts == null)
                {
                    return;
                }

                var list = new List <RavenJArray>
                {
                    new RavenJArray(ReplicationData.GetHistory(metadata))             // first item to interleave
                };
                foreach (var prop in conflicts)
                {
                    RavenJObject deletedMetadata;
                    Database.Delete(prop.Value <string>(), null, transactionInformation, out deletedMetadata);

                    if (deletedMetadata != null)
                    {
                        var conflictHistory = new RavenJArray(ReplicationData.GetHistory(deletedMetadata));
                        conflictHistory.Add(new RavenJObject
                        {
                            { Constants.RavenReplicationVersion, deletedMetadata[Constants.RavenReplicationVersion] },
                            { Constants.RavenReplicationSource, deletedMetadata[Constants.RavenReplicationSource] }
                        });
                        list.Add(conflictHistory);
                    }
                }


                int  index = 0;
                bool added = true;
                while (added) // interleave the history from all conflicts
                {
                    added = false;
                    foreach (var deletedMetadata in list)
                    {
                        // add the conflict history to the mix, so we make sure that we mark that we resolved the conflict
                        if (index < deletedMetadata.Length)
                        {
                            history.Add(deletedMetadata[index]);
                            added = true;
                        }
                    }
                    index++;
                }

                while (history.Length > Constants.ChangeHistoryLength)
                {
                    history.RemoveAt(0);
                }
            }
        }
예제 #48
0
 private void RemoveNonDisplayedMetadata(RavenJObject metaData)
 {
     metaData.Remove("@etag");
     metaData.Remove("@id");
 }
예제 #49
0
 private void RemoveMetadataReservedProperties(RavenJObject metadata)
 {
     RemoveReservedProperties(metadata);
     metadata.Remove("Raven-Last-Modified");
     metadata.Remove("Last-Modified");
 }
        public Task PutDocument(RavenJObject document, int size)
        {
            if (document == null)
                return new CompletedTask();

            var metadata = document.Value<RavenJObject>("@metadata");
            var id = metadata.Value<string>("@id");
            if (String.IsNullOrWhiteSpace(id))
                throw new InvalidDataException("Error while importing document from the dump: \n\r Missing id in the document metadata. This shouldn't be happening, most likely the dump you are importing from is corrupt");

            document.Remove("@metadata");

            Operation.Store(document, metadata, id, size);

            return new CompletedTask();
        }
        public RavenJObject StripReplicationInformationFromMetadata(RavenJObject metadata)
        {
            if (metadata != null)
            {
                metadata.Remove(Constants.RavenReplicationHistory);
                metadata.Remove(Constants.RavenReplicationSource);
                metadata.Remove(Constants.RavenReplicationVersion);
            }

            return metadata;
        }
예제 #52
0
		private static void RemoveReservedProperties(RavenJObject document)
		{
			document.Remove(string.Empty);
			var toRemove = document.Keys.Where(propertyName => propertyName.StartsWith("@") || headersToIgnoreServer.Contains(propertyName)).ToList();
			foreach (var propertyName in toRemove)
			{
				document.Remove(propertyName);
			}
		}
예제 #53
0
		private static void RemoveMetadataReservedProperties(RavenJObject metadata)
		{
			RemoveReservedProperties(metadata);
			metadata.Remove("Raven-Last-Modified");
			metadata.Remove("Last-Modified");
		}
예제 #54
0
		public override void OnPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation)
		{
			using (Database.DisableAllTriggersForCurrentThread())
			{
				if (metadata.Remove(Constants.RavenReplicationConflictSkipResolution))
				{
					if (key.IndexOf("/conflicts/", StringComparison.OrdinalIgnoreCase) == -1)
					{
						metadata["@Http-Status-Code"] = 409;
						metadata["@Http-Status-Description"] = "Conflict";
					}

					return;
				}

				metadata.Remove(Constants.RavenReplicationConflict);// you can't put conflicts

				var oldVersion = Database.Documents.Get(key, transactionInformation);
				if (oldVersion == null)
					return;
				if (oldVersion.Metadata[Constants.RavenReplicationConflict] == null)
					return;

				var history = new RavenJArray();
				metadata[Constants.RavenReplicationHistory] = history;

				// this is a conflict document, holding document keys in the 
				// values of the properties
				var conflicts = oldVersion.DataAsJson.Value<RavenJArray>("Conflicts");
				if(conflicts == null)
					return;

			    var list = new List<RavenJArray>
			    {
			        new RavenJArray(ReplicationData.GetHistory(metadata)) // first item to interleave
			    };

				foreach (var prop in conflicts)
				{
					RavenJObject deletedMetadata;
					Database.Documents.Delete(prop.Value<string>(), null, transactionInformation, out deletedMetadata);

				    if (deletedMetadata != null)
				    {
                        var conflictHistory = new RavenJArray(ReplicationData.GetHistory(deletedMetadata));
                        conflictHistory.Add(new RavenJObject
				        {
				            {Constants.RavenReplicationVersion, deletedMetadata[Constants.RavenReplicationVersion]},
				            {Constants.RavenReplicationSource, deletedMetadata[Constants.RavenReplicationSource]}
				        });
				        list.Add(conflictHistory);
				    }
				}


			    int index = 0;
                bool added = true;
                while (added) // interleave the history from all conflicts
                {
                    added = false;
                    foreach (var deletedMetadata in list)
			        {
                        // add the conflict history to the mix, so we make sure that we mark that we resolved the conflict
			            if (index < deletedMetadata.Length)
			            {
			                history.Add(deletedMetadata[index]);
			                added = true;
			            }
			        }
			        index++;
			    }

                while (history.Length > Constants.ChangeHistoryLength)
                {
                    history.RemoveAt(0);
                }
			}
		}
예제 #55
0
			public void EntityToDocument(string key, object entity, RavenJObject document, RavenJObject metadata)
			{
				if (entity is Custom == false)
					return;
				document.Remove("Value");
			}
예제 #56
-1
		public override void AfterPut(string key, RavenJObject document, RavenJObject metadata, Etag etag, TransactionInformation transactionInformation)
		{
			VersioningConfiguration versioningConfiguration;
			if (TryGetVersioningConfiguration(key, metadata, out versioningConfiguration) == false)
				return;

			using (Database.DisableAllTriggersForCurrentThread())
			{
				var copyMetadata = new RavenJObject(metadata);
				copyMetadata[VersioningUtil.RavenDocumentRevisionStatus] = RavenJToken.FromObject("Historical");
				copyMetadata[Constants.RavenReadOnly] = true;
				copyMetadata.Remove(VersioningUtil.RavenDocumentRevision);
				object parentRevision;
				metadata.__ExternalState.TryGetValue("Parent-Revision", out parentRevision);
				if (parentRevision != null)
				{
					copyMetadata[VersioningUtil.RavenDocumentParentRevision] = key + "/revisions/" + parentRevision;
					copyMetadata[VersioningUtil.RavenDocumentParentRevision] = key + "/revisions/" + parentRevision;
				}

				object value;
				metadata.__ExternalState.TryGetValue("Next-Revision", out value);
				Database.Put(key + "/revisions/" + value, null, (RavenJObject)document.CreateSnapshot(), copyMetadata,
							 transactionInformation);
			}
		}