Temporary repo to track my changes on LTS functions app porting
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace CDP
  7. {
  8. public class GroupsDocument
  9. {
  10. public string id
  11. {
  12. get
  13. {
  14. return GenPartitionKey(AppKey, UserId, Index);
  15. }
  16. }
  17. public string AppKey { get; set; }
  18. public string UserId { get; set; }
  19. public int Index { get; set; }
  20. public List<GroupsRecord> Records { get; set; }
  21. public GroupsDocument()
  22. {
  23. Index = 0;
  24. Records = new List<GroupsRecord>();
  25. }
  26. public static string GenRootPartitionKey(string id, DateTime dt)
  27. {
  28. return string.Format($"{id}-{dt.Year}-{dt.DayOfYear}");
  29. }
  30. public static string GenPartitionKey(string appKey, string userId, int index = 0)
  31. {
  32. return string.Format($"{appKey}-{userId}-{index}");
  33. }
  34. }
  35. public class GroupsRecord
  36. {
  37. public string id { get; set; }
  38. public string Name { get; set; }
  39. public string Description { get; set; }
  40. public List<ContactRecord> Contacts { get; set; }
  41. public DateTime EventTime { get; set; }
  42. public GroupsRecord()
  43. {
  44. EventTime = DateTime.UtcNow;
  45. }
  46. public GroupsRecord(string name, string description, List<ContactRecord> contacts)
  47. {
  48. id = Guid.NewGuid().ToString();
  49. Name = name;
  50. Description = description;
  51. Contacts = contacts != null ? contacts : new List<ContactRecord>();
  52. EventTime = DateTime.UtcNow;
  53. }
  54. public int CalculateRecordSize()
  55. {
  56. int size = 0;
  57. size += 8800; // sizeof(DateTime); 175*50 = 8750 bytes to support at max 50 contacts in a group
  58. size += Encoding.UTF8.GetByteCount(Name) == 0 ? 20 : Encoding.UTF8.GetByteCount(Name); // UserId
  59. size += sizeof(int);
  60. return size;
  61. }
  62. }
  63. public class MetadataDocumentGroups
  64. {
  65. public string id { get; set; }
  66. public List<string> PartitionKeys { get; set; }
  67. public MetadataDocumentGroups()
  68. {
  69. PartitionKeys = new List<string>();
  70. id = id + "-meta";
  71. }
  72. public string GetLatestKey(string appKey, string userId)
  73. {
  74. PartitionKeys.Sort();
  75. if (PartitionKeys.Count == 0)
  76. return GroupsDocument.GenPartitionKey(appKey, userId);
  77. return PartitionKeys.Last();
  78. }
  79. }
  80. }