stores.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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) (definition.StoreBuilder, definition.TsBuilder, error)
  23. var (
  24. storeBuilders = map[string]StoreCreator{
  25. "sqlite": sql.BuildStores,
  26. }
  27. globalStores *stores = nil
  28. )
  29. type stores struct {
  30. kv map[string]kv.KeyValue
  31. ts map[string]kv.Tskv
  32. mu sync.Mutex
  33. kvBuilder definition.StoreBuilder
  34. tsBuilder definition.TsBuilder
  35. }
  36. func newStores(c definition.Config) (*stores, error) {
  37. databaseType := c.Type
  38. if builder, ok := storeBuilders[databaseType]; ok {
  39. kvBuilder, tsBuilder, err := builder(c)
  40. if err != nil {
  41. return nil, err
  42. } else {
  43. return &stores{
  44. kv: make(map[string]kv.KeyValue),
  45. ts: make(map[string]kv.Tskv),
  46. mu: sync.Mutex{},
  47. kvBuilder: kvBuilder,
  48. tsBuilder: tsBuilder,
  49. }, nil
  50. }
  51. } else {
  52. return nil, fmt.Errorf("unknown database type: %s", databaseType)
  53. }
  54. }
  55. func (s *stores) GetKV(table string) (error, kv.KeyValue) {
  56. s.mu.Lock()
  57. defer s.mu.Unlock()
  58. if ks, contains := s.kv[table]; contains {
  59. return nil, ks
  60. }
  61. ks, err := s.kvBuilder.CreateStore(table)
  62. if err != nil {
  63. return err, nil
  64. }
  65. s.kv[table] = ks
  66. return nil, ks
  67. }
  68. func (s *stores) DropKV(table string) {
  69. s.mu.Lock()
  70. defer s.mu.Unlock()
  71. if ks, contains := s.kv[table]; contains {
  72. _ = ks.Drop()
  73. delete(s.ts, table)
  74. }
  75. }
  76. func (s *stores) GetTS(table string) (error, kv.Tskv) {
  77. s.mu.Lock()
  78. defer s.mu.Unlock()
  79. if tts, contains := s.ts[table]; contains {
  80. return nil, tts
  81. }
  82. err, tts := s.tsBuilder.CreateTs(table)
  83. if err != nil {
  84. return err, nil
  85. }
  86. s.ts[table] = tts
  87. return nil, tts
  88. }
  89. func (s *stores) DropTS(table string) {
  90. s.mu.Lock()
  91. defer s.mu.Unlock()
  92. if tts, contains := s.ts[table]; contains {
  93. _ = tts.Drop()
  94. delete(s.ts, table)
  95. }
  96. }
  97. func GetKV(table string) (error, kv.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, kv.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. }