public ResponseResult<FeedModel> LoadActivities(string feedId, FeedOptions feedOptions) { feedOptions = feedOptions ?? FeedOptions.Default; var feedIdBytes = Encoding.UTF8.GetBytes(feedId); var feed = ActivityStreams.Api.WebApiApplication.FeedFactory.GG(feedIdBytes); if (feed == null) return new ResponseResult<FeedModel>(); var activities = WebApiApplication.ActivityRepository.Load(feed, feedOptions); return new ResponseResult<FeedModel>(new FeedModel(activities)); }
public IEnumerable<Message> GetMessages(Guid categoryId, int pageIndex, int pageSize) { var options = new FeedOptions() { MaxItemCount = pageSize }; return Client.CreateDocumentQuery<ResourceEntity<Message>>(Documents.DocumentsLink, options) .Where(x => x.Object.CategoryId == categoryId) .OrderByDescending(x => x.Object.CreatedAt) .Select(x => x.Object) .AsEnumerable(); }
public IEnumerable<Activity> Get(Feed feed, FeedOptions feedOptions) { feedOptions = feedOptions ?? FeedOptions.Default; var statement = LoadActivityStreamQueryTemplateDesc; SortedSet<Activity> activities = new SortedSet<Activity>(Activity.ComparerDesc); var sortOrder = feedOptions.SortOrder; var paging = feedOptions.Paging; if (sortOrder == SortOrder.Ascending) { statement = LoadActivityStreamQueryTemplateAsc; activities = new SortedSet<Activity>(Activity.ComparerAsc); } foreach (var streamId in feed.Streams) { var streamIdQuery = Convert.ToBase64String(streamId.StreamId); var prepared = session .Prepare(statement) .Bind(streamIdQuery, paging.Timestamp) .SetAutoPage(false) .SetPageSize(paging.Take); var rowSet = session.Execute(prepared); foreach (var row in rowSet.GetRows()) { using (var stream = new MemoryStream(row.GetValue<byte[]>("data"))) { var storedActivity = (Activity)serializer.Deserialize(stream); activities.Add(storedActivity); } } } return activities.Take(paging.Take); }
/// <summary> /// FanIn /// </summary> public IEnumerable<Activity> Load(Feed feed, FeedOptions feedOptions) { feedOptions = feedOptions ?? FeedOptions.Default; var snapshot = new Dictionary<byte[], Queue<Activity>>(activityStreamStore.Count, new ByteArrayEqualityComparer()); foreach (var item in activityStreamStore) { snapshot.Add(item.Key, new Queue<Activity>(item.Value)); } SortedSet<Activity> buffer = new SortedSet<Activity>(Activity.ComparerDesc); var streams = feed.Streams.ToList(); var streamsCount = streams.Count; var snapshotCount = snapshot.Count; // Init for (int streamIndexInsideSubsciption = 0; streamIndexInsideSubsciption < streamsCount; streamIndexInsideSubsciption++) { if (snapshotCount <= streamIndexInsideSubsciption) break; var streamId = streams[streamIndexInsideSubsciption]; var activity = snapshot[streamId.StreamId].Dequeue(); buffer.Add(activity); } while (buffer.Count > 0) { Activity nextActivity = buffer.FirstOrDefault(); buffer.Remove(nextActivity); var streamQueue = snapshot[nextActivity.StreamId]; if (streamQueue.Count > 0) { var candidate = snapshot[nextActivity.StreamId].Dequeue(); buffer.Add(candidate); } yield return nextActivity; } }
/// <summary> /// This method uses a ReadCollectionsFeedAsync method to read a list of all collections on a database. /// It demonstrates a pattern for how to control paging and deal with continuations /// This should not be needed for reading a list of collections as there are unlikely to be many hundred, /// but this same pattern is introduced here and can be used on other ReadFeed methods. /// </summary> /// <returns>A List of DocuemntCollection entities</returns> private static async Task<List<DocumentCollection>> ReadCollectionsFeedAsync(string databaseSelfLink) { string continuation = null; List<DocumentCollection> collections = new List<DocumentCollection>(); do { FeedOptions options = new FeedOptions { RequestContinuation = continuation, MaxItemCount = 50 }; FeedResponse<DocumentCollection> response = (FeedResponse<DocumentCollection>) await client.ReadDocumentCollectionFeedAsync(databaseSelfLink, options); foreach (DocumentCollection col in response) { collections.Add(col); } continuation = response.ResponseContinuation; } while (!String.IsNullOrEmpty(continuation)); return collections; }
private async Task<bool> Restore(PublishRoot root, PublishProject publishProject, string restoreDirectory, IEnumerable<FrameworkName> targetFrameworks) { var appEnv = PlatformServices.Default.Application; var feedOptions = new FeedOptions(); feedOptions.IgnoreFailedSources = true; feedOptions.Sources.Add(root.TargetPackagesPath); feedOptions.TargetPackagesFolder = root.TargetPackagesPath; var restoreCommand = new RestoreCommand(appEnv); restoreCommand.TargetFrameworks.AddRange(targetFrameworks); restoreCommand.RequestedRuntimes = root.RuntimeIdentifiers; restoreCommand.SkipRestoreEvents = true; restoreCommand.SkipInstall = true; // This is a workaround for #1322. Since we use restore to generate the lock file // after publish, it's possible to fail restore after copying the closure // if framework assemblies and packages have the same name. This is more likely now // since dependencies may exist in the top level restoreCommand.IgnoreMissingDependencies = true; restoreCommand.CheckHashFile = false; restoreCommand.RestoreDirectories.Add(restoreDirectory); restoreCommand.FeedOptions = feedOptions; // Mute "dnu restore" subcommand restoreCommand.Reports = Reports.Constants.NullReports; var success = await restoreCommand.Execute(); return success; }
private static async Task QueryWithOrderByForPartitionedCollectionAsync(Uri collectionUri) { // The .NET client automatically iterates through all the pages of query results // Developers can explicitly control paging by creating an IDocumentQueryable // using the IQueryable object, then by reading the ResponseContinuationToken values // and passing them back as RequestContinuationToken in FeedOptions. List<Family> familiesSerial = new List<Family>(); String queryText = "SELECT * FROM Families order by Families.LastName"; // 0 maximum parallel tasks, effectively serial execution FeedOptions options = new FeedOptions { MaxDegreeOfParallelism = 0, MaxBufferedItemCount = 100, EnableCrossPartitionQuery = true }; var query = client.CreateDocumentQuery<Family>(collectionUri, queryText, options).AsDocumentQuery(); while (query.HasMoreResults) { foreach (Family family in await query.ExecuteNextAsync()) { familiesSerial.Add(family); } } Assert("Order By Query expected two families", familiesSerial.ToList().Count == 2); // 1 maximum parallel tasks, 1 dedicated asynchrousnous task to continuously make REST calls List<Family> familiesParallel1 = new List<Family>(); options = new FeedOptions { MaxDegreeOfParallelism = 1, MaxBufferedItemCount = 100, EnableCrossPartitionQuery = true }; // using AsDocumentQuery you get access to whether or not the query HasMoreResults // If it does, just call ExecuteNextAsync until there are no more results // No need to supply a continuation token here as the server keeps track of progress query = client.CreateDocumentQuery<Family>(collectionUri, queryText, options).AsDocumentQuery(); while (query.HasMoreResults) { foreach (Family family in await query.ExecuteNextAsync()) { familiesParallel1.Add(family); } } Assert("Order By Query expected two families", familiesParallel1.ToList().Count == 2); AssertSequenceEqual("Parallel query returns result out of order compared to serial execution", familiesSerial, familiesParallel1); // 10 maximum parallel tasks, a maximum of 10 dedicated asynchrousnous tasks to continuously make REST calls List<Family> familiesParallel10 = new List<Family>(); options = new FeedOptions { MaxDegreeOfParallelism = 10, MaxBufferedItemCount = 100, EnableCrossPartitionQuery = true }; query = client.CreateDocumentQuery<Family>(collectionUri, queryText, options).AsDocumentQuery(); while (query.HasMoreResults) { foreach (Family family in await query.ExecuteNextAsync()) { familiesParallel10.Add(family); } } Assert("Order By Query expected two families", familiesParallel10.ToList().Count == 2); AssertSequenceEqual("Parallel query returns result out of order compared to serial execution", familiesSerial, familiesParallel10); }
public static async Task Main(string[] args) { // Task 2 //using (DocumentClient client = new DocumentClient(_endpointUri, _primaryKey)) //{ // Uri collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId); // string sql = "SELECT TOP 5 VALUE s.studentAlias FROM coll s WHERE s.enrollmentYear = 2018 ORDER BY s.studentAlias"; // IQueryable<string> query = client.CreateDocumentQuery<string>(collectionLink, new SqlQuerySpec(sql)); // foreach (string alias in query) // { // await Console.Out.WriteLineAsync(alias); // } // Console.Read(); //} // Task 3 //using (DocumentClient client = new DocumentClient(_endpointUri, _primaryKey)) //{ // Uri collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId); // //string sql = "SELECT s.clubs FROM students s WHERE s.enrollmentYear = 2018"; // string sql = "SELECT s.clubs FROM students s WHERE s.enrollmentYear = 2018"; // IQueryable<Student> query = client.CreateDocumentQuery<Student>(collectionLink, new SqlQuerySpec(sql)); // foreach (Student student in query) // foreach (string club in student.Clubs) // { // await Console.Out.WriteLineAsync(club); // } // Console.Read(); //} //// Point 27 //using (DocumentClient client = new DocumentClient(_endpointUri, _primaryKey)) //{ // Uri collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId); // //string sql = "SELECT s.clubs FROM students s WHERE s.enrollmentYear = 2018"; // string sql = "SELECT activity FROM students s JOIN activity IN s.clubs WHERE s.enrollmentYear = 2018"; // IQueryable<StudentActivity> query = client.CreateDocumentQuery<StudentActivity>(collectionLink, new SqlQuerySpec(sql)); // foreach (StudentActivity studentActivity in query) // { // await Console.Out.WriteLineAsync(studentActivity.Activity); // } // Console.Read(); //} ////// Point 40 //using (DocumentClient client = new DocumentClient(_endpointUri, _primaryKey)) //{ // Uri collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId); // string sql = "SELECT VALUE activity FROM students s JOIN activity IN s.clubs WHERE s.enrollmentYear = 2018"; // IQueryable<string> query = client.CreateDocumentQuery<string>(collectionLink, new SqlQuerySpec(sql)); // foreach (string activity in query) // { // await Console.Out.WriteLineAsync(activity); // } // Console.Read(); //} //// Task IV: Projecting Query Results //using (DocumentClient client = new DocumentClient(_endpointUri, _primaryKey)) //{ // Uri collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId); // string sql = // "SELECT VALUE { 'id': s.id, 'name': CONCAT(s.firstName, ' ', s.lastName), 'email': { 'home': s.homeEmailAddress, 'school': CONCAT(s.studentAlias, '@contoso.edu') } } FROM students s WHERE s.enrollmentYear = 2018"; // IQueryable<StudentProfile> query = client.CreateDocumentQuery<StudentProfile>(collectionLink, new SqlQuerySpec(sql)); // foreach (StudentProfile profile in query) // { // await Console.Out.WriteLineAsync($"[{profile.Id}]\t{profile.Name,-20}\t{profile.Email.School,-50}\t{profile.Email.Home}"); // } //} //// Exercise 4: Implement Pagination using the .NET SDK // using (DocumentClient client = new DocumentClient(_endpointUri, _primaryKey)) // { // Uri collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId); // string sql = // "SELECT VALUE { 'id': s.id, 'name': CONCAT(s.firstName, ' ', s.lastName), 'email': { 'home': s.homeEmailAddress, 'school': CONCAT(s.studentAlias, '@contoso.edu') } } FROM students s WHERE s.enrollmentYear = 2018"; // IDocumentQuery<StudentProfile> query = client.CreateDocumentQuery<StudentProfile>(collectionLink, new SqlQuerySpec(sql), new FeedOptions { MaxItemCount = 100 }).AsDocumentQuery(); // int pageCount = 0; // while (query.HasMoreResults) // { // await Console.Out.WriteLineAsync($"---Page #{++pageCount:0000}---"); // foreach (StudentProfile profile in await query.ExecuteNextAsync()) // { // await Console.Out.WriteLineAsync($"\t[{profile.Id}]\t{profile.Name,-20}\t{profile.Email.School,-50}\t{profile.Email.Home}"); // } // } // Console.Read(); // } //Exercise 5: Implement Cross-Partition Queries //Task 1 //using (DocumentClient client = new DocumentClient(_endpointUri, _primaryKey)) //{ // await client.OpenAsync(); // Uri collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId); // IEnumerable<Student> query = client // .CreateDocumentQuery<Student>(collectionLink, new FeedOptions { PartitionKey = new PartitionKey(2016) }) // .Where(student => student.projectedGraduationYear == 2020); // foreach (Student student in query) // { // Console.Out.WriteLine($"Enrolled: {student.enrollmentYear}\tGraduation: {student.projectedGraduationYear}\t{student.studentAlias}"); // } //} //Task 2 Execute Cross-Partition Query //using (DocumentClient client = new DocumentClient(_endpointUri, _primaryKey)) //{ // await client.OpenAsync(); // Uri collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId); // IEnumerable<Student> query = client // .CreateDocumentQuery<Student>(collectionLink, new FeedOptions { EnableCrossPartitionQuery = true }) // .Where(student => student.projectedGraduationYear == 2020); // foreach (Student student in query) // { // Console.Out.WriteLine($"Enrolled: {student.enrollmentYear}\tGraduation: {student.projectedGraduationYear}\t{student.studentAlias}"); // } // Console.Read(); //} //// Task 3 Implement Continuation Token //using (DocumentClient client = new DocumentClient(_endpointUri, _primaryKey)) //{ // await client.OpenAsync(); // Uri collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId); // string continuationToken = String.Empty; // do // { // FeedOptions options = new FeedOptions // { // EnableCrossPartitionQuery = true, // RequestContinuation = continuationToken // }; // IDocumentQuery<Student> query = client // .CreateDocumentQuery<Student>(collectionLink, options) // .Where(student => student.age < 18) // .AsDocumentQuery(); // FeedResponse<Student> results = await query.ExecuteNextAsync<Student>(); // continuationToken = results.ResponseContinuation; // await Console.Out.WriteLineAsync($"ContinuationToken:\t{continuationToken}"); // foreach (Student result in results) // { // await Console.Out.WriteLineAsync($"[Age: {result.age}]\t{result.studentAlias}@consoto.edu"); // } // await Console.Out.WriteLineAsync(); // } // while (!String.IsNullOrEmpty(continuationToken)); //// Task IV Observe How Partitions Are Accessed in a Cross-Partition Query //using (DocumentClient client = new DocumentClient(_endpointUri, _primaryKey)) //{ // await client.OpenAsync(); // Uri collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId); // FeedOptions options = new FeedOptions // { // EnableCrossPartitionQuery = true // }; // string sql = "SELECT * FROM students s WHERE s.academicStatus.suspension = true"; // IDocumentQuery<Student> query = client // .CreateDocumentQuery<Student>(collectionLink, sql, options) // .AsDocumentQuery(); // int pageCount = 0; // while (query.HasMoreResults) // { // await Console.Out.WriteLineAsync($"---Page #{++pageCount:0000}---"); // foreach (Student result in await query.ExecuteNextAsync()) // { // await Console.Out.WriteLineAsync($"Enrollment: {result.enrollmentYear}\tBalance: {result.financialData.tuitionBalance}\t{result.studentAlias}@consoto.edu"); // } // } //} //// Point 11 using (DocumentClient client = new DocumentClient(_endpointUri, _primaryKey)) { await client.OpenAsync(); Uri collectionLink = UriFactory.CreateDocumentCollectionUri(_databaseId, _collectionId); FeedOptions options = new FeedOptions { EnableCrossPartitionQuery = true }; //Point 11 //string sql = "SELECT * FROM students s WHERE s.financialData.tuitionBalance > 14000"; //Point 17 //string sql = "SELECT * FROM students s WHERE s.financialData.tuitionBalance > 14950"; //Point 23 //string sql = "SELECT * FROM students s WHERE s.financialData.tuitionBalance > 14996"; //Point 29 string sql = "SELECT * FROM students s WHERE s.financialData.tuitionBalance > 14998"; IDocumentQuery <Student> query = client .CreateDocumentQuery <Student>(collectionLink, sql, options) .AsDocumentQuery(); int pageCount = 0; while (query.HasMoreResults) { await Console.Out.WriteLineAsync($"---Page #{++pageCount:0000}---"); foreach (Student result in await query.ExecuteNextAsync()) { await Console.Out.WriteLineAsync($"Enrollment: {result.enrollmentYear}\tBalance: {result.financialData.tuitionBalance}\t{result.studentAlias}@consoto.edu"); } } } }
private static int ShowQueryIsAllowed(DocumentCollection collection, string query, FeedOptions options = null) { return client.CreateDocumentQuery(collection.SelfLink, query, options).AsEnumerable().Count(); }
private async Task<IList<LeaderboardEntryContract<PhotoContract>>> GetHighestNetWorthPhotos(int count) { var feedOptions = new FeedOptions { MaxItemCount = count }; var photoQuery = _documentClient.CreateDocumentQuery<PhotoDocument>(DocumentCollectionUri, feedOptions) .Where(d => d.DocumentType == PhotoDocument.DocumentTypeIdentifier) .Where(d => d.DocumentVersion == _currentDocumentVersion) .Where(p => p.Status == PhotoStatus.Active) .OrderByDescending(p => p.GoldCount) .AsDocumentQuery(); var photoDocumentResponse = await photoQuery.ExecuteNextAsync<PhotoDocument>(); var photoContracts = await CreatePhotoContractsAndLoadUserData(photoDocumentResponse.ToList()); var rank = 1; var mostGoldPhotos = photoContracts.Select(p => new LeaderboardEntryContract<PhotoContract> { Model = p, Value = p.NumberOfGoldVotes, Rank = rank++ }).ToList(); return mostGoldPhotos; }
private static bool IsParallelQuery(FeedOptions feedOptions) { return(feedOptions.MaxDegreeOfParallelism != 0); }
private async Task<bool> Restore(PublishRoot root, PublishProject publishProject, string restoreDirectory) { var appEnv = (IApplicationEnvironment)root.HostServices.GetService(typeof(IApplicationEnvironment)); var feedOptions = new FeedOptions(); feedOptions.IgnoreFailedSources = true; feedOptions.Sources.Add(root.TargetPackagesPath); feedOptions.TargetPackagesFolder = root.TargetPackagesPath; var restoreCommand = new RestoreCommand(appEnv); foreach (var framework in root.Frameworks) { restoreCommand.TargetFrameworks.Add(framework.Key); } restoreCommand.SkipRestoreEvents = true; restoreCommand.SkipInstall = true; // This is a workaround for #1322. Since we use restore to generate the lock file // after publish, it's possible to fail restore after copying the closure // if framework assemblies and packages have the same name. This is more likely now // since dependencies may exist in the top level restoreCommand.IgnoreMissingDependencies = true; restoreCommand.CheckHashFile = false; restoreCommand.RestoreDirectories.Add(restoreDirectory); restoreCommand.FeedOptions = feedOptions; // Mute "dnu restore" subcommand restoreCommand.Reports = Reports.Constants.NullReports; var success = await restoreCommand.Execute(); return success; }
public IQueryable <ReadOnlyDummy> CreateQueryWithFeedOptions() { FeedOptions = new FeedOptions(); return(ReadOnlyDocumentRepository.CreateQuery(FeedOptions)); }
public static async Task <IDocumentQueryExecutionContext> CreateDocumentQueryExecutionContextAsync( IDocumentQueryClient client, ResourceType resourceTypeEnum, Type resourceType, Expression expression, FeedOptions feedOptions, string resourceLink, bool isContinuationExpected, CancellationToken token, Guid correlatedActivityId) { CosmosContainerSettings collection = null; if (resourceTypeEnum.IsCollectionChild()) { CollectionCache collectionCache = await client.GetCollectionCacheAsync(); using ( DocumentServiceRequest request = DocumentServiceRequest.Create( OperationType.Query, resourceTypeEnum, resourceLink, AuthorizationTokenType.Invalid)) //this request doesnt actually go to server { collection = await collectionCache.ResolveCollectionAsync(request, token); } if (feedOptions != null && feedOptions.PartitionKey != null && feedOptions.PartitionKey.Equals(PartitionKey.None)) { feedOptions.PartitionKey = PartitionKey.FromInternalKey(collection.GetNoneValue()); } } DocumentQueryExecutionContextBase.InitParams constructorParams = new DocumentQueryExecutionContextBase.InitParams( client, resourceTypeEnum, resourceType, expression, feedOptions, resourceLink, false, correlatedActivityId); // For non-Windows platforms(like Linux and OSX) in .NET Core SDK, we cannot use ServiceInterop, so need to bypass in that case. // We are also now bypassing this for 32 bit host process running even on Windows as there are many 32 bit apps that will not work without this if (CustomTypeExtensions.ByPassQueryParsing()) { // We create a ProxyDocumentQueryExecutionContext that will be initialized with DefaultDocumentQueryExecutionContext // which will be used to send the query to Gateway and on getting 400(bad request) with 1004(cross partition query not servable), we initialize it with // PipelinedDocumentQueryExecutionContext by providing the partition query execution info that's needed(which we get from the exception returned from Gateway). ProxyDocumentQueryExecutionContext proxyQueryExecutionContext = ProxyDocumentQueryExecutionContext.CreateAsync( client, resourceTypeEnum, resourceType, expression, feedOptions, resourceLink, token, collection, isContinuationExpected, correlatedActivityId); return(proxyQueryExecutionContext); } DefaultDocumentQueryExecutionContext queryExecutionContext = await DefaultDocumentQueryExecutionContext.CreateAsync( constructorParams, isContinuationExpected, token); // If isContinuationExpected is false, we want to check if there are aggregates. if ( resourceTypeEnum.IsCollectionChild() && resourceTypeEnum.IsPartitioned() && (feedOptions.EnableCrossPartitionQuery || !isContinuationExpected)) { //todo:elasticcollections this may rely on information from collection cache which is outdated //if collection is deleted/created with same name. //need to make it not rely on information from collection cache. PartitionedQueryExecutionInfo partitionedQueryExecutionInfo = await queryExecutionContext.GetPartitionedQueryExecutionInfoAsync( partitionKeyDefinition : collection.PartitionKey, requireFormattableOrderByQuery : true, isContinuationExpected : isContinuationExpected, allowNonValueAggregateQuery : false, cancellationToken : token); if (DocumentQueryExecutionContextFactory.ShouldCreateSpecializedDocumentQueryExecutionContext( resourceTypeEnum, feedOptions, partitionedQueryExecutionInfo, collection.PartitionKey, isContinuationExpected)) { List <PartitionKeyRange> targetRanges = await GetTargetPartitionKeyRanges( queryExecutionContext, partitionedQueryExecutionInfo, collection, feedOptions); return(await CreateSpecializedDocumentQueryExecutionContext( constructorParams, partitionedQueryExecutionInfo, targetRanges, collection.ResourceId, isContinuationExpected, token)); } } return(queryExecutionContext); }
public async Task <AccountDetailsViewModel> Handle(GetAccountDetailsQuery request, CancellationToken cancellationToken) { //========================================================================== // PRE QUERY CHECKLIST //========================================================================== // 1. CACHING: Check if item exists in cache. // a. Use MediatR, Caching Library or Caching Routine within Accounts // // NOTE: Redis Multiplexer is already setup in our DI container using IRedisContext //-------------------------------------------------------------------------- // Create the query string sqlQuery = "SELECT * FROM Documents d WHERE d.NameKey ='" + Common.Transformations.NameKey.Transform(request.NameKey) + "'"; var sqlSpec = new SqlQuerySpec { QueryText = sqlQuery }; // Generate collection uri Uri collectionUri = UriFactory.CreateDocumentCollectionUri(_documentContext.Settings.Database, _documentContext.Settings.Collection); // Generate FeedOptions/ParitionKey var feedOptions = new FeedOptions { PartitionKey = new PartitionKey(Common.Constants.DocumentType.Account()) }; // Run query against the document store var result = _documentContext.Client.CreateDocumentQuery <AccountDocumentModel>( collectionUri, sqlSpec, feedOptions ); AccountDocumentModel accountDocumentModel; try { accountDocumentModel = result.AsEnumerable().FirstOrDefault(); } catch (Exception ex) { // throw AzureCosmoDBException (if a custom exception type is desired) // ... Will be caught, logged and handled by the ExceptionHandlerMiddleware // ...Or pass along as inner exception: throw new Exception("An error occured trying to use the document store", ex); } // Create our ViewModel and transform our document model var accountViewModel = new AccountDetailsViewModel(); //========================================================================== // POST QUERY CHECKLIST //========================================================================== // 1. CACHING: Update results in cache. // a. Use MediatR, Caching Library or Caching Routine within Accounts // // NOTE: Redis Multiplexer is already setup in our DI container using IRedisContext //-------------------------------------------------------------------------- // TODO: Check user role to include data for the view to use accountViewModel.DeleteEnabled = true; accountViewModel.EditEnabled = true; if (accountDocumentModel != null) { //Use AutoMapper to transform DocumentModel into Domain Model (Configure via Core.Startup.AutoMapperConfiguration) var account = AutoMapper.Mapper.Map <Account>(accountDocumentModel); accountViewModel.Account = account; } return(accountViewModel); }
/// <summary> /// Lists the databases within a collection. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queryOptions"></param> /// <returns></returns> public async Task <FeedResponse <Microsoft.Azure.Documents.Database> > ListDatabasesAsync <T>(FeedOptions queryOptions = null) { // DISCUSS: Rename ReadFeed to List? No strong opinion - this matches Tables. throw new NotImplementedException(); }
public Task <int> CountAsync(Expression <Func <T, bool> > predicate = null, Func <IQueryable <T>, IQueryable <T> > clauses = null, FeedOptions feedOptions = null) { var failure = _countExceptionConditions.Select(func => func()).FirstOrDefault(); if (failure != default) { return(Task.FromException <int>(failure)); } IEnumerable <T> entities; lock (_entities) { entities = _entities.Select(i => i.Entity).ToArray(); } if (predicate != default) { entities = entities.Where(predicate.Compile()); } if (clauses != default) { entities = clauses.Invoke(entities.AsQueryable()); } return(Task.FromResult(entities.Count())); }
public async Task <IList <T> > FindAsync(Expression <Func <T, bool> > predicate = null, Func <IQueryable <T>, IQueryable <T> > clauses = null, FeedOptions feedOptions = null) { var result = await FindAsync(feedOptions?.MaxItemCount ?? 0, null, predicate, clauses, feedOptions); return(result.Items); }
public EntityListQueryResult <T> List <T>(Expression <Func <T, bool> > where, string sortBy, int page, int pageSize) where T : Entity { // we want to inject the entity type into the predicate so we ensure to only return records of the correct type where = InjectEntityTypeFilter(where); // create a query for T that includes the sorting and filtering var updatedQuery = _documentClient.CreateDocumentQuery <T>(_collectionUri) .Where(where); // split the sort string var listSort = sortBy.Split(','); // loop through the sorting options and create a sort expression string from them var firstIteration = true; foreach (var sortOption in listSort) { // NOTE: DocumentDb only supports sorting by one property currently // TODO: Once DocumentDb supports sorting by multiple properties we can remove this restriction if (!firstIteration) { break; } string propertyName; string methodName; // if the sort option starts with "-" we order descending, otherwise ascending if (sortOption.StartsWith("-", StringComparison.Ordinal)) { propertyName = sortOption.Remove(0, 1).ToPascalCase(); methodName = "OrderByDescending"; } else { propertyName = sortOption.ToPascalCase(); methodName = "OrderBy"; } firstIteration = false; var type = typeof(T); var property = type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.NonPublic); var parameter = Expression.Parameter(type, type.Name); var propertyAccess = Expression.MakeMemberAccess(parameter, property); var orderByExp = Expression.Lambda(propertyAccess, parameter); var typeArguments = new[] { type, property.PropertyType }; var resultExp = Expression.Call(typeof(Queryable), methodName, typeArguments, updatedQuery.Expression, Expression.Quote(orderByExp)); updatedQuery = updatedQuery.Provider.CreateQuery <T>(resultExp); } // if any of the predicate conditions is by Id, we need to change the case to "id" so documentdb will work var stringQuery = updatedQuery.ToString(); if (stringQuery.Contains("root[\\\"Id\\\"]")) { stringQuery = stringQuery.Replace("root[\\\"Id\\\"]", "root[\\\"id\\\"]"); } // we need to return TotalCount and TotalPages for all queries int totalCount; List <T> dataList; // if page is > 1 then we need to perform two queries. The first to get just the Id's of the documents that match the query and the second // to pull the actual documents that match the page/pageSize parameters if (page > 0) { var options = new FeedOptions { MaxItemCount = int.MaxValue }; // we want to change the query to return only the value of the id property, not the property itself (so we get an IEnumerable<string> // of ids back instead of an IEnumerable<dynamic> that has an id property and a value) var idStringQuery = stringQuery.Replace("SELECT * FROM", "SELECT VALUE root.id FROM"); // convert the query to a dynamic so that we can easily pull just the query text dynamic idQuery = JsonConvert.DeserializeObject <dynamic>(idStringQuery); // get a list of ids that match the query var ids = _documentClient.CreateDocumentQuery <string>(_collectionUri, (string)idQuery.query, options) .ToList(); // capture the count of how many documents match the WHERE clause totalCount = ids.Count; // if we are asking for more documents than exist, make sure we don't skip them all var skip = (page - 1) * pageSize; // update the original query to just pull all data where the ids are in the id list WE HAVE TO MAINTAIN THE ORIGINAL SORT BY THAT THE // idQuery USES!!! var startIndex = idStringQuery.IndexOf("ORDER BY", StringComparison.Ordinal); var orderBy = idStringQuery.Substring(startIndex, idStringQuery.Length - startIndex - 3); stringQuery = $"SELECT * FROM root WHERE root.id IN ('{string.Join("','", ids.Skip(skip).Take(pageSize))}')" + " " + orderBy.Replace("\\\"", "'"); var jObjects = _documentClient.CreateDocumentQuery <JObject>(_collectionUri, stringQuery).ToList(); dataList = jObjects.Select(jObject => JsonConvert.DeserializeObject <T>(jObject.ToString(), _jsonSerializerSettings)).ToList(); } else { // convert the query to a dynamic so that we can easily pull just the query text dynamic jsonQuery = JsonConvert.DeserializeObject <dynamic>(stringQuery); stringQuery = jsonQuery.query; var jObjects = _documentClient.CreateDocumentQuery <JObject>(_collectionUri, stringQuery).ToList(); dataList = jObjects.Select(jObject => JsonConvert.DeserializeObject <T>(jObject.ToString(), _jsonSerializerSettings)).ToList(); // capture the count of how many documents match the WHERE clause totalCount = dataList.Count; } // calculate the number of total pages var actualPages = totalCount / (double)pageSize; var totalPages = (int)Math.Ceiling(actualPages); return(new EntityListQueryResult <T> { TotalCount = totalCount, TotalPages = totalPages, DataList = dataList }); }
public async Task <IActionResult> DeleteLinks( [HttpTrigger(AuthorizationLevel.Function, "DELETE", Route = "links")] HttpRequest req, [CosmosDB(ConnectionStringSetting = "LinkLinkConnection")] DocumentClient docClient, ILogger log) { string handle = GetAccountInfo().HashedID; //not logged in? Bye... if (string.IsNullOrEmpty(handle)) { return(new UnauthorizedResult()); } string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); IEnumerable <string> vanityUrls = JsonConvert.DeserializeObject <IEnumerable <string> >(requestBody); string queryValues = string.Join(",", vanityUrls.Select(url => $"\"{url}\"")); log.LogInformation($"Request to remove the following collections: {queryValues}"); string sql = $"SELECT c._self, c.userId c.vanityUrl from c WHERE c.vanityUrl IN ({queryValues}) "; int deleteCount = 0; string resultMessage = string.Empty; try { FeedOptions feedOpts = new FeedOptions { EnableCrossPartitionQuery = true }; Uri collUri = UriFactory.CreateDocumentCollectionUri("linkylinkdb", "linkbundles"); var docQuery = docClient.CreateDocumentQuery(collUri, sql, feedOpts).AsDocumentQuery(); while (docQuery.HasMoreResults) { var docs = await docQuery.ExecuteNextAsync(); foreach (var doc in docs) { string userId = doc.GetPropertyValue <string>("userId"); string vanityUrl = doc.GetPropertyValue <string>("vanityUrl"); if (!handle.Equals(userId, StringComparison.InvariantCultureIgnoreCase)) { log.LogWarning($"{userId} is trying to delete {vanityUrl} but is not the owner."); log.LogWarning($"Skipping deletion of colloection: {vanityUrl}."); continue; } RequestOptions reqOptions = new RequestOptions { PartitionKey = new PartitionKey(doc.vanityUrl) }; await docClient.DeleteDocumentAsync(doc._self, reqOptions); deleteCount++; } } resultMessage = (deleteCount == vanityUrls.Count()) ? "All collections removed" : "Some colletions were not removed"; } catch (Exception ex) { log.LogError(ex, ex.Message); return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } return(new OkObjectResult(new { deleted = deleteCount, message = resultMessage })); }
public Task <IFeedResponse <PartitionKeyRange> > ReadPartitionKeyRangeFeedAsync(string partitionKeyRangesOrCollectionLink, FeedOptions options) { return(_meter.MeasureAsync( "ReadPartitionKeyRangeFeedAsync", options.PartitionKeyRangeId, async() => await _inner.ReadPartitionKeyRangeFeedAsync(partitionKeyRangesOrCollectionLink, options))); }
public FeedOptions GetFeedOptions() { FeedOptions feedOptions = new FeedOptions(); try { feedOptions.MaxItemCount = Convert.ToInt32(toolStripTextMaxItemCount.Text, CultureInfo.InvariantCulture); } catch (Exception) { // Ignore the exception and use the defualt value } if (cbEnableScan.CheckState == CheckState.Checked) { feedOptions.EnableScanInQuery = true; } else if (cbEnableScan.CheckState == CheckState.Unchecked) { feedOptions.EnableScanInQuery = false; } return feedOptions; }
public IEnumerable <T> CreateDocumentQuery <T>(string query, FeedOptions options) where T : class { throw new NotImplementedException(); }
/// <summary> /// Execute a simple query using LINQ and SQL. Here we filter using the "LastName" property. /// </summary> /// <param name="databaseName">The name/ID of the database.</param> /// <param name="collectionName">The name/ID of the collection.</param> private void ExecuteSimpleQuery(string databaseName, string collectionName) { // Set some common query options FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1 }; // Run a simple query via LINQ. DocumentDB indexes all properties, so queries can be completed efficiently and with low latency. // Here we find the Andersen family via its LastName IQueryable<Family> familyQuery = this.client.CreateDocumentQuery<Family>( UriFactory.CreateDocumentCollectionUri(databaseName, collectionName), queryOptions) .Where(f => f.LastName == "Andersen"); // The query is executed synchronously here, but can also be executed asynchronously via the IDocumentQuery<T> interface Console.WriteLine("Running LINQ query..."); foreach (Family family in familyQuery) { Console.WriteLine("\tRead {0}", family); } // Now execute the same query via direct SQL IQueryable<Family> familyQueryInSql = this.client.CreateDocumentQuery<Family>( UriFactory.CreateDocumentCollectionUri(databaseName, collectionName), "SELECT * FROM Family WHERE Family.LastName = 'Andersen'", queryOptions); Console.WriteLine("Running direct SQL query..."); foreach (Family family in familyQueryInSql) { Console.WriteLine("\tRead {0}", family); } Console.WriteLine("Press any key to continue ..."); Console.ReadKey(); }
public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log) { log.Info("C# HTTP trigger function processed a request."); var connectionstring = Environment.GetEnvironmentVariable("NoSQLConnectionString"); var SQLconnectionstring = Environment.GetEnvironmentVariable("SQLConnectionString"); var PrimaryKey = Environment.GetEnvironmentVariable("NoSqlPrimaryKey"); DocumentClient client = new DocumentClient(new Uri(connectionstring), PrimaryKey); FeedOptions feedOptions = new FeedOptions(); Database databaseInfo = client.CreateDatabaseQuery().Where(x => x.Id == "TitanCosmosDB").AsEnumerable().FirstOrDefault(); string DB = databaseInfo.SelfLink; var collectionlinK = UriFactory.CreateDocumentCollectionUri("TitanCosmosDB", "TitanHeartBeatCollection"); string SqlSelect = "SELECT DISTINCT a.[MacId], a.[ShipmentID],a.[ShipmasterID] FROM [VW_Gateway_Pallet_ShipmentAssociation] a"; string queryHrtBt = "SELECT TOP 1 c.gatewayId, c.current_longitude,c.current_latitude,c.current_system_time,c.last_recorded_latitude,c.last_recorded_longitude FROM c where c.gatewayId = 'C031061830-00519'and c.current_longitude = 77.6570483 order by c.current_system_time desc"; //and c.current_longitude = 77.6570483 List <GatewayShipmentOld> gatewayShipmentList = new List <GatewayShipmentOld>(); using (SqlConnection conn = new SqlConnection(SQLconnectionstring)) { using (SqlCommand cmd = new SqlCommand(SqlSelect, conn)) { conn.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { GatewayShipmentOld ObjgatewayShipment = new GatewayShipmentOld(); ObjgatewayShipment.MacId = (string)reader["MacId"]; ObjgatewayShipment.ShipmentId = (string)reader["ShipmentID"]; ObjgatewayShipment.ShipmasterId = (int)reader["ShipmasterId"]; gatewayShipmentList.Add(ObjgatewayShipment); } reader.Close(); conn.Close(); } } foreach (GatewayShipmentOld item in gatewayShipmentList) { HeartBeat objHeartBeat = new HeartBeat(); queryHrtBt = "SELECT TOP 1 c.gatewayId,c.current_system_time FROM c " + " where c.gatewayId = '" + item.MacId + "' order by c.current_system_time desc"; IQueryable <HeartBeat> heartBeat = client.CreateDocumentQuery <HeartBeat>(collectionlinK, queryHrtBt); objHeartBeat = heartBeat.ToList().SingleOrDefault(); SqlConnection conn = new SqlConnection(SQLconnectionstring); if (objHeartBeat != null) { //Updating Device Status Online or Offline. string status = "Online"; var currentTime = DateTime.Now.ToUniversalTime(); var CosmosTime = objHeartBeat.current_system_time; if ((currentTime - CosmosTime).TotalMinutes >= 15) { status = "Offline"; } string quryUpdateDevice = "UPDATE DeviceInfo SET Status = @Status, [HealthCheckTime]= @CheckTime WHERE MacId = @gatewayID"; SqlCommand commandDevice = new SqlCommand(quryUpdateDevice, conn); commandDevice.Parameters.Clear(); commandDevice.Parameters.Add("@gatewayID", SqlDbType.NVarChar).Value = objHeartBeat.gatewayId; commandDevice.Parameters.Add("@Status", SqlDbType.NVarChar).Value = status; commandDevice.Parameters.Add("@CheckTime", SqlDbType.DateTime).Value = DateTime.UtcNow; conn.Open(); commandDevice.ExecuteNonQuery(); conn.Close(); log.Info("Status Changed to 'Offline'"); } } return(req.CreateResponse(HttpStatusCode.OK, "Successfully updated device status.")); }
/// <summary> /// Fetches the photo stream data for a specified user. /// </summary> /// <param name="userId">The user id.</param> /// <param name="continuationToken">Last captured ticks in the form of a string.</param> /// <param name="includeNonActivePhotos">By default, false. If true, non-active photos are included.</param> /// <returns>List of photos up to the page size.</returns> public async Task<PagedResponse<PhotoContract>> GetUserPhotoStream(string userId, string continuationToken, bool includeNonActivePhotos = false) { var feedOptions = new FeedOptions { MaxItemCount = PhotoStreamPageSize, RequestContinuation = continuationToken }; var query = _documentClient.CreateDocumentQuery<PhotoDocument>(DocumentCollectionUri, feedOptions) .Where(d => d.DocumentType == PhotoDocument.DocumentTypeIdentifier) .Where(d => d.DocumentVersion == _currentDocumentVersion) .Where(p => p.UserId == userId); if (!includeNonActivePhotos) { query = query .Where(p => p.Status == PhotoStatus.Active); } var documentQuery = query .OrderByDescending(p => p.CreatedDateTime.Epoch) .AsDocumentQuery(); var documentResponse = await documentQuery.ExecuteNextAsync<PhotoDocument>(); var photoContracts = await CreatePhotoContractsAndLoadUserData(documentResponse.ToList()); var result = new PagedResponse<PhotoContract> { Items = photoContracts, ContinuationToken = documentResponse.ResponseContinuation }; return result; }
public async Task <Offer> GetOfferForCollectionAsync(string databaseId, string collectionId, FeedOptions feedOptions = null, CancellationToken cancellationToken = default) { var collection = await GetCollectionAsync(databaseId, collectionId); return(await DocumentClient.CreateOfferQuery(feedOptions).SingleOrDefaultAsync(x => x.ResourceLink == collection.SelfLink, cancellationToken)); }
public async Task <IList <U> > SelectAsync <U>(Expression <Func <T, U> > selector, Func <IQueryable <U>, IQueryable <U> > selectClauses = null, FeedOptions feedOptions = null) { var result = await SelectAsync(feedOptions?.MaxItemCount ?? 0, default, selector, selectClauses, feedOptions);
public async Task <OfferV2> GetOfferV2ForCollectionAsync(string databaseId, string collectionId, FeedOptions feedOptions = null, CancellationToken cancellationToken = default) { return((OfferV2) await GetOfferForCollectionAsync(databaseId, collectionId, feedOptions, cancellationToken)); }
private async Task QueryWithPagination() { await this.client.CreateDatabaseIfNotExistsAsync(new Database() { Id = DatabaseName }); this.CreatePartitionedCollectionIfNotExists(DatabaseName, PartitionedCollectionName); Uri documentCollectionUri = UriFactory.CreateDocumentCollectionUri(DatabaseName, PartitionedCollectionName); await this.client.UpsertDocumentAsync(documentCollectionUri, new Person() { Id = "1", FirstName = "David", LastName = "Smith" }); await this.client.UpsertDocumentAsync(documentCollectionUri, new Person() { Id = "2", FirstName = "Robert", LastName = "Johnson" }); await this.client.UpsertDocumentAsync(documentCollectionUri, new Person() { Id = "3", FirstName = "William", LastName = "Smith" }); FeedOptions options = new FeedOptions { MaxItemCount = 1, EnableCrossPartitionQuery = true }; var smithFamily = this.client.CreateDocumentQuery <Person>(documentCollectionUri, options) .Where(d => d.LastName == "Smith") .ToList(); Assert.AreEqual(2, smithFamily.Count); var persons = this.client.CreateDocumentQuery <Person>(documentCollectionUri, options) .ToList(); Assert.AreEqual(3, persons.Count); var query = this.client.CreateDocumentQuery <Person>(documentCollectionUri, options); var documentQuery = query.AsDocumentQuery(); List <Person> personsList = new List <Person>(); while (documentQuery.HasMoreResults) { var feedResponse = await documentQuery.ExecuteNextAsync <Person>(); int maxItemCount = options.MaxItemCount ?? default(int); Assert.IsTrue(feedResponse.Count >= 0 && feedResponse.Count <= maxItemCount); personsList.AddRange(feedResponse); } Assert.AreEqual(3, personsList.Count); await this.CleanupDocumentCollection(documentCollectionUri); }
public async Task <IEnumerable <OfferV2> > QueryOffersV2Async(Expression <Func <Offer, bool> > predicate = null, FeedOptions feedOptions = null, CancellationToken cancellationToken = default) { if (predicate == null) { predicate = x => true; } var offers = await DocumentClient.CreateOfferQuery(feedOptions).Where(predicate).ToListAsync(cancellationToken); return(offers.Cast <OfferV2>()); }
public IEnumerable<Activity> Load(Feed feed, FeedOptions feedOptions) { var result = store.Get(feed, feedOptions); return result; }
public IQueryable <T> Query <T>(string databaseId, string collectionId, string sql, object parameters = null, FeedOptions feedOptions = null) { var collectionUri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId); var sqlParameters = parameters.ConvertToSqlParameterCollection(); return(GetSqlBasedQueryableForType <T>(collectionUri, sql, sqlParameters, feedOptions)); }
/// <summary> /// This method uses a ReadDatabaseFeedAsync method to read a list of all databases on the account. /// It demonstrates a pattern for how to control paging and deal with continuations /// This should not be needed for reading a list of databases as there are unlikely to be many hundred, /// but this same pattern is introduced here and can be used on other ReadFeed methods. /// </summary> /// <returns>A List of Database entities</returns> private static async Task<List<Database>> ListDatabasesAsync() { string continuation = null; List<Database> databases = new List<Database>(); do { FeedOptions options = new FeedOptions { RequestContinuation = continuation, MaxItemCount = 50 }; FeedResponse<Database> response = await client.ReadDatabaseFeedAsync(options); foreach (Database db in response) { databases.Add(db); } continuation = response.ResponseContinuation; } while (!String.IsNullOrEmpty(continuation)); return databases; }
private IQueryable <T> GetSqlBasedQueryableForType <T>(Uri collectionUri, string sql, SqlParameterCollection parameters, FeedOptions feedOptions) { var sqlQuerySpec = parameters != null && parameters.Any() ? new SqlQuerySpec(sql, parameters) : new SqlQuerySpec(sql); var queryable = DocumentClient.CreateDocumentQuery <T>(collectionUri, sqlQuerySpec, feedOptions); return(queryable); }
private bool UpdateLockFile(PublishRoot root) { var appEnv = (IApplicationEnvironment)root.HostServices.GetService(typeof(IApplicationEnvironment)); var feedOptions = new FeedOptions(); feedOptions.IgnoreFailedSources = true; feedOptions.Sources.Add(root.TargetPackagesPath); feedOptions.TargetPackagesFolder = root.TargetPackagesPath; var tasks = new Task<bool>[root.Projects.Count]; for (int i = 0; i < root.Projects.Count; i++) { var project = root.Projects[i]; var restoreCommand = new RestoreCommand(appEnv); foreach (var runtime in root.Runtimes) { restoreCommand.TargetFrameworks.Add(project.SelectFrameworkForRuntime(runtime)); } var restoreDirectory = project.IsPackage ? Path.Combine(project.TargetPath, "root") : project.TargetPath; restoreCommand.SkipRestoreEvents = true; restoreCommand.SkipInstall = true; // This is a workaround for #1322. Since we use restore to generate the lock file // after publish, it's possible to fail restore after copying the closure // if framework assemblies and packages have the same name. This is more likely now // since dependencies may exist in the top level restoreCommand.IgnoreMissingDependencies = true; restoreCommand.CheckHashFile = false; restoreCommand.RestoreDirectories.Add(restoreDirectory); restoreCommand.FeedOptions = feedOptions; restoreCommand.Reports = root.Reports.ShallowCopy(); // Mute "dnu restore" completely if it is invoked by "dnu publish --quiet" restoreCommand.Reports.Information = root.Reports.Quiet; tasks[i] = restoreCommand.Execute(); } Task.WaitAll(tasks); return tasks.All(t => t.Result); }
public async Task <IEnumerable <DocumentCollection> > QueryCollectionsAsync(string databaseId, Expression <Func <DocumentCollection, bool> > predicate = null, FeedOptions feedOptions = null, CancellationToken cancellationToken = default) { if (predicate == null) { predicate = x => true; } var databaseUri = UriFactory.CreateDatabaseUri(databaseId); return(await DocumentClient.CreateDocumentCollectionQuery(databaseUri, feedOptions).Where(predicate).ToListAsync(cancellationToken)); }
public void FillWithChildren() { try { Document document = ((Document)this.Tag); DocumentCollection collection = ((DocumentCollection)this.Parent.Tag); FeedOptions options = new FeedOptions(); if (collection.PartitionKey != null && collection.PartitionKey.Paths.Count > 0) { options.PartitionKey = new PartitionKey(DocumentAnalyzer.ExtractPartitionKeyValue(document, collection.PartitionKey)); } FeedResponse<Attachment> attachments; using (PerfStatus.Start("ReadAttachmentFeed")) { attachments = this.client.ReadAttachmentFeedAsync(document.GetLink(this.client), options).Result; } foreach (var attachment in attachments) { ResourceNode node = new ResourceNode(client, attachment, ResourceType.Attachment); this.Nodes.Add(node); } Program.GetMain().SetResponseHeaders(attachments.ResponseHeaders); } catch (AggregateException e) { Program.GetMain().SetResultInBrowser(null, e.InnerException.ToString(), true); } catch (Exception e) { Program.GetMain().SetResultInBrowser(null, e.ToString(), true); } }
private static async Task QueryWithPagingAsync(string collectionLink) { // The .NET client automatically iterates through all the pages of query results // Developers can explicitly control paging by creating an IDocumentQueryable // using the IQueryable object, then by reading the ResponseContinuationToken values // and passing them back as RequestContinuationToken in FeedOptions. List <Family> families = new List <Family>(); // tell server we only want 1 record FeedOptions options = new FeedOptions { MaxItemCount = 1, EnableCrossPartitionQuery = true }; // using AsDocumentQuery you get access to whether or not the query HasMoreResults // If it does, just call ExecuteNextAsync until there are no more results // No need to supply a continuation token here as the server keeps track of progress var query = client.CreateDocumentQuery <Family>(collectionLink, options).AsDocumentQuery(); while (query.HasMoreResults) { foreach (Family family in await query.ExecuteNextAsync()) { families.Add(family); } } // The above sample works fine whilst in a loop as above, but // what if you load a page of 1 record and then in a different // Session at a later stage want to continue from where you were? // well, now you need to capture the continuation token // and use it on subsequent queries query = client.CreateDocumentQuery <Family>( collectionLink, new FeedOptions { MaxItemCount = 1, EnableCrossPartitionQuery = true }).AsDocumentQuery(); var feedResponse = await query.ExecuteNextAsync <Family>(); string continuation = feedResponse.ResponseContinuation; foreach (var f in feedResponse.AsEnumerable().OrderBy(f => f.Id)) { if (f.Id != "AndersenFamily") { throw new ApplicationException("Should only be the first family"); } } // Now the second time around use the contiuation token you got // and start the process from that point query = client.CreateDocumentQuery <Family>( collectionLink, new FeedOptions { MaxItemCount = 1, RequestContinuation = continuation, EnableCrossPartitionQuery = true }).AsDocumentQuery(); feedResponse = await query.ExecuteNextAsync <Family>(); foreach (var f in feedResponse.AsEnumerable().OrderBy(f => f.Id)) { if (f.Id != "WakefieldFamily") { throw new ApplicationException("Should only be the second family"); } } }
private static async Task<List<DocumentCollection>> ReadCollectionsFeedAsync(string databaseSelfLink) { // This method uses a ReadCollectionsFeedAsync method to read a list of all collections on a database. // It demonstrates a pattern for how to control paging and deal with continuations // This should not be needed for reading a list of collections as there are unlikely to be many hundred, // but this same pattern is introduced here and can be used on other ReadFeed methods. string continuation = null; List<DocumentCollection> collections = new List<DocumentCollection>(); do { FeedOptions options = new FeedOptions { RequestContinuation = continuation, MaxItemCount = 50 }; FeedResponse<DocumentCollection> response = (FeedResponse<DocumentCollection>) await client.ReadDocumentCollectionFeedAsync(databaseSelfLink, options); foreach (DocumentCollection col in response) { collections.Add(col); } continuation = response.ResponseContinuation; } while (!String.IsNullOrEmpty(continuation)); return collections; }
public async Task <INameValueCollection> CreateCommonHeadersAsync(FeedOptions feedOptions) { INameValueCollection requestHeaders = new StringKeyValueCollection(); Cosmos.ConsistencyLevel defaultConsistencyLevel = (Cosmos.ConsistencyLevel)(await this.client.GetDefaultConsistencyLevelAsync()); Cosmos.ConsistencyLevel?desiredConsistencyLevel = (Cosmos.ConsistencyLevel?) await this.client.GetDesiredConsistencyLevelAsync(); if (!string.IsNullOrEmpty(feedOptions.SessionToken) && !ReplicatedResourceClient.IsReadingFromMaster(this.resourceTypeEnum, OperationType.ReadFeed)) { if (defaultConsistencyLevel == Cosmos.ConsistencyLevel.Session || (desiredConsistencyLevel.HasValue && desiredConsistencyLevel.Value == Cosmos.ConsistencyLevel.Session)) { // Query across partitions is not supported today. Master resources (for e.g., database) // can span across partitions, whereas server resources (viz: collection, document and attachment) // don't span across partitions. Hence, session token returned by one partition should not be used // when quering resources from another partition. // Since master resources can span across partitions, don't send session token to the backend. // As master resources are sync replicated, we should always get consistent query result for master resources, // irrespective of the chosen replica. // For server resources, which don't span partitions, specify the session token // for correct replica to be chosen for servicing the query result. requestHeaders[HttpConstants.HttpHeaders.SessionToken] = feedOptions.SessionToken; } } requestHeaders[HttpConstants.HttpHeaders.Continuation] = feedOptions.RequestContinuation; requestHeaders[HttpConstants.HttpHeaders.IsQuery] = bool.TrueString; // Flow the pageSize only when we are not doing client eval if (feedOptions.MaxItemCount.HasValue) { requestHeaders[HttpConstants.HttpHeaders.PageSize] = feedOptions.MaxItemCount.ToString(); } requestHeaders[HttpConstants.HttpHeaders.EnableCrossPartitionQuery] = feedOptions.EnableCrossPartitionQuery.ToString(); if (feedOptions.MaxDegreeOfParallelism != 0) { requestHeaders[HttpConstants.HttpHeaders.ParallelizeCrossPartitionQuery] = bool.TrueString; } if (this.feedOptions.EnableScanInQuery != null) { requestHeaders[HttpConstants.HttpHeaders.EnableScanInQuery] = this.feedOptions.EnableScanInQuery.ToString(); } if (this.feedOptions.EmitVerboseTracesInQuery != null) { requestHeaders[HttpConstants.HttpHeaders.EmitVerboseTracesInQuery] = this.feedOptions.EmitVerboseTracesInQuery.ToString(); } if (this.feedOptions.EnableLowPrecisionOrderBy != null) { requestHeaders[HttpConstants.HttpHeaders.EnableLowPrecisionOrderBy] = this.feedOptions.EnableLowPrecisionOrderBy.ToString(); } if (!string.IsNullOrEmpty(this.feedOptions.FilterBySchemaResourceId)) { requestHeaders[HttpConstants.HttpHeaders.FilterBySchemaResourceId] = this.feedOptions.FilterBySchemaResourceId; } if (this.feedOptions.ResponseContinuationTokenLimitInKb != null) { requestHeaders[HttpConstants.HttpHeaders.ResponseContinuationTokenLimitInKB] = this.feedOptions.ResponseContinuationTokenLimitInKb.ToString(); } if (this.feedOptions.ConsistencyLevel.HasValue) { await this.client.EnsureValidOverwrite((Documents.ConsistencyLevel) feedOptions.ConsistencyLevel.Value); requestHeaders.Set(HttpConstants.HttpHeaders.ConsistencyLevel, this.feedOptions.ConsistencyLevel.Value.ToString()); } else if (desiredConsistencyLevel.HasValue) { requestHeaders.Set(HttpConstants.HttpHeaders.ConsistencyLevel, desiredConsistencyLevel.Value.ToString()); } if (this.feedOptions.EnumerationDirection.HasValue) { requestHeaders.Set(HttpConstants.HttpHeaders.EnumerationDirection, this.feedOptions.EnumerationDirection.Value.ToString()); } if (this.feedOptions.ReadFeedKeyType.HasValue) { requestHeaders.Set(HttpConstants.HttpHeaders.ReadFeedKeyType, this.feedOptions.ReadFeedKeyType.Value.ToString()); } if (this.feedOptions.StartId != null) { requestHeaders.Set(HttpConstants.HttpHeaders.StartId, this.feedOptions.StartId); } if (this.feedOptions.EndId != null) { requestHeaders.Set(HttpConstants.HttpHeaders.EndId, this.feedOptions.EndId); } if (this.feedOptions.StartEpk != null) { requestHeaders.Set(HttpConstants.HttpHeaders.StartEpk, this.feedOptions.StartEpk); } if (this.feedOptions.EndEpk != null) { requestHeaders.Set(HttpConstants.HttpHeaders.EndEpk, this.feedOptions.EndEpk); } if (this.feedOptions.PopulateQueryMetrics) { requestHeaders[HttpConstants.HttpHeaders.PopulateQueryMetrics] = bool.TrueString; } if (this.feedOptions.ForceQueryScan) { requestHeaders[HttpConstants.HttpHeaders.ForceQueryScan] = bool.TrueString; } if (this.feedOptions.CosmosSerializationOptions != null) { requestHeaders[HttpConstants.HttpHeaders.ContentSerializationFormat] = this.feedOptions.CosmosSerializationOptions.ContentSerializationFormat; } else if (this.feedOptions.ContentSerializationFormat.HasValue) { requestHeaders[HttpConstants.HttpHeaders.ContentSerializationFormat] = this.feedOptions.ContentSerializationFormat.Value.ToString(); } return(requestHeaders); }
private static void ShowQueryReturnsResults(DocumentCollection collection, string query, FeedOptions options = null) { int count = ShowQueryIsAllowed(collection, query, options); if (count == 0) { throw new ApplicationException("Expected results"); } }
/// <inheritdoc/> public async Task <List <Instance> > GetInstancesInStateOfInstanceOwner(int instanceOwnerPartyId, string instanceState) { List <Instance> instances = new List <Instance>(); string instanceOwnerPartyIdString = instanceOwnerPartyId.ToString(); FeedOptions feedOptions = new FeedOptions { PartitionKey = new PartitionKey(instanceOwnerPartyIdString) }; if (_settings.CollectMetrics) { feedOptions.PopulateQueryMetrics = true; } IQueryable <Instance> filter; if (instanceState.Equals("active")) { filter = _client.CreateDocumentQuery <Instance>(_collectionUri, feedOptions) .Where(i => i.InstanceOwner.PartyId == instanceOwnerPartyIdString) .Where(i => (!i.VisibleAfter.HasValue || i.VisibleAfter <= DateTime.UtcNow)) .Where(i => !i.Status.SoftDeleted.HasValue) .Where(i => !i.Status.HardDeleted.HasValue) .Where(i => !i.Status.Archived.HasValue); } else if (instanceState.Equals("deleted")) { filter = _client.CreateDocumentQuery <Instance>(_collectionUri, feedOptions) .Where(i => i.InstanceOwner.PartyId == instanceOwnerPartyIdString) .Where(i => i.Status.SoftDeleted.HasValue) .Where(i => !i.Status.HardDeleted.HasValue); } else if (instanceState.Equals("archived")) { filter = _client.CreateDocumentQuery <Instance>(_collectionUri, feedOptions) .Where(i => i.InstanceOwner.PartyId == instanceOwnerPartyIdString) .Where(i => i.Status.Archived.HasValue) .Where(i => !i.Status.SoftDeleted.HasValue) .Where(i => !i.Status.HardDeleted.HasValue); } else { // empty list return(instances); } IDocumentQuery <Instance> query = filter.AsDocumentQuery(); FeedResponse <Instance> feedResponse = await query.ExecuteNextAsync <Instance>(); if (_settings.CollectMetrics) { _logger.LogError($"Metrics retrieving {instanceState} instances for {instanceOwnerPartyId}: {JsonConvert.SerializeObject(feedResponse.QueryMetrics)}"); } instances = feedResponse.ToList(); await PostProcess(instances); return(instances); }
private static void ShowQueryIsNotAllowed(DocumentCollection collection, string query, FeedOptions options = null) { try { client.CreateDocumentQuery(collection.SelfLink, query, options).AsEnumerable().Any(); } catch (Exception e) { var baseEx = (DocumentClientException)e.GetBaseException(); if (baseEx.StatusCode != HttpStatusCode.BadRequest) { throw; } } }
/// <inheritdoc/> public async Task <InstanceQueryResponse> GetInstancesFromQuery( Dictionary <string, StringValues> queryParams, string continuationToken, int size) { InstanceQueryResponse queryResponse = new InstanceQueryResponse { Count = 0, Instances = new List <Instance>() }; while (queryResponse.Count < size) { FeedOptions feedOptions = new FeedOptions { EnableCrossPartitionQuery = true, MaxItemCount = size - queryResponse.Count, }; if (continuationToken != null) { feedOptions.RequestContinuation = continuationToken; } IQueryable <Instance> queryBuilder = _client.CreateDocumentQuery <Instance>(_collectionUri, feedOptions); try { queryBuilder = BuildQueryFromParameters(queryParams, queryBuilder); } catch (Exception e) { queryResponse.Exception = e.Message; return(queryResponse); } try { IDocumentQuery <Instance> documentQuery = queryBuilder.AsDocumentQuery(); FeedResponse <Instance> feedResponse = await documentQuery.ExecuteNextAsync <Instance>(); if (feedResponse.Count == 0) { queryResponse.ContinuationToken = string.Empty; break; } List <Instance> instances = feedResponse.ToList(); await PostProcess(instances); queryResponse.Instances.AddRange(instances); queryResponse.Count += instances.Count; if (string.IsNullOrEmpty(feedResponse.ResponseContinuation)) { queryResponse.ContinuationToken = string.Empty; break; } queryResponse.ContinuationToken = feedResponse.ResponseContinuation; continuationToken = feedResponse.ResponseContinuation; } catch (Exception e) { _logger.LogError("error: {e}"); queryResponse.Exception = e.Message; break; } } return(queryResponse); }
/// <summary> /// Gets hero photos. /// </summary> /// <param name="count">The number of hero photos.</param> /// <param name="daysOld">The number of days old the photos can be.</param> /// <returns>List interface of hero photos.</returns> public async Task<IList<PhotoContract>> GetHeroPhotos(int count, int daysOld) { var feedOptions = new FeedOptions { MaxItemCount = count }; var cutOffDate = new DateDocument { Date = DateTime.Now.AddDays(-daysOld) }; var query = _documentClient.CreateDocumentQuery<PhotoDocument>(DocumentCollectionUri, feedOptions) .Where(d => d.DocumentType == PhotoDocument.DocumentTypeIdentifier) .Where(d => d.DocumentVersion == _currentDocumentVersion) .Where(p => p.Status == PhotoStatus.Active) .Where(p => p.CreatedDateTime.Epoch >= cutOffDate.Epoch) .OrderByDescending(p => p.GoldCount) .AsDocumentQuery(); var documentResponse = await query.ExecuteNextAsync<PhotoDocument>(); return await CreatePhotoContractsAndLoadUserData(documentResponse.ToList()); }
public async Task Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "v1/good")] HttpRequest req, ILogger log) { // POSTデータからパラメータを取得 var jsonContent = await req.ReadAsStringAsync(); log.LogInformation($"Request:{jsonContent}"); var feedOptions = new FeedOptions { MaxItemCount = 1, EnableCrossPartitionQuery = true }; try { dynamic data = JsonConvert.DeserializeObject(jsonContent); string image_id = data?.id; // バリデーションチェック if (string.IsNullOrWhiteSpace(image_id) || !(Regex.IsMatch(image_id, @"^[0-9]{14}$"))) { log.LogInformation("バリデーションエラー"); return; } var doc = _documentClient.CreateDocumentQuery <DocumentInterface>( UriFactory.CreateDocumentCollectionUri(AppSettings.Instance.DATABASE_ID, AppSettings.Instance.COLLECTION_ID), feedOptions) .Where(x => x.ImageId == image_id) .AsEnumerable() .FirstOrDefault(); if (doc == null) { log.LogInformation("更新対象なし"); return; } var requestOptions = new RequestOptions() { PartitionKey = new PartitionKey(doc.ImageId), AccessCondition = new AccessCondition() { Condition = doc.Etag, Type = AccessConditionType.IfMatch } }; TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time"); var update = new DocumentInterface() { Id = doc.Id, ImageId = doc.ImageId, User = doc.User, UserIcon = doc.UserIcon, PictureUrl = doc.PictureUrl, Score = doc.Score, GoodCnt = doc.GoodCnt + 1, CreatedDatatime = doc.CreatedDatatime, UpdatedDatatime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now.ToUniversalTime(), tst) }; await _documentClient.ReplaceDocumentAsync( UriFactory.CreateDocumentUri(AppSettings.Instance.DATABASE_ID, AppSettings.Instance.COLLECTION_ID, doc.Id), update, requestOptions); var imageInfo = _documentClient.CreateDocumentQuery <DocumentInterface>(UriFactory.CreateDocumentCollectionUri(AppSettings.Instance.DATABASE_ID, AppSettings.Instance.COLLECTION_ID), feedOptions) .OrderByDescending(x => x.Score) .ToList(); var rank = new RankResponseInterface(); rank.Images = new List <Images>(); var count = 0; foreach (var res in imageInfo) { var image = new Images { ImageId = res.ImageId, User = res.User, UserIcon = res.UserIcon, PictureUrl = res.PictureUrl, Score = res.Score, GoodCnt = res.GoodCnt, Rank = count += 1 }; rank.Images.Add(image); } await _httpClientService.PostJsonAsync <RankResponseInterface>(AppSettings.Instance.SIGNALR_URL, rank); } catch (Exception ex) { log.LogError($"Error:{ex.Message}"); log.LogError($"StackTrace:{ex.StackTrace}"); } }
private async Task<IList<LeaderboardEntryContract<UserContract>>> GetHighestNetWorthUser(int count) { var feedOptions = new FeedOptions { MaxItemCount = count }; var userQuery = _documentClient.CreateDocumentQuery<UserDocument>(DocumentCollectionUri, feedOptions) .Where(d => d.DocumentType == UserDocument.DocumentTypeIdentifier) .Where(d => d.DocumentVersion == _currentDocumentVersion) .Where(u => u.Id != SystemUserId) .OrderByDescending(u => u.GoldBalance) .AsDocumentQuery(); var userDocumentResponse = await userQuery.ExecuteNextAsync<UserDocument>(); var rank = 1; var mostGoldUsers = userDocumentResponse.Select(u => new LeaderboardEntryContract<UserContract> { Model = u.ToContract(), Value = u.GoldBalance, Rank = rank++ }).ToList(); return mostGoldUsers; }
public async Task <dynamic> IsSmsPasscodeMatched(string phoneno, string passcode) { var passed = false; var error = RPCERR.PASSCODE_EXPIRED; var phonenoHash = phoneno.ToHash(R.SALT_SMS); var passcodeHash = (phoneno + passcode).ToHash(R.SALT_SMS); var pk = PhoneToPK(phoneno); var feedOpt = new FeedOptions { PartitionKey = new PartitionKey(pk), }; var reqOpt = new RequestOptions { PartitionKey = new PartitionKey(pk), }; // 取得SmsPasscode var result = await client.CreateDocumentQuery <SmsPasscode>(SmsPasscode._URI_COL, feedOpt) .Where(p => p.phoneno == phonenoHash) .AsDocumentQuery() .ToListAsync(); if (result.Count() > 0) { var sms = result.ToList().First(); //是否在有效期間內 if (sms.verifyAvailTime.CompareTo(DateTime.UtcNow) > 0) { //是否嘗試3次內 if (sms.verifyCount < 3) { //是否正確 if (passcodeHash == sms.passcode) { passed = true; //刪除sms await client.DeleteDocumentAsync(UriFactory.CreateDocumentUri(R.DB_NAME, SmsPasscode._COLLECTION_NAME, sms.id), reqOpt); } else { error = RPCERR.PASSCODE_MISMATCH; //更新sms sms.verifyCount++; await client.UpsertDocumentAsync(SmsPasscode._URI_COL, sms, reqOpt); } } else { error = RPCERR.PASSCODE_VERIFY_EXCEEDED; } } else { error = RPCERR.PASSCODE_EXPIRED; } } return(new { passed, error }); }
private static async Task QueryWithPagingAsync(Uri collectionUri) { // The .NET client automatically iterates through all the pages of query results // Developers can explicitly control paging by creating an IDocumentQueryable // using the IQueryable object, then by reading the ResponseContinuationToken values // and passing them back as RequestContinuationToken in FeedOptions. List<Family> families = new List<Family>(); // tell server we only want 1 record FeedOptions options = new FeedOptions { MaxItemCount = 1, EnableCrossPartitionQuery = true }; // using AsDocumentQuery you get access to whether or not the query HasMoreResults // If it does, just call ExecuteNextAsync until there are no more results // No need to supply a continuation token here as the server keeps track of progress var query = client.CreateDocumentQuery<Family>(collectionUri, options).AsDocumentQuery(); while (query.HasMoreResults) { foreach (Family family in await query.ExecuteNextAsync()) { families.Add(family); } } // The above sample works fine whilst in a loop as above, but // what if you load a page of 1 record and then in a different // Session at a later stage want to continue from where you were? // well, now you need to capture the continuation token // and use it on subsequent queries query = client.CreateDocumentQuery<Family>( collectionUri, new FeedOptions { MaxItemCount = 1, EnableCrossPartitionQuery = true }).AsDocumentQuery(); var feedResponse = await query.ExecuteNextAsync<Family>(); string continuation = feedResponse.ResponseContinuation; foreach (var f in feedResponse.AsEnumerable().OrderBy(f => f.Id)) { if (f.Id != "AndersenFamily") throw new ApplicationException("Should only be the first family"); } // Now the second time around use the contiuation token you got // and start the process from that point query = client.CreateDocumentQuery<Family>( collectionUri, new FeedOptions { MaxItemCount = 1, RequestContinuation = continuation, EnableCrossPartitionQuery = true }).AsDocumentQuery(); feedResponse = await query.ExecuteNextAsync<Family>(); foreach (var f in feedResponse.AsEnumerable().OrderBy(f => f.Id)) { if (f.Id != "WakefieldFamily") throw new ApplicationException("Should only be the second family"); } }
public IEnumerable <T> CreateDocumentQuery <T>(string query, FeedOptions options) where T : class { return(client.CreateDocumentQuery <T>(collection.DocumentsLink, query, options).AsEnumerable()); }
private IDocumentQuery <dynamic> CreateDocumentQuery(string queryText, FeedOptions feedOptions) { return(_client.CreateDocumentQuery((Tag as DocumentCollection).GetLink(_client), queryText, feedOptions).AsDocumentQuery()); }
public Task <CosmosDbRepositoryPagedResults <T> > FindAsync(int pageSize, string continuationToken, Expression <Func <T, bool> > predicate = null, Func <IQueryable <T>, IQueryable <T> > clauses = null, FeedOptions feedOptions = null) { var failure = _findExceptionConditions.Select(func => func()).FirstOrDefault(); if (failure != default) { return(Task.FromException <CosmosDbRepositoryPagedResults <T> >(failure)); } IEnumerable <T> entities; if (string.IsNullOrEmpty(continuationToken)) { lock (_entities) { entities = _entities.Select(i => i.Entity).ToArray(); } } else { entities = JsonConvert.DeserializeObject <T[]>(continuationToken); } if (predicate != default) { entities = entities.Where(predicate.Compile()); } if (clauses != default) { entities = clauses.Invoke(entities.AsQueryable()); } var result = new CosmosDbRepositoryPagedResults <T>() { Items = entities.Select(DeepClone).ToList() }; if (pageSize > 0 && pageSize < result.Items.Count) { result.ContinuationToken = JsonConvert.SerializeObject(result.Items.Skip(pageSize)); result.Items = result.Items.Take(pageSize).ToList(); } return(Task.FromResult(result)); }