Beispiel #1
0
        public void OneExtra()
        {
            var starts = new List <int>();
            var ends   = new List <int>();

            var count = Chunker.ExecuteInChunks(1001, (startList, endList, s, e) =>
            {
                startList.Add(s);
                endList.Add(e);
                return(1);
            }, starts, ends);

            Assert.AreEqual(0, starts[0]);
            Assert.AreEqual(499, ends[0]);

            Assert.AreEqual(500, starts[1]);
            Assert.AreEqual(999, ends[1]);

            Assert.AreEqual(1000, starts[2]);
            Assert.AreEqual(1000, ends[2]);

            Assert.AreEqual(3, starts.Count);
            Assert.AreEqual(3, ends.Count);
            Assert.AreEqual(3, count);
        }
        public void Chunking()
        {
            var starts = new List <int>();
            var ends   = new List <int>();

            var count = Chunker.ExecuteInChunks(1500, (startList, endList, s, e) =>
            {
                startList.Add(s);
                endList.Add(e);
                return(1);
            }, starts, ends, CancellationToken.None);

            Assert.AreEqual(0, starts[0]);
            Assert.AreEqual(499, ends[0]);

            Assert.AreEqual(500, starts[1]);
            Assert.AreEqual(999, ends[1]);

            Assert.AreEqual(1000, starts[2]);
            Assert.AreEqual(1499, ends[2]);

            Assert.AreEqual(3, starts.Count);
            Assert.AreEqual(3, ends.Count);
            Assert.AreEqual(3, count);
        }
Beispiel #3
0
        public void LessThenChunkSize()
        {
            List <int> starts = new List <int>();
            List <int> ends   = new List <int>();

            Chunker.ExecuteInChunks(1, (s, e) =>
            {
                starts.Add(s);
                ends.Add(e);
            });

            Assert.AreEqual(0, starts[0]);
            Assert.AreEqual(0, ends[0]);

            Assert.AreEqual(1, starts.Count);
            Assert.AreEqual(1, ends.Count);
        }
Beispiel #4
0
        public void LessThenChunkSize()
        {
            var starts = new List <int>();
            var ends   = new List <int>();

            var count = Chunker.ExecuteInChunks(1, (startList, endList, s, e) =>
            {
                startList.Add(s);
                endList.Add(e);
                return(1);
            }, starts, ends);

            Assert.AreEqual(0, starts[0]);
            Assert.AreEqual(0, ends[0]);

            Assert.AreEqual(1, starts.Count);
            Assert.AreEqual(1, ends.Count);
            Assert.AreEqual(1, count);
        }
Beispiel #5
0
        public void OneExtra()
        {
            List <int> starts = new List <int>();
            List <int> ends   = new List <int>();

            Chunker.ExecuteInChunks(1001, (s, e) =>
            {
                starts.Add(s);
                ends.Add(e);
            });

            Assert.AreEqual(0, starts[0]);
            Assert.AreEqual(499, ends[0]);

            Assert.AreEqual(500, starts[1]);
            Assert.AreEqual(999, ends[1]);

            Assert.AreEqual(1000, starts[2]);
            Assert.AreEqual(1000, ends[2]);

            Assert.AreEqual(3, starts.Count);
            Assert.AreEqual(3, ends.Count);
        }
        public static void Clean(int deletionBatchSize, DocumentDatabase database, DateTime expiryThreshold, CancellationToken token)
        {
            var stopwatch = Stopwatch.StartNew();
            var items     = new List <ICommandData>(deletionBatchSize);

            try
            {
                var query = new IndexQuery
                {
                    Start          = 0,
                    DisableCaching = true,
                    Cutoff         = SystemTime.UtcNow,
                    PageSize       = deletionBatchSize,
                    Query          = $"LastModified:[* TO {expiryThreshold.Ticks}]",
                    FieldsToFetch  = new[]
                    {
                        "__document_id"
                    },
                    SortedFields = new[]
                    {
                        new SortedField("LastModified")
                        {
                            Field      = "LastModified",
                            Descending = false
                        }
                    }
                };
                var indexName = new ExpirySagaAuditIndex().IndexName;
                database.Query(indexName, query, token,
                               (doc, commands) =>
                {
                    var id = doc.Value <string>("__document_id");
                    if (string.IsNullOrEmpty(id))
                    {
                        return;
                    }

                    commands.Add(new DeleteCommandData
                    {
                        Key = id
                    });
                }, items);
            }
            catch (OperationCanceledException)
            {
                logger.Info("Cleanup operation cancelled");
                return;
            }

            if (token.IsCancellationRequested)
            {
                return;
            }

            var deletionCount = Chunker.ExecuteInChunks(items.Count, (itemsForBatch, db, s, e) =>
            {
                if (logger.IsDebugEnabled)
                {
                    logger.Debug($"Batching deletion of {s}-{e} saga history documents.");
                }

                var results = db.Batch(itemsForBatch.GetRange(s, e - s + 1), CancellationToken.None);
                if (logger.IsDebugEnabled)
                {
                    logger.Debug($"Batching deletion of {s}-{e} saga history documents completed.");
                }

                return(results.Count(x => x.Deleted == true));
            }, items, database, token);

            if (deletionCount == 0)
            {
                logger.Info("No expired saga history documents found");
            }
            else
            {
                logger.Info($"Deleted {deletionCount} expired saga history documents. Batch execution took {stopwatch.ElapsedMilliseconds} ms");
            }
        }