Пример #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) Validate(clusterConfig *ClusterConfiguration, checkForDb bool) error {
	if err := clusterConfig.DoesShardSpaceExist(s); err != nil {
		return err
	}
	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")
	}
	reg := s.Regex
	if strings.HasPrefix(reg, "/") {
		if strings.HasSuffix(reg, "/i") {
			reg = fmt.Sprintf("(?i)%s", reg[1:len(reg)-2])
		} else {
			reg = reg[1 : len(reg)-1]
		}
	}
	r, err := regexp.Compile(reg)
	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
}
Пример #3
0
func (s *ShardSpace) ParsedRetentionPeriod() time.Duration {
	if s.RetentionPolicy == "" {
		return DEFAULT_RETENTION_POLICY_DURATION
	} else if s.RetentionPolicy == "inf" {
		return time.Duration(0)
	}
	d, _ := common.ParseTimeDuration(s.RetentionPolicy)
	return time.Duration(d)
}
Пример #4
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
}
Пример #5
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)
	}
}
Пример #6
0
func (self GroupByClause) GetGroupByTime() (*time.Duration, error) {
	for _, groupBy := range self.Elems {
		if groupBy.IsFunctionCall() && strings.ToLower(groupBy.Name) == "time" {
			// TODO: check the number of arguments and return an error
			if len(groupBy.Elems) != 1 {
				return nil, common.NewQueryError(common.WrongNumberOfArguments, "time function only accepts one argument")
			}

			if groupBy.Elems[0].Type != ValueDuration {
				log.Debug("Get a time function without a duration argument %v", groupBy.Elems[0].Type)
			}
			arg := groupBy.Elems[0].Name
			durationInt, err := common.ParseTimeDuration(arg)
			if err != nil {
				return nil, common.NewQueryError(common.InvalidArgument, fmt.Sprintf("invalid argument %s to the time function", arg))
			}
			duration := time.Duration(durationInt)
			return &duration, nil
		}
	}
	return nil, nil
}