Пример #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 BulkInsertOperation(string database, IDocumentStore documentStore, DocumentSessionListeners listeners, BulkInsertOptions options)
        {
            this.documentStore = documentStore;
            databaseCommands   = database == null
                                                   ? documentStore.DatabaseCommands.ForSystemDatabase()
                                                   : documentStore.DatabaseCommands.ForDatabase(database);

            generateEntityIdOnTheClient = new GenerateEntityIdOnTheClient(documentStore, entity => documentStore.Conventions.GenerateDocumentKey(database, databaseCommands, entity));
            operation    = databaseCommands.GetBulkInsertOperation(options);
            entityToJson = new EntityToJson(documentStore, listeners);
        }
Пример #4
0
		public BulkInsertOperation(string database, IDocumentStore documentStore, DocumentSessionListeners listeners, BulkInsertOptions options)
		{
			this.documentStore = documentStore;
			databaseCommands = database == null
				                   ? documentStore.DatabaseCommands.ForSystemDatabase()
				                   : documentStore.DatabaseCommands.ForDatabase(database);

			generateEntityIdOnTheClient = new GenerateEntityIdOnTheClient(documentStore, entity => documentStore.Conventions.GenerateDocumentKey(database, databaseCommands, entity));
			operation = databaseCommands.GetBulkInsertOperation(options);
			entityToJson = new EntityToJson(documentStore, listeners);
		}
Пример #5
0
        public BulkInsertOperation(string database, IDocumentStore documentStore, DocumentSessionListeners listeners, BulkInsertOptions options, IDatabaseChanges changes)
        {
            this.documentStore = documentStore;

            database = database ?? MultiDatabase.GetDatabaseName(documentStore.Url);

            // Fitzchak: Should not be ever null because of the above code, please refactor this.
            DatabaseCommands = database == null
                                ? documentStore.AsyncDatabaseCommands.ForSystemDatabase()
                                : documentStore.AsyncDatabaseCommands.ForDatabase(database);

            generateEntityIdOnTheClient = new GenerateEntityIdOnTheClient(documentStore, entity => documentStore.Conventions.GenerateDocumentKeyAsync(database, DatabaseCommands, entity).ResultUnwrap());
            Operation    = GetBulkInsertOperation(options, DatabaseCommands, changes);
            entityToJson = new EntityToJson(documentStore, listeners);
        }
Пример #6
0
		public BulkInsertOperation(string database, IDocumentStore documentStore, DocumentSessionListeners listeners, BulkInsertOptions options, IDatabaseChanges changes)
		{
			this.documentStore = documentStore;

			database = database ?? MultiDatabase.GetDatabaseName(documentStore.Url);

			// Fitzchak: Should not be ever null because of the above code, please refactor this.
			DatabaseCommands = database == null
				? documentStore.AsyncDatabaseCommands.ForSystemDatabase()
				: documentStore.AsyncDatabaseCommands.ForDatabase(database);

			generateEntityIdOnTheClient = new GenerateEntityIdOnTheClient(documentStore.Conventions, entity => documentStore.Conventions.GenerateDocumentKeyAsync(database, DatabaseCommands, entity).ResultUnwrap());
			Operation = GetBulkInsertOperation(options, DatabaseCommands, changes);
			entityToJson = new EntityToJson(documentStore, listeners);
		}
Пример #7
0
        /// <summary>
        /// Saves all the changes to the Raven server.
        /// </summary>


        public void SaveChanges()
        {
            using (EntityToJson.EntitiesToJsonCachingScope())
            {
                var data = PrepareForSaveChanges();

                if (data.Commands.Count == 0)
                {
                    return;
                }
                IncrementRequestCount();
                LogBatch(data);

                var batchResults = DatabaseCommands.Batch(data.Commands);
                UpdateBatchResults(batchResults, data);
            }
        }
Пример #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="InMemoryDocumentSessionOperations"/> class.
 /// </summary>
 protected InMemoryDocumentSessionOperations(
     string dbName,
     DocumentStoreBase documentStore,
     DocumentSessionListeners listeners,
     Guid id)
 {
     Id                                 = id;
     this.dbName                        = dbName;
     this.documentStore                 = documentStore;
     this.listeners                     = listeners;
     ResourceManagerId                  = documentStore.ResourceManagerId;
     UseOptimisticConcurrency           = false;
     AllowNonAuthoritativeInformation   = true;
     NonAuthoritativeInformationTimeout = TimeSpan.FromSeconds(15);
     MaxNumberOfRequestsPerSession      = documentStore.Conventions.MaxNumberOfRequestsPerSession;
     GenerateEntityIdOnTheClient        = new GenerateEntityIdOnTheClient(documentStore, GenerateKey);
     EntityToJson                       = new EntityToJson(documentStore, listeners);
 }
Пример #9
0
        public BulkInsertOperation(string database, IDocumentStore documentStore, DocumentSessionListeners listeners)
        {
            this.documentStore = documentStore;

            database = database ?? MultiDatabase.GetDatabaseName(documentStore.Url);

            // Fitzchak: Should not be ever null because of the above code, please refactor this.
            DatabaseCommands = database == null
                ? documentStore.AsyncDatabaseCommands.ForSystemDatabase()
                : documentStore.AsyncDatabaseCommands.ForDatabase(database);

            generateEntityIdOnTheClient = new GenerateEntityIdOnTheClient(documentStore.Conventions, entity =>
                                                                          AsyncHelpers.RunSync(() => documentStore.Conventions.GenerateDocumentKeyAsync(database, DatabaseCommands, entity)));

            // ReSharper disable once VirtualMemberCallInContructor
            Operation    = GetBulkInsertOperation(DatabaseCommands);
            entityToJson = new EntityToJson(documentStore, listeners);
        }
Пример #10
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);
        }
Пример #11
0
        /// <summary>
        /// Saves all the changes to the Raven server.
        /// </summary>


        public void SaveChanges()
        {
            using (EntityToJson.EntitiesToJsonCachingScope())
            {
                var data = PrepareForSaveChanges();

                if (data.Commands.Count == 0)
                {
                    return;
                }
                IncrementRequestCount();
                LogBatch(data);

                var batchResults = DatabaseCommands.Batch(data.Commands);
                if (batchResults == null)
                {
                    throw new InvalidOperationException("Cannot call Save Changes after the document store was disposed.");
                }
                UpdateBatchResults(batchResults, data);
            }
        }