Пример #1
0
        /// <summary>
        /// Creates a necessary partition if it does not exist.
        /// </summary>
        public static void CreatePartition(NpgsqlConnection conn, string tableName,
                                           DateTime today, PartitionSize partitionSize, out string partitionName)
        {
            DateTime startDate;
            DateTime endDate;

            if (partitionSize == PartitionSize.OneMonth)
            {
                startDate = new DateTime(today.Year, today.Month, 1, 0, 0, 0, DateTimeKind.Utc);
                endDate   = startDate.AddMonths(1);
            }
            else // PartitionSize.OneYear
            {
                startDate = new DateTime(today.Year, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                endDate   = startDate.AddYears(1);
            }

            partitionName = tableName +
                            "_" + startDate.ToString(PartitionDateFormat) +
                            "_" + endDate.ToString(PartitionDateFormat);

            new NpgsqlCommand(
                $"CREATE TABLE IF NOT EXISTS {partitionName} PARTITION OF {tableName} " +
                $"FOR VALUES FROM('{startDate:yyyy-MM-dd} 00:00:00Z') TO ('{endDate:yyyy-MM-dd} 00:00:00Z')",
                conn).ExecuteNonQuery();
        }
Пример #2
0
 public static IEnumerable <IReadOnlyCollection <T> > PartitionUnique <T>([NotNull] this ICollection <T> thisValue, int size, PartitionSize type, IEqualityComparer <T> comparer = null)
 {
     if (size < 0)
     {
         throw new ArgumentOutOfRangeException(nameof(size));
     }
     if (size == 0 || thisValue.Count == 0)
     {
         return(Enumerable.Empty <IReadOnlyCollection <T> >());
     }
     if (type == PartitionSize.TotalCount)
     {
         size = (int)Math.Ceiling(thisValue.Count / (double)size);
     }
     return(thisValue.PartitionUnique(size, comparer));
 }
Пример #3
0
        public static IEnumerable <IDictionary <TKey, TValue> > Partition <TKey, TValue>([NotNull] this IDictionary <TKey, TValue> thisValue, int size, PartitionSize type = PartitionSize.PerPartition)
        {
            if (size < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(size));
            }
            if (size == 0 || thisValue.Count == 0)
            {
                yield break;
            }

            int sz;

            switch (type)
            {
            case PartitionSize.TotalCount:
                sz = (int)Math.Ceiling(thisValue.Count / (double)size);
                break;

            default:
                sz = size;
                break;
            }

            int n = 0;
            IDictionary <TKey, TValue> dictionary = new Dictionary <TKey, TValue>(sz);

            foreach (KeyValuePair <TKey, TValue> item in thisValue)
            {
                dictionary.Add(item);
                n++;

                if (n == thisValue.Count || dictionary.Count == sz)
                {
                    yield return(dictionary);

                    dictionary.Clear();
                }
            }
        }