consistency.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package models
  2. import (
  3. "errors"
  4. "strings"
  5. )
  6. // ConsistencyLevel represent a required replication criteria before a write can
  7. // be returned as successful.
  8. //
  9. // The consistency level is handled in open-source InfluxDB but only applicable to clusters.
  10. type ConsistencyLevel int
  11. const (
  12. // ConsistencyLevelAny allows for hinted handoff, potentially no write happened yet.
  13. ConsistencyLevelAny ConsistencyLevel = iota
  14. // ConsistencyLevelOne requires at least one data node acknowledged a write.
  15. ConsistencyLevelOne
  16. // ConsistencyLevelQuorum requires a quorum of data nodes to acknowledge a write.
  17. ConsistencyLevelQuorum
  18. // ConsistencyLevelAll requires all data nodes to acknowledge a write.
  19. ConsistencyLevelAll
  20. )
  21. var (
  22. // ErrInvalidConsistencyLevel is returned when parsing the string version
  23. // of a consistency level.
  24. ErrInvalidConsistencyLevel = errors.New("invalid consistency level")
  25. )
  26. // ParseConsistencyLevel converts a consistency level string to the corresponding ConsistencyLevel const.
  27. func ParseConsistencyLevel(level string) (ConsistencyLevel, error) {
  28. switch strings.ToLower(level) {
  29. case "any":
  30. return ConsistencyLevelAny, nil
  31. case "one":
  32. return ConsistencyLevelOne, nil
  33. case "quorum":
  34. return ConsistencyLevelQuorum, nil
  35. case "all":
  36. return ConsistencyLevelAll, nil
  37. default:
  38. return 0, ErrInvalidConsistencyLevel
  39. }
  40. }