Ejemplo n.º 1
0
        /// <summary>
        /// Saves all the changes to the Raven server.
        /// </summary>
        void IDocumentSession.SaveChanges()
        {
            using (EntityToJson.EntitiesToJsonCachingScope())
            {
                var data = PrepareForSaveChanges();
                if (data.Commands.Count == 0 && deferredCommandsByShard.Count == 0)
                {
                    return;                     // nothing to do here
                }
                IncrementRequestCount();
                LogBatch(data);

                // split by shards
                var saveChangesPerShard = GetChangesToSavePerShard(data);

                // execute on all shards
                foreach (var shardAndObjects in saveChangesPerShard)
                {
                    var shardId = shardAndObjects.Key;

                    IDatabaseCommands databaseCommands;
                    if (shardDbCommands.TryGetValue(shardId, out databaseCommands) == false)
                    {
                        throw new InvalidOperationException(
                                  string.Format("ShardedDocumentStore cannot found a DatabaseCommands for shard id '{0}'.", shardId));
                    }

                    var results = databaseCommands.Batch(shardAndObjects.Value.Commands);
                    UpdateBatchResults(results, shardAndObjects.Value);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Saves all the changes to the Raven server.
        /// </summary>
        async Task IAsyncDocumentSession.SaveChangesAsync(CancellationToken token)
        {
            await asyncDocumentKeyGeneration.GenerateDocumentKeysForSaveChanges().ConfigureAwait(false);

            var cachingScope = EntityToJson.EntitiesToJsonCachingScope();

            try
            {
                var data = PrepareForSaveChanges();
                if (data.Commands.Count == 0 && deferredCommandsByShard.Count == 0)
                {
                    cachingScope.Dispose();
                    return;  // nothing to do here
                }

                IncrementRequestCount();
                LogBatch(data);

                // split by shards
                var saveChangesPerShard = await GetChangesToSavePerShardAsync(data).ConfigureAwait(false);

                var saveTasks   = new Task <BatchResult[]> [saveChangesPerShard.Count];
                var saveChanges = new List <SaveChangesData>();
                // execute on all shards
                foreach (var shardAndObjects in saveChangesPerShard)
                {
                    token.ThrowIfCancellationRequested();
                    var shardId = shardAndObjects.Key;

                    IAsyncDatabaseCommands databaseCommands;
                    if (shardDbCommands.TryGetValue(shardId, out databaseCommands) == false)
                    {
                        throw new InvalidOperationException(
                                  string.Format("ShardedDocumentStore cannot found a DatabaseCommands for shard id '{0}'.", shardId));
                    }

                    var localCopy = shardAndObjects.Value;
                    saveTasks[saveChanges.Count] = databaseCommands.BatchAsync(localCopy.Commands.ToArray(), data.Options);
                    saveChanges.Add(localCopy);
                }
                await Task.WhenAll(saveTasks).ConfigureAwait(false);

                for (int index = 0; index < saveTasks.Length; index++)
                {
                    var results = await saveTasks[index].ConfigureAwait(false);
                    UpdateBatchResults(results, saveChanges[index]);
                }
            }
            catch
            {
                cachingScope.Dispose();
                throw;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Begins the async save changes operation
        /// </summary>
        /// <returns></returns>
        public async Task SaveChangesAsync(CancellationToken token = default(CancellationToken))
        {
            await asyncDocumentKeyGeneration.GenerateDocumentKeysForSaveChanges().WithCancellation(token);

            using (EntityToJson.EntitiesToJsonCachingScope())
            {
                var data = PrepareForSaveChanges();
                if (data.Commands.Count == 0)
                {
                    return;
                }

                IncrementRequestCount();

                var result = await AsyncDatabaseCommands.BatchAsync(data.Commands.ToArray(), token);

                UpdateBatchResults(result, data);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Begins the async save changes operation
        /// </summary>
        /// <returns></returns>
        public Task SaveChangesAsync()
        {
            return(asyncDocumentKeyGeneration.GenerateDocumentKeysForSaveChanges()
                   .ContinueWith(keysTask =>
            {
                keysTask.AssertNotFailed();

                var cachingScope = EntityToJson.EntitiesToJsonCachingScope();
                try
                {
                    var data = PrepareForSaveChanges();
                    if (data.Commands.Count == 0)
                    {
                        cachingScope.Dispose();
                        return new CompletedTask();
                    }

                    IncrementRequestCount();

                    return AsyncDatabaseCommands.BatchAsync(data.Commands.ToArray())
                    .ContinueWith(task =>
                    {
                        try
                        {
                            UpdateBatchResults(task.Result, data);
                        }
                        finally
                        {
                            cachingScope.Dispose();
                        }
                    });
                }
                catch
                {
                    cachingScope.Dispose();
                    throw;
                }
            }).Unwrap());
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Saves all the changes to the Raven server.
        /// </summary>
        Task IAsyncDocumentSession.SaveChangesAsync()
        {
            return(asyncDocumentKeyGeneration.GenerateDocumentKeysForSaveChanges()
                   .ContinueWith(keysTask =>
            {
                keysTask.AssertNotFailed();

                var cachingScope = EntityToJson.EntitiesToJsonCachingScope();
                try
                {
                    var data = PrepareForSaveChanges();
                    if (data.Commands.Count == 0 && deferredCommandsByShard.Count == 0)
                    {
                        cachingScope.Dispose();
                        return new CompletedTask();                                                                                          // nothing to do here
                    }

                    IncrementRequestCount();
                    LogBatch(data);

                    // split by shards
                    var saveChangesPerShard = GetChangesToSavePerShard(data);

                    var saveTasks = new List <Func <Task <BatchResult[]> > >();
                    var saveChanges = new List <SaveChangesData>();
                    // execute on all shards
                    foreach (var shardAndObjects in saveChangesPerShard)
                    {
                        var shardId = shardAndObjects.Key;

                        IAsyncDatabaseCommands databaseCommands;
                        if (shardDbCommands.TryGetValue(shardId, out databaseCommands) == false)
                        {
                            throw new InvalidOperationException(
                                string.Format("ShardedDocumentStore cannot found a DatabaseCommands for shard id '{0}'.", shardId));
                        }

                        var localCopy = shardAndObjects.Value;
                        saveChanges.Add(localCopy);
                        saveTasks.Add(() => databaseCommands.BatchAsync(localCopy.Commands.ToArray()));
                    }

                    return saveTasks.StartInParallel().ContinueWith(task =>
                    {
                        try
                        {
                            var results = task.Result;
                            for (int index = 0; index < results.Length; index++)
                            {
                                UpdateBatchResults(results[index], saveChanges[index]);
                            }
                        }
                        finally
                        {
                            cachingScope.Dispose();
                        }
                    });
                }
                catch
                {
                    cachingScope.Dispose();
                    throw;
                }
            }).Unwrap());
        }