Skip to content

Commit

Permalink
Use safe.ToBytes method where ownership may be complicated.
Browse files Browse the repository at this point in the history
  • Loading branch information
notbdu committed Feb 25, 2019
1 parent 6981181 commit 9c3765a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
4 changes: 2 additions & 2 deletions document/field/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion server/http/handlers/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions server/http/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions storage/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions x/safe/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 9c3765a

Please sign in to comment.