Exemplo n.º 1
0
        public T[] LoadInternal <T>(string[] ids)
        {
            if (ids.Length == 0)
            {
                return(new T[0]);
            }

            // only load documents that aren't already cached
            var idsOfNotExistingObjects = ids.Where(id => IsLoaded(id) == false && IsDeleted(id) == false)
                                          .Distinct(StringComparer.OrdinalIgnoreCase)
                                          .ToArray();

            if (idsOfNotExistingObjects.Length > 0)
            {
                IncrementRequestCount();
                var             multiLoadOperation = new MultiLoadOperation(this, DatabaseCommands.DisableAllCaching, idsOfNotExistingObjects, null);
                MultiLoadResult multiLoadResult;
                do
                {
                    multiLoadOperation.LogOperation();
                    using (multiLoadOperation.EnterMultiLoadContext())
                    {
                        multiLoadResult = DatabaseCommands.Get(idsOfNotExistingObjects, null);
                    }
                } while (multiLoadOperation.SetResult(multiLoadResult));

                multiLoadOperation.Complete <T>();
            }

            return(ids.Select(Load <T>).ToArray());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Begins the async multi load operation
        /// </summary>
        public async Task <T[]> LoadAsyncInternal <T>(string[] ids, CancellationToken token = default(CancellationToken))
        {
            if (ids.Length == 0)
            {
                return(new T[0]);
            }

            // only load documents that aren't already cached
            var idsOfNotExistingObjects = ids.Where(id => IsLoaded(id) == false && IsDeleted(id) == false)
                                          .Distinct(StringComparer.OrdinalIgnoreCase)
                                          .ToArray();

            if (idsOfNotExistingObjects.Length > 0)
            {
                IncrementRequestCount();
                var             multiLoadOperation = new MultiLoadOperation(this, AsyncDatabaseCommands.DisableAllCaching, idsOfNotExistingObjects, null);
                MultiLoadResult multiLoadResult;
                do
                {
                    multiLoadOperation.LogOperation();
                    using (multiLoadOperation.EnterMultiLoadContext())
                    {
                        multiLoadResult = await AsyncDatabaseCommands.GetAsync(idsOfNotExistingObjects, null);
                    }
                } while (multiLoadOperation.SetResult(multiLoadResult));

                multiLoadOperation.Complete <T>();
            }

            var loadTasks  = ids.Select(async id => await LoadAsync <T>(id, token)).ToArray();
            var loadedData = await Task.WhenAll(loadTasks).WithCancellation(token);

            return(loadedData);
        }
Exemplo n.º 3
0
        public T[] LoadInternal <T>(string[] ids, KeyValuePair <string, Type>[] includes)
        {
            if (ids.Length == 0)
            {
                return(new T[0]);
            }

            if (CheckIfIdAlreadyIncluded(ids, includes))
            {
                return(ids.Select(Load <T>).ToArray());
            }
            var includePaths = includes != null?includes.Select(x => x.Key).ToArray() : null;


            IncrementRequestCount();
            var             multiLoadOperation = new MultiLoadOperation(this, DatabaseCommands.DisableAllCaching, ids, includes);
            MultiLoadResult multiLoadResult;

            do
            {
                multiLoadOperation.LogOperation();
                using (multiLoadOperation.EnterMultiLoadContext())
                {
                    multiLoadResult = DatabaseCommands.Get(ids, includePaths);
                }
            } while (multiLoadOperation.SetResult(multiLoadResult));

            return(multiLoadOperation.Complete <T>());
        }
Exemplo n.º 4
0
        public T[] LoadInternal <T>(string[] ids, string[] includes)
        {
            var results   = new T[ids.Length];
            var idsToLoad = GetIdsThatNeedLoading <T>(ids, includes);

            if (!idsToLoad.Any())
            {
                return(results);
            }

            IncrementRequestCount();

            foreach (var shard in idsToLoad)
            {
                var currentShardIds     = shard.Select(x => x.Id).ToArray();
                var multiLoadOperations = shardStrategy.ShardAccessStrategy.Apply(shard.Key, new ShardRequestData
                {
                    EntityType = typeof(T),
                    Keys       = currentShardIds.ToList()
                }, (dbCmd, i) =>
                {
                    var multiLoadOperation = new MultiLoadOperation(this, dbCmd.DisableAllCaching, currentShardIds, includes);
                    MultiLoadResult multiLoadResult;
                    do
                    {
                        multiLoadOperation.LogOperation();
                        using (multiLoadOperation.EnterMultiLoadContext())
                        {
                            multiLoadResult = dbCmd.Get(currentShardIds, includes);
                        }
                    } while (multiLoadOperation.SetResult(multiLoadResult));
                    return(multiLoadOperation);
                });
                foreach (var multiLoadOperation in multiLoadOperations)
                {
                    var loadResults = multiLoadOperation.Complete <T>();
                    for (int i = 0; i < loadResults.Length; i++)
                    {
                        if (ReferenceEquals(loadResults[i], null))
                        {
                            continue;
                        }
                        var id           = currentShardIds[i];
                        var itemPosition = Array.IndexOf(ids, id);
                        if (ReferenceEquals(results[itemPosition], default(T)) == false)
                        {
                            throw new InvalidOperationException("Found document with id: " + id +
                                                                " on more than a single shard, which is not allowed. Document keys have to be unique cluster-wide.");
                        }
                        results[itemPosition] = loadResults[i];
                    }
                }
            }
            return(ids.Select(id =>             // so we get items that were skipped because they are already in the session cache
            {
                object val;
                entitiesByKey.TryGetValue(id, out val);
                return (T)val;
            }).ToArray());
        }
Exemplo n.º 5
0
        public static T[] MoreLikeThis <T>(this ISyncAdvancedSessionOperation advancedSession, string index, MoreLikeThisQuery parameters)
        {
            if (string.IsNullOrEmpty(index))
            {
                throw new ArgumentException("Index name cannot be null or empty", "index");
            }

            parameters.IndexName = index;

            // /morelikethis/(index-name)/(ravendb-document-id)?fields=(fields)
            var cmd = advancedSession.DocumentStore.DatabaseCommands;

            var inMemoryDocumentSessionOperations = ((InMemoryDocumentSessionOperations)advancedSession);

            inMemoryDocumentSessionOperations.IncrementRequestCount();

            var             multiLoadOperation = new MultiLoadOperation(inMemoryDocumentSessionOperations, cmd.DisableAllCaching, null, null);
            MultiLoadResult multiLoadResult;

            do
            {
                multiLoadOperation.LogOperation();
                using (multiLoadOperation.EnterMultiLoadContext())
                {
                    multiLoadResult = cmd.MoreLikeThis(parameters);
                }
            } while (multiLoadOperation.SetResult(multiLoadResult));

            return(multiLoadOperation.Complete <T>());
        }
Exemplo n.º 6
0
        public static T[] MoreLikeThis <T>(this ISyncAdvancedSessionOperation advancedSession, string index, MoreLikeThisQueryParameters parameters)
        {
            var cmd = advancedSession.DocumentStore.DatabaseCommands as ServerClient;

            if (cmd == null)
            {
                throw new NotImplementedException("Embedded client isn't supported by the MoreLikeThis bundle");
            }


            var inMemoryDocumentSessionOperations = ((InMemoryDocumentSessionOperations)advancedSession);

            // /morelikethis/(index-name)/(ravendb-document-id)?fields=(fields)
            EnsureIsNotNullOrEmpty(index, "index");

            inMemoryDocumentSessionOperations.IncrementRequestCount();
            var             multiLoadOperation = new MultiLoadOperation(inMemoryDocumentSessionOperations, cmd.DisableAllCaching, null, null);
            MultiLoadResult multiLoadResult;

            do
            {
                multiLoadOperation.LogOperation();
                using (multiLoadOperation.EnterMultiLoadContext())
                {
                    var result = cmd.ExecuteGetRequest(parameters.GetRequestUri(index));

                    multiLoadResult = ((RavenJObject)result).Deserialize <MultiLoadResult>(inMemoryDocumentSessionOperations.Conventions);
                }
            } while (multiLoadOperation.SetResult(multiLoadResult));

            return(multiLoadOperation.Complete <T>());
        }
Exemplo n.º 7
0
        public static async Task <T[]> MoreLikeThisAsync <T>(this IAsyncAdvancedSessionOperations advancedSession, string index, string transformer, MoreLikeThisQuery parameters)
        {
            if (string.IsNullOrEmpty(index))
            {
                throw new ArgumentException("Index name cannot be null or empty", "index");
            }

            parameters.IndexName          = index;
            parameters.ResultsTransformer = transformer;

            // /morelikethis/(index-name)/(ravendb-document-id)?fields=(fields)
            var cmd = ((AsyncDocumentSession)advancedSession).AsyncDatabaseCommands;

            var inMemoryDocumentSessionOperations = ((InMemoryDocumentSessionOperations)advancedSession);

            inMemoryDocumentSessionOperations.IncrementRequestCount();

            var             multiLoadOperation = new MultiLoadOperation(inMemoryDocumentSessionOperations, cmd.DisableAllCaching, null, null);
            MultiLoadResult multiLoadResult;

            do
            {
                multiLoadOperation.LogOperation();
                using (multiLoadOperation.EnterMultiLoadContext())
                {
                    multiLoadResult = await cmd.MoreLikeThisAsync(parameters).ConfigureAwait(false);
                }
            } while (multiLoadOperation.SetResult(multiLoadResult));

            return(multiLoadOperation.Complete <T>());
        }
Exemplo n.º 8
0
        public T[] LoadInternal <T>(string[] ids, string[] includes)
        {
            if (ids.Length == 0)
            {
                return(new T[0]);
            }

            IncrementRequestCount();
            var idsAndShards = ids.Select(id => new
            {
                id,
                shards = GetCommandsToOperateOn(new ShardRequestData
                {
                    EntityType = typeof(T),
                    Key        = id
                })
            })
                               .GroupBy(x => x.shards, new DbCmdsListComparer());

            var results = new T[ids.Length];

            foreach (var shard in idsAndShards)
            {
                var currentShardIds     = shard.Select(x => x.id).ToArray();
                var multiLoadOperations = shardStrategy.ShardAccessStrategy.Apply(shard.Key, (dbCmd, i) =>
                {
                    var multiLoadOperation = new MultiLoadOperation(this, dbCmd.DisableAllCaching, currentShardIds);
                    MultiLoadResult multiLoadResult;
                    do
                    {
                        multiLoadOperation.LogOperation();
                        using (multiLoadOperation.EnterMultiLoadContext())
                        {
                            multiLoadResult = dbCmd.Get(currentShardIds, includes);
                        }
                    } while (multiLoadOperation.SetResult(multiLoadResult));
                    return(multiLoadOperation);
                });
                foreach (var multiLoadOperation in multiLoadOperations)
                {
                    var loadResults = multiLoadOperation.Complete <T>();
                    for (int i = 0; i < loadResults.Length; i++)
                    {
                        if (ReferenceEquals(loadResults[i], null))
                        {
                            continue;
                        }
                        var id           = currentShardIds[i];
                        var itemPosition = Array.IndexOf(ids, id);
                        if (ReferenceEquals(results[itemPosition], default(T)) == false)
                        {
                            throw new InvalidOperationException("Found document with id: " + id + " on more than a single shard, which is not allowed. Document keys have to be unique cluster-wide.");
                        }
                        results[itemPosition] = loadResults[i];
                    }
                }
            }
            return(results);
        }
        public async Task<T[]> LoadAsyncInternal<T>(string[] ids, KeyValuePair<string, Type>[] includes, CancellationToken token = default (CancellationToken))
        {
            var results = new T[ids.Length];
            var includePaths = includes != null ? includes.Select(x => x.Key).ToArray() : null;
            var idsToLoad = GetIdsThatNeedLoading<T>(ids, includePaths, transformer: null).ToList();

            if (!idsToLoad.Any())
                return results;

            IncrementRequestCount();

            foreach (var shard in idsToLoad)
            {
                var currentShardIds = shard.Select(x => x.Id).ToArray();
                var multiLoadOperations = await shardStrategy.ShardAccessStrategy.ApplyAsync(shard.Key, new ShardRequestData
                {
                    EntityType = typeof(T),
                    Keys = currentShardIds.ToList()
                }, async (dbCmd, i) =>
                {
                    var multiLoadOperation = new MultiLoadOperation(this, dbCmd.DisableAllCaching, currentShardIds, includes);
                    MultiLoadResult multiLoadResult;
                    do
                    {
                        multiLoadOperation.LogOperation();
                        using (multiLoadOperation.EnterMultiLoadContext())
                        {
                            multiLoadResult = await dbCmd.GetAsync(currentShardIds, includePaths, token: token).ConfigureAwait(false);
                        }
                    } while (multiLoadOperation.SetResult(multiLoadResult));
                    return multiLoadOperation;
                }).WithCancellation(token).ConfigureAwait(false);
                foreach (var multiLoadOperation in multiLoadOperations)
                {
                    token.ThrowIfCancellationRequested();
                    var loadResults = multiLoadOperation.Complete<T>();
                    for (int i = 0; i < loadResults.Length; i++)
                    {
                        if (ReferenceEquals(loadResults[i], null))
                            continue;
                        var id = currentShardIds[i];
                        var itemPosition = Array.IndexOf(ids, id);
                        if (ReferenceEquals(results[itemPosition], default(T)) == false)
                        {
                            throw new InvalidOperationException("Found document with id: " + id +
                                                                " on more than a single shard, which is not allowed. Document keys have to be unique cluster-wide.");
                        }
                        results[itemPosition] = loadResults[i];
                    }
                }
            }
            return ids.Select(id => // so we get items that were skipped because they are already in the session cache
            {
                object val;
                entitiesByKey.TryGetValue(id, out val);
                return (T)val;
            }).ToArray();
        }
Exemplo n.º 10
0
        /// <summary>
        /// Begins the async multi load operation
        /// </summary>
        public async Task <T[]> LoadAsyncInternal <T>(string[] ids, KeyValuePair <string, Type>[] includes)
        {
            IncrementRequestCount();
            var multiLoadOperation = new MultiLoadOperation(this, AsyncDatabaseCommands.DisableAllCaching, ids, includes);

            multiLoadOperation.LogOperation();
            var includePaths = includes != null?includes.Select(x => x.Key).ToArray() : null;

            MultiLoadResult result;

            do
            {
                multiLoadOperation.LogOperation();
                using (multiLoadOperation.EnterMultiLoadContext())
                {
                    result = await AsyncDatabaseCommands.GetAsync(ids, includePaths);
                }
            } while (multiLoadOperation.SetResult(result));
            return(multiLoadOperation.Complete <T>());
        }
Exemplo n.º 11
0
 private Task <T[]> LoadAsyncInternal <T>(string[] ids, string[] includes, MultiLoadOperation multiLoadOperation)
 {
     multiLoadOperation.LogOperation();
     using (multiLoadOperation.EnterMultiLoadContext())
     {
         return(AsyncDatabaseCommands.GetAsync(ids, includes)
                .ContinueWith(t =>
         {
             if (multiLoadOperation.SetResult(t.Result) == false)
             {
                 return Task.Factory.StartNew(() => multiLoadOperation.Complete <T>());
             }
             return LoadAsyncInternal <T>(ids, includes, multiLoadOperation);
         })
                .Unwrap());
     }
 }
Exemplo n.º 12
0
        internal T[] LoadInternal <T>(string[] ids, string[] includes)
        {
            if (ids.Length == 0)
            {
                return(new T[0]);
            }

            IncrementRequestCount();
            var             multiLoadOperation = new MultiLoadOperation(this, DatabaseCommands.DisableAllCaching, ids);
            MultiLoadResult multiLoadResult;

            do
            {
                multiLoadOperation.LogOperation();
                using (multiLoadOperation.EnterMultiLoadContext())
                {
                    multiLoadResult = DatabaseCommands.Get(ids, includes);
                }
            } while (multiLoadOperation.SetResult(multiLoadResult));

            return(multiLoadOperation.Complete <T>());
        }
Exemplo n.º 13
0
        public Task <T[]> LoadAsyncInternal <T>(string[] ids, string[] includes)
        {
            var results   = new T[ids.Length];
            var idsToLoad = GetIdsThatNeedLoading <T>(ids, includes);

            if (!idsToLoad.Any())
            {
                return(CompletedTask.With(new T[ids.Length]));
            }

            IncrementRequestCount();

            var loadTasks = idsToLoad.Select(shardsAndIds => (Func <Task>)(() =>
            {
                var shards = shardsAndIds.Key;
                var idsForCurrentShards = shardsAndIds.Select(x => x.Id).ToArray();

                var multiLoadOperations = shardStrategy.ShardAccessStrategy.ApplyAsync(shards, new ShardRequestData
                {
                    EntityType = typeof(T),
                    Keys       = idsForCurrentShards
                }, (commands, i) =>
                {
                    var multiLoadOperation = new MultiLoadOperation(this, commands.DisableAllCaching, idsForCurrentShards, includes);

                    Func <Task <MultiLoadOperation> > executer = null;
                    executer = () =>
                    {
                        multiLoadOperation.LogOperation();

                        IDisposable loadContext = multiLoadOperation.EnterMultiLoadContext();
                        return(commands.GetAsync(idsForCurrentShards, includes).ContinueWith(task =>
                        {
                            loadContext.Dispose();

                            if (multiLoadOperation.SetResult(task.Result))
                            {
                                return executer();
                            }
                            return CompletedTask.With(multiLoadOperation);
                        }).Unwrap());
                    };

                    return(executer());
                });

                return(multiLoadOperations.ContinueWith(task =>
                {
                    foreach (var loadResults in task.Result.Select(multiLoadOperation => multiLoadOperation.Complete <T>()))
                    {
                        for (int i = 0; i < loadResults.Length; i++)
                        {
                            if (ReferenceEquals(loadResults[i], null))
                            {
                                continue;
                            }
                            var id = idsForCurrentShards[i];
                            var itemPosition = Array.IndexOf(ids, id);
                            if (ReferenceEquals(results[itemPosition], default(T)) == false)
                            {
                                throw new InvalidOperationException("Found document with id: " + id + " on more than a single shard, which is not allowed. Document keys have to be unique cluster-wide.");
                            }
                            results[itemPosition] = loadResults[i];
                        }
                    }
                }));
            }));

            return(loadTasks.StartInParallel().WithResult(() => results));
        }