migration.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2021 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 util
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/pkg/kv"
  18. "github.com/patrickmn/go-cache"
  19. "io/ioutil"
  20. "os"
  21. "path"
  22. )
  23. func migration(dir string) error {
  24. fpath := path.Join(dir, "stores.data")
  25. c := cache.New(cache.NoExpiration, 0)
  26. if err := gocacheOpen(c, fpath); nil != err {
  27. return err
  28. }
  29. defer gocacheClose(c, fpath)
  30. keys, err := gocacheKeys(c)
  31. if nil != err {
  32. return err
  33. }
  34. store := kv.GetDefaultKVStore(dir)
  35. if err := store.Open(); nil != err {
  36. return err
  37. }
  38. defer store.Close()
  39. for _, k := range keys {
  40. if value, ok := c.Get(k); !ok {
  41. return fmt.Errorf("not found %s from %s", k, fpath)
  42. } else {
  43. if err := store.Setnx(k, value); nil != err {
  44. return err
  45. }
  46. if err := gocacheDel(c, k); nil != err {
  47. return err
  48. }
  49. }
  50. }
  51. return os.Remove(fpath)
  52. }
  53. func DataMigration(dir string) error {
  54. var dirs []string
  55. dirs = append(dirs, dir)
  56. for i := 0; i < len(dirs); i++ {
  57. files, err := ioutil.ReadDir(dirs[i])
  58. if nil != err {
  59. return err
  60. }
  61. for _, file := range files {
  62. fname := file.Name()
  63. if file.IsDir() {
  64. dirs = append(dirs, path.Join(dirs[i], fname))
  65. } else if "stores.data" == fname {
  66. return migration(dirs[i])
  67. }
  68. }
  69. }
  70. return nil
  71. }
  72. func gocacheClose(c *cache.Cache, path string) error {
  73. if e := c.SaveFile(path); e != nil {
  74. return e
  75. }
  76. c.Flush()
  77. return nil
  78. }
  79. func gocacheOpen(c *cache.Cache, path string) error {
  80. if _, err := os.Stat(path); os.IsNotExist(err) {
  81. return nil
  82. }
  83. if e := c.LoadFile(path); e != nil {
  84. return e
  85. }
  86. return nil
  87. }
  88. func gocacheKeys(c *cache.Cache) (keys []string, err error) {
  89. if c == nil {
  90. return nil, fmt.Errorf("cache has not been initialized yet.")
  91. }
  92. its := c.Items()
  93. keys = make([]string, 0, len(its))
  94. for k := range its {
  95. keys = append(keys, k)
  96. }
  97. return keys, nil
  98. }
  99. func gocacheSet(c *cache.Cache, path, key string, value interface{}) error {
  100. if c == nil {
  101. return fmt.Errorf("cache has not been initialized yet.")
  102. }
  103. return c.Add(key, value, cache.NoExpiration)
  104. }
  105. func gocacheDel(c *cache.Cache, key string) error {
  106. if c == nil {
  107. return fmt.Errorf("cache has not been initialized yet.")
  108. }
  109. c.Delete(key)
  110. return nil
  111. }