api.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2021 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 api
  15. import (
  16. "context"
  17. )
  18. type SourceTuple interface {
  19. Message() map[string]interface{}
  20. Meta() map[string]interface{}
  21. }
  22. type DefaultSourceTuple struct {
  23. Mess map[string]interface{} `json:"message"`
  24. M map[string]interface{} `json:"meta"`
  25. }
  26. func NewDefaultSourceTuple(message map[string]interface{}, meta map[string]interface{}) *DefaultSourceTuple {
  27. return &DefaultSourceTuple{
  28. Mess: message,
  29. M: meta,
  30. }
  31. }
  32. func (t *DefaultSourceTuple) Message() map[string]interface{} {
  33. return t.Mess
  34. }
  35. func (t *DefaultSourceTuple) Meta() map[string]interface{} {
  36. return t.M
  37. }
  38. type Source interface {
  39. // Open Should be sync function for normal case. The container will run it in go func
  40. Open(ctx StreamContext, consumer chan<- SourceTuple, errCh chan<- error)
  41. // Configure Called during initialization. Configure the source with the data source(e.g. topic for mqtt) and the properties read from the yaml
  42. Configure(datasource string, props map[string]interface{}) error
  43. Closable
  44. }
  45. type Function interface {
  46. //The argument is a list of xsql.Expr
  47. Validate(args []interface{}) error
  48. //Execute the function, return the result and if execution is successful.
  49. //If execution fails, return the error and false.
  50. Exec(args []interface{}, ctx FunctionContext) (interface{}, bool)
  51. //If this function is an aggregate function. Each parameter of an aggregate function will be a slice
  52. IsAggregate() bool
  53. }
  54. type Sink interface {
  55. //Should be sync function for normal case. The container will run it in go func
  56. Open(ctx StreamContext) error
  57. //Called during initialization. Configure the sink with the properties from rule action definition
  58. Configure(props map[string]interface{}) error
  59. //Called when each row of data has transferred to this sink
  60. Collect(ctx StreamContext, data interface{}) error
  61. Closable
  62. }
  63. type Closable interface {
  64. Close(ctx StreamContext) error
  65. }
  66. type Logger interface {
  67. Debug(args ...interface{})
  68. Info(args ...interface{})
  69. Warn(args ...interface{})
  70. Error(args ...interface{})
  71. Debugln(args ...interface{})
  72. Infoln(args ...interface{})
  73. Warnln(args ...interface{})
  74. Errorln(args ...interface{})
  75. Debugf(format string, args ...interface{})
  76. Infof(format string, args ...interface{})
  77. Warnf(format string, args ...interface{})
  78. Errorf(format string, args ...interface{})
  79. }
  80. type StreamContext interface {
  81. context.Context
  82. GetLogger() Logger
  83. GetRuleId() string
  84. GetOpId() string
  85. GetInstanceId() int
  86. WithMeta(ruleId string, opId string) StreamContext
  87. WithInstance(instanceId int) StreamContext
  88. WithCancel() (StreamContext, context.CancelFunc)
  89. }
  90. type FunctionContext interface {
  91. StreamContext
  92. GetFuncId() int
  93. }