示例#1
0
func (s *ShardSpace) ParsedShardDuration() time.Duration {
	if s.ShardDuration != "" {
		d, _ := common.ParseTimeDuration(s.ShardDuration)
		return time.Duration(d)
	}
	return DEFAULT_SHARD_DURATION
}
示例#2
0
func (s *ShardSpace) ParsedRetentionPeriod() time.Duration {
	if s.RetentionPolicy == "" {
		return DEFAULT_RETENTION_POLICY_DURATION
	} else if s.RetentionPolicy == "inf" {
		return InfiniteRetention
	}
	d, _ := common.ParseTimeDuration(s.RetentionPolicy)
	return time.Duration(d)
}
示例#3
0
func (s *ShardSpace) Validate(clusterConfig *ClusterConfiguration, checkForDb bool) error {
	if clusterConfig.ShardSpaceExists(s) {
		return fmt.Errorf("Shard space %s exists for db %s", s.Name, s.Database)
	}
	if checkForDb {
		if !clusterConfig.DatabaseExists(s.Database) {
			return fmt.Errorf("Database '%s' doesn't exist.", s.Database)
		}
	}

	if s.Name == "" {
		return fmt.Errorf("Shard space must have a name")
	}
	if s.Regex == "" {
		return fmt.Errorf("A regex is required for a shard space")
	}
	r, err := s.compileRegex(s.Regex)
	if err != nil {
		return fmt.Errorf("Error parsing regex: %s", err)
	}
	s.compiledRegex = r
	if s.Split == 0 {
		s.Split = DEFAULT_SPLIT
	}
	if s.ReplicationFactor == 0 {
		s.ReplicationFactor = DEFAULT_REPLICATION_FACTOR
	}
	if s.ShardDuration != "" {
		if _, err := common.ParseTimeDuration(s.ShardDuration); err != nil {
			return err
		}
	}
	if s.RetentionPolicy != "" && s.RetentionPolicy != "inf" {
		if _, err := common.ParseTimeDuration(s.RetentionPolicy); err != nil {
			return err
		}
	}
	return nil
}
示例#4
0
// parse time expressions, e.g. now() - 1d
func parseTime(value *Value) (int64, error) {
	if value.Type != ValueExpression {
		if value.IsFunctionCall() && strings.ToLower(value.Name) == "now" {
			return time.Now().UTC().UnixNano(), nil
		}

		if value.IsFunctionCall() {
			return 0, fmt.Errorf("Invalid use of function %s", value.Name)
		}

		if value.Type == ValueString {
			t, err := parseTimeString(value.Name)
			if err != nil {
				return 0, err
			}
			return t.UnixNano(), err
		}

		duration, err := common.ParseTimeDuration(value.Name)

		return duration, err
	}

	leftValue, err := parseTime(value.Elems[0])
	if err != nil {
		return 0, err
	}
	rightValue, err := parseTime(value.Elems[1])
	if err != nil {
		return 0, err
	}
	switch value.Name {
	case "+":
		return leftValue + rightValue, nil
	case "-":
		return leftValue - rightValue, nil
	default:
		return 0, fmt.Errorf("Cannot use '%s' in a time expression", value.Name)
	}
}