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.

49 lines
1.1 KiB

  1. package topic
  2. import "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  3. const PartitionCount = 4096
  4. type Partition struct {
  5. RangeStart int32
  6. RangeStop int32 // exclusive
  7. RingSize int32
  8. }
  9. func (partition Partition) Equals(other Partition) bool {
  10. if partition.RangeStart != other.RangeStart {
  11. return false
  12. }
  13. if partition.RangeStop != other.RangeStop {
  14. return false
  15. }
  16. if partition.RingSize != other.RingSize {
  17. return false
  18. }
  19. return true
  20. }
  21. func FromPbPartition(partition *mq_pb.Partition) Partition {
  22. return Partition{
  23. RangeStart: partition.RangeStart,
  24. RangeStop: partition.RangeStop,
  25. RingSize: partition.RingSize,
  26. }
  27. }
  28. func SplitPartitions(targetCount int32) []*Partition {
  29. partitions := make([]*Partition, 0, targetCount)
  30. partitionSize := PartitionCount / targetCount
  31. for i := int32(0); i < targetCount; i++ {
  32. partitionStop := (i + 1) * partitionSize
  33. if i == targetCount-1 {
  34. partitionStop = PartitionCount
  35. }
  36. partitions = append(partitions, &Partition{
  37. RangeStart: i * partitionSize,
  38. RangeStop: partitionStop,
  39. RingSize: PartitionCount,
  40. })
  41. }
  42. return partitions
  43. }