public override void Execute(DocumentsOperationContext context, RavenTransaction tx) { foreach (var document in Documents) { BlittableJsonReaderObject metadata; if (document.TryGet(Constants.Metadata.Key, out metadata) == false) { throw new InvalidOperationException("A document must have a metadata"); } // We are using the id term here and not key in order to be backward compatiable with old export files. string key; if (metadata.TryGet(Constants.Metadata.Id, out key) == false) { throw new InvalidOperationException("Document's metadata must include the document's key."); } DynamicJsonValue mutatedMetadata; metadata.Modifications = mutatedMetadata = new DynamicJsonValue(metadata); mutatedMetadata.Remove(Constants.Metadata.Id); mutatedMetadata.Remove(Constants.Metadata.Etag); if (IsRevision) { long etag; if (metadata.TryGet(Constants.Metadata.Etag, out etag) == false) { throw new InvalidOperationException("Document's metadata must include the document's key."); } _database.BundleLoader.VersioningStorage.PutDirect(context, key, etag, document); } else if (_buildVersion < 4000 && key.Contains("/revisions/")) { long etag; if (metadata.TryGet(Constants.Metadata.Etag, out etag) == false) { throw new InvalidOperationException("Document's metadata must include the document's key."); } var endIndex = key.IndexOf("/revisions/", StringComparison.OrdinalIgnoreCase); key = key.Substring(0, endIndex); _database.BundleLoader.VersioningStorage.PutDirect(context, key, etag, document); } else { _database.DocumentsStorage.Put(context, key, null, document); } } }
public void ModifyMetadata(BlittableJsonReaderObject metadata, ref DynamicJsonValue modifications) { if (metadata == null) { return; } if (_operateOnTypes.HasFlag(DatabaseItemType.Counters) == false && metadata.TryGet(Constants.Documents.Metadata.Counters, out BlittableJsonReaderArray _)) { modifications.Remove(Constants.Documents.Metadata.Counters); } if (_operateOnTypes.HasFlag(DatabaseItemType.Attachments) == false && metadata.TryGet(Constants.Documents.Metadata.Attachments, out BlittableJsonReaderArray _)) { modifications.Remove(Constants.Documents.Metadata.Attachments); } }
public bool PutFromDocument(DocumentsOperationContext context, CollectionName collectionName, string key, long newEtagBigEndian, BlittableJsonReaderObject document) { var enableVersioning = false; BlittableJsonReaderObject metadata; if (document.TryGet(Constants.Metadata.Key, out metadata)) { bool disableVersioning; if (metadata.TryGet(Constants.Versioning.RavenDisableVersioning, out disableVersioning)) { DynamicJsonValue mutatedMetadata; Debug.Assert(metadata.Modifications == null); metadata.Modifications = mutatedMetadata = new DynamicJsonValue(metadata); mutatedMetadata.Remove(Constants.Versioning.RavenDisableVersioning); if (disableVersioning) { return(false); } } if (metadata.TryGet(Constants.Versioning.RavenEnableVersioning, out enableVersioning)) { DynamicJsonValue mutatedMetadata = metadata.Modifications; if (mutatedMetadata == null) { metadata.Modifications = mutatedMetadata = new DynamicJsonValue(metadata); } mutatedMetadata.Remove(Constants.Versioning.RavenEnableVersioning); } } var configuration = GetVersioningConfiguration(collectionName); if (enableVersioning == false && configuration.Active == false) { return(false); } var table = context.Transaction.InnerTransaction.OpenTable(DocsSchema, RevisionDocuments); Slice prefixSlice; using (DocumentKeyWorker.GetSliceFromKey(context, key, out prefixSlice)) { var revisionsCount = IncrementCountOfRevisions(context, prefixSlice, 1); DeleteOldRevisions(context, table, prefixSlice, configuration.MaxRevisions, revisionsCount); PutInternal(context, key, newEtagBigEndian, document, table); } return(true); }
public override BlittableJsonReaderObject GetUpdatedValue(JsonOperationContext context, BlittableJsonReaderObject previousValue, long index) { if (string.IsNullOrWhiteSpace(Value.Name)) { Value.Name = GenerateTaskName(previousValue); } var prevTaskId = Value.TaskId; Value.TaskId = index; string oldName = null; if (previousValue != null) { previousValue.Modifications ??= new DynamicJsonValue(); if (previousValue.TryGet(Value.Name, out object _) == false) { if (prevTaskId != 0) { foreach (var propertyName in previousValue.GetPropertyNames()) { var property = (BlittableJsonReaderObject)previousValue[propertyName]; if (property.TryGet(nameof(ServerWideBackupConfiguration.TaskId), out long taskId) == false) { throw new RachisInvalidOperationException( $"Current {nameof(ServerWideBackupConfiguration)} has no {nameof(ServerWideBackupConfiguration.TaskId)} " + $"or the {nameof(ServerWideBackupConfiguration.TaskId)} is not of the correct type. " + $"Should not happen\n {previousValue}"); } if (taskId != prevTaskId) { continue; } oldName = propertyName; break; } if (oldName == null) { throw new RachisInvalidOperationException( $"Can't find {nameof(ServerWideBackupConfiguration)} with {nameof(ServerWideBackupConfiguration.Name)} {Value.Name} or with {nameof(ServerWideBackupConfiguration.TaskId)} {prevTaskId}. " + $"If you try to create new {nameof(ServerWideBackupConfiguration)} set the {nameof(ServerWideBackupConfiguration.TaskId)} to 0." + $"If you try to update exist {nameof(ServerWideBackupConfiguration)} and change its name, request the configuration again from the server to get the current {nameof(ServerWideBackupConfiguration.TaskId)} and send the {nameof(PutServerWideBackupConfigurationCommand)} again"); } } } var modifications = new DynamicJsonValue(previousValue); if (oldName != null && oldName.Equals(Value.Name) == false) { modifications.Remove(oldName); } modifications[Value.Name] = Value.ToJson(); var blittableJsonReaderObject = context.ReadObject(previousValue, Name); return(blittableJsonReaderObject); } var djv = new DynamicJsonValue { [Value.Name] = Value.ToJson() }; return(context.ReadObject(djv, Name)); }