rpc: add cache to basic parameters

Need to cache values of string, bool, int and array because they can be
reused multiple times by RPC handlers. Other values don't need to be
cached.
This commit is contained in:
Anna Shaleva 2021-10-28 17:24:38 +03:00
parent 2fd04fbb35
commit 867bb708fc

View file

@ -23,6 +23,7 @@ type (
// the client. // the client.
Param struct { Param struct {
json.RawMessage json.RawMessage
cache interface{}
} }
// FuncParam represents a function argument parameter used in the // FuncParam represents a function argument parameter used in the
@ -86,13 +87,19 @@ func (p *Param) GetStringStrict() (string, error) {
if p.IsNull() { if p.IsNull() {
return "", errNotAString return "", errNotAString
} }
if p.cache == nil {
var s string var s string
err := json.Unmarshal(p.RawMessage, &s) err := json.Unmarshal(p.RawMessage, &s)
if err != nil { if err != nil {
return "", errNotAString return "", errNotAString
} }
p.cache = s
}
if s, ok := p.cache.(string); ok {
return s, nil return s, nil
} }
return "", errNotAString
}
// GetString returns string value of the parameter or tries to cast parameter to a string value. // GetString returns string value of the parameter or tries to cast parameter to a string value.
func (p *Param) GetString() (string, error) { func (p *Param) GetString() (string, error) {
@ -102,26 +109,41 @@ func (p *Param) GetString() (string, error) {
if p.IsNull() { if p.IsNull() {
return "", errNotAString return "", errNotAString
} }
if p.cache == nil {
var s string var s string
err := json.Unmarshal(p.RawMessage, &s) err := json.Unmarshal(p.RawMessage, &s)
if err == nil { if err == nil {
return s, nil p.cache = s
} } else {
var i int var i int
err = json.Unmarshal(p.RawMessage, &i) err = json.Unmarshal(p.RawMessage, &i)
if err == nil { if err == nil {
return strconv.Itoa(i), nil p.cache = i
} } else {
var b bool var b bool
err = json.Unmarshal(p.RawMessage, &b) err = json.Unmarshal(p.RawMessage, &b)
if err == nil { if err == nil {
if b { p.cache = b
} else {
return "", errNotAString
}
}
}
}
switch t := p.cache.(type) {
case string:
return t, nil
case int:
return strconv.Itoa(t), nil
case bool:
if t {
return "true", nil return "true", nil
} }
return "false", nil return "false", nil
} default:
return "", errNotAString return "", errNotAString
} }
}
// GetBooleanStrict returns boolean value of the parameter. // GetBooleanStrict returns boolean value of the parameter.
func (p *Param) GetBooleanStrict() (bool, error) { func (p *Param) GetBooleanStrict() (bool, error) {
@ -129,9 +151,11 @@ func (p *Param) GetBooleanStrict() (bool, error) {
return false, errMissingParameter return false, errMissingParameter
} }
if bytes.Equal(p.RawMessage, jsonTrueBytes) { if bytes.Equal(p.RawMessage, jsonTrueBytes) {
p.cache = true
return true, nil return true, nil
} }
if bytes.Equal(p.RawMessage, jsonFalseBytes) { if bytes.Equal(p.RawMessage, jsonFalseBytes) {
p.cache = false
return false, nil return false, nil
} }
return false, errNotABool return false, errNotABool
@ -146,22 +170,37 @@ func (p *Param) GetBoolean() (bool, error) {
return false, errNotABool return false, errNotABool
} }
var b bool var b bool
if p.cache == nil {
err := json.Unmarshal(p.RawMessage, &b) err := json.Unmarshal(p.RawMessage, &b)
if err == nil { if err == nil {
return b, nil p.cache = b
} } else {
var s string var s string
err = json.Unmarshal(p.RawMessage, &s) err = json.Unmarshal(p.RawMessage, &s)
if err == nil { if err == nil {
return s != "", nil p.cache = s
} } else {
var i int var i int
err = json.Unmarshal(p.RawMessage, &i) err = json.Unmarshal(p.RawMessage, &i)
if err == nil { if err == nil {
return i != 0, nil p.cache = i
} } else {
return false, errNotABool return false, errNotABool
} }
}
}
}
switch t := p.cache.(type) {
case bool:
return t, nil
case string:
return t != "", nil
case int:
return t != 0, nil
default:
return false, errNotABool
}
}
// GetIntStrict returns int value of the parameter if the parameter is integer. // GetIntStrict returns int value of the parameter if the parameter is integer.
func (p *Param) GetIntStrict() (int, error) { func (p *Param) GetIntStrict() (int, error) {
@ -171,13 +210,19 @@ func (p *Param) GetIntStrict() (int, error) {
if p.IsNull() { if p.IsNull() {
return 0, errNotAnInt return 0, errNotAnInt
} }
if p.cache == nil {
var i int var i int
err := json.Unmarshal(p.RawMessage, &i) err := json.Unmarshal(p.RawMessage, &i)
if err != nil { if err != nil {
return i, errNotAnInt return i, errNotAnInt
} }
p.cache = i
}
if i, ok := p.cache.(int); ok {
return i, nil return i, nil
} }
return 0, errNotAnInt
}
// GetInt returns int value of the parameter or tries to cast parameter to an int value. // GetInt returns int value of the parameter or tries to cast parameter to an int value.
func (p *Param) GetInt() (int, error) { func (p *Param) GetInt() (int, error) {
@ -187,27 +232,41 @@ func (p *Param) GetInt() (int, error) {
if p.IsNull() { if p.IsNull() {
return 0, errNotAnInt return 0, errNotAnInt
} }
if p.cache == nil {
var i int var i int
err := json.Unmarshal(p.RawMessage, &i) err := json.Unmarshal(p.RawMessage, &i)
if err == nil { if err == nil {
return i, nil p.cache = i
} } else {
var s string var s string
err = json.Unmarshal(p.RawMessage, &s) err = json.Unmarshal(p.RawMessage, &s)
if err == nil { if err == nil {
return strconv.Atoi(s) p.cache = s
} } else {
var b bool var b bool
err = json.Unmarshal(p.RawMessage, &b) err = json.Unmarshal(p.RawMessage, &b)
if err == nil { if err == nil {
i = 0 p.cache = b
if b { } else {
i = 1
}
return i, nil
}
return 0, errNotAnInt return 0, errNotAnInt
} }
}
}
}
switch t := p.cache.(type) {
case int:
return t, nil
case string:
return strconv.Atoi(t)
case bool:
if t {
return 1, nil
}
return 0, nil
default:
return 0, errNotAnInt
}
}
// GetArray returns a slice of Params stored in the parameter. // GetArray returns a slice of Params stored in the parameter.
func (p *Param) GetArray() ([]Param, error) { func (p *Param) GetArray() ([]Param, error) {
@ -217,13 +276,19 @@ func (p *Param) GetArray() ([]Param, error) {
if p.IsNull() { if p.IsNull() {
return nil, errNotAnArray return nil, errNotAnArray
} }
if p.cache == nil {
a := []Param{} a := []Param{}
err := json.Unmarshal(p.RawMessage, &a) err := json.Unmarshal(p.RawMessage, &a)
if err != nil { if err != nil {
return nil, errNotAnArray return nil, errNotAnArray
} }
p.cache = a
}
if a, ok := p.cache.([]Param); ok {
return a, nil return a, nil
} }
return nil, errNotAnArray
}
// GetUint256 returns Uint256 value of the parameter. // GetUint256 returns Uint256 value of the parameter.
func (p *Param) GetUint256() (util.Uint256, error) { func (p *Param) GetUint256() (util.Uint256, error) {
@ -274,6 +339,7 @@ func (p *Param) GetFuncParam() (FuncParam, error) {
if p == nil { if p == nil {
return FuncParam{}, errMissingParameter return FuncParam{}, errMissingParameter
} }
// This one doesn't need to be cached, it's used only once.
fp := FuncParam{} fp := FuncParam{}
err := json.Unmarshal(p.RawMessage, &fp) err := json.Unmarshal(p.RawMessage, &fp)
return fp, err return fp, err
@ -303,6 +369,7 @@ func (p *Param) GetBytesBase64() ([]byte, error) {
// GetSignerWithWitness returns SignerWithWitness value of the parameter. // GetSignerWithWitness returns SignerWithWitness value of the parameter.
func (p *Param) GetSignerWithWitness() (SignerWithWitness, error) { func (p *Param) GetSignerWithWitness() (SignerWithWitness, error) {
// This one doesn't need to be cached, it's used only once.
aux := new(signerWithWitnessAux) aux := new(signerWithWitnessAux)
err := json.Unmarshal(p.RawMessage, aux) err := json.Unmarshal(p.RawMessage, aux)
if err != nil { if err != nil {