funcs_trans.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package function
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/compressor"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/lf-edge/ekuiper/pkg/ast"
  20. "github.com/lf-edge/ekuiper/pkg/cast"
  21. "github.com/lf-edge/ekuiper/pkg/message"
  22. )
  23. type compressFunc struct {
  24. compressType string
  25. compressor message.Compressor
  26. }
  27. func (c *compressFunc) Validate(args []interface{}) error {
  28. var eargs []ast.Expr
  29. for _, arg := range args {
  30. if t, ok := arg.(ast.Expr); ok {
  31. eargs = append(eargs, t)
  32. } else {
  33. // should never happen
  34. return fmt.Errorf("receive invalid arg %v", arg)
  35. }
  36. }
  37. return ValidateTwoStrArg(nil, eargs)
  38. }
  39. func (c *compressFunc) Exec(args []interface{}, ctx api.FunctionContext) (interface{}, bool) {
  40. if args[0] == nil {
  41. return nil, true
  42. }
  43. arg0, err := cast.ToBytes(args[0], cast.CONVERT_SAMEKIND)
  44. if err != nil {
  45. return fmt.Errorf("require string or bytea parameter, but got %v", args[0]), false
  46. }
  47. arg1 := cast.ToStringAlways(args[1])
  48. if c.compressor != nil {
  49. if c.compressType != arg1 {
  50. return fmt.Errorf("compress type must be consistent, previous %s, now %s", c.compressType, arg1), false
  51. }
  52. } else {
  53. ctx.GetLogger().Infof("creating compressor %s", arg1)
  54. c.compressor, err = compressor.GetCompressor(arg1)
  55. if err != nil {
  56. return err, false
  57. }
  58. c.compressType = arg1
  59. }
  60. r, e := c.compressor.Compress(arg0)
  61. if e != nil {
  62. return e, false
  63. }
  64. return r, true
  65. }
  66. func (c *compressFunc) IsAggregate() bool {
  67. return false
  68. }
  69. type decompressFunc struct {
  70. compressType string
  71. decompressor message.Decompressor
  72. }
  73. func (d *decompressFunc) Validate(args []interface{}) error {
  74. var eargs []ast.Expr
  75. for _, arg := range args {
  76. if t, ok := arg.(ast.Expr); ok {
  77. eargs = append(eargs, t)
  78. } else {
  79. // should never happen
  80. return fmt.Errorf("receive invalid arg %v", arg)
  81. }
  82. }
  83. return ValidateTwoStrArg(nil, eargs)
  84. }
  85. func (d *decompressFunc) Exec(args []interface{}, ctx api.FunctionContext) (interface{}, bool) {
  86. if args[0] == nil {
  87. return nil, true
  88. }
  89. arg0, err := cast.ToBytes(args[0], cast.CONVERT_SAMEKIND)
  90. if err != nil {
  91. return fmt.Errorf("require string or bytea parameter, but got %v", args[0]), false
  92. }
  93. arg1 := cast.ToStringAlways(args[1])
  94. if d.decompressor != nil {
  95. if d.compressType != arg1 {
  96. return fmt.Errorf("decompress type must be consistent, previous %s, now %s", d.compressType, arg1), false
  97. }
  98. } else {
  99. ctx.GetLogger().Infof("creating decompressor %s", arg1)
  100. d.decompressor, err = compressor.GetDecompressor(arg1)
  101. if err != nil {
  102. return err, false
  103. }
  104. d.compressType = arg1
  105. }
  106. r, e := d.decompressor.Decompress(arg0)
  107. if e != nil {
  108. return e, false
  109. }
  110. return r, true
  111. }
  112. func (d *decompressFunc) IsAggregate() bool {
  113. return false
  114. }