Пример #1
0
        /// <summary>
        /// Creates the put entity command.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="documentMetadata">The document metadata.</param>
        /// <returns></returns>
        protected ICommandData CreatePutEntityCommand(object entity, DocumentMetadata documentMetadata)
        {
            string id;

            if (GenerateEntityIdOnTheClient.TryGetIdFromInstance(entity, out id) &&
                documentMetadata.Key != null &&
                documentMetadata.Key.Equals(id, StringComparison.InvariantCultureIgnoreCase) == false)
            {
                throw new InvalidOperationException("Entity " + entity.GetType().FullName + " had document key '" +
                                                    documentMetadata.Key + "' but now has document key property '" + id + "'." +
                                                    Environment.NewLine +
                                                    "You cannot change the document key property of a entity loaded into the session");
            }

            var json = EntityToJson.ConvertEntityToJson(documentMetadata.Key, entity, documentMetadata.Metadata);

            var etag = UseOptimisticConcurrency || documentMetadata.ForceConcurrencyCheck
                                                   ? (documentMetadata.ETag ?? Guid.Empty)
                                                   : (Guid?)null;

            return(new PutCommandData
            {
                Document = json,
                Etag = etag,
                Key = documentMetadata.Key,
                Metadata = (RavenJObject)documentMetadata.Metadata.CloneToken(),
            });
        }
Пример #2
0
        /// <summary>
        /// Determines if the entity have changed.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="documentMetadata">The document metadata.</param>
        /// <returns></returns>
        protected bool EntityChanged(object entity, DocumentMetadata documentMetadata)
        {
            if (documentMetadata == null)
            {
                return(true);
            }

            string id;

            if (GenerateEntityIdOnTheClient.TryGetIdFromInstance(entity, out id) &&
                string.Equals(documentMetadata.Key, id, StringComparison.InvariantCultureIgnoreCase) == false)
            {
                return(true);
            }

            // prevent saves of a modified read only entity
            if (documentMetadata.OriginalMetadata.ContainsKey(Constants.RavenReadOnly) &&
                documentMetadata.OriginalMetadata.Value <bool>(Constants.RavenReadOnly) &&
                documentMetadata.Metadata.ContainsKey(Constants.RavenReadOnly) &&
                documentMetadata.Metadata.Value <bool>(Constants.RavenReadOnly))
            {
                return(false);
            }

            var newObj = EntityToJson.ConvertEntityToJson(documentMetadata.Key, entity, documentMetadata.Metadata);

            return(RavenJToken.DeepEquals(newObj, documentMetadata.OriginalValue) == false ||
                   RavenJToken.DeepEquals(documentMetadata.Metadata, documentMetadata.OriginalMetadata) == false);
        }
Пример #3
0
        public async Task StoreAsync(object entity, string id)
        {
            var metadata = new RavenJObject();
            var tag      = documentStore.Conventions.GetDynamicTagName(entity);

            if (tag != null)
            {
                metadata.Add(Constants.Headers.RavenEntityName, tag);
            }

            var data = entityToJson.ConvertEntityToJson(id, entity, metadata);

            OnBeforeEntityInsert(id, data, metadata);

            await Operation.WriteAsync(id, metadata, data).ConfigureAwait(false);
        }
Пример #4
0
        public void Store(object entity, string id)
        {
            var metadata = new RavenJObject();

            var tag = documentStore.Conventions.GetTypeTagName(entity.GetType());

            if (tag != null)
            {
                metadata.Add(Constants.RavenEntityName, tag);
            }

            var data = entityToJson.ConvertEntityToJson(id, entity, metadata);

            OnBeforeEntityInsert(id, data, metadata);

            operation.Write(id, metadata, data);
        }
Пример #5
0
        /// <summary>
        /// Updates the batch results.
        /// </summary>
        protected void UpdateBatchResults(IList <BatchResult> batchResults, SaveChangesData saveChangesData)
        {
            for (var i = saveChangesData.DeferredCommandsCount; i < batchResults.Count; i++)
            {
                var batchResult = batchResults[i];
                if (batchResult.Method != "PUT")
                {
                    continue;
                }

                var entity = saveChangesData.Entities[i - saveChangesData.DeferredCommandsCount];
                DocumentMetadata documentMetadata;
                if (entitiesAndMetadata.TryGetValue(entity, out documentMetadata) == false)
                {
                    continue;
                }

                batchResult.Metadata["@etag"]     = new RavenJValue(batchResult.Etag.ToString());
                entitiesByKey[batchResult.Key]    = entity;
                documentMetadata.ETag             = batchResult.Etag;
                documentMetadata.Key              = batchResult.Key;
                documentMetadata.OriginalMetadata = (RavenJObject)batchResult.Metadata.CloneToken();
                documentMetadata.Metadata         = batchResult.Metadata;
                documentMetadata.OriginalValue    = EntityToJson.ConvertEntityToJson(documentMetadata.Key, entity, documentMetadata.Metadata);

                GenerateEntityIdOnTheClient.TrySetIdentity(entity, batchResult.Key);

                foreach (var documentStoreListener in listeners.StoreListeners)
                {
                    documentStoreListener.AfterStore(batchResult.Key, entity, batchResult.Metadata);
                }
            }

            var lastPut = batchResults.LastOrDefault(x => x.Method == "PUT");

            if (lastPut == null)
            {
                return;
            }

            documentStore.LastEtagHolder.UpdateLastWrittenEtag(lastPut.Etag);
        }
Пример #6
0
        public void Store(object entity, string id)
        {
            if (Operation.IsAborted)
            {
                throw new InvalidOperationException("Bulk insert has been aborted or the operation was timed out");
            }

            var metadata = new RavenJObject();

            var tag = documentStore.Conventions.GetDynamicTagName(entity);

            if (tag != null)
            {
                metadata.Add(Constants.RavenEntityName, tag);
            }

            var data = entityToJson.ConvertEntityToJson(id, entity, metadata);

            OnBeforeEntityInsert(id, data, metadata);

            Operation.Write(id, metadata, data);
        }