github.com/qxnw/lib4go@v0.0.0-20180426074627-c80c7e84b925/influxdb/models/consistency.go (about)

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