コード例 #1
0
        /// <summary>
        /// Performs validation of the <paramref name="rules"/>.
        /// </summary>
        /// <param name="rules"> The set of retention rules to validate. </param>
        public void Validate([NotNull, ItemNotNull] IReadOnlyCollection <RetentionRule> rules)
        {
            AssertArg.NotNullOrEmpty(rules, nameof(rules));
            AssertArg.NoNullItems(rules, nameof(rules));

            AssertNoDuplicates(rules);

            AssertNoContradictions(rules);
        }
コード例 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AppConfig"/> class.
        /// </summary>
        /// <param name="cleanupDirectoryPath">
        /// The path to the directory to cleanup.
        /// </param>
        /// <param name="retentionRules">
        /// The sequence of retention rules.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="cleanupDirectoryPath"/> is <see langword="null"/> or empty or whitespace or
        /// <paramref name="retentionRules"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="retentionRules"/> contains a <see langword="null"/> item.
        /// </exception>
        public AppConfig(
            [NotNull] string cleanupDirectoryPath,
            [NotNull, ItemNotNull] IReadOnlyCollection <RetentionRule> retentionRules)
        {
            AssertArg.NotNullOrWhiteSpace(cleanupDirectoryPath, nameof(cleanupDirectoryPath));
            AssertArg.NotNull(retentionRules, nameof(retentionRules));
            AssertArg.NoNullItems(retentionRules, nameof(retentionRules));

            CleanupDirectoryPath = cleanupDirectoryPath;
            RetentionRules       = retentionRules.ToReadOnlyList();
        }
コード例 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RetentionPolicy" /> class.
        /// </summary>
        /// <param name="rules">
        /// The collection of rules that define the policy.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="rules"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Any item in the <paramref name="rules"/> is <see langword="null"/> or
        /// <paramref name="rules"/> is empty or
        /// <paramref name="rules"/> contains duplicate or mutually contradictory items.
        /// </exception>
        public RetentionPolicy([NotNull, ItemNotNull] IReadOnlyCollection <RetentionRule> rules)
        {
            AssertArg.NotNullOrEmpty(rules, nameof(rules));
            AssertArg.NoNullItems(rules, nameof(rules));

            Validator.Validate(rules);

            _orderedRules = rules
                            .OrderBy(r => r.OlderThan)
                            .ToReadOnlyList();
        }
コード例 #4
0
        /// <summary>
        /// Finds expired resources among the <paramref name="resources"/> specified.
        /// </summary>
        /// <typeparam name="T"> The type of an identifier of a resource. </typeparam>
        /// <param name="resources">The sequence resources.</param>
        /// <returns> The sequence of identifiers of expired resources. </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="resources"/> is <see langword="null"/>.
        /// </exception>
        public IEnumerable <T> FindExpiredResources <T>(IReadOnlyCollection <IResource <T> > resources)
        {
            AssertArg.NotNull(resources, nameof(resources));
            AssertArg.NoNullItems(resources, nameof(resources));

            var orderedResources       = resources.OrderBy(i => i.Age).ToArray();
            var examineScopeStartIndex = 0;
            var examineScopeLength     = orderedResources.Length;

            foreach (var rule in _orderedRules)
            {
                if (examineScopeLength <= 0)
                {
                    break;
                }

                var examineScopeResources = orderedResources
                                            .Skip(examineScopeStartIndex)
                                            .Take(examineScopeLength)
                                            .ToArray();

                var outOfRuleCount = examineScopeResources
                                     .TakeWhile(i => i.Age <= rule.OlderThan)
                                     .Count();

                var retainCount = examineScopeResources
                                  .Skip(outOfRuleCount)
                                  .Take(rule.AllowedAmount)
                                  .Count();

                examineScopeStartIndex += outOfRuleCount;
                examineScopeLength      = retainCount;
            }

            var totalRetainCount =
                examineScopeStartIndex +
                examineScopeLength;

            return(orderedResources
                   .Skip(totalRetainCount)
                   .Select(i => i.Id));
        }