stores.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2021-2022 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/store/definition"
  18. "github.com/lf-edge/ekuiper/internal/pkg/store/sql"
  19. "github.com/lf-edge/ekuiper/pkg/kv"
  20. "sync"
  21. )
  22. type StoreCreator func(conf definition.Config, name string) (definition.StoreBuilder, definition.TsBuilder, error)
  23. var (
  24. storeBuilders = map[string]StoreCreator{
  25. "sqlite": sql.BuildStores,
  26. }
  27. globalStores *stores = nil
  28. cacheStores *stores = nil
  29. )
  30. type stores struct {
  31. kv map[string]kv.KeyValue
  32. ts map[string]kv.Tskv
  33. mu sync.Mutex
  34. kvBuilder definition.StoreBuilder
  35. tsBuilder definition.TsBuilder
  36. }
  37. func newStores(c definition.Config, name string) (*stores, error) {
  38. databaseType := c.Type
  39. if builder, ok := storeBuilders[databaseType]; ok {
  40. kvBuilder, tsBuilder, err := builder(c, name)
  41. if err != nil {
  42. return nil, err
  43. } else {
  44. return &stores{
  45. kv: make(map[string]kv.KeyValue),
  46. ts: make(map[string]kv.Tskv),
  47. mu: sync.Mutex{},
  48. kvBuilder: kvBuilder,
  49. tsBuilder: tsBuilder,
  50. }, nil
  51. }
  52. } else {
  53. return nil, fmt.Errorf("unknown database type: %s", databaseType)
  54. }
  55. }
  56. func (s *stores) GetKV(table string) (error, kv.KeyValue) {
  57. s.mu.Lock()
  58. defer s.mu.Unlock()
  59. if ks, contains := s.kv[table]; contains {
  60. return nil, ks
  61. }
  62. ks, err := s.kvBuilder.CreateStore(table)
  63. if err != nil {
  64. return err, nil
  65. }
  66. s.kv[table] = ks
  67. return nil, ks
  68. }
  69. func (s *stores) DropKV(table string) {
  70. s.mu.Lock()
  71. defer s.mu.Unlock()
  72. if ks, contains := s.kv[table]; contains {
  73. _ = ks.Drop()
  74. delete(s.ts, table)
  75. }
  76. }
  77. func (s *stores) GetTS(table string) (error, kv.Tskv) {
  78. s.mu.Lock()
  79. defer s.mu.Unlock()
  80. if tts, contains := s.ts[table]; contains {
  81. return nil, tts
  82. }
  83. err, tts := s.tsBuilder.CreateTs(table)
  84. if err != nil {
  85. return err, nil
  86. }
  87. s.ts[table] = tts
  88. return nil, tts
  89. }
  90. func (s *stores) DropTS(table string) {
  91. s.mu.Lock()
  92. defer s.mu.Unlock()
  93. if tts, contains := s.ts[table]; contains {
  94. _ = tts.Drop()
  95. delete(s.ts, table)
  96. }
  97. }
  98. func GetKV(table string) (error, kv.KeyValue) {
  99. if globalStores == nil {
  100. return fmt.Errorf("global stores are not initialized"), nil
  101. }
  102. return globalStores.GetKV(table)
  103. }
  104. func GetTS(table string) (error, kv.Tskv) {
  105. if globalStores == nil {
  106. return fmt.Errorf("global stores are not initialized"), nil
  107. }
  108. return globalStores.GetTS(table)
  109. }
  110. func DropTS(table string) error {
  111. if globalStores == nil {
  112. return fmt.Errorf("global stores are not initialized")
  113. }
  114. globalStores.DropTS(table)
  115. return nil
  116. }
  117. func DropKV(table string) error {
  118. if globalStores == nil {
  119. return fmt.Errorf("global stores are not initialized")
  120. }
  121. globalStores.DropKV(table)
  122. return nil
  123. }
  124. func GetCacheKV(table string) (error, kv.KeyValue) {
  125. if cacheStores == nil {
  126. return fmt.Errorf("cache stores are not initialized"), nil
  127. }
  128. return cacheStores.GetKV(table)
  129. }
  130. func DropCacheKV(table string) error {
  131. if cacheStores == nil {
  132. return fmt.Errorf("cache stores are not initialized")
  133. }
  134. cacheStores.DropKV(table)
  135. return nil
  136. }