stores.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 store
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/pkg/db"
  18. "github.com/lf-edge/ekuiper/internal/pkg/ts"
  19. kv2 "github.com/lf-edge/ekuiper/pkg/kv"
  20. "sync"
  21. )
  22. type stores struct {
  23. kv map[string]kv2.KeyValue
  24. ts map[string]kv2.Tskv
  25. mu sync.Mutex
  26. kvBuilder Builder
  27. tsBuilder ts.Builder
  28. }
  29. func newStores(db db.Database) (error, *stores) {
  30. var err error
  31. var kvBuilder Builder
  32. var tsBuilder ts.Builder
  33. kvBuilder, err = CreateStoreBuilder(db)
  34. if err != nil {
  35. return err, nil
  36. }
  37. err, tsBuilder = ts.CreateTsBuilder(db)
  38. if err != nil {
  39. return err, nil
  40. }
  41. return nil, &stores{
  42. kv: make(map[string]kv2.KeyValue),
  43. ts: make(map[string]kv2.Tskv),
  44. mu: sync.Mutex{},
  45. kvBuilder: kvBuilder,
  46. tsBuilder: tsBuilder,
  47. }
  48. }
  49. func (s *stores) GetKV(table string) (error, kv2.KeyValue) {
  50. s.mu.Lock()
  51. defer s.mu.Unlock()
  52. if ks, contains := s.kv[table]; contains {
  53. return nil, ks
  54. }
  55. ks, err := s.kvBuilder.CreateStore(table)
  56. if err != nil {
  57. return err, nil
  58. }
  59. s.kv[table] = ks
  60. return nil, ks
  61. }
  62. func (s *stores) DropKV(table string) {
  63. s.mu.Lock()
  64. defer s.mu.Unlock()
  65. if ks, contains := s.kv[table]; contains {
  66. _ = ks.Drop()
  67. delete(s.ts, table)
  68. }
  69. }
  70. func (s *stores) GetTS(table string) (error, kv2.Tskv) {
  71. s.mu.Lock()
  72. defer s.mu.Unlock()
  73. if tts, contains := s.ts[table]; contains {
  74. return nil, tts
  75. }
  76. err, tts := s.tsBuilder.CreateTs(table)
  77. if err != nil {
  78. return err, nil
  79. }
  80. s.ts[table] = tts
  81. return nil, tts
  82. }
  83. func (s *stores) DropTS(table string) {
  84. s.mu.Lock()
  85. defer s.mu.Unlock()
  86. if tts, contains := s.ts[table]; contains {
  87. _ = tts.Drop()
  88. delete(s.ts, table)
  89. }
  90. }
  91. var globalStores *stores = nil
  92. func InitGlobalStores(db db.Database) error {
  93. var err error
  94. err, globalStores = newStores(db)
  95. return err
  96. }
  97. func GetKV(table string) (error, kv2.KeyValue) {
  98. if globalStores == nil {
  99. return fmt.Errorf("global stores are not initialized"), nil
  100. }
  101. return globalStores.GetKV(table)
  102. }
  103. func GetTS(table string) (error, kv2.Tskv) {
  104. if globalStores == nil {
  105. return fmt.Errorf("global stores are not initialized"), nil
  106. }
  107. return globalStores.GetTS(table)
  108. }
  109. func DropTS(table string) error {
  110. if globalStores == nil {
  111. return fmt.Errorf("global stores are not initialized")
  112. }
  113. globalStores.DropTS(table)
  114. return nil
  115. }
  116. func DropKV(table string) error {
  117. if globalStores == nil {
  118. return fmt.Errorf("global stores are not initialized")
  119. }
  120. globalStores.DropKV(table)
  121. return nil
  122. }