public async Task Checkout(string id) { var table = Table.LoadTable(dbClient, "carts_id_cid"); var filter = new QueryFilter(); filter.AddCondition("id", QueryOperator.Equal, id); var queryConfig = new QueryOperationConfig { Filter = filter, Limit = 1 }; var result = table.Query(queryConfig); if (result.IsDone) { throw new ArgumentException("id"); } else { var querySet = await result.GetNextSetAsync(); var document = querySet.First(); document["status"] = "DONE"; await table.UpdateItemAsync(document); } }
public async Task <IEnumerable <TEntity> > ExecuteAsync() { var query = new QueryOperationConfig(); foreach (var filterAction in this.collection) { filterAction(query.Filter); } if (this.limit != null) { query.Limit = this.limit.Value; } if (this.indexName != null) { query.IndexName = this.indexName; } if (this.propertiesToGet != null) { query.AttributesToGet = this.propertiesToGet.ToList(); query.Select = SelectValues.SpecificAttributes; } var search = this.context.FromQueryAsync <TEntity>(query); return(await search.GetRemainingAsync()); }
private QueryOperationConfig GetQueryOperationConfig(List <QueryFilterCondition> QueryFilterConditions, bool consistentRead = true, List <string> attributesToGet = null) { QueryFilter queryFilter = new QueryFilter(); foreach (var queryFilterCondition in QueryFilterConditions) { if (queryFilterCondition.Type == QueryFilterConditionType.one) { queryFilter.AddCondition(queryFilterCondition.AttributeName, queryFilterCondition.Condition); } if (queryFilterCondition.Type == QueryFilterConditionType.two) { queryFilter.AddCondition(queryFilterCondition.AttributeName, queryFilterCondition.QueryOperator, queryFilterCondition.AttributeValues); } if (queryFilterCondition.Type == QueryFilterConditionType.three) { queryFilter.AddCondition(queryFilterCondition.AttributeName, queryFilterCondition.QueryOperator, queryFilterCondition.DynamoDBEntry); } } QueryOperationConfig queryOperationConfig = new QueryOperationConfig() { ConsistentRead = consistentRead, Filter = queryFilter }; if (attributesToGet != null) { queryOperationConfig.AttributesToGet = attributesToGet; } return(queryOperationConfig); }
public async Task DeleteItem(string id, string item_id) { var table = Table.LoadTable(dbClient, "carts_id_cid"); var filter = new QueryFilter(); filter.AddCondition("id", QueryOperator.Equal, id); var queryConfig = new QueryOperationConfig { Filter = filter, Limit = 1 }; var result = table.Query(queryConfig); if (result.IsDone) { throw new ArgumentException("id"); } else { var querySet = await result.GetNextSetAsync(); var document = querySet.First(); var items = JsonConvert.DeserializeObject <List <CartItem> >(document["items"].AsString()); var item = items.FirstOrDefault(x => x.id.ToString() == item_id); items.Remove(item); await table.UpdateItemAsync(document); } }
public async Task <(object Snapshot, long Index)> GetSnapshotAsync(string actorName) { var config = new QueryOperationConfig { ConsistentRead = true, BackwardSearch = true, Limit = 1 }; config.Filter.AddCondition(_options.SnapshotsTableHashKey, QueryOperator.Equal, actorName); var query = _snapshotsTable.Query(config); var results = await query.GetNextSetAsync(); var doc = results.FirstOrDefault(); if (doc == null) { return(null, 0); } var snapshotIndexE = doc.GetValueOrThrow(_options.SnapshotsTableSortKey); var dataTypeE = doc.GetValueOrThrow(_options.SnapshotsTableDataTypeKey); var dataE = doc.GetValueOrThrow(_options.SnapshotsTableDataKey); var dataType = Type.GetType(dataTypeE.AsString()); var data = _dynamoDBContext.FromDocumentDynamic(dataE.AsDocument(), dataType); return(data, snapshotIndexE.AsLong()); }
protected virtual async Task <T[]> GetByIndex(string index, string key, string value, int limit = int.MaxValue) { Table table = GetTable(TableName); QueryFilter filter = new QueryFilter(key, QueryOperator.Equal, value); QueryOperationConfig config = new QueryOperationConfig(); config.Filter = filter; config.IndexName = index; config.Limit = limit; Search search = table.Query(config); List <Document> documents = new List <Document>(); do { documents.AddRange(await search.GetNextSetAsync()); } while (!search.IsDone); return(!documents.Any() ? new T[0] : documents.Select(ConvertDocumentToObject).ToArray()); }
public async Task <List <Coffee> > GetAll() { var query = new QueryOperationConfig(); var result = this._context.FromQueryAsync <Coffee>(query); return(await result.GetRemainingAsync()); }
/// <summary> /// Returns messages that have yet to be dispatched /// </summary> /// <param name="millSecondsSinceSent">How long ago as the message sent?</param> /// <param name="pageSize">How many messages to return at once?</param> /// <param name="pageNumber">Which page number of messages</param> /// <returns>A list of messages that are outstanding for dispatch</returns> public IEnumerable <Message> OutstandingMessages( double millisecondsDispatchedSince, int pageSize = 100, int pageNumber = 1, Dictionary <string, object> args = null) { if (args == null) { throw new ArgumentException("Missing required argument", nameof(args)); } var sinceTime = DateTime.UtcNow.Subtract(TimeSpan.FromMilliseconds(millisecondsDispatchedSince)); var topic = (string)args["Topic"]; // We get all the messages for topic, added within a time range // There should be few enough of those that we can efficiently filter for those // that don't have a delivery date. var queryConfig = new QueryOperationConfig { IndexName = _configuration.OutstandingIndexName, KeyExpression = new KeyTopicCreatedTimeExpression().Generate(topic, sinceTime), FilterExpression = new NoDispatchTimeExpression().Generate(), ConsistentRead = false }; //block async to make this sync var messages = PageAllMessagesAsync(queryConfig).Result.ToList(); return(messages.Select(msg => msg.ConvertToMessage())); }
private async Task <List <string> > QueryTable(string userId, string dataId) { var config = new QueryOperationConfig(); if (string.IsNullOrWhiteSpace(dataId)) { config.KeyExpression = new Expression { ExpressionStatement = $"UserId = :{nameof(userId)}", ExpressionAttributeValues = new Dictionary <string, DynamoDBEntry> { { $":{nameof(userId)}", userId } } }; } else { config.KeyExpression = new Expression { ExpressionStatement = $"UserId = :{nameof(userId)} and DataId = :{nameof(dataId)}", ExpressionAttributeValues = new Dictionary <string, DynamoDBEntry> { { $":{nameof(userId)}", userId }, { $":{nameof(dataId)}", dataId } } }; } var dataDocuments = await _dataDynamoDbTable.Query(config).GetRemainingAsync(); var values = dataDocuments.Select(dataDocument => dataDocument["DataAsJson"]?.AsString()).ToList(); return(values); }
private async Task <List <string> > GetPatientData(string patientIdentifier) { var config = new QueryOperationConfig { KeyExpression = new Expression { ExpressionStatement = $"{nameof(DataModel.PatientId)} = :{nameof(patientIdentifier)}", ExpressionAttributeValues = new Dictionary <string, DynamoDBEntry> { { $":{nameof(patientIdentifier)}", patientIdentifier } } }, Select = SelectValues.SpecificAttributes, AttributesToGet = new List <string> { nameof(DataModel.DataAsJson) } }; var dataDocuments = await _dataDynamoDbTable.Query(config).GetRemainingAsync(); if (dataDocuments == null || !dataDocuments.Any()) { throw new DataNotFoundException(patientIdentifier); } var values = dataDocuments.Select(dataDocument => GetValue(dataDocument[nameof(DataModel.DataAsJson)])).ToList(); return(values); }
private static void FindRepliesPostedWithinTimePeriod(Table table, string forumName, string threadSubject) { DateTime startDate = DateTime.UtcNow.Subtract(new TimeSpan(21, 0, 0, 0)); DateTime endDate = DateTime.UtcNow.Subtract(new TimeSpan(1, 0, 0, 0)); QueryFilter filter = new QueryFilter("Id", QueryOperator.Equal, forumName + "#" + threadSubject); filter.AddCondition("ReplyDateTime", QueryOperator.Between, startDate, endDate); QueryOperationConfig config = new QueryOperationConfig() { Limit = 2, // 2 items/page. Select = SelectValues.SpecificAttributes, AttributesToGet = new List<string> { "Message", "ReplyDateTime", "PostedBy" }, ConsistentRead = true, Filter = filter }; Search search = table.Query(config); List<Document> documentList = new List<Document>(); do { documentList = search.GetNextSet(); Console.WriteLine("\nFindRepliesPostedWithinTimePeriod: printing replies posted within dates: {0} and {1} ............", startDate, endDate); foreach (var document in documentList) { PrintDocument(document); } } while (!search.IsDone); }
private static void FindRepliesInLast15DaysWithConfig(Table table, string forumName, string threadName) { DateTime twoWeeksAgoDate = DateTime.UtcNow - TimeSpan.FromDays(15); QueryFilter filter = new QueryFilter("Id", QueryOperator.Equal, forumName + "#" + threadName); filter.AddCondition("ReplyDateTime", QueryOperator.GreaterThan, twoWeeksAgoDate); // You are specifying optional parameters so use QueryOperationConfig. QueryOperationConfig config = new QueryOperationConfig() { Filter = filter, // Optional parameters. Select = SelectValues.SpecificAttributes, AttributesToGet = new List<string> { "Message", "ReplyDateTime", "PostedBy" }, ConsistentRead = true }; Search search = table.Query(config); List<Document> documentSet = new List<Document>(); do { documentSet = search.GetNextSet(); Console.WriteLine("\nFindRepliesInLast15DaysWithConfig: printing ............"); foreach (var document in documentSet) PrintDocument(document); } while (!search.IsDone); }
/// <summary> /// Returns messages that have been successfully dispatched. Eventually consistent. /// </summary> /// <param name="millisecondsDispatchedSince">How long ago was the message dispatched?</param> /// <param name="pageSize">How many messages returned at once?</param> /// <param name="pageNumber">Which page of the dispatched messages to return?</param> /// <param name="outboxTimeout"></param> /// <param name="args">Used to pass through the topic we are searching for messages in. Use Key: "Topic"</param> /// <returns>A list of dispatched messages</returns> public IEnumerable <Message> DispatchedMessages( double millisecondsDispatchedSince, int pageSize = 100, int pageNumber = 1, int outboxTimeout = -1, Dictionary <string, object> args = null) { if (args == null) { throw new ArgumentException("Missing required argument", nameof(args)); } var sinceTime = DateTime.UtcNow.Subtract(TimeSpan.FromMilliseconds(millisecondsDispatchedSince)); var topic = (string)args["Topic"]; //in theory this is all values on that index that have a Delivered data (sparse index) //we just need to filter for ones in the right date range //As it is a GSI it can't use a consistent read var queryConfig = new QueryOperationConfig { IndexName = _configuration.DeliveredIndexName, KeyExpression = new KeyTopicDeliveredTimeExpression().Generate(topic, sinceTime), ConsistentRead = false }; //block async to make this sync var messages = PageAllMessagesAsync(queryConfig).Result.ToList(); return(messages.Select(msg => msg.ConvertToMessage())); }
/// <summary> /// Fetch the specified metadata field from the metadata DynamoDB table /// </summary> /// <param name="searchkey"></param> /// <returns></returns> public string FetchMetadata(string searchkey) { QueryFilter filter = new QueryFilter(); filter.AddCondition(Constants.KEY, QueryOperator.Equal, searchkey); QueryOperationConfig config = new QueryOperationConfig(); config.Select = SelectValues.AllAttributes; config.Filter = filter; Search searchRes = metadataTable.Query(config); List <Document> documentSet = new List <Document>(); do { try { // We can assume there is only one entry per key. // Hence, we are fetching the first item as required documentSet = searchRes.GetNextSetAsync().Result; Console.WriteLine("\nFindRepliesInLast15DaysWithConfig: printing ............"); return(documentSet[0][Constants.VALUE]); } catch (Exception ex) { Logger.AddLog(ex.ToString()); } } while (!searchRes.IsDone); return(String.Empty); }
public async Task <IList <T> > Get <T>(string tableName, QueryFilter queryFilter, string paginationToken = null, bool consistentRead = true, CancellationToken cancellationToken = default) where T : class { var table = Table.LoadTable(AmazonDynamoDBClient, tableName); var scanOps = new ScanOperationConfig(); if (!string.IsNullOrEmpty(paginationToken)) { scanOps.PaginationToken = paginationToken; } QueryOperationConfig config = new QueryOperationConfig() { Filter = queryFilter, Select = SelectValues.SpecificAttributes, AttributesToGet = new List <string> { AttributeValue }, ConsistentRead = consistentRead }; var results = table.Query(config); List <Document> data = await results.GetNextSetAsync(cancellationToken).ConfigureAwait(false); return(TransformData <T>(data)); }
public async Task <double?> GetTotalCashFlowForAccountAsync(string accountId) { var table = Table.LoadTable(_dynamoDb, "AccountBalances"); var query = new QueryOperationConfig(); query.IndexName = "CFAccountId-Date-index"; query.KeyExpression = new Expression(); query.KeyExpression.ExpressionStatement = "CFAccountId = :v_accountId"; query.KeyExpression.ExpressionAttributeValues[":v_accountId"] = accountId; query.AttributesToGet = new List <string> { "CF" }; query.Select = SelectValues.SpecificAttributes; double?result = null; var search = table.Query(query); do { var list = await search.GetNextSetAsync(); foreach (var doc in list) { if (doc != null && !doc.ContainsKey("CF") && doc["CF"] != null) { result = result.HasValue ? result + doc["CF"].AsDouble() : doc["CF"].AsDouble(); } } } while (!search.IsDone); return(result); }
private Search ConvertFromQuery <T>(QueryOperationConfig queryConfig, DynamoDBOperationConfig operationConfig) { Table table = GetTargetTableInternal <T>(operationConfig); Search search = table.Query(queryConfig); return(search); }
private ContextSearch ConvertQueryHelper <T>(DynamoDBFlatConfig currentConfig, ItemStorageConfig storageConfig, QueryFilter filter, List <string> indexNames) { Table table = GetTargetTable(storageConfig, currentConfig); string indexName = GetQueryIndexName(currentConfig, indexNames); var queryConfig = new QueryOperationConfig { Filter = filter, ConsistentRead = currentConfig.ConsistentRead.Value, BackwardSearch = currentConfig.BackwardQuery.Value, IndexName = indexName, ConditionalOperator = currentConfig.ConditionalOperator }; if (string.IsNullOrEmpty(indexName)) { queryConfig.Select = SelectValues.SpecificAttributes; List <string> attributesToGet = storageConfig.AttributesToGet; queryConfig.AttributesToGet = attributesToGet; } else { queryConfig.Select = SelectValues.AllProjectedAttributes; } Search query = table.Query(queryConfig); return(new ContextSearch(query, currentConfig)); }
public IEnumerable <LeaderBoardEntry> GetLeaderBoard(string match, int duration) { DynamoDBContext context = new DynamoDBContext(_client); QueryFilter filter = new QueryFilter(); filter.AddCondition("Match", QueryOperator.Equal, match); filter.AddCondition("TimeStamp", QueryOperator.GreaterThanOrEqual, DateTime.Now.AddHours(-duration)); QueryOperationConfig queryOperationConfig = new QueryOperationConfig { Filter = filter, IndexName = "LeaderBoardIndex" }; var playerStats = context.FromQueryAsync <PlayerStat>(queryOperationConfig).GetNextSetAsync().GetAwaiter().GetResult(); playerStats.Sort((x, y) => { return(y.Score.CompareTo(x.Score)); }); List <LeaderBoardEntry> leaderBoardEntries = new List <LeaderBoardEntry>(); foreach (var playerStat in playerStats) { if (leaderBoardEntries.Find(item => item.UserName == playerStat.UserName) == null) { leaderBoardEntries.Add(new LeaderBoardEntry { Kills = playerStat.Kills, Score = playerStat.Score, UserName = playerStat.UserName, Rank = leaderBoardEntries.FindAll(item => item.Score > playerStat.Score).Count + 1 }); } } return(leaderBoardEntries); }
public static async Task <PagedResult <TEntity> > GetPagedQueryResultsAsync <TEntity>( this IDynamoDBContext dynamoDbContext, QueryOperationConfig queryConfig) where TEntity : class { var dbResults = new List <TEntity>(); var table = dynamoDbContext.GetTargetTable <TEntity>(); var search = table.Query(queryConfig); var resultsSet = await search.GetNextSetAsync().ConfigureAwait(false); var paginationToken = search.PaginationToken; if (resultsSet.Any()) { dbResults.AddRange(dynamoDbContext.FromDocuments <TEntity>(resultsSet)); // Look ahead for any more, but only if we have a token if (!string.IsNullOrEmpty(PaginationDetails.EncodeToken(paginationToken))) { queryConfig.PaginationToken = paginationToken; queryConfig.Limit = 1; search = table.Query(queryConfig); resultsSet = await search.GetNextSetAsync().ConfigureAwait(false); if (!resultsSet.Any()) { paginationToken = null; } } } return(new PagedResult <TEntity>(dbResults, new PaginationDetails(paginationToken))); }
/// <summary> /// Executes a Query operation against DynamoDB, finding items /// that match the specified conditions. /// </summary> /// <typeparam name="T">Type of object.</typeparam> /// <param name="queryConfig">Query request object.</param> /// <param name="operationConfig">Config object which can be used to override the table used.</param> /// <returns>Lazy-loaded collection of results.</returns> public IEnumerable<T> FromQuery<T>(QueryOperationConfig queryConfig, DynamoDBOperationConfig operationConfig) { if (queryConfig == null) throw new ArgumentNullException("queryConfig"); var search = ConvertFromQuery<T>(queryConfig, operationConfig); return FromSearch<T>(search); }
public String[] Query(String tableName, String expression = null, String[] attributes = null) { var results = new List <string>(); Table table = Table.LoadTable(client, tableName); QueryOperationConfig config = new QueryOperationConfig() { Select = SelectValues.AllAttributes, ConsistentRead = true }; if (attributes != null) { config.Select = SelectValues.SpecificAttributes; config.AttributesToGet = new List <string> (attributes); } if (expression != null) { config.Filter = CreateFilter(expression); } Search tableResult = table.Query(config); var docs = tableResult.GetRemaining(); foreach (Document doc in docs) { results.Add(doc.ToJson()); } return(results.ToArray()); }
public async Task <Product> GetById(string id) { var table = Table.LoadTable(dbClient, "products_id_sku"); var filter = new QueryFilter(); filter.AddCondition("id", QueryOperator.Equal, id); var queryConfig = new QueryOperationConfig { Filter = filter, Limit = 1 }; var result = table.Query(queryConfig); if (result.IsDone) { throw new ArgumentException("sku"); } else { var querySet = await result.GetNextSetAsync(); var document = querySet.First(); Product product = ProductFromDocument(document); return(product); } }
private ContextSearch ConvertFromQuery <T>(QueryOperationConfig queryConfig, DynamoDBOperationConfig operationConfig) { DynamoDBFlatConfig flatConfig = new DynamoDBFlatConfig(operationConfig, Config); Table table = GetTargetTableInternal <T>(operationConfig); Search search = table.Query(queryConfig); return(new ContextSearch(search, flatConfig)); }
protected static QueryOperationConfig CreateQueryConfiguration(QueryFilter filter) { var queryOperationConfig = new QueryOperationConfig { Filter = filter }; return(queryOperationConfig); }
private void FindRepliesPostedWithinTimePeriod(Table table, string forumName, string threadSubject) { this.displayMessage += "\n***FindRepliesPostedWithinTimePeriod***\n"; if (replyTable == null) { this.displayMessage += "\nLoad table before running query"; return; } ThreadPool.QueueUserWorkItem((s) => { try { this.displayMessage = "\nRunning on background thread"; DateTime startDate = DateTime.UtcNow.Subtract(new TimeSpan(21, 0, 0, 0)); DateTime endDate = DateTime.UtcNow.Subtract(new TimeSpan(1, 0, 0, 0)); QueryFilter filter = new QueryFilter("Id", QueryOperator.Equal, forumName + "#" + threadSubject); filter.AddCondition("ReplyDateTime", QueryOperator.Between, startDate, endDate); QueryOperationConfig config = new QueryOperationConfig() { Limit = 2, // 2 items/page. Select = SelectValues.SpecificAttributes, AttributesToGet = new List <string> { "Message", "ReplyDateTime", "PostedBy" }, ConsistentRead = true, Filter = filter }; Search search = table.Query(config); List <Document> documentList = new List <Document>(); do { documentList = search.GetNextSet(); this.displayMessage += String.Format("\nFindRepliesPostedWithinTimePeriod: printing replies posted within dates: {0} and {1} ............", startDate, endDate); foreach (var document in documentList) { PrintDocument(document); } } while (!search.IsDone); this.displayMessage += "\nCompleted !\n"; } catch (Exception ex) { this.displayMessage += "\nFindRepliesPostedWithinTimePeriod:" + ex.Message; Debug.LogException(ex); } }); }
private void AddQueryFilterCondition(string name, QueryOperator operation, IEnumerable <object> values) { Primitive primitiveValue = null; DynamoDBEntry[] primitiveValues = null; if (values != null) { var valuesArray = values.ToArray(); if (valuesArray.Length == 1) { primitiveValue = converter.ToPrimative(valuesArray.First()); } else { primitiveValues = valuesArray.Select(v => (DynamoDBEntry)converter.ToPrimative(v)).ToArray(); } } if (queryOperation == null) { if (!IsKeyField(name)) { throw new ApplicationException("The first Filter must be a key field"); } queryOperation = new QueryOperationConfig(); if (indexName != null) { queryOperation.IndexName = indexName; if (indexAttributes != null) { queryOperation.Select = SelectValues.SpecificAttributes; queryOperation.AttributesToGet = indexAttributes; } } if (primitiveValue != null) { queryOperation.Filter = new QueryFilter(name, operation, primitiveValue); } else if (primitiveValues != null) { queryOperation.Filter = new QueryFilter(name, operation, primitiveValues); } } else { if (primitiveValue != null) { queryOperation.Filter.AddCondition(name, operation, primitiveValue); } else if (primitiveValues != null) { queryOperation.Filter.AddCondition(name, operation, primitiveValues); } } }
public Task <List <Match> > GetTeamSeasonAwayMatches(short year, byte season, int teamId) { var awayGamesQuery = new QueryOperationConfig { IndexName = "AwayTeamMatches", Filter = new QueryFilter("AwayTeamIdYearSeason", QueryOperator.Equal, $"{teamId}#{year}#{season}") }; return(Context.FromQueryAsync <Match>(awayGamesQuery).GetRemainingAsync()); }
private Search TryExecuteQuery(TranslationResult translationResult, Type entityType) { QueryFilter queryFilter; string indexName; // if we failed to compose a query with table's keys and local secondary indexes if (!translationResult.TryGetQueryFilterForTable(this.TableDefinition, out queryFilter, out indexName)) { // then trying to find a suitable Global Secondary Index var matchingIndex = this.TableDefinition .GlobalSecondaryIndexes.Values .FirstOrDefault ( index => translationResult.TryGetQueryFilterForGlobalSeconaryIndex(index, out queryFilter) ); if (matchingIndex == null) { return(null); } indexName = matchingIndex.IndexName; } var queryConfig = new QueryOperationConfig { Filter = queryFilter, CollectResults = false, ConsistentRead = this._consistentRead, IndexName = indexName, FilterExpression = translationResult.CustomizationHooks.CustomFilterExpression }; // if a projection is specified - then getting only the required list of fields if (translationResult.AttributesToGet != null) { queryConfig.Select = SelectValues.SpecificAttributes; queryConfig.AttributesToGet = translationResult.AttributesToGet; } translationResult.CustomizationHooks.ConfigureQueryOperationCallback?.Invoke(queryConfig); var searchResult = this.TableDefinition.Query(queryConfig); if (string.IsNullOrEmpty(queryConfig.IndexName)) { this.Log("DynamoDb query: {0}", translationResult); } else { this.Log("DynamoDb index query: {0}. Index name: {1}", translationResult, queryConfig.IndexName); } return(searchResult); }
/// <summary> /// Configures an async Query operation against DynamoDB, finding items /// that match the specified conditions. /// </summary> /// <typeparam name="T">Type of object.</typeparam> /// <param name="queryConfig">Query request object.</param> /// <param name="operationConfig">Config object which can be used to override the table used.</param> /// <returns>AsyncSearch which can be used to retrieve DynamoDB data.</returns> public AsyncSearch <T> FromQueryAsync <T>(QueryOperationConfig queryConfig, DynamoDBOperationConfig operationConfig) { if (queryConfig == null) { throw new ArgumentNullException("queryConfig"); } var query = ConvertFromQuery <T>(queryConfig, operationConfig); return(FromSearchAsync <T>(query)); }