using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CDP { public class GroupsDocument { public string id { get { return GenPartitionKey(AppKey, UserId, Index); } } public string AppKey { get; set; } public string UserId { get; set; } public int Index { get; set; } public List Records { get; set; } public GroupsDocument() { Index = 0; Records = new List(); } public static string GenRootPartitionKey(string id, DateTime dt) { return string.Format($"{id}-{dt.Year}-{dt.DayOfYear}"); } public static string GenPartitionKey(string appKey, string userId, int index = 0) { return string.Format($"{appKey}-{userId}-{index}"); } } public class GroupsRecord { public string id { get; set; } public string Name { get; set; } public string Description { get; set; } public List Contacts { get; set; } public DateTime EventTime { get; set; } public GroupsRecord() { EventTime = DateTime.UtcNow; } public GroupsRecord(string name, string description, List contacts) { id = Guid.NewGuid().ToString(); Name = name; Description = description; Contacts = contacts != null ? contacts : new List(); EventTime = DateTime.UtcNow; } public int CalculateRecordSize() { int size = 0; size += 8800; // sizeof(DateTime); 175*50 = 8750 bytes to support at max 50 contacts in a group size += Encoding.UTF8.GetByteCount(Name) == 0 ? 20 : Encoding.UTF8.GetByteCount(Name); // UserId size += sizeof(int); return size; } } public class MetadataDocumentGroups { public string id { get; set; } public List PartitionKeys { get; set; } public MetadataDocumentGroups() { PartitionKeys = new List(); id = id + "-meta"; } public string GetLatestKey(string appKey, string userId) { PartitionKeys.Sort(); if (PartitionKeys.Count == 0) return GroupsDocument.GenPartitionKey(appKey, userId); return PartitionKeys.Last(); } } }