Scan filter.
Inheritance: Filter
Esempio n. 1
0
 /// <summary>
 /// Initiates a Search object to Scan a DynamoDB table, with the
 /// specified filter.
 ///
 /// No calls are made until the Search object is used.
 /// </summary>
 /// <param name="filter">Filter to apply to the scan.</param>
 /// <returns>Resultant Search container.</returns>
 public Search Scan(ScanFilter filter)
 {
     return(Scan(new ScanOperationConfig {
         Filter = filter
     }));
 }
Esempio n. 2
0
 /// <summary>
 /// Initiates a Search object to Scan a DynamoDB table, with the
 /// specified filter.
 /// 
 /// No calls are made until the Search object is used.
 /// </summary>
 /// <param name="filter">Filter to apply to the scan.</param>
 /// <returns>Resultant Search container.</returns>
 public Search Scan(ScanFilter filter)
 {
     return Scan(new ScanOperationConfig { Filter = filter });
 }
 /// <summary>
 /// Initializes a default Table.Scan config object
 /// Filter is empty, Limit is Int32.MaxValue
 /// </summary>
 public ScanOperationConfig()
 {
     Filter = new ScanFilter();
     Limit = Int32.MaxValue;
     Select = SelectValues.AllAttributes;
     ConditionalOperator = ConditionalOperatorValues.And;
 }
        /// <summary>
        /// Prepares parameters for a scan operation from the list of conditions
        /// </summary>
        internal static ScanFilter GetScanFilterForTable(this TranslationResult translationResult, Table tableDefinition)
        {
            // the last thing to do is to make a full scan
            var scanFilter = new ScanFilter();

            //TODO: check for BETWEEN operator
            foreach (var condition in translationResult.Conditions.Flatten())
            {
                if 
                (
                    (condition.Item2.Values.Length == 1)
                    &&
                    (condition.Item2.Values[0] == null)
                )
                {
                    switch (condition.Item2.Operator)
                    {
                    case ScanOperator.Equal:
                        scanFilter.AddCondition(condition.Item1, ScanOperator.IsNull );
                    break;
                    case ScanOperator.NotEqual:
                        scanFilter.AddCondition(condition.Item1, ScanOperator.IsNotNull);
                    break;
                    default:
                        throw new InvalidOperationException(string.Format("You cannot use {0} operator with null value", condition.Item2.Operator));
                    }
                }
                else
                {
                    scanFilter.AddCondition(condition.Item1, condition.Item2.Operator, condition.Item2.Values);
                }
            }

            return scanFilter;
        }
        public void CommandSubscribe() {
            Table replyTable = Table.LoadTable(client, tableName);

            Console.WriteLine("Listening to the events continuously.");

            Int64 last_key = 0;
            while (true) {
                var scan_filter = new ScanFilter();
                if (last_key > 0) {
                    scan_filter.AddCondition("K", ScanOperator.GreaterThan, last_key);
                }
                Search search = replyTable.Scan(scan_filter);

                List<Document> documentList = new List<Document>();
                do {
                    documentList = search.GetNextSet();
                    foreach (var document in documentList) {
                        Console.WriteLine("{0} : {1}", document["K"], document["V"]);
                        Int64 current_key = Int64.Parse(document["K"].AsString());
                        if (!(current_key > last_key)) {
                            Console.WriteLine("ERROR: {0} should be > {1}.", current_key, last_key);
                        }
                        else {
                            last_key = current_key;
                        }
                    }

                } while (!search.IsDone);

                // TODO(dkorolev): Do something smarter here, and wrap the whole thing into an IObservable<>.
                System.Threading.Thread.Sleep(50);
            }
        }
        public static void RunDocumentModelSample()
        {
            Console.WriteLine("Loading Businesses table");
            Table table = Table.LoadTable(client, "Businesses");

            Console.WriteLine("Creating and saving first item");
            Document chainStore2 = new Document();
            chainStore2["Name"] = "Big Sales Inc";
            chainStore2["Id"] = 2;
            chainStore2["Owner"] = "Big Sales Corp";
            chainStore2["Managers"] = new List<string> { "Samantha Jones", "Richard Frost" };
            chainStore2["FoundedDate"] = new DateTime(1980, 7, 4);
            chainStore2["Address"] = "123 Main Street, New York, New York";
            chainStore2["Employees"] = 46;
            chainStore2["State"] = "NY";
            table.PutItem(chainStore2);

            Console.WriteLine("Creating and saving first item");
            Document chainStore13 = new Document();
            chainStore13["Name"] = "Big Sales Inc";
            chainStore13["Id"] = 13;
            chainStore13["Owner"] = "Big Sales Corp";
            chainStore13["Managers"] = new List<string> { "Anil Garg", "Alex Short" };
            chainStore13["FoundedDate"] = new DateTime(1999, 3, 15);
            chainStore13["Address"] = "1999 West Ave, Chicago, Illinois";
            chainStore13["Employees"] = 54;
            chainStore13["State"] = "IL";
            table.PutItem(chainStore13);

            Console.WriteLine("Creating and saving second item");
            Document tinyDiner = new Document();
            tinyDiner["Name"] = "Tiny Map-themed Diner";
            tinyDiner["Id"] = 0;
            tinyDiner["Owner"] = "John Doe";
            tinyDiner["FoundedDate"] = new DateTime(1974, 12, 10);
            tinyDiner["Address"] = "800 Lincoln Ave, Seattle, Washington";
            tinyDiner["State"] = "WA";
            table.PutItem(tinyDiner);


            Console.WriteLine("Creating and saving third item");
            Document internetStore = new Document();
            internetStore["Name"] = "Best Online Store Ever";
            internetStore["Id"] = 0;
            internetStore["Owner"] = "Jane Doe";
            internetStore["FoundedDate"] = new DateTime(1994, 2, 19);
            internetStore["Employees"] = 5;
            internetStore["Url"] = "http://www.best-online-store-ever.fake";
            internetStore["Phone"] = "425-555-1234";
            table.PutItem(internetStore);


            Console.WriteLine("Loading item");
            Document doc1 = table.GetItem("Big Sales Inc", 2);
            Console.WriteLine("Attribute counts match (should be true): " +
                (chainStore2.GetAttributeNames().Count == doc1.GetAttributeNames().Count));

            Console.WriteLine("Loading item...");
            Document doc2 = table.GetItem("Best Online Store Ever", 0);
            Console.WriteLine("Attribute counts match (should be true): " +
                (chainStore2.GetAttributeNames().Count == doc1.GetAttributeNames().Count));
            Console.WriteLine("Change item: remove one attribute, add one, modify one attribute");
            doc2["Phone"] = null;
            doc2["Twitter"] = "best-online-store-ever";
            doc2["Employees"] = 4;
            Console.WriteLine("Updating item");
            table.UpdateItem(doc2);

            Console.WriteLine("Reloading item");
            doc2 = table.GetItem("Best Online Store Ever", 0);
            Console.WriteLine("Phone attribute present (should be false): " + doc2.Contains("Phone"));
            Console.WriteLine("Twitter attribute present (should be true): " + doc2.Contains("Twitter"));
            Console.WriteLine("Employees attribute equals 4: " + (object.Equals(doc2["Employees"].AsPrimitive().Value, "4")));

            Console.WriteLine("Loading nonexistent item");
            Document doc3 = table.GetItem("Big Sales Inc", 3);
            Console.WriteLine("Returned document == null (should be true): " + (doc3 == null));



            Search query;
            Console.WriteLine();
            Console.WriteLine("Querying for items (Equals)");
            query = table.Query("Big Sales Inc", new QueryFilter("Id", QueryOperator.Equal, 2));
            List<Document> queryItems1 = query.GetRemaining();
            Console.WriteLine("Number of items returned (should be 1): " + queryItems1.Count);

            Console.WriteLine();
            Console.WriteLine("Querying for items (Between)");
            QueryFilter filter = new QueryFilter();
            filter.AddCondition("Name", QueryOperator.Equal, "Big Sales Inc");
            filter.AddCondition("Id", QueryOperator.Between, 0, 15);
            QueryOperationConfig queryConfig = new QueryOperationConfig
            {
                Filter = filter,
                Limit = 1
            };
            query = table.Query(queryConfig);
            int totalItems = 0;
            while (!query.IsDone)
            {
                Console.WriteLine("Retrieving next set (page) of items");
                List<Document> querySet = query.GetNextSet();
                Console.WriteLine("Number of items returned in set (should be 1, unless last set, which will be 0): " + querySet.Count);

                foreach (Document doc in querySet)
                {
                    Console.WriteLine("Retrieving individual properties");
                    Primitive name = doc["Name"].AsPrimitive();
                    Primitive id = doc["Id"].AsPrimitive();
                    PrimitiveList managers = doc["Managers"].AsPrimitiveList();
                    Console.WriteLine("Name = {0}, Id = {1}, # of managers = {2}", name.Value, id.Value, managers.Entries.Count);
                    totalItems++;
                }
            }
            Console.WriteLine("Total items found (should be 2): " + totalItems);



            Search scan;
            ScanFilter scanFilter;

            Console.WriteLine();
            Console.WriteLine("Scanning for items (GreaterThan)");
            scanFilter = new ScanFilter();
            scanFilter.AddCondition("Employees", ScanOperator.GreaterThan, 50);
            scan = table.Scan(scanFilter);
            List<Document> scanItems1 = scan.GetRemaining();
            Console.WriteLine("Number of items returned (should be 1): " + scanItems1.Count);

            Console.WriteLine();
            Console.WriteLine("Scanning for items (GreaterThan and LessThan)");
            scanFilter = new ScanFilter();
            scanFilter.AddCondition("Employees", ScanOperator.GreaterThan, 2);
            scanFilter.AddCondition("FoundedDate", ScanOperator.LessThan, new DateTime(1993, 1, 1));
            scan = table.Scan(scanFilter);
            List<Document> scanItems2 = scan.GetRemaining();
            Console.WriteLine("Number of items returned (should be 1): " + scanItems2.Count);


            Console.WriteLine();
            Console.WriteLine("Retrieving an item");
            Document existingDoc = table.GetItem("Big Sales Inc", 13);
            Console.WriteLine("Returned document == null (should be false): " + (existingDoc == null));
            Console.WriteLine("Deleting item");
            table.DeleteItem("Big Sales Inc", 13);
            Console.WriteLine("Retrieving same item");
            existingDoc = table.GetItem("Big Sales Inc", 13);
            Console.WriteLine("Returned document == null (should be true): " + (existingDoc == null));


            Console.WriteLine();
            Console.WriteLine("Scanning for items (no filter) and deleting all");
            scanFilter = new ScanFilter();
            scan = table.Scan(scanFilter);
            List<Document> scanItems3 = scan.GetRemaining();
            Console.WriteLine("Number of items returned (should be 3): " + scanItems3.Count);
            for (int i = 0; i < scanItems3.Count; i++)
            {
                Document item = scanItems3[i];
                Console.WriteLine("Deleting item {0} of {1}", i + 1, scanItems3.Count);
                table.DeleteItem(item);
            }

            Console.WriteLine("Scanning table again");
            scan = table.Scan(scanFilter);
            scanItems3 = scan.GetRemaining();
            Console.WriteLine("Number of items returned (should be 0): " + scanItems3.Count);
        }
Esempio n. 7
0
 /// <summary>
 /// Initializes a default Table.Scan config object
 /// Filter is empty, Limit is Int32.MaxValue
 /// </summary>
 public ScanOperationConfig()
 {
     Filter = new ScanFilter();
     Limit  = Int32.MaxValue;
     Select = SelectValues.AllAttributes;
 }
        /// <summary>
        /// A utility method for cleaning up expired sessions that IIS failed to delete. The method performs a scan on the table
        /// with a condition that the expiration date is in the past and calls delete on all the keys returned. Scans can be costly on performance
        /// so use this method sparingly like a nightly or weekly clean job.
        /// </summary>
        /// <param name="dbClient">The AmazonDynamoDB client used to find a delete expired sessions.</param>
        /// <param name="tableName">The table to search.</param>
        public static void DeleteExpiredSessions(IAmazonDynamoDB dbClient, string tableName)
        {
            LogInfo("DeleteExpiredSessions");
            Table table = Table.LoadTable(dbClient, tableName, DynamoDBEntryConversion.V1);

            ScanFilter filter = new ScanFilter();
            filter.AddCondition(ATTRIBUTE_EXPIRES, ScanOperator.LessThan, DateTime.Now);

            ScanOperationConfig config = new ScanOperationConfig();
            config.AttributesToGet = new List<string> { ATTRIBUTE_SESSION_ID };
            config.Select = SelectValues.SpecificAttributes;
            config.Filter = filter;

            DocumentBatchWrite batchWrite = table.CreateBatchWrite();
            Search search = table.Scan(config);

            do
            {
                List<Document> page = search.GetNextSet();
                foreach (var document in page)
                {
                    batchWrite.AddItemToDelete(document);
                }
            } while (!search.IsDone);

            batchWrite.Execute();
        }