Skip to content

Commit

Permalink
Add GRPC server query RPC endpoints (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
xichen2020 committed Mar 3, 2019
1 parent 1fbc445 commit 1c001ec
Show file tree
Hide file tree
Showing 19 changed files with 9,174 additions and 547 deletions.
17 changes: 13 additions & 4 deletions calculation/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math"

"github.com/xichen2020/eventdb/document/field"
"github.com/xichen2020/eventdb/generated/proto/servicepb"
)

// Result represents a merge-able calculation result. In simple cases this can be simply
Expand Down Expand Up @@ -325,10 +326,6 @@ func (arr ResultArray) New() ResultArray {
}
resArray := make(ResultArray, 0, len(arr))
for _, res := range arr {
if res == nil {
resArray = append(resArray, nil)
continue
}
resArray = append(resArray, res.New())
}
return resArray
Expand All @@ -345,6 +342,18 @@ func (arr ResultArray) MergeInPlace(other ResultArray) {
}
}

// ToProto converts a result array to a value array protobuf message.
func (arr ResultArray) ToProto() []servicepb.CalculationValue {
if len(arr) == 0 {
return nil
}
values := make([]servicepb.CalculationValue, 0, len(arr))
for _, res := range arr {
values = append(values, res.Value().ToProto())
}
return values
}

// NewResultArrayFromValueTypesFn creates a new result array based on the field value types.
type NewResultArrayFromValueTypesFn func(valueTypes field.OptionalTypeArray) (ResultArray, error)

Expand Down
19 changes: 19 additions & 0 deletions calculation/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math"

"github.com/xichen2020/eventdb/document/field"
"github.com/xichen2020/eventdb/generated/proto/servicepb"
"github.com/xichen2020/eventdb/x/compare"
)

Expand Down Expand Up @@ -45,6 +46,24 @@ func (u ValueUnion) MarshalJSON() ([]byte, error) {
}
}

// ToProto converts a value to a calculation value protobuf message.
func (u ValueUnion) ToProto() servicepb.CalculationValue {
switch u.Type {
case NumberType:
return servicepb.CalculationValue{
Type: servicepb.CalculationValue_NUMBER,
NumberVal: u.NumberVal,
}
case StringType:
return servicepb.CalculationValue{
Type: servicepb.CalculationValue_STRING,
StringVal: u.StringVal,
}
default:
panic(fmt.Errorf("unexpected calculation value type %v", u.Type))
}
}

// NewNumberUnion creates a new number union.
func NewNumberUnion(v float64) ValueUnion {
return ValueUnion{Type: NumberType, NumberVal: v}
Expand Down
41 changes: 41 additions & 0 deletions document/field/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math"

"github.com/xichen2020/eventdb/generated/proto/servicepb"
"github.com/xichen2020/eventdb/x/compare"
"github.com/xichen2020/eventdb/x/safe"

Expand Down Expand Up @@ -220,6 +221,33 @@ func (v ValueUnion) MarshalJSON() ([]byte, error) {
}
}

// ToProto converts a value to a value proto message.
func (v *ValueUnion) ToProto() (servicepb.FieldValue, error) {
var fb servicepb.FieldValue
switch v.Type {
case NullType:
fb.Type = servicepb.FieldValue_NULL
case BoolType:
fb.Type = servicepb.FieldValue_BOOL
fb.BoolVal = v.BoolVal
case IntType:
fb.Type = servicepb.FieldValue_INT
fb.IntVal = int64(v.IntVal)
case DoubleType:
fb.Type = servicepb.FieldValue_DOUBLE
fb.DoubleVal = v.DoubleVal
case StringType:
fb.Type = servicepb.FieldValue_STRING
fb.StringVal = safe.ToBytes(v.StringVal)
case TimeType:
fb.Type = servicepb.FieldValue_TIME
fb.TimeNanosVal = v.TimeNanosVal
default:
return servicepb.FieldValue{}, fmt.Errorf("unknown field value type: %v", v.Type)
}
return fb, nil
}

// Equal returns true if two value unions are considered equal.
func (v *ValueUnion) Equal(other *ValueUnion) bool {
if v == nil && other == nil {
Expand Down Expand Up @@ -414,6 +442,19 @@ func (v Values) Clone() Values {
return cloned
}

// ToProto converts a value array to a value array protobuf message.
func (v Values) ToProto() ([]servicepb.FieldValue, error) {
res := make([]servicepb.FieldValue, 0, len(v))
for _, fv := range v {
pbFieldValue, err := fv.ToProto()
if err != nil {
return nil, err
}
res = append(res, pbFieldValue)
}
return res, nil
}

// OptionalType is a type "option" which is either "null" or has a valid type.
// It is similar to a `*ValueType` from a functionality perspective but has
// lower GC overhead.
Expand Down
Loading

0 comments on commit 1c001ec

Please sign in to comment.