diff --git a/document/field/value.go b/document/field/value.go index d1e1375..37176d5 100644 --- a/document/field/value.go +++ b/document/field/value.go @@ -6,7 +6,7 @@ import ( "math" "github.com/xichen2020/eventdb/x/compare" - "github.com/xichen2020/eventdb/x/unsafe" + "github.com/xichen2020/eventdb/x/safe" "github.com/cespare/xxhash" ) @@ -269,7 +269,7 @@ func (v *ValueUnion) Hash() uint64 { // NB(xichen): Hashing on bit patterns for doubles might be problematic. return 31*hash + math.Float64bits(v.DoubleVal) case StringType: - return 31*hash + xxhash.Sum64(unsafe.ToBytes(v.StringVal)) + return 31*hash + xxhash.Sum64(safe.ToBytes(v.StringVal)) case TimeType: return 31*hash + uint64(v.TimeNanosVal) } diff --git a/server/http/handlers/options.go b/server/http/handlers/options.go index 62b5ace..0037efd 100644 --- a/server/http/handlers/options.go +++ b/server/http/handlers/options.go @@ -5,6 +5,7 @@ import ( "github.com/xichen2020/eventdb/parser/json" "github.com/xichen2020/eventdb/parser/json/value" + "github.com/xichen2020/eventdb/x/safe" "github.com/xichen2020/eventdb/x/unsafe" "github.com/m3db/m3x/clock" @@ -128,7 +129,7 @@ func defaultNamespaceFn(v *value.Value) ([]byte, error) { if err != nil { return nil, err } - return unsafe.ToBytes(ns), nil + return safe.ToBytes(ns), nil } // defaultTimeNanosFn parses the time value as a string in RFC3339 format. diff --git a/server/http/handlers/service.go b/server/http/handlers/service.go index 8d5acdc..62abb60 100644 --- a/server/http/handlers/service.go +++ b/server/http/handlers/service.go @@ -16,7 +16,6 @@ import ( "github.com/xichen2020/eventdb/query" "github.com/xichen2020/eventdb/storage" "github.com/xichen2020/eventdb/x/safe" - "github.com/xichen2020/eventdb/x/unsafe" "github.com/m3db/m3x/clock" "github.com/m3db/m3x/context" @@ -329,7 +328,7 @@ func (s *service) writeBatch(data []byte) error { var multiErr xerrors.MultiError for nsStr, events := range docsByNamespace { - nsBytes := unsafe.ToBytes(nsStr) + nsBytes := safe.ToBytes(nsStr) if err := s.db.WriteBatch(nsBytes, events); err != nil { multiErr = multiErr.Add(err) } diff --git a/storage/database.go b/storage/database.go index e5d1bfb..094f1ca 100644 --- a/storage/database.go +++ b/storage/database.go @@ -9,7 +9,7 @@ import ( "github.com/xichen2020/eventdb/query" "github.com/xichen2020/eventdb/sharding" "github.com/xichen2020/eventdb/x/hash" - "github.com/xichen2020/eventdb/x/unsafe" + "github.com/xichen2020/eventdb/x/safe" "github.com/m3db/m3x/clock" "github.com/m3db/m3x/context" @@ -202,7 +202,7 @@ func (d *db) QueryRaw( q query.ParsedRawQuery, ) (*query.RawResults, error) { callStart := d.nowFn() - n, err := d.namespaceFor(unsafe.ToBytes(q.Namespace)) + n, err := d.namespaceFor(safe.ToBytes(q.Namespace)) if err != nil { d.metrics.queryRaw.ReportError(d.nowFn().Sub(callStart)) return nil, err @@ -217,7 +217,7 @@ func (d *db) QueryGrouped( q query.ParsedGroupedQuery, ) (*query.GroupedResults, error) { callStart := d.nowFn() - n, err := d.namespaceFor(unsafe.ToBytes(q.Namespace)) + n, err := d.namespaceFor(safe.ToBytes(q.Namespace)) if err != nil { return nil, err } @@ -231,7 +231,7 @@ func (d *db) QueryTimeBucket( q query.ParsedTimeBucketQuery, ) (*query.TimeBucketResults, error) { callStart := d.nowFn() - n, err := d.namespaceFor(unsafe.ToBytes(q.Namespace)) + n, err := d.namespaceFor(safe.ToBytes(q.Namespace)) if err != nil { return nil, err } diff --git a/x/safe/convert.go b/x/safe/convert.go index 6d91c15..63e25f2 100644 --- a/x/safe/convert.go +++ b/x/safe/convert.go @@ -6,3 +6,10 @@ package safe func ToString(b []byte) string { return string(b) } + +// ToBytes safely converts a string to a byte slice. +// TODO: This method should be removed when we move over to handling bytes in the +// database hot path instead of strings. +func ToBytes(s string) []byte { + return []byte(s) +}