Skip to content

Commit

Permalink
Rename everything from String -> Bytes and optimize query results API…
Browse files Browse the repository at this point in the history
… defs.
  • Loading branch information
notbdu committed Mar 7, 2019
1 parent 295bbfc commit 2140857
Show file tree
Hide file tree
Showing 101 changed files with 1,477 additions and 1,490 deletions.
22 changes: 11 additions & 11 deletions calculation/calculation_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ func (f Op) MustNewResult(t field.ValueType) Result {
return r
}

// String returns the string representation of the calculation operator.
func (f Op) String() string {
if s, exists := opStrings[f]; exists {
// Bytes returns the string representation of the calculation operator.
func (f Op) Bytes() string {
if s, exists := opBytess[f]; exists {
return s
}
// nolint: goconst
Expand Down Expand Up @@ -155,7 +155,7 @@ var (
Min: struct{}{},
Max: struct{}{},
}
opStrings = map[Op]string{
opBytess = map[Op]string{
Count: "COUNT",
Sum: "SUM",
Avg: "AVG",
Expand Down Expand Up @@ -184,13 +184,13 @@ var (
Min: map[field.ValueType]newResultFn{
field.IntType: NewMinNumberResult,
field.DoubleType: NewMinNumberResult,
field.StringType: NewMinStringResult,
field.BytesType: NewMinBytesResult,
field.TimeType: NewMinNumberResult,
},
Max: map[field.ValueType]newResultFn{
field.IntType: NewMaxNumberResult,
field.DoubleType: NewMaxNumberResult,
field.StringType: NewMaxStringResult,
field.BytesType: NewMaxBytesResult,
field.TimeType: NewMaxNumberResult,
},
}
Expand All @@ -201,7 +201,7 @@ var (
field.BoolType: struct{}{},
field.IntType: struct{}{},
field.DoubleType: struct{}{},
field.StringType: struct{}{},
field.BytesType: struct{}{},
field.TimeType: struct{}{},
},
Sum: field.ValueTypeSet{
Expand All @@ -217,21 +217,21 @@ var (
Min: field.ValueTypeSet{
field.IntType: struct{}{},
field.DoubleType: struct{}{},
field.StringType: struct{}{},
field.BytesType: struct{}{},
field.TimeType: struct{}{},
},
Max: field.ValueTypeSet{
field.IntType: struct{}{},
field.DoubleType: struct{}{},
field.StringType: struct{}{},
field.BytesType: struct{}{},
field.TimeType: struct{}{},
},
}
)

func init() {
stringToOps = make(map[string]Op, len(opStrings))
for k, v := range opStrings {
stringToOps = make(map[string]Op, len(opBytess))
for k, v := range opBytess {
stringToOps[v] = k
}
}
58 changes: 29 additions & 29 deletions calculation/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Result interface {

var (
nan = math.NaN()
emptyString []byte
emptyBytes []byte

errMergingDifferentResultTypes = errors.New("merging calculation results with different result types")
)
Expand Down Expand Up @@ -172,29 +172,29 @@ func (r *minNumberResult) Value() ValueUnion {

func (r *minNumberResult) MarshalJSON() ([]byte, error) { return json.Marshal(r.Value()) }

type minStringResult struct {
type minBytesResult struct {
hasValues bool
v []byte
}

// NewMinStringResult creates a new minimum string result.
func NewMinStringResult() Result { return &minStringResult{} }
// NewMinBytesResult creates a new minimum string result.
func NewMinBytesResult() Result { return &minBytesResult{} }

func (r *minStringResult) New() Result { return NewMinStringResult() }
func (r *minBytesResult) New() Result { return NewMinBytesResult() }

func (r *minStringResult) Add(v ValueUnion) {
func (r *minBytesResult) Add(v ValueUnion) {
if !r.hasValues {
r.hasValues = true
r.v = v.StringVal
r.v = v.BytesVal
return
}
if bytes.GreaterThan(r.v, v.StringVal) {
r.v = v.StringVal
if bytes.GreaterThan(r.v, v.BytesVal) {
r.v = v.BytesVal
}
}

func (r *minStringResult) MergeInPlace(other Result) error {
or, ok := other.(*minStringResult)
func (r *minBytesResult) MergeInPlace(other Result) error {
or, ok := other.(*minBytesResult)
if !ok {
return errMergingDifferentResultTypes
}
Expand All @@ -211,14 +211,14 @@ func (r *minStringResult) MergeInPlace(other Result) error {
return nil
}

func (r *minStringResult) Value() ValueUnion {
func (r *minBytesResult) Value() ValueUnion {
if !r.hasValues {
return NewStringUnion(emptyString)
return NewBytesUnion(emptyBytes)
}
return NewStringUnion(r.v)
return NewBytesUnion(r.v)
}

func (r *minStringResult) MarshalJSON() ([]byte, error) { return json.Marshal(r.Value()) }
func (r *minBytesResult) MarshalJSON() ([]byte, error) { return json.Marshal(r.Value()) }

type maxNumberResult struct {
hasValues bool
Expand Down Expand Up @@ -268,29 +268,29 @@ func (r *maxNumberResult) Value() ValueUnion {

func (r *maxNumberResult) MarshalJSON() ([]byte, error) { return json.Marshal(r.Value()) }

type maxStringResult struct {
type maxBytesResult struct {
hasValues bool
v []byte
}

// NewMaxStringResult creates a new maximum string result.
func NewMaxStringResult() Result { return &maxStringResult{} }
// NewMaxBytesResult creates a new maximum string result.
func NewMaxBytesResult() Result { return &maxBytesResult{} }

func (r *maxStringResult) New() Result { return NewMaxStringResult() }
func (r *maxBytesResult) New() Result { return NewMaxBytesResult() }

func (r *maxStringResult) Add(v ValueUnion) {
func (r *maxBytesResult) Add(v ValueUnion) {
if !r.hasValues {
r.hasValues = true
r.v = v.StringVal
r.v = v.BytesVal
return
}
if bytes.LessThan(r.v, v.StringVal) {
r.v = v.StringVal
if bytes.LessThan(r.v, v.BytesVal) {
r.v = v.BytesVal
}
}

func (r *maxStringResult) MergeInPlace(other Result) error {
or, ok := other.(*maxStringResult)
func (r *maxBytesResult) MergeInPlace(other Result) error {
or, ok := other.(*maxBytesResult)
if !ok {
return errMergingDifferentResultTypes
}
Expand All @@ -307,14 +307,14 @@ func (r *maxStringResult) MergeInPlace(other Result) error {
return nil
}

func (r *maxStringResult) Value() ValueUnion {
func (r *maxBytesResult) Value() ValueUnion {
if !r.hasValues {
return NewStringUnion(emptyString)
return NewBytesUnion(emptyBytes)
}
return NewStringUnion(r.v)
return NewBytesUnion(r.v)
}

func (r *maxStringResult) MarshalJSON() ([]byte, error) { return json.Marshal(r.Value()) }
func (r *maxBytesResult) MarshalJSON() ([]byte, error) { return json.Marshal(r.Value()) }

// ResultArray is an array of calculation result.
type ResultArray []Result
Expand Down
35 changes: 17 additions & 18 deletions calculation/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/xichen2020/eventdb/document/field"
"github.com/xichen2020/eventdb/generated/proto/servicepb"
"github.com/xichen2020/eventdb/x/compare"
"github.com/xichen2020/eventdb/x/safe"
)

// ValueType is the type of a calculation value.
Expand All @@ -18,7 +17,7 @@ type ValueType int
// A list of supported value types.
const (
NumberType ValueType = iota
StringType
BytesType
)

var (
Expand All @@ -29,7 +28,7 @@ var (
type ValueUnion struct {
Type ValueType
NumberVal float64
StringVal []byte
BytesVal []byte
}

// NewValueFromProto creates a value from protobuf message.
Expand All @@ -39,9 +38,9 @@ func NewValueFromProto(pbValue servicepb.CalculationValue) (ValueUnion, error) {
case servicepb.CalculationValue_NUMBER:
v.Type = NumberType
v.NumberVal = pbValue.NumberVal
case servicepb.CalculationValue_STRING:
v.Type = StringType
v.StringVal = safe.ToBytes(pbValue.StringVal)
case servicepb.CalculationValue_BYTES:
v.Type = BytesType
v.BytesVal = pbValue.BytesVal
default:
return v, fmt.Errorf("invalid protobuf calculation value type %v", pbValue.Type)
}
Expand All @@ -57,8 +56,8 @@ func (u ValueUnion) MarshalJSON() ([]byte, error) {
return nullBytes, nil
}
return json.Marshal(u.NumberVal)
case StringType:
return json.Marshal(safe.ToString(u.StringVal))
case BytesType:
return json.Marshal(string(u.BytesVal))
default:
return nil, fmt.Errorf("unexpected value type %v", u.Type)
}
Expand All @@ -72,10 +71,10 @@ func (u ValueUnion) ToProto() servicepb.CalculationValue {
Type: servicepb.CalculationValue_NUMBER,
NumberVal: u.NumberVal,
}
case StringType:
case BytesType:
return servicepb.CalculationValue{
Type: servicepb.CalculationValue_STRING,
StringVal: safe.ToString(u.StringVal),
Type: servicepb.CalculationValue_BYTES,
BytesVal: u.BytesVal,
}
default:
panic(fmt.Errorf("unexpected calculation value type %v", u.Type))
Expand All @@ -87,9 +86,9 @@ func NewNumberUnion(v float64) ValueUnion {
return ValueUnion{Type: NumberType, NumberVal: v}
}

// NewStringUnion creates a new string union.
func NewStringUnion(v []byte) ValueUnion {
return ValueUnion{Type: StringType, StringVal: v}
// NewBytesUnion creates a new string union.
func NewBytesUnion(v []byte) ValueUnion {
return ValueUnion{Type: BytesType, BytesVal: v}
}

// ValueCompareFn compares two value unions.
Expand All @@ -108,8 +107,8 @@ func CompareValue(v1, v2 ValueUnion) (int, error) {
switch v1.Type {
case NumberType:
return compare.DoubleCompare(v1.NumberVal, v2.NumberVal), nil
case StringType:
return bytes.Compare(v1.StringVal, v2.StringVal), nil
case BytesType:
return bytes.Compare(v1.BytesVal, v2.BytesVal), nil
}
panic("should never reach here")
}
Expand Down Expand Up @@ -181,7 +180,7 @@ func doubleToValue(v *field.ValueUnion) ValueUnion {
}

func stringToValue(v *field.ValueUnion) ValueUnion {
return NewStringUnion(v.StringVal)
return NewBytesUnion(v.BytesVal)
}

func timeToValue(v *field.ValueUnion) ValueUnion {
Expand Down Expand Up @@ -213,7 +212,7 @@ var (
field.BoolType: boolToValue,
field.IntType: intToValue,
field.DoubleType: doubleToValue,
field.StringType: stringToValue,
field.BytesType: stringToValue,
field.TimeType: timeToValue,
}
)
14 changes: 7 additions & 7 deletions document/field/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
func TestNewValuesLessThan(t *testing.T) {
v1 := Values{
{
Type: StringType,
StringVal: []byte("foo"),
Type: BytesType,
BytesVal: []byte("foo"),
},
{
Type: IntType,
Expand All @@ -24,8 +24,8 @@ func TestNewValuesLessThan(t *testing.T) {

v2 := Values{
{
Type: StringType,
StringVal: []byte("foo"),
Type: BytesType,
BytesVal: []byte("foo"),
},
{
Type: IntType,
Expand All @@ -49,8 +49,8 @@ func TestNewValuesLessThan(t *testing.T) {
func TestFilterValues(t *testing.T) {
v := Values{
{
Type: StringType,
StringVal: []byte("foo"),
Type: BytesType,
BytesVal: []byte("foo"),
},
{
Type: IntType,
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestArrayBasedIterator(t *testing.T) {
},
{
Path: []string{"baz"},
Value: NewStringUnion([]byte("blah")),
Value: NewBytesUnion([]byte("blah")),
},
}
it := NewArrayBasedIterator(fields, nil)
Expand Down
Loading

0 comments on commit 2140857

Please sign in to comment.