registry.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright 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 schema
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "strings"
  20. "sync"
  21. "github.com/lf-edge/ekuiper/internal/conf"
  22. "github.com/lf-edge/ekuiper/internal/pkg/def"
  23. "github.com/lf-edge/ekuiper/internal/pkg/httpx"
  24. )
  25. // Initialize in the server startup
  26. var registry *Registry
  27. type Files struct {
  28. SchemaFile string
  29. SoFile string
  30. }
  31. // Registry is a global registry for schemas
  32. // It stores the schema ids and the ref to its file content in memory
  33. // The schema definition is stored in the file system and will only be loaded once used
  34. type Registry struct {
  35. sync.RWMutex
  36. // The map of schema files for all types
  37. schemas map[def.SchemaType]map[string]*Files
  38. }
  39. // Registry provide the method to add, update, get and parse and delete schemas
  40. // InitRegistry initialize the registry, only called once by the server
  41. func InitRegistry() error {
  42. registry = &Registry{
  43. schemas: make(map[def.SchemaType]map[string]*Files, len(def.SchemaTypes)),
  44. }
  45. dataDir, err := conf.GetDataLoc()
  46. if err != nil {
  47. return fmt.Errorf("cannot find etc folder: %s", err)
  48. }
  49. for _, schemaType := range def.SchemaTypes {
  50. schemaDir := filepath.Join(dataDir, "schemas", string(schemaType))
  51. var newSchemas map[string]*Files
  52. files, err := os.ReadDir(schemaDir)
  53. if err != nil {
  54. conf.Log.Warnf("cannot read schema directory: %s", err)
  55. newSchemas = make(map[string]*Files)
  56. } else {
  57. newSchemas = make(map[string]*Files, len(files))
  58. for _, file := range files {
  59. fileName := filepath.Base(file.Name())
  60. ext := filepath.Ext(fileName)
  61. schemaId := strings.TrimSuffix(fileName, filepath.Ext(fileName))
  62. ffs, ok := newSchemas[schemaId]
  63. if !ok {
  64. ffs = &Files{}
  65. newSchemas[schemaId] = ffs
  66. }
  67. switch ext {
  68. case ".so":
  69. ffs.SoFile = filepath.Join(schemaDir, file.Name())
  70. default:
  71. ffs.SchemaFile = filepath.Join(schemaDir, file.Name())
  72. }
  73. conf.Log.Infof("schema file %s.%s loaded", schemaType, fileName)
  74. }
  75. }
  76. registry.schemas[schemaType] = newSchemas
  77. }
  78. return nil
  79. }
  80. func GetAllForType(schemaType def.SchemaType) ([]string, error) {
  81. registry.RLock()
  82. defer registry.RUnlock()
  83. if _, ok := registry.schemas[schemaType]; !ok {
  84. return nil, fmt.Errorf("schema type %s not found", schemaType)
  85. }
  86. result := make([]string, 0, len(registry.schemas[schemaType]))
  87. for k := range registry.schemas[schemaType] {
  88. result = append(result, k)
  89. }
  90. return result, nil
  91. }
  92. func Register(info *Info) error {
  93. if _, ok := registry.schemas[info.Type]; !ok {
  94. return fmt.Errorf("schema type %s not found", info.Type)
  95. }
  96. if _, ok := registry.schemas[info.Type][info.Name]; ok {
  97. return fmt.Errorf("schema %s.%s already registered", info.Type, info.Name)
  98. }
  99. return CreateOrUpdateSchema(info)
  100. }
  101. func CreateOrUpdateSchema(info *Info) error {
  102. if _, ok := registry.schemas[info.Type]; !ok {
  103. return fmt.Errorf("schema type %s not found", info.Type)
  104. }
  105. dataDir, _ := conf.GetDataLoc()
  106. etcDir := filepath.Join(dataDir, "schemas", string(info.Type))
  107. if err := os.MkdirAll(etcDir, os.ModePerm); err != nil {
  108. return err
  109. }
  110. ffs := &Files{}
  111. if info.Content != "" || info.FilePath != "" {
  112. schemaFile := filepath.Join(etcDir, info.Name+schemaExt[info.Type])
  113. if _, err := os.Stat(schemaFile); os.IsNotExist(err) {
  114. file, err := os.Create(schemaFile)
  115. if err != nil {
  116. return err
  117. }
  118. defer file.Close()
  119. }
  120. if info.Content != "" {
  121. err := os.WriteFile(schemaFile, []byte(info.Content), 0666)
  122. if err != nil {
  123. return err
  124. }
  125. } else {
  126. err := httpx.DownloadFile(schemaFile, info.FilePath)
  127. if err != nil {
  128. return err
  129. }
  130. }
  131. ffs.SchemaFile = schemaFile
  132. }
  133. if info.SoPath != "" {
  134. soFile := filepath.Join(etcDir, info.Name+".so")
  135. err := httpx.DownloadFile(soFile, info.SoPath)
  136. if err != nil {
  137. return err
  138. }
  139. ffs.SoFile = soFile
  140. }
  141. registry.schemas[info.Type][info.Name] = ffs
  142. return nil
  143. }
  144. func GetSchema(schemaType def.SchemaType, name string) (*Info, error) {
  145. schemaFile, err := GetSchemaFile(schemaType, name)
  146. if err != nil {
  147. return nil, err
  148. }
  149. if schemaFile.SchemaFile != "" {
  150. content, err := os.ReadFile(schemaFile.SchemaFile)
  151. if err != nil {
  152. return nil, fmt.Errorf("cannot read schema file %s: %s", schemaFile, err)
  153. }
  154. return &Info{
  155. Type: schemaType,
  156. Name: name,
  157. Content: string(content),
  158. FilePath: schemaFile.SchemaFile,
  159. SoPath: schemaFile.SoFile,
  160. }, nil
  161. } else {
  162. return &Info{
  163. Type: schemaType,
  164. Name: name,
  165. SoPath: schemaFile.SoFile,
  166. }, nil
  167. }
  168. }
  169. func GetSchemaFile(schemaType def.SchemaType, name string) (*Files, error) {
  170. registry.RLock()
  171. defer registry.RUnlock()
  172. if _, ok := registry.schemas[schemaType]; !ok {
  173. return nil, fmt.Errorf("schema type %s not found", schemaType)
  174. }
  175. if _, ok := registry.schemas[schemaType][name]; !ok {
  176. return nil, fmt.Errorf("schema type %s, file %s not found", schemaType, name)
  177. }
  178. schemaFile := registry.schemas[schemaType][name]
  179. return schemaFile, nil
  180. }
  181. func DeleteSchema(schemaType def.SchemaType, name string) error {
  182. registry.Lock()
  183. defer registry.Unlock()
  184. if _, ok := registry.schemas[schemaType]; !ok {
  185. return fmt.Errorf("schema type %s not found", schemaType)
  186. }
  187. if _, ok := registry.schemas[schemaType][name]; !ok {
  188. return fmt.Errorf("schema %s.%s not found", schemaType, name)
  189. }
  190. schemaFile := registry.schemas[schemaType][name]
  191. if schemaFile.SchemaFile != "" {
  192. err := os.Remove(schemaFile.SchemaFile)
  193. if err != nil {
  194. conf.Log.Errorf("cannot delete schema file %s: %s", schemaFile.SchemaFile, err)
  195. }
  196. }
  197. if schemaFile.SoFile != "" {
  198. err := os.Remove(schemaFile.SoFile)
  199. if err != nil {
  200. conf.Log.Errorf("cannot delete schema so file %s: %s", schemaFile.SoFile, err)
  201. }
  202. }
  203. delete(registry.schemas[schemaType], name)
  204. return nil
  205. }