1
0
Fork 0
master
Philip O'Toole 4 years ago
parent e948d7410a
commit a5cd120296

@ -19,7 +19,7 @@ var (
// ErrNotLegacyCommand is returned when a command is not legacy encoded.
ErrNotLegacyCommand = errors.New("not legacy command")
// ErrUnknownType is returned when an unknown command type is encountered.
// ErrUnknownCommandType is returned when an unknown command type is encountered.
ErrUnknownCommandType = errors.New("unknown command type")
// ErrUnsupportedType is returned when a request contains an unsupported type.
@ -32,6 +32,7 @@ type commandType int
// Value is the type for parameters passed to a parameterized SQL statement.
type Value interface{}
// Command is the type of legacy JSON-encoded commands in the Raft log.
type Command struct {
Typ commandType `json:"typ,omitempty"`
Sub json.RawMessage `json:"sub,omitempty"`
@ -52,6 +53,7 @@ type metadataSetSub struct {
Data map[string]string `json:"data,omitempty"`
}
// Unmarshal unmarshals a legacy JSON-encoded command in the Raft log.
func Unmarshal(b []byte, c *command.Command) error {
if b == nil || len(b) == 0 || b[0] != '{' {
return ErrNotLegacyCommand

@ -11,15 +11,19 @@ import (
)
const (
DefaultBatchThreshold = 5
DefaultSizeThreshold = 150
defaultBatchThreshold = 5
defaultSizeThreshold = 150
)
// Requester is the interface objects must support to be marshaled
// successfully.
type Requester interface {
proto.Message
GetRequest() *Request
}
// RequestMarshaler marshals Request objects, potentially performing
// gzip compression.
type RequestMarshaler struct {
BatchThreshold int
SizeThreshold int
@ -52,6 +56,7 @@ func init() {
stats.Add(numPrecompressedBytes, 0)
}
// NewRequestMarshaler returns an initialized RequestMarshaler.
func NewRequestMarshaler() *RequestMarshaler {
w, err := gzip.NewWriterLevel(nil, gzip.BestCompression)
if err != nil {
@ -59,12 +64,14 @@ func NewRequestMarshaler() *RequestMarshaler {
}
return &RequestMarshaler{
BatchThreshold: DefaultBatchThreshold,
SizeThreshold: DefaultSizeThreshold,
BatchThreshold: defaultBatchThreshold,
SizeThreshold: defaultSizeThreshold,
gz: w,
}
}
// Marshal marshals a Requester object, returning a byte slice, a bool
// indicating whether the contents are compressed, or an error.
func (m *RequestMarshaler) Marshal(r Requester) ([]byte, bool, error) {
stats.Add(numRequests, 0)
compress := false
@ -118,6 +125,8 @@ func (m *RequestMarshaler) Marshal(r Requester) ([]byte, bool, error) {
return b, compress, nil
}
// Stats returns status and diagnostic information about
// the RequestMarshaler.
func (m *RequestMarshaler) Stats() map[string]interface{} {
return map[string]interface{}{
"compression_size": m.SizeThreshold,
@ -126,31 +135,38 @@ func (m *RequestMarshaler) Stats() map[string]interface{} {
}
}
// Marshal marshals a Command.
func Marshal(c *Command) ([]byte, error) {
return proto.Marshal(c)
}
// Unmarshal unmarshals a Command
func Unmarshal(b []byte, c *Command) error {
return proto.Unmarshal(b, c)
}
// MarshalMetadataSet marshals a MetadataSet command
func MarshalMetadataSet(c *MetadataSet) ([]byte, error) {
return proto.Marshal(c)
}
// UnMarshalMetadataSet unmarshals a MetadataSet command
func UnMarshalMetadataSet(b []byte, c *MetadataSet) error {
return proto.Unmarshal(b, c)
}
// MarshalMetadataDelete marshals a MetadataDelete command
func MarshalMetadataDelete(c *MetadataDelete) ([]byte, error) {
return proto.Marshal(c)
}
// UnMarshalMetadataDelete unmarshals a MetadataDelete command
func UnMarshalMetadataDelete(b []byte, c *MetadataDelete) error {
return proto.Unmarshal(b, c)
}
// Assumes m is the is the right type....caller must use c.Type
// UnmarshalSubCommand unmarshalls a sub command m. It assumes that
// m is the correct type.
func UnmarshalSubCommand(c *Command, m proto.Message) error {
b := c.SubCommand
if c.Compressed {

@ -1,3 +1,5 @@
// Package system supports running end-to-end type testing of rqlite
/* MIT License
*
* Copyright (c) 2017 Roland Singer [roland.singer@desertbit.com]

Loading…
Cancel
Save