registry.go 5.1 KB

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