CoreRange.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. namespace InABox.Core
  3. {
  4. public enum CoreRangeType
  5. {
  6. /// <summary>
  7. /// Get specific range in the database query.
  8. /// </summary>
  9. Database,
  10. /// <summary>
  11. /// Get everything from the database, but only return a small number to the client.
  12. /// </summary>
  13. Paged
  14. }
  15. public class CoreRange : ISerializeBinary
  16. {
  17. public static CoreRange All => new CoreRange()
  18. {
  19. Type = CoreRangeType.Database,
  20. Offset = 0,
  21. Limit = int.MaxValue
  22. };
  23. public static CoreRange Database(int limit) => new CoreRange()
  24. {
  25. Type = CoreRangeType.Database,
  26. Offset = 0,
  27. Limit = limit
  28. };
  29. public static CoreRange Paged(int pagesize) => new CoreRange()
  30. {
  31. Type = CoreRangeType.Paged,
  32. Offset = 0,
  33. Limit = pagesize
  34. };
  35. public CoreRange Next()
  36. {
  37. Offset = (int)Math.Min((long)int.MaxValue, (long)Offset + (long)Limit);
  38. return this;
  39. }
  40. public CoreRangeType Type { get; set; }
  41. public int Offset { get; set; }
  42. public int Limit { get; set; }
  43. public void SerializeBinary(CoreBinaryWriter writer)
  44. {
  45. writer.Write((byte)Type);
  46. writer.Write(Offset);
  47. writer.Write(Limit);
  48. }
  49. public void DeserializeBinary(CoreBinaryReader reader)
  50. {
  51. Type = (CoreRangeType)reader.ReadByte();
  52. Offset = reader.ReadInt32();
  53. Limit = reader.ReadInt32();
  54. }
  55. }
  56. }