func_context.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 context
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/topo/connection"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. )
  20. type DefaultFuncContext struct {
  21. api.StreamContext
  22. funcId int
  23. }
  24. func NewDefaultFuncContext(ctx api.StreamContext, id int) *DefaultFuncContext {
  25. return &DefaultFuncContext{
  26. StreamContext: ctx,
  27. funcId: id,
  28. }
  29. }
  30. func (c *DefaultFuncContext) IncrCounter(key string, amount int) error {
  31. return c.StreamContext.IncrCounter(c.convertKey(key), amount)
  32. }
  33. func (c *DefaultFuncContext) GetCounter(key string) (int, error) {
  34. return c.StreamContext.GetCounter(c.convertKey(key))
  35. }
  36. func (c *DefaultFuncContext) PutState(key string, value interface{}) error {
  37. return c.StreamContext.PutState(c.convertKey(key), value)
  38. }
  39. func (c *DefaultFuncContext) GetState(key string) (interface{}, error) {
  40. return c.StreamContext.GetState(c.convertKey(key))
  41. }
  42. func (c *DefaultFuncContext) DeleteState(key string) error {
  43. return c.StreamContext.DeleteState(c.convertKey(key))
  44. }
  45. func (c *DefaultFuncContext) GetFuncId() int {
  46. return c.funcId
  47. }
  48. func (c *DefaultFuncContext) GetConnection(connectSelector string) (interface{}, error) {
  49. return connection.GetConnection(connectSelector)
  50. }
  51. func (c *DefaultFuncContext) ReleaseConnection(connectSelector string) {
  52. connection.ReleaseConnection(connectSelector)
  53. }
  54. func (c *DefaultFuncContext) convertKey(key string) string {
  55. return fmt.Sprintf("$$func%d_%s", c.funcId, key)
  56. }