/// <summary> /// Begins the async multi load operation /// </summary> public async Task <T[]> LoadAsyncInternal <T>(string[] ids, KeyValuePair <string, Type>[] includes, CancellationToken token = default(CancellationToken)) { if (CheckIfIdAlreadyIncluded(ids, includes)) { var loadTasks = ids.Select(id => LoadAsync <T>(id, token)).ToArray(); var loadedData = await Task.WhenAll(loadTasks).WithCancellation(token); return(loadedData); } 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, token : token).ConfigureAwait(false); } } while (multiLoadOperation.SetResult(result)); return(multiLoadOperation.Complete <T>()); }
/// <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); }
public async Task <T[]> LoadAsyncInternal <T>(string[] ids, KeyValuePair <string, Type>[] includes, string transformer, Dictionary <string, RavenJToken> queryInputs = null) { if (ids.Length == 0) { return(new T[0]); } IncrementRequestCount(); var includePaths = includes != null?includes.Select(x => x.Key).ToArray() : null; if (typeof(T).IsArray) { // Returns array of arrays, public APIs don't surface that yet though as we only support Transform // With a single Id var arrayOfArrays = (await AsyncDatabaseCommands.GetAsync(ids, includePaths, transformer, queryInputs)) .Results .Select(x => x.Value <RavenJArray>("$values").Cast <RavenJObject>()) .Select(values => { var array = values.Select(y => { HandleInternalMetadata(y); return(ConvertToEntity <T>(null, y, new RavenJObject())); }).ToArray(); var newArray = Array.CreateInstance(typeof(T).GetElementType(), array.Length); Array.Copy(array, newArray, array.Length); return(newArray); }) .Cast <T>() .ToArray(); return(arrayOfArrays); } var items = (await this.AsyncDatabaseCommands.GetAsync(ids, includePaths, transformer, queryInputs)) .Results .SelectMany(x => x.Value <RavenJArray>("$values").ToArray()) .Select(JsonExtensions.ToJObject) .Select(x => { this.HandleInternalMetadata(x); return(this.ConvertToEntity <T>(null, x, new RavenJObject())); }) .Cast <T>() .ToArray(); if (items.Length > ids.Length) { throw new InvalidOperationException(String.Format("A load was attempted with transformer {0}, and more than one item was returned per entity - please use {1}[] as the projection type instead of {1}", transformer, typeof(T).Name)); } return(items); }
/// <summary> /// Get the json document by key from the store /// </summary> private async Task <JsonDocument> GetJsonDocumentAsync(string documentKey) { var jsonDocument = await AsyncDatabaseCommands.GetAsync(documentKey); if (jsonDocument == null) { throw new InvalidOperationException("Document '" + documentKey + "' no longer exists and was probably deleted"); } return(jsonDocument); }
public async Task RefreshAsync <T>(T entity, CancellationToken token = default(CancellationToken)) { DocumentMetadata value; if (entitiesAndMetadata.TryGetValue(entity, out value) == false) { throw new InvalidOperationException("Cannot refresh a transient instance"); } IncrementRequestCount(); var jsonDocument = await AsyncDatabaseCommands.GetAsync(value.Key, token); RefreshInternal(entity, jsonDocument, value); }
private async Task <T> CompleteLoadAsync <T>(string id, LoadOperation loadOperation, CancellationToken token = default(CancellationToken)) { loadOperation.LogOperation(); using (loadOperation.EnterLoadContext()) { var result = await AsyncDatabaseCommands.GetAsync(id, token); if (loadOperation.SetResult(result) == false) { return(loadOperation.Complete <T>()); } return(await CompleteLoadAsync <T>(id, loadOperation, token).WithCancellation(token)); } }
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()); } }
private Task <T> CompleteLoadAsync <T>(string id, LoadOperation loadOperation) { loadOperation.LogOperation(); using (loadOperation.EnterLoadContext()) { return(AsyncDatabaseCommands.GetAsync(id) .ContinueWith(task => { if (loadOperation.SetResult(task.Result) == false) { return Task.Factory.StartNew(() => loadOperation.Complete <T>()); } return CompleteLoadAsync <T>(id, loadOperation); }) .Unwrap()); } }
public async Task <T[]> LoadUsingTransformerInternalAsync <T>(string[] ids, KeyValuePair <string, Type>[] includes, string transformer, Dictionary <string, RavenJToken> transformerParameters = null, CancellationToken token = default(CancellationToken)) { if (transformer == null) { throw new ArgumentNullException("transformer"); } if (ids.Length == 0) { return(new T[0]); } IncrementRequestCount(); var includeNames = includes != null?includes.Select(x => x.Key).ToArray() : new string[0]; var multiLoadResult = await AsyncDatabaseCommands.GetAsync(ids, includeNames, transformer, transformerParameters, token : token); return(new LoadTransformerOperation(this, transformer, ids).Complete <T>(multiLoadResult)); }
/// <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>()); }
/// <summary> /// Begins the async load operation /// </summary> /// <param name="id">The id.</param> /// <returns></returns> public Task <T> LoadAsync <T>(string id) { object entity; if (entitiesByKey.TryGetValue(id, out entity)) { var tcs = new TaskCompletionSource <T>(); tcs.TrySetResult((T)entity); return(tcs.Task); } IncrementRequestCount(); return(AsyncDatabaseCommands.GetAsync(id) .ContinueWith(task => { JsonDocument documentFound; try { documentFound = task.Result; } catch (WebException ex) { var httpWebResponse = ex.Response as HttpWebResponse; if (httpWebResponse != null && httpWebResponse.StatusCode == HttpStatusCode.NotFound) { return default(T); } throw; } if (documentFound == null) { return default(T); } return TrackEntity <T>(documentFound); })); }