Skip to content

Commit

Permalink
Safely convert strings. (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
notbdu authored and xichen2020 committed Feb 26, 2019
1 parent 1ea5edc commit 6532a6b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 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
6 changes: 3 additions & 3 deletions parser/json/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"unicode/utf8"

"github.com/xichen2020/eventdb/parser/json/value"
"github.com/xichen2020/eventdb/x/unsafe"
"github.com/xichen2020/eventdb/x/safe"
)

const (
Expand Down Expand Up @@ -130,7 +130,7 @@ func (p *parser) Parse(str string) (*value.Value, error) {
}

func (p *parser) ParseBytes(b []byte) (*value.Value, error) {
return p.Parse(unsafe.ToString(b))
return p.Parse(safe.ToString(b))
}

func (p *parser) reset() {
Expand Down Expand Up @@ -401,7 +401,7 @@ func (p *parser) parseStringAsRaw() (string, error) {
case '"':
p.pos += i + 1
escapedBytes = append(escapedBytes, data[prev:i]...)
return unsafe.ToString(escapedBytes), nil
return safe.ToString(escapedBytes), nil

case '\\':
escapedBytes = append(escapedBytes, data[prev:i]...)
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
6 changes: 3 additions & 3 deletions server/http/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/xichen2020/eventdb/parser/json/value"
"github.com/xichen2020/eventdb/query"
"github.com/xichen2020/eventdb/storage"
"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 @@ -318,7 +318,7 @@ func (s *service) writeBatch(data []byte) error {
if err != nil {
return fmt.Errorf("cannot parse document from %s: %v", docBytes, err)
}
nsStr := unsafe.ToString(ns)
nsStr := safe.ToString(ns)
docsByNamespace[nsStr] = append(docsByNamespace[nsStr], doc)
start = end + 1
batchSize++
Expand All @@ -328,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
4 changes: 2 additions & 2 deletions storage/mutable_segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/xichen2020/eventdb/persist"
"github.com/xichen2020/eventdb/query"
"github.com/xichen2020/eventdb/x/hash"
"github.com/xichen2020/eventdb/x/safe"
"github.com/xichen2020/eventdb/x/strings"
"github.com/xichen2020/eventdb/x/unsafe"

"github.com/m3db/m3x/context"
)
Expand Down Expand Up @@ -572,7 +572,7 @@ func (s *mutableSeg) writeTimestampFieldWithLock(docID int32, val int64) {
func (s *mutableSeg) writeRawDocSourceFieldWithLock(docID int32, val []byte) {
v := field.ValueUnion{
Type: field.StringType,
StringVal: unsafe.ToString(val),
StringVal: safe.ToString(val),
}
s.rawDocSourceField.Add(docID, v)
}
Expand Down
15 changes: 15 additions & 0 deletions x/safe/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package safe

// ToString safely converts a byte slice to a string.
// TODO: This method should be removed when we move over to handling bytes in the
// database hot path instead of strings.
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 6532a6b

Please sign in to comment.