stores.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2021 INTECH Process Automation 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 kv
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/pkg/db"
  18. "github.com/lf-edge/ekuiper/internal/pkg/store"
  19. "github.com/lf-edge/ekuiper/internal/pkg/ts"
  20. st "github.com/lf-edge/ekuiper/pkg/kv/stores"
  21. "sync"
  22. )
  23. type stores struct {
  24. kv map[string]st.KeyValue
  25. ts map[string]st.Tskv
  26. mu sync.Mutex
  27. kvBuilder store.Builder
  28. tsBuilder ts.Builder
  29. }
  30. func newStores(db db.Database) (error, *stores) {
  31. var err error
  32. var kvBuilder store.Builder
  33. var tsBuilder ts.Builder
  34. err, kvBuilder = store.CreateStoreBuilder(db)
  35. if err != nil {
  36. return err, nil
  37. }
  38. err, tsBuilder = ts.CreateTsBuilder(db)
  39. if err != nil {
  40. return err, nil
  41. }
  42. return nil, &stores{
  43. kv: make(map[string]st.KeyValue),
  44. ts: make(map[string]st.Tskv),
  45. mu: sync.Mutex{},
  46. kvBuilder: kvBuilder,
  47. tsBuilder: tsBuilder,
  48. }
  49. }
  50. func (s *stores) GetKV(table string) (error, st.KeyValue) {
  51. s.mu.Lock()
  52. defer s.mu.Unlock()
  53. if ks, contains := s.kv[table]; contains {
  54. return nil, ks
  55. }
  56. err, ks := s.kvBuilder.CreateStore(table)
  57. if err != nil {
  58. return err, nil
  59. }
  60. s.kv[table] = ks
  61. return nil, ks
  62. }
  63. func (s *stores) GetTS(table string) (error, st.Tskv) {
  64. s.mu.Lock()
  65. defer s.mu.Unlock()
  66. if tts, contains := s.ts[table]; contains {
  67. return nil, tts
  68. }
  69. err, tts := s.tsBuilder.CreateTs(table)
  70. if err != nil {
  71. return err, nil
  72. }
  73. s.ts[table] = tts
  74. return nil, tts
  75. }
  76. var globalStores *stores = nil
  77. func InitGlobalStores(db db.Database) error {
  78. var err error
  79. err, globalStores = newStores(db)
  80. return err
  81. }
  82. func GetKV(table string) (error, st.KeyValue) {
  83. if globalStores == nil {
  84. return fmt.Errorf("global stores are not initialized"), nil
  85. }
  86. return globalStores.GetKV(table)
  87. }
  88. func GetTS(table string) (error, st.Tskv) {
  89. if globalStores == nil {
  90. return fmt.Errorf("global stores are not initialized"), nil
  91. }
  92. return globalStores.GetTS(table)
  93. }