stores.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright 2021-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 store
  15. import (
  16. "fmt"
  17. "path"
  18. "strings"
  19. "sync"
  20. "github.com/lf-edge/ekuiper/internal/pkg/store/definition"
  21. "github.com/lf-edge/ekuiper/internal/pkg/store/sql"
  22. "github.com/lf-edge/ekuiper/pkg/kv"
  23. )
  24. type StoreCreator func(conf definition.Config, name string) (definition.StoreBuilder, definition.TsBuilder, error)
  25. var (
  26. storeBuilders = map[string]StoreCreator{
  27. "sqlite": sql.BuildStores,
  28. }
  29. globalStores *stores = nil
  30. cacheStores *stores = nil
  31. extStateStores *stores = nil
  32. )
  33. type stores struct {
  34. kv map[string]kv.KeyValue
  35. ts map[string]kv.Tskv
  36. mu sync.Mutex
  37. kvBuilder definition.StoreBuilder
  38. tsBuilder definition.TsBuilder
  39. }
  40. func newStores(c definition.Config, name string) (*stores, error) {
  41. databaseType := c.Type
  42. if builder, ok := storeBuilders[databaseType]; ok {
  43. kvBuilder, tsBuilder, err := builder(c, name)
  44. if err != nil {
  45. return nil, err
  46. } else {
  47. return &stores{
  48. kv: make(map[string]kv.KeyValue),
  49. ts: make(map[string]kv.Tskv),
  50. mu: sync.Mutex{},
  51. kvBuilder: kvBuilder,
  52. tsBuilder: tsBuilder,
  53. }, nil
  54. }
  55. } else {
  56. return nil, fmt.Errorf("unknown database type: %s", databaseType)
  57. }
  58. }
  59. func newExtStateStores(c definition.Config, name string) (*stores, error) {
  60. databaseType := c.ExtStateType
  61. if builder, ok := storeBuilders[databaseType]; ok {
  62. kvBuilder, tsBuilder, err := builder(c, name)
  63. if err != nil {
  64. return nil, err
  65. } else {
  66. return &stores{
  67. kv: make(map[string]kv.KeyValue),
  68. ts: make(map[string]kv.Tskv),
  69. mu: sync.Mutex{},
  70. kvBuilder: kvBuilder,
  71. tsBuilder: tsBuilder,
  72. }, nil
  73. }
  74. } else {
  75. return nil, fmt.Errorf("unknown extStateStore type: %s", databaseType)
  76. }
  77. }
  78. func (s *stores) GetKV(table string) (kv.KeyValue, error) {
  79. s.mu.Lock()
  80. defer s.mu.Unlock()
  81. if ks, contains := s.kv[table]; contains {
  82. return ks, nil
  83. }
  84. ks, err := s.kvBuilder.CreateStore(table)
  85. if err != nil {
  86. return nil, err
  87. }
  88. s.kv[table] = ks
  89. return ks, nil
  90. }
  91. func (s *stores) DropKV(table string) {
  92. s.mu.Lock()
  93. defer s.mu.Unlock()
  94. if ks, contains := s.kv[table]; contains {
  95. _ = ks.Drop()
  96. delete(s.ts, table)
  97. }
  98. }
  99. func (s *stores) DropRefKVs(tablePrefix string) {
  100. s.mu.Lock()
  101. defer s.mu.Unlock()
  102. for table, ks := range s.kv {
  103. if strings.HasPrefix(table, tablePrefix) {
  104. _ = ks.Drop()
  105. delete(s.kv, table)
  106. }
  107. }
  108. }
  109. func (s *stores) GetTS(table string) (kv.Tskv, error) {
  110. s.mu.Lock()
  111. defer s.mu.Unlock()
  112. if tts, contains := s.ts[table]; contains {
  113. return tts, nil
  114. }
  115. tts, err := s.tsBuilder.CreateTs(table)
  116. if err != nil {
  117. return nil, err
  118. }
  119. s.ts[table] = tts
  120. return tts, nil
  121. }
  122. func (s *stores) DropTS(table string) {
  123. s.mu.Lock()
  124. defer s.mu.Unlock()
  125. if tts, contains := s.ts[table]; contains {
  126. _ = tts.Drop()
  127. delete(s.ts, table)
  128. }
  129. }
  130. func GetKV(table string) (kv.KeyValue, error) {
  131. if globalStores == nil {
  132. return nil, fmt.Errorf("global stores are not initialized")
  133. }
  134. return globalStores.GetKV(table)
  135. }
  136. func GetTS(table string) (kv.Tskv, error) {
  137. if globalStores == nil {
  138. return nil, fmt.Errorf("global stores are not initialized")
  139. }
  140. return globalStores.GetTS(table)
  141. }
  142. func DropTS(table string) error {
  143. if globalStores == nil {
  144. return fmt.Errorf("global stores are not initialized")
  145. }
  146. globalStores.DropTS(table)
  147. return nil
  148. }
  149. func DropKV(table string) error {
  150. if globalStores == nil {
  151. return fmt.Errorf("global stores are not initialized")
  152. }
  153. globalStores.DropKV(table)
  154. return nil
  155. }
  156. func GetCacheKV(table string) (kv.KeyValue, error) {
  157. if cacheStores == nil {
  158. return nil, fmt.Errorf("cache stores are not initialized")
  159. }
  160. return cacheStores.GetKV(table)
  161. }
  162. func DropCacheKV(table string) error {
  163. if cacheStores == nil {
  164. return fmt.Errorf("cache stores are not initialized")
  165. }
  166. cacheStores.DropKV(table)
  167. return nil
  168. }
  169. func DropCacheKVForRule(rule string) error {
  170. if cacheStores == nil {
  171. return fmt.Errorf("cache stores are not initialized")
  172. }
  173. cacheStores.DropRefKVs(path.Join("sink", rule))
  174. return nil
  175. }
  176. func GetExtStateKV(table string) (kv.KeyValue, error) {
  177. if extStateStores == nil {
  178. return nil, fmt.Errorf("extState stores are not initialized")
  179. }
  180. return extStateStores.GetKV(table)
  181. }