influxdb/util.go

97 lines
1.9 KiB
Go
Raw Normal View History

2014-10-22 05:32:19 +00:00
package influxdb
2014-10-23 04:21:48 +00:00
import (
"fmt"
2014-11-11 05:25:03 +00:00
"code.google.com/p/log4go"
2014-10-23 04:21:48 +00:00
)
2015-02-01 18:47:48 +00:00
// TimePrecision represents a level of time precision.
2014-10-23 04:21:48 +00:00
type TimePrecision int
const (
2015-02-01 18:47:48 +00:00
// MicrosecondPrecision is 1/1,000,000 th of a second.
2014-10-23 04:21:48 +00:00
MicrosecondPrecision TimePrecision = iota
2015-02-01 18:47:48 +00:00
// MillisecondPrecision is 1/1,000 th of a second.
2014-10-23 04:21:48 +00:00
MillisecondPrecision
2015-02-01 18:47:48 +00:00
// SecondPrecision is 1 second precision.
2014-10-23 04:21:48 +00:00
SecondPrecision
)
2014-11-11 05:25:03 +00:00
func parseTimePrecision(s string) (TimePrecision, error) {
switch s {
case "u":
return MicrosecondPrecision, nil
case "m":
log4go.Warn("time_precision=m will be disabled in future release, use time_precision=ms instead")
fallthrough
case "ms":
return MillisecondPrecision, nil
case "s":
return SecondPrecision, nil
case "":
return MillisecondPrecision, nil
2014-10-22 05:32:19 +00:00
}
2014-11-11 05:25:03 +00:00
return 0, fmt.Errorf("Unknown time precision %s", s)
2014-10-22 05:32:19 +00:00
}
2014-10-23 04:21:48 +00:00
func hasDuplicates(ss []string) bool {
m := make(map[string]struct{}, len(ss))
for _, s := range ss {
if _, ok := m[s]; ok {
return true
}
m[s] = struct{}{}
}
return false
}
func removeField(fields []string, name string) []string {
index := -1
for idx, field := range fields {
if field == name {
index = idx
break
}
}
if index == -1 {
return fields
}
return append(fields[:index], fields[index+1:]...)
}
func removeTimestampFieldDefinition(fields []string) []string {
fields = removeField(fields, "time")
return removeField(fields, "sequence_number")
}
2015-01-28 04:36:19 +00:00
func mapKeyList(m interface{}) []string {
switch m.(type) {
case map[string]string:
return mapStrStrKeyList(m.(map[string]string))
2015-01-29 20:00:15 +00:00
case map[string]uint32:
return mapStrUint32KeyList(m.(map[string]uint32))
2015-01-28 04:36:19 +00:00
}
return nil
}
func mapStrStrKeyList(m map[string]string) []string {
l := make([]string, 0, len(m))
2015-02-01 18:47:48 +00:00
for k := range m {
2015-01-28 04:36:19 +00:00
l = append(l, k)
}
return l
}
2015-01-29 20:00:15 +00:00
func mapStrUint32KeyList(m map[string]uint32) []string {
2015-01-28 04:36:19 +00:00
l := make([]string, 0, len(m))
2015-02-01 18:47:48 +00:00
for k := range m {
2015-01-28 04:36:19 +00:00
l = append(l, k)
}
return l
}