diff --git a/.gitignore b/.gitignore index 2471c023..8c992b8c 100644 --- a/.gitignore +++ b/.gitignore @@ -202,4 +202,4 @@ pkg/**/*.html web/.yarn/ docs/.yarn/ coverage.txt -locker/ +/locker/ diff --git a/cmd/distribution.go b/cmd/distribution.go index bfd6d402..3116f630 100644 --- a/cmd/distribution.go +++ b/cmd/distribution.go @@ -15,6 +15,8 @@ package cmd import ( + "context" + "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -22,6 +24,7 @@ import ( "github.com/go-sigma/sigma/pkg/cmds/distribution" "github.com/go-sigma/sigma/pkg/configs" "github.com/go-sigma/sigma/pkg/dal" + "github.com/go-sigma/sigma/pkg/dal/badger" "github.com/go-sigma/sigma/pkg/inits" "github.com/go-sigma/sigma/pkg/logger" "github.com/go-sigma/sigma/pkg/modules/locker" @@ -46,6 +49,12 @@ var distributionCmd = &cobra.Command{ config := ptr.To(configs.GetConfiguration()) + err = badger.Initialize(context.Background(), config) + if err != nil { + log.Error().Err(err).Msg("Initialize badger with error") + return + } + err = locker.Initialize(config) if err != nil { log.Error().Err(err).Msg("Initialize locker with error") diff --git a/cmd/server.go b/cmd/server.go index 566a2e64..c5648e77 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -15,6 +15,8 @@ package cmd import ( + "context" + "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -22,6 +24,7 @@ import ( "github.com/go-sigma/sigma/pkg/cmds/server" "github.com/go-sigma/sigma/pkg/configs" "github.com/go-sigma/sigma/pkg/dal" + "github.com/go-sigma/sigma/pkg/dal/badger" "github.com/go-sigma/sigma/pkg/inits" "github.com/go-sigma/sigma/pkg/logger" "github.com/go-sigma/sigma/pkg/modules/locker" @@ -45,6 +48,12 @@ var serverCmd = &cobra.Command{ config := ptr.To(configs.GetConfiguration()) + err = badger.Initialize(context.Background(), config) + if err != nil { + log.Error().Err(err).Msg("Initialize badger with error") + return + } + err = locker.Initialize(config) if err != nil { log.Error().Err(err).Msg("Initialize locker with error") diff --git a/cmd/worker.go b/cmd/worker.go index 67f47f0e..75ae6817 100644 --- a/cmd/worker.go +++ b/cmd/worker.go @@ -15,6 +15,8 @@ package cmd import ( + "context" + "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -22,6 +24,7 @@ import ( "github.com/go-sigma/sigma/pkg/cmds/worker" "github.com/go-sigma/sigma/pkg/configs" "github.com/go-sigma/sigma/pkg/dal" + "github.com/go-sigma/sigma/pkg/dal/badger" "github.com/go-sigma/sigma/pkg/inits" "github.com/go-sigma/sigma/pkg/logger" "github.com/go-sigma/sigma/pkg/modules/locker" @@ -45,6 +48,12 @@ var workerCmd = &cobra.Command{ config := ptr.To(configs.GetConfiguration()) + err = badger.Initialize(context.Background(), config) + if err != nil { + log.Error().Err(err).Msg("Initialize badger with error") + return + } + err = locker.Initialize(config) if err != nil { log.Error().Err(err).Msg("Initialize locker with error") diff --git a/conf/config.yaml b/conf/config.yaml index 61d7a1c6..abadd2b0 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -24,23 +24,30 @@ database: sslmode: disable redis: - # redis type available: none, external + # redis type available: none, external. Following all of redis config just use reference here. # none: means never use redis # external: means use the specific redis instance type: none url: redis://:sigma@localhost:6379/0 +badger: + # badger is used to implement lock and cache in a single-node mode. + enabled: true + path: /var/lib/sigma/badger/ + cache: - # the cache type available is: redis, inmemory, database - type: database - ttl: 72h - # please attention in multi + # the cache type available is: redis, inmemory, badger + # please attention in multi-node mode, you should use redis + type: badger inmemory: + prefix: sigma-cache size: 10240 redis: - database: - size: 10240 - threshold: 0.2 + prefix: sigma-cache + ttl: 72h + badger: + prefix: sigma-cache + ttl: 72h workqueue: # the workqueue type available: redis, kafka, database, inmemory @@ -53,10 +60,12 @@ workqueue: concurrency: 1024 locker: - # the locker type available: redis, database - type: database - database: {} - redis: {} + # the locker type available: redis, badger + type: badger + badger: + prefix: sigma-locker + redis: + prefix: sigma-locker namespace: # push image to registry, if namespace not exist, it will be created automatically diff --git a/go.mod b/go.mod index 4e75c956..24fb186e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/go-sigma/sigma -go 1.22.0 +go 1.22.2 require ( code.gitea.io/sdk/gitea v0.18.0 @@ -11,7 +11,7 @@ require ( github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible github.com/anchore/syft v1.3.0 github.com/aquasecurity/trivy v0.50.4 - github.com/aws/aws-sdk-go v1.51.32 + github.com/aws/aws-sdk-go v1.52.1 github.com/caarlos0/env/v9 v9.0.0 github.com/casbin/casbin/v2 v2.88.0 github.com/casbin/gorm-adapter/v3 v3.24.0 diff --git a/go.sum b/go.sum index 764b5267..00ff16da 100644 --- a/go.sum +++ b/go.sum @@ -223,8 +223,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so= github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw= -github.com/aws/aws-sdk-go v1.51.32 h1:A6mPui7QP4mwmovyzgtdedbRbNur1Iu0/El7hBWNHms= -github.com/aws/aws-sdk-go v1.51.32/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= +github.com/aws/aws-sdk-go v1.52.1 h1:pYpPIuvVsawYDR0Nt3VrceizUAbtpTN3Z7xBzcZWwfI= +github.com/aws/aws-sdk-go v1.52.1/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= github.com/aws/aws-sdk-go-v2 v1.26.0 h1:/Ce4OCiM3EkpW7Y+xUnfAFpchU78K7/Ug01sZni9PgA= github.com/aws/aws-sdk-go-v2 v1.26.0/go.mod h1:35hUlJVYd+M++iLI3ALmVwMOyRYMmRqUXpTtRGW+K9I= github.com/aws/aws-sdk-go-v2/config v1.27.9 h1:gRx/NwpNEFSk+yQlgmk1bmxxvQ5TyJ76CWXs9XScTqg= diff --git a/pkg/configs/configuration.go b/pkg/configs/configuration.go index 1c7c54de..62bb57ad 100644 --- a/pkg/configs/configuration.go +++ b/pkg/configs/configuration.go @@ -38,6 +38,7 @@ type Configuration struct { Database ConfigurationDatabase `yaml:"database"` Deploy enums.Deploy `yaml:"deploy"` Redis ConfigurationRedis `yaml:"redis"` + Badger ConfigurationBadger `yaml:"badger"` Cache ConfigurationCache `yaml:"cache"` WorkQueue ConfigurationWorkQueue `yaml:"workqueue"` Locker ConfigurationLocker `yaml:"locker"` @@ -101,28 +102,36 @@ type ConfigurationRedis struct { Url string `yaml:"url"` } +// ConfigurationBadger ... +type ConfigurationBadger struct { + Enabled bool `yaml:"enabled"` + Path string `yaml:"path"` +} + // ConfigurationCacheRedis ... type ConfigurationCacheRedis struct { + Prefix string `yaml:"prefix"` + Ttl time.Duration `yaml:"ttl"` } -// ConfigurationCacheInmemory ... -type ConfigurationCacheInmemory struct { - Size int `yaml:"size"` +// ConfigurationCacheBadger ... +type ConfigurationCacheBadger struct { + Prefix string `yaml:"prefix"` + Ttl time.Duration `yaml:"ttl"` } -// ConfigurationCacheDatabase ... -type ConfigurationCacheDatabase struct { - Size int64 `yaml:"size"` - Threshold float64 `yaml:"threshold"` +// ConfigurationCacheInmemory ... +type ConfigurationCacheInmemory struct { + Prefix string `yaml:"prefix"` + Size int `yaml:"size"` } // ConfigurationCache ... type ConfigurationCache struct { Type enums.CacherType `yaml:"type"` - Ttl time.Duration `yaml:"ttl"` Redis ConfigurationCacheRedis `yaml:"redis"` Inmemory ConfigurationCacheInmemory `yaml:"inmemory"` - Database ConfigurationCacheDatabase `yaml:"database"` + Badger ConfigurationCacheBadger `yaml:"badger"` } type ConfigurationWorkQueueRedis struct { @@ -148,19 +157,21 @@ type ConfigurationWorkQueue struct { Inmemory ConfigurationWorkQueueInmemmory `yaml:"inmemory"` } -// ConfigurationLockerDatabase ... -type ConfigurationLockerDatabase struct { - Path string `yaml:"path"` +// ConfigurationLockerBadger ... +type ConfigurationLockerBadger struct { + Prefix string `yaml:"prefix"` } // ConfigurationLockerRedis ... -type ConfigurationLockerRedis struct{} +type ConfigurationLockerRedis struct { + Prefix string `yaml:"prefix"` +} // ConfigurationLocker ... type ConfigurationLocker struct { - Type enums.LockerType `yaml:"type"` - Database ConfigurationLockerDatabase `yaml:"database"` - Redis ConfigurationLockerRedis `yaml:"redis"` + Type enums.LockerType `yaml:"type"` + Badger ConfigurationLockerBadger `yaml:"badger"` + Redis ConfigurationLockerRedis `yaml:"redis"` } // ConfigurationNamespace ... diff --git a/pkg/configs/default.go b/pkg/configs/default.go index 03adb5e4..a01e4a88 100644 --- a/pkg/configs/default.go +++ b/pkg/configs/default.go @@ -15,6 +15,7 @@ package configs import ( + "strings" "time" "github.com/spf13/viper" @@ -45,9 +46,6 @@ func defaultSettings() { if configuration.Namespace.Visibility.String() == "" { configuration.Namespace.Visibility = enums.VisibilityPrivate } - if configuration.Cache.Ttl == 0 { - configuration.Cache.Ttl = 72 * time.Hour - } if configuration.Daemon.Builder.Kubernetes.Namespace == "" { configuration.Daemon.Builder.Kubernetes.Namespace = "default" } @@ -57,13 +55,37 @@ func defaultSettings() { if configuration.WorkQueue.Inmemory.Concurrency == 0 { configuration.WorkQueue.Inmemory.Concurrency = 1024 } - if configuration.Cache.Inmemory.Size == 0 { + + // for cache + if configuration.Cache.Type == enums.CacherTypeInmemory && configuration.Cache.Inmemory.Size == 0 { configuration.Cache.Inmemory.Size = 10240 } - if configuration.Cache.Ttl == 0 { - configuration.Cache.Ttl = time.Second * 30 + if configuration.Cache.Type == enums.CacherTypeInmemory && len(strings.TrimSpace(configuration.Cache.Inmemory.Prefix)) == 0 { + configuration.Cache.Inmemory.Prefix = "sigma-cache" + } + if configuration.Cache.Type == enums.CacherTypeRedis && configuration.Cache.Redis.Ttl == 0 { + configuration.Cache.Redis.Ttl = time.Hour * 72 + } + if configuration.Cache.Type == enums.CacherTypeRedis && len(strings.TrimSpace(configuration.Cache.Redis.Prefix)) == 0 { + configuration.Cache.Redis.Prefix = "sigma-cache" + } + if configuration.Cache.Type == enums.CacherTypeBadger && configuration.Cache.Badger.Ttl == 0 { + configuration.Cache.Badger.Ttl = time.Hour * 72 + } + if configuration.Cache.Type == enums.CacherTypeBadger && len(strings.TrimSpace(configuration.Cache.Badger.Prefix)) == 0 { + configuration.Cache.Badger.Prefix = "sigma-cache" + } + + // for badger + if configuration.Badger.Enabled && len(strings.TrimSpace(configuration.Badger.Path)) == 0 { + configuration.Badger.Path = "/var/lib/sigma/badger/" + } + + // for locker + if configuration.Locker.Type == enums.LockerTypeBadger && strings.TrimSpace(configuration.Locker.Badger.Prefix) == "" { + configuration.Locker.Badger.Prefix = "sigma-locker" } - if configuration.Locker.Type == enums.LockerTypeDatabase && configuration.Locker.Database.Path == "" { - configuration.Locker.Database.Path = "/var/lib/sigma/badger" + if configuration.Locker.Type == enums.LockerTypeRedis && strings.TrimSpace(configuration.Locker.Redis.Prefix) == "" { + configuration.Locker.Redis.Prefix = "sigma-locker" } } diff --git a/pkg/dal/auth_test.go b/pkg/dal/auth_test.go index b077485c..1c201c96 100644 --- a/pkg/dal/auth_test.go +++ b/pkg/dal/auth_test.go @@ -28,6 +28,7 @@ import ( "github.com/go-sigma/sigma/pkg/configs" "github.com/go-sigma/sigma/pkg/dal" + "github.com/go-sigma/sigma/pkg/dal/badger" "github.com/go-sigma/sigma/pkg/dal/dao" "github.com/go-sigma/sigma/pkg/dal/models" "github.com/go-sigma/sigma/pkg/logger" @@ -38,6 +39,8 @@ import ( func TestAuth(t *testing.T) { logger.SetLevel("debug") + assert.NoError(t, badger.Initialize(context.Background(), configs.Configuration{})) + err := locker.Initialize(configs.Configuration{}) assert.NoError(t, err) diff --git a/pkg/dal/badger/badger.go b/pkg/dal/badger/badger.go new file mode 100644 index 00000000..5b1ab32f --- /dev/null +++ b/pkg/dal/badger/badger.go @@ -0,0 +1,68 @@ +// Copyright 2024 sigma +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package badger + +import ( + "context" + "fmt" + "os" + "strings" + + "github.com/dgraph-io/badger/v4" + "github.com/rs/zerolog/log" + + "github.com/go-sigma/sigma/pkg/configs" +) + +type logger struct{} + +// Errorf is the error log +func (l logger) Errorf(msg string, opts ...interface{}) { + log.Error().Msg(strings.TrimSpace(fmt.Sprintf(msg, opts...))) +} + +// Warningf is the warning log +func (l logger) Warningf(msg string, opts ...interface{}) { + log.Warn().Msg(strings.TrimSpace(fmt.Sprintf(msg, opts...))) +} + +// Infof is the info log +func (l logger) Infof(msg string, opts ...interface{}) { + log.Info().Msg(strings.TrimSpace(fmt.Sprintf(msg, opts...))) +} + +// Debugf is the debug log +func (l logger) Debugf(msg string, opts ...interface{}) { + log.Debug().Msg(strings.TrimSpace(fmt.Sprintf(msg, opts...))) +} + +var Client *badger.DB + +// Initialize init redis +func Initialize(ctx context.Context, config configs.Configuration) error { + var err error + dir := config.Badger.Path + if dir == "" { + dir, err = os.MkdirTemp("", "locker") + if err != nil { + panic("make temp dir for badger failed") + } + } + Client, err = badger.Open(badger.DefaultOptions(dir).WithLogger(&logger{})) + if err != nil { + return err + } + return nil +} diff --git a/pkg/dal/cmd/gen.go b/pkg/dal/cmd/gen.go index d4c0d201..dfc8e432 100644 --- a/pkg/dal/cmd/gen.go +++ b/pkg/dal/cmd/gen.go @@ -51,7 +51,6 @@ func main() { models.BuilderRunner{}, models.WorkQueue{}, models.Locker{}, - models.Cache{}, models.Setting{}, models.DaemonGcTagRule{}, models.DaemonGcTagRunner{}, @@ -68,7 +67,6 @@ func main() { models.NamespaceMember{}, ) - g.ApplyInterface(func(models.CacheQuery) {}, models.Cache{}) g.ApplyInterface(func(models.ArtifactSizeByNamespaceOrRepository) {}, models.Artifact{}) g.ApplyInterface(func(models.ArtifactAssociated) {}, models.Artifact{}) g.ApplyInterface(func(models.BlobAssociateWithArtifact) {}, models.Blob{}) diff --git a/pkg/dal/dao/cache.go b/pkg/dal/dao/cache.go deleted file mode 100644 index e5bde45e..00000000 --- a/pkg/dal/dao/cache.go +++ /dev/null @@ -1,105 +0,0 @@ -// Copyright 2023 sigma -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package dao - -import ( - "context" - - "gorm.io/gorm" - - "github.com/go-sigma/sigma/pkg/dal/models" - "github.com/go-sigma/sigma/pkg/dal/query" -) - -//go:generate mockgen -destination=mocks/cache.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao CacheService -//go:generate mockgen -destination=mocks/cache_factory.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao CacheServiceFactory - -// CacheService is the interface that provides methods to operate on cache model -type CacheService interface { - // Save save a new cache record in the database - Save(ctx context.Context, key string, val []byte, size int64, threshold float64) error - // Delete get a cache record - Delete(ctx context.Context, key string) error - // Get get a cache record - Get(ctx context.Context, key string) (*models.Cache, error) -} - -type cacheService struct { - tx *query.Query -} - -// CacheServiceFactory is the interface that provides the cache service factory methods. -type CacheServiceFactory interface { - New(txs ...*query.Query) CacheService -} - -type cacheServiceFactory struct{} - -// NewCacheServiceFactory creates a new cache service factory. -func NewCacheServiceFactory() CacheServiceFactory { - return &cacheServiceFactory{} -} - -func (s *cacheServiceFactory) New(txs ...*query.Query) CacheService { - tx := query.Q - if len(txs) > 0 { - tx = txs[0] - } - return &cacheService{ - tx: tx, - } -} - -// Create creates a new cache record in the database -func (s cacheService) Save(ctx context.Context, key string, val []byte, size int64, threshold float64) error { - total, err := s.tx.Cache.WithContext(ctx).Count() - if err != nil { - return err - } - err = query.Q.Transaction(func(tx *query.Query) error { - if total > int64((float64(total) * (1 + threshold))) { - err = tx.Cache.WithContext(ctx).DeleteOutsideThreshold(size, threshold) - if err != nil { - return err - } - } - err = tx.Cache.WithContext(ctx).Save(&models.Cache{Key: key, Val: val}) - if err != nil { - return err - } - return nil - }) - if err != nil { - return err - } - return nil -} - -// Delete get a cache record -func (s cacheService) Delete(ctx context.Context, key string) error { - matched, err := s.tx.Cache.WithContext(ctx).Unscoped().Where(s.tx.Cache.Key.Eq(key)).Delete() - if err != nil { - return err - } - if matched.RowsAffected == 0 { - return gorm.ErrRecordNotFound - } - return nil -} - -// Get get a cache record -func (s cacheService) Get(ctx context.Context, key string) (*models.Cache, error) { - return s.tx.Cache.WithContext(ctx).Where(s.tx.Cache.Key.Eq(key)).First() -} diff --git a/pkg/dal/dao/cache_test.go b/pkg/dal/dao/cache_test.go deleted file mode 100644 index ee651b8b..00000000 --- a/pkg/dal/dao/cache_test.go +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2024 sigma -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package dao_test - -import ( - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/go-sigma/sigma/pkg/dal/dao" - "github.com/go-sigma/sigma/pkg/dal/query" -) - -func TestCacheServiceFactory(t *testing.T) { - f := dao.NewCacheServiceFactory() - assert.NotNil(t, f.New()) - assert.NotNil(t, f.New(query.Q)) -} diff --git a/pkg/dal/dao/locker.go b/pkg/dal/dao/locker.go deleted file mode 100644 index f314b464..00000000 --- a/pkg/dal/dao/locker.go +++ /dev/null @@ -1,118 +0,0 @@ -// Copyright 2023 sigma -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package dao - -import ( - "context" - "errors" - "fmt" - "time" - - "gorm.io/gorm" - - "github.com/go-sigma/sigma/pkg/dal/models" - "github.com/go-sigma/sigma/pkg/dal/query" - "github.com/go-sigma/sigma/pkg/modules/locker/definition" -) - -//go:generate mockgen -destination=mocks/locker.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao LockerService -//go:generate mockgen -destination=mocks/locker_factory.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao LockerServiceFactory - -// LockerService is the interface that provides methods to operate on locker model -type LockerService interface { - // Create creates a new work queue record in the database - Create(ctx context.Context, key, val string, expire int64) error - // Delete get a locker record - Delete(ctx context.Context, key, val string) error - // Renew renew a locker record - Renew(ctx context.Context, key, val string, expire int64) error -} - -type lockerService struct { - tx *query.Query -} - -// LockerServiceFactory is the interface that provides the locker service factory methods. -type LockerServiceFactory interface { - New(txs ...*query.Query) LockerService -} - -type lockerServiceFactory struct{} - -// NewLockerServiceFactory creates a new locker service factory. -func NewLockerServiceFactory() LockerServiceFactory { - return &lockerServiceFactory{} -} - -func (s *lockerServiceFactory) New(txs ...*query.Query) LockerService { - tx := query.Q - if len(txs) > 0 { - tx = txs[0] - } - return &lockerService{ - tx: tx, - } -} - -// Create creates a new work queue record in the database -func (s lockerService) Create(ctx context.Context, key, value string, expire int64) error { - lock, err := s.tx.Locker.WithContext(ctx).Where(s.tx.Locker.Key.Eq(key)).First() - if err == nil { - if lock.Expire < time.Now().UnixMilli() { - _, err = s.tx.Locker.WithContext(ctx).Where(s.tx.Locker.Key.Eq(key)).Delete() - if err != nil { - return err - } - } else { - return fmt.Errorf("Locker %s already exists", key) - } - } - if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { - return err - } - return s.tx.Locker.WithContext(ctx).Create(&models.Locker{Key: key, Value: value, Expire: expire}) -} - -// Delete get a locker record -func (s lockerService) Delete(ctx context.Context, key, value string) error { - _, err := s.tx.Locker.WithContext(ctx).Unscoped().Where( - s.tx.Locker.Key.Eq(key), s.tx.Locker.Value.Eq(value)).Delete() - return err -} - -// Renew renew a locker record -func (s lockerService) Renew(ctx context.Context, key, value string, expire int64) error { - lock, err := s.tx.Locker.WithContext(ctx).Where(s.tx.Locker.Key.Eq(key)).First() - if err == nil { - if lock.Value != value { - return definition.ErrLockNotHeld - } - if lock.Expire < time.Now().UnixMilli() { - _, err = s.tx.Locker.WithContext(ctx).Where(s.tx.Locker.Key.Eq(key)).Delete() - if err != nil { - return err - } - return definition.ErrLockAlreadyExpired - } else { - _, err := s.tx.Locker.WithContext(ctx).Where(s.tx.Locker.Key.Eq(key)).UpdateColumns(map[string]any{ - query.Locker.Expire.ColumnName().String(): expire, - }) - if err != nil { - return err - } - } - } - return err -} diff --git a/pkg/dal/dao/locker_test.go b/pkg/dal/dao/locker_test.go deleted file mode 100644 index 89527741..00000000 --- a/pkg/dal/dao/locker_test.go +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2024 sigma -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package dao_test - -import ( - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/go-sigma/sigma/pkg/dal/dao" - "github.com/go-sigma/sigma/pkg/dal/query" -) - -func TestLockerServiceFactory(t *testing.T) { - f := dao.NewLockerServiceFactory() - assert.NotNil(t, f.New()) - assert.NotNil(t, f.New(query.Q)) -} diff --git a/pkg/dal/dao/mocks/cache.go b/pkg/dal/dao/mocks/cache.go deleted file mode 100644 index 7eacbdcc..00000000 --- a/pkg/dal/dao/mocks/cache.go +++ /dev/null @@ -1,84 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: github.com/go-sigma/sigma/pkg/dal/dao (interfaces: CacheService) -// -// Generated by this command: -// -// mockgen -destination=mocks/cache.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao CacheService -// - -// Package mocks is a generated GoMock package. -package mocks - -import ( - context "context" - reflect "reflect" - - models "github.com/go-sigma/sigma/pkg/dal/models" - gomock "go.uber.org/mock/gomock" -) - -// MockCacheService is a mock of CacheService interface. -type MockCacheService struct { - ctrl *gomock.Controller - recorder *MockCacheServiceMockRecorder -} - -// MockCacheServiceMockRecorder is the mock recorder for MockCacheService. -type MockCacheServiceMockRecorder struct { - mock *MockCacheService -} - -// NewMockCacheService creates a new mock instance. -func NewMockCacheService(ctrl *gomock.Controller) *MockCacheService { - mock := &MockCacheService{ctrl: ctrl} - mock.recorder = &MockCacheServiceMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockCacheService) EXPECT() *MockCacheServiceMockRecorder { - return m.recorder -} - -// Delete mocks base method. -func (m *MockCacheService) Delete(arg0 context.Context, arg1 string) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Delete", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// Delete indicates an expected call of Delete. -func (mr *MockCacheServiceMockRecorder) Delete(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockCacheService)(nil).Delete), arg0, arg1) -} - -// Get mocks base method. -func (m *MockCacheService) Get(arg0 context.Context, arg1 string) (*models.Cache, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Get", arg0, arg1) - ret0, _ := ret[0].(*models.Cache) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// Get indicates an expected call of Get. -func (mr *MockCacheServiceMockRecorder) Get(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockCacheService)(nil).Get), arg0, arg1) -} - -// Save mocks base method. -func (m *MockCacheService) Save(arg0 context.Context, arg1 string, arg2 []byte, arg3 int64, arg4 float64) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Save", arg0, arg1, arg2, arg3, arg4) - ret0, _ := ret[0].(error) - return ret0 -} - -// Save indicates an expected call of Save. -func (mr *MockCacheServiceMockRecorder) Save(arg0, arg1, arg2, arg3, arg4 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Save", reflect.TypeOf((*MockCacheService)(nil).Save), arg0, arg1, arg2, arg3, arg4) -} diff --git a/pkg/dal/dao/mocks/cache_factory.go b/pkg/dal/dao/mocks/cache_factory.go deleted file mode 100644 index 0a93bb6f..00000000 --- a/pkg/dal/dao/mocks/cache_factory.go +++ /dev/null @@ -1,59 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: github.com/go-sigma/sigma/pkg/dal/dao (interfaces: CacheServiceFactory) -// -// Generated by this command: -// -// mockgen -destination=mocks/cache_factory.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao CacheServiceFactory -// - -// Package mocks is a generated GoMock package. -package mocks - -import ( - reflect "reflect" - - dao "github.com/go-sigma/sigma/pkg/dal/dao" - query "github.com/go-sigma/sigma/pkg/dal/query" - gomock "go.uber.org/mock/gomock" -) - -// MockCacheServiceFactory is a mock of CacheServiceFactory interface. -type MockCacheServiceFactory struct { - ctrl *gomock.Controller - recorder *MockCacheServiceFactoryMockRecorder -} - -// MockCacheServiceFactoryMockRecorder is the mock recorder for MockCacheServiceFactory. -type MockCacheServiceFactoryMockRecorder struct { - mock *MockCacheServiceFactory -} - -// NewMockCacheServiceFactory creates a new mock instance. -func NewMockCacheServiceFactory(ctrl *gomock.Controller) *MockCacheServiceFactory { - mock := &MockCacheServiceFactory{ctrl: ctrl} - mock.recorder = &MockCacheServiceFactoryMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockCacheServiceFactory) EXPECT() *MockCacheServiceFactoryMockRecorder { - return m.recorder -} - -// New mocks base method. -func (m *MockCacheServiceFactory) New(arg0 ...*query.Query) dao.CacheService { - m.ctrl.T.Helper() - varargs := []any{} - for _, a := range arg0 { - varargs = append(varargs, a) - } - ret := m.ctrl.Call(m, "New", varargs...) - ret0, _ := ret[0].(dao.CacheService) - return ret0 -} - -// New indicates an expected call of New. -func (mr *MockCacheServiceFactoryMockRecorder) New(arg0 ...any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "New", reflect.TypeOf((*MockCacheServiceFactory)(nil).New), arg0...) -} diff --git a/pkg/dal/dao/mocks/locker.go b/pkg/dal/dao/mocks/locker.go deleted file mode 100644 index 49119b64..00000000 --- a/pkg/dal/dao/mocks/locker.go +++ /dev/null @@ -1,82 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: github.com/go-sigma/sigma/pkg/dal/dao (interfaces: LockerService) -// -// Generated by this command: -// -// mockgen -destination=mocks/locker.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao LockerService -// - -// Package mocks is a generated GoMock package. -package mocks - -import ( - context "context" - reflect "reflect" - - gomock "go.uber.org/mock/gomock" -) - -// MockLockerService is a mock of LockerService interface. -type MockLockerService struct { - ctrl *gomock.Controller - recorder *MockLockerServiceMockRecorder -} - -// MockLockerServiceMockRecorder is the mock recorder for MockLockerService. -type MockLockerServiceMockRecorder struct { - mock *MockLockerService -} - -// NewMockLockerService creates a new mock instance. -func NewMockLockerService(ctrl *gomock.Controller) *MockLockerService { - mock := &MockLockerService{ctrl: ctrl} - mock.recorder = &MockLockerServiceMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockLockerService) EXPECT() *MockLockerServiceMockRecorder { - return m.recorder -} - -// Create mocks base method. -func (m *MockLockerService) Create(arg0 context.Context, arg1 string, arg2 int64) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Create", arg0, arg1, arg2) - ret0, _ := ret[0].(error) - return ret0 -} - -// Create indicates an expected call of Create. -func (mr *MockLockerServiceMockRecorder) Create(arg0, arg1, arg2 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Create", reflect.TypeOf((*MockLockerService)(nil).Create), arg0, arg1, arg2) -} - -// Delete mocks base method. -func (m *MockLockerService) Delete(arg0 context.Context, arg1 string) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Delete", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// Delete indicates an expected call of Delete. -func (mr *MockLockerServiceMockRecorder) Delete(arg0, arg1 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockLockerService)(nil).Delete), arg0, arg1) -} - -// Renew mocks base method. -func (m *MockLockerService) Renew(arg0 context.Context, arg1 string, arg2 int64) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Renew", arg0, arg1, arg2) - ret0, _ := ret[0].(error) - return ret0 -} - -// Renew indicates an expected call of Renew. -func (mr *MockLockerServiceMockRecorder) Renew(arg0, arg1, arg2 any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Renew", reflect.TypeOf((*MockLockerService)(nil).Renew), arg0, arg1, arg2) -} diff --git a/pkg/dal/dao/mocks/locker_factory.go b/pkg/dal/dao/mocks/locker_factory.go deleted file mode 100644 index a484e9d7..00000000 --- a/pkg/dal/dao/mocks/locker_factory.go +++ /dev/null @@ -1,59 +0,0 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: github.com/go-sigma/sigma/pkg/dal/dao (interfaces: LockerServiceFactory) -// -// Generated by this command: -// -// mockgen -destination=mocks/locker_factory.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao LockerServiceFactory -// - -// Package mocks is a generated GoMock package. -package mocks - -import ( - reflect "reflect" - - dao "github.com/go-sigma/sigma/pkg/dal/dao" - query "github.com/go-sigma/sigma/pkg/dal/query" - gomock "go.uber.org/mock/gomock" -) - -// MockLockerServiceFactory is a mock of LockerServiceFactory interface. -type MockLockerServiceFactory struct { - ctrl *gomock.Controller - recorder *MockLockerServiceFactoryMockRecorder -} - -// MockLockerServiceFactoryMockRecorder is the mock recorder for MockLockerServiceFactory. -type MockLockerServiceFactoryMockRecorder struct { - mock *MockLockerServiceFactory -} - -// NewMockLockerServiceFactory creates a new mock instance. -func NewMockLockerServiceFactory(ctrl *gomock.Controller) *MockLockerServiceFactory { - mock := &MockLockerServiceFactory{ctrl: ctrl} - mock.recorder = &MockLockerServiceFactoryMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockLockerServiceFactory) EXPECT() *MockLockerServiceFactoryMockRecorder { - return m.recorder -} - -// New mocks base method. -func (m *MockLockerServiceFactory) New(arg0 ...*query.Query) dao.LockerService { - m.ctrl.T.Helper() - varargs := []any{} - for _, a := range arg0 { - varargs = append(varargs, a) - } - ret := m.ctrl.Call(m, "New", varargs...) - ret0, _ := ret[0].(dao.LockerService) - return ret0 -} - -// New indicates an expected call of New. -func (mr *MockLockerServiceFactoryMockRecorder) New(arg0 ...any) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "New", reflect.TypeOf((*MockLockerServiceFactory)(nil).New), arg0...) -} diff --git a/pkg/dal/dao/mocks/workq.go b/pkg/dal/dao/mocks/workq.go index 6a571163..d0cf5f24 100644 --- a/pkg/dal/dao/mocks/workq.go +++ b/pkg/dal/dao/mocks/workq.go @@ -56,7 +56,7 @@ func (mr *MockWorkQueueServiceMockRecorder) Create(arg0, arg1 any) *gomock.Call } // Get mocks base method. -func (m *MockWorkQueueService) Get(arg0 context.Context, arg1 string) (*models.WorkQueue, error) { +func (m *MockWorkQueueService) Get(arg0 context.Context, arg1 enums.Daemon) (*models.WorkQueue, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Get", arg0, arg1) ret0, _ := ret[0].(*models.WorkQueue) diff --git a/pkg/dal/models/cache.go b/pkg/dal/models/cache.go deleted file mode 100644 index bbf4d1bc..00000000 --- a/pkg/dal/models/cache.go +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2023 sigma -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package models - -import ( - "gorm.io/plugin/soft_delete" -) - -// Cache cache -type Cache struct { - CreatedAt int64 `gorm:"autoCreateTime:milli"` - UpdatedAt int64 `gorm:"autoUpdateTime:milli"` - DeletedAt soft_delete.DeletedAt `gorm:"softDelete:milli"` - ID int64 `gorm:"primaryKey"` - - Key string `gorm:"uniqueIndex,size:256"` - Val []byte -} - -// CacheQuery ... -type CacheQuery interface { - // DELETE FROM @@table WHERE id in ( - // SELECT id from @@table ORDER BY created_at ASC LIMIT ( - // (SELECT COUNT(id) FROM @@table) - (@size * (1 - @threshold)))) - DeleteOutsideThreshold(size int64, threshold float64) error -} diff --git a/pkg/dal/query/caches.gen.go b/pkg/dal/query/caches.gen.go deleted file mode 100644 index 2004b19b..00000000 --- a/pkg/dal/query/caches.gen.go +++ /dev/null @@ -1,366 +0,0 @@ -// Code generated by gorm.io/gen. DO NOT EDIT. -// Code generated by gorm.io/gen. DO NOT EDIT. -// Code generated by gorm.io/gen. DO NOT EDIT. - -package query - -import ( - "context" - "strings" - - "gorm.io/gorm" - "gorm.io/gorm/clause" - "gorm.io/gorm/schema" - - "gorm.io/gen" - "gorm.io/gen/field" - - "gorm.io/plugin/dbresolver" - - "github.com/go-sigma/sigma/pkg/dal/models" -) - -func newCache(db *gorm.DB, opts ...gen.DOOption) cache { - _cache := cache{} - - _cache.cacheDo.UseDB(db, opts...) - _cache.cacheDo.UseModel(&models.Cache{}) - - tableName := _cache.cacheDo.TableName() - _cache.ALL = field.NewAsterisk(tableName) - _cache.CreatedAt = field.NewInt64(tableName, "created_at") - _cache.UpdatedAt = field.NewInt64(tableName, "updated_at") - _cache.DeletedAt = field.NewUint64(tableName, "deleted_at") - _cache.ID = field.NewInt64(tableName, "id") - _cache.Key = field.NewString(tableName, "key") - _cache.Val = field.NewBytes(tableName, "val") - - _cache.fillFieldMap() - - return _cache -} - -type cache struct { - cacheDo cacheDo - - ALL field.Asterisk - CreatedAt field.Int64 - UpdatedAt field.Int64 - DeletedAt field.Uint64 - ID field.Int64 - Key field.String - Val field.Bytes - - fieldMap map[string]field.Expr -} - -func (c cache) Table(newTableName string) *cache { - c.cacheDo.UseTable(newTableName) - return c.updateTableName(newTableName) -} - -func (c cache) As(alias string) *cache { - c.cacheDo.DO = *(c.cacheDo.As(alias).(*gen.DO)) - return c.updateTableName(alias) -} - -func (c *cache) updateTableName(table string) *cache { - c.ALL = field.NewAsterisk(table) - c.CreatedAt = field.NewInt64(table, "created_at") - c.UpdatedAt = field.NewInt64(table, "updated_at") - c.DeletedAt = field.NewUint64(table, "deleted_at") - c.ID = field.NewInt64(table, "id") - c.Key = field.NewString(table, "key") - c.Val = field.NewBytes(table, "val") - - c.fillFieldMap() - - return c -} - -func (c *cache) WithContext(ctx context.Context) *cacheDo { return c.cacheDo.WithContext(ctx) } - -func (c cache) TableName() string { return c.cacheDo.TableName() } - -func (c cache) Alias() string { return c.cacheDo.Alias() } - -func (c cache) Columns(cols ...field.Expr) gen.Columns { return c.cacheDo.Columns(cols...) } - -func (c *cache) GetFieldByName(fieldName string) (field.OrderExpr, bool) { - _f, ok := c.fieldMap[fieldName] - if !ok || _f == nil { - return nil, false - } - _oe, ok := _f.(field.OrderExpr) - return _oe, ok -} - -func (c *cache) fillFieldMap() { - c.fieldMap = make(map[string]field.Expr, 6) - c.fieldMap["created_at"] = c.CreatedAt - c.fieldMap["updated_at"] = c.UpdatedAt - c.fieldMap["deleted_at"] = c.DeletedAt - c.fieldMap["id"] = c.ID - c.fieldMap["key"] = c.Key - c.fieldMap["val"] = c.Val -} - -func (c cache) clone(db *gorm.DB) cache { - c.cacheDo.ReplaceConnPool(db.Statement.ConnPool) - return c -} - -func (c cache) replaceDB(db *gorm.DB) cache { - c.cacheDo.ReplaceDB(db) - return c -} - -type cacheDo struct{ gen.DO } - -// DELETE FROM @@table WHERE id in ( -// SELECT id from @@table ORDER BY created_at ASC LIMIT ( -// (SELECT COUNT(id) FROM @@table) - (@size * (1 - @threshold)))) -func (c cacheDo) DeleteOutsideThreshold(size int64, threshold float64) (err error) { - var params []interface{} - - var generateSQL strings.Builder - params = append(params, size) - params = append(params, threshold) - generateSQL.WriteString("DELETE FROM caches WHERE id in ( SELECT id from caches ORDER BY created_at ASC LIMIT ( (SELECT COUNT(id) FROM caches) - (? * (1 - ?)))) ") - - var executeSQL *gorm.DB - executeSQL = c.UnderlyingDB().Exec(generateSQL.String(), params...) // ignore_security_alert - err = executeSQL.Error - - return -} - -func (c cacheDo) Debug() *cacheDo { - return c.withDO(c.DO.Debug()) -} - -func (c cacheDo) WithContext(ctx context.Context) *cacheDo { - return c.withDO(c.DO.WithContext(ctx)) -} - -func (c cacheDo) ReadDB() *cacheDo { - return c.Clauses(dbresolver.Read) -} - -func (c cacheDo) WriteDB() *cacheDo { - return c.Clauses(dbresolver.Write) -} - -func (c cacheDo) Session(config *gorm.Session) *cacheDo { - return c.withDO(c.DO.Session(config)) -} - -func (c cacheDo) Clauses(conds ...clause.Expression) *cacheDo { - return c.withDO(c.DO.Clauses(conds...)) -} - -func (c cacheDo) Returning(value interface{}, columns ...string) *cacheDo { - return c.withDO(c.DO.Returning(value, columns...)) -} - -func (c cacheDo) Not(conds ...gen.Condition) *cacheDo { - return c.withDO(c.DO.Not(conds...)) -} - -func (c cacheDo) Or(conds ...gen.Condition) *cacheDo { - return c.withDO(c.DO.Or(conds...)) -} - -func (c cacheDo) Select(conds ...field.Expr) *cacheDo { - return c.withDO(c.DO.Select(conds...)) -} - -func (c cacheDo) Where(conds ...gen.Condition) *cacheDo { - return c.withDO(c.DO.Where(conds...)) -} - -func (c cacheDo) Order(conds ...field.Expr) *cacheDo { - return c.withDO(c.DO.Order(conds...)) -} - -func (c cacheDo) Distinct(cols ...field.Expr) *cacheDo { - return c.withDO(c.DO.Distinct(cols...)) -} - -func (c cacheDo) Omit(cols ...field.Expr) *cacheDo { - return c.withDO(c.DO.Omit(cols...)) -} - -func (c cacheDo) Join(table schema.Tabler, on ...field.Expr) *cacheDo { - return c.withDO(c.DO.Join(table, on...)) -} - -func (c cacheDo) LeftJoin(table schema.Tabler, on ...field.Expr) *cacheDo { - return c.withDO(c.DO.LeftJoin(table, on...)) -} - -func (c cacheDo) RightJoin(table schema.Tabler, on ...field.Expr) *cacheDo { - return c.withDO(c.DO.RightJoin(table, on...)) -} - -func (c cacheDo) Group(cols ...field.Expr) *cacheDo { - return c.withDO(c.DO.Group(cols...)) -} - -func (c cacheDo) Having(conds ...gen.Condition) *cacheDo { - return c.withDO(c.DO.Having(conds...)) -} - -func (c cacheDo) Limit(limit int) *cacheDo { - return c.withDO(c.DO.Limit(limit)) -} - -func (c cacheDo) Offset(offset int) *cacheDo { - return c.withDO(c.DO.Offset(offset)) -} - -func (c cacheDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *cacheDo { - return c.withDO(c.DO.Scopes(funcs...)) -} - -func (c cacheDo) Unscoped() *cacheDo { - return c.withDO(c.DO.Unscoped()) -} - -func (c cacheDo) Create(values ...*models.Cache) error { - if len(values) == 0 { - return nil - } - return c.DO.Create(values) -} - -func (c cacheDo) CreateInBatches(values []*models.Cache, batchSize int) error { - return c.DO.CreateInBatches(values, batchSize) -} - -// Save : !!! underlying implementation is different with GORM -// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values) -func (c cacheDo) Save(values ...*models.Cache) error { - if len(values) == 0 { - return nil - } - return c.DO.Save(values) -} - -func (c cacheDo) First() (*models.Cache, error) { - if result, err := c.DO.First(); err != nil { - return nil, err - } else { - return result.(*models.Cache), nil - } -} - -func (c cacheDo) Take() (*models.Cache, error) { - if result, err := c.DO.Take(); err != nil { - return nil, err - } else { - return result.(*models.Cache), nil - } -} - -func (c cacheDo) Last() (*models.Cache, error) { - if result, err := c.DO.Last(); err != nil { - return nil, err - } else { - return result.(*models.Cache), nil - } -} - -func (c cacheDo) Find() ([]*models.Cache, error) { - result, err := c.DO.Find() - return result.([]*models.Cache), err -} - -func (c cacheDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*models.Cache, err error) { - buf := make([]*models.Cache, 0, batchSize) - err = c.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error { - defer func() { results = append(results, buf...) }() - return fc(tx, batch) - }) - return results, err -} - -func (c cacheDo) FindInBatches(result *[]*models.Cache, batchSize int, fc func(tx gen.Dao, batch int) error) error { - return c.DO.FindInBatches(result, batchSize, fc) -} - -func (c cacheDo) Attrs(attrs ...field.AssignExpr) *cacheDo { - return c.withDO(c.DO.Attrs(attrs...)) -} - -func (c cacheDo) Assign(attrs ...field.AssignExpr) *cacheDo { - return c.withDO(c.DO.Assign(attrs...)) -} - -func (c cacheDo) Joins(fields ...field.RelationField) *cacheDo { - for _, _f := range fields { - c = *c.withDO(c.DO.Joins(_f)) - } - return &c -} - -func (c cacheDo) Preload(fields ...field.RelationField) *cacheDo { - for _, _f := range fields { - c = *c.withDO(c.DO.Preload(_f)) - } - return &c -} - -func (c cacheDo) FirstOrInit() (*models.Cache, error) { - if result, err := c.DO.FirstOrInit(); err != nil { - return nil, err - } else { - return result.(*models.Cache), nil - } -} - -func (c cacheDo) FirstOrCreate() (*models.Cache, error) { - if result, err := c.DO.FirstOrCreate(); err != nil { - return nil, err - } else { - return result.(*models.Cache), nil - } -} - -func (c cacheDo) FindByPage(offset int, limit int) (result []*models.Cache, count int64, err error) { - result, err = c.Offset(offset).Limit(limit).Find() - if err != nil { - return - } - - if size := len(result); 0 < limit && 0 < size && size < limit { - count = int64(size + offset) - return - } - - count, err = c.Offset(-1).Limit(-1).Count() - return -} - -func (c cacheDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) { - count, err = c.Count() - if err != nil { - return - } - - err = c.Offset(offset).Limit(limit).Scan(result) - return -} - -func (c cacheDo) Scan(result interface{}) (err error) { - return c.DO.Scan(result) -} - -func (c cacheDo) Delete(models ...*models.Cache) (result gen.ResultInfo, err error) { - return c.DO.Delete(models) -} - -func (c *cacheDo) withDO(do gen.Dao) *cacheDo { - c.DO = *do.(*gen.DO) - return c -} diff --git a/pkg/dal/query/gen.go b/pkg/dal/query/gen.go index 190a5d14..15a85dd6 100644 --- a/pkg/dal/query/gen.go +++ b/pkg/dal/query/gen.go @@ -25,7 +25,6 @@ var ( BlobUpload *blobUpload Builder *builder BuilderRunner *builderRunner - Cache *cache CasbinRule *casbinRule CodeRepository *codeRepository CodeRepositoryBranch *codeRepositoryBranch @@ -67,7 +66,6 @@ func SetDefault(db *gorm.DB, opts ...gen.DOOption) { BlobUpload = &Q.BlobUpload Builder = &Q.Builder BuilderRunner = &Q.BuilderRunner - Cache = &Q.Cache CasbinRule = &Q.CasbinRule CodeRepository = &Q.CodeRepository CodeRepositoryBranch = &Q.CodeRepositoryBranch @@ -110,7 +108,6 @@ func Use(db *gorm.DB, opts ...gen.DOOption) *Query { BlobUpload: newBlobUpload(db, opts...), Builder: newBuilder(db, opts...), BuilderRunner: newBuilderRunner(db, opts...), - Cache: newCache(db, opts...), CasbinRule: newCasbinRule(db, opts...), CodeRepository: newCodeRepository(db, opts...), CodeRepositoryBranch: newCodeRepositoryBranch(db, opts...), @@ -154,7 +151,6 @@ type Query struct { BlobUpload blobUpload Builder builder BuilderRunner builderRunner - Cache cache CasbinRule casbinRule CodeRepository codeRepository CodeRepositoryBranch codeRepositoryBranch @@ -199,7 +195,6 @@ func (q *Query) clone(db *gorm.DB) *Query { BlobUpload: q.BlobUpload.clone(db), Builder: q.Builder.clone(db), BuilderRunner: q.BuilderRunner.clone(db), - Cache: q.Cache.clone(db), CasbinRule: q.CasbinRule.clone(db), CodeRepository: q.CodeRepository.clone(db), CodeRepositoryBranch: q.CodeRepositoryBranch.clone(db), @@ -251,7 +246,6 @@ func (q *Query) ReplaceDB(db *gorm.DB) *Query { BlobUpload: q.BlobUpload.replaceDB(db), Builder: q.Builder.replaceDB(db), BuilderRunner: q.BuilderRunner.replaceDB(db), - Cache: q.Cache.replaceDB(db), CasbinRule: q.CasbinRule.replaceDB(db), CodeRepository: q.CodeRepository.replaceDB(db), CodeRepositoryBranch: q.CodeRepositoryBranch.replaceDB(db), @@ -293,7 +287,6 @@ type queryCtx struct { BlobUpload *blobUploadDo Builder *builderDo BuilderRunner *builderRunnerDo - Cache *cacheDo CasbinRule *casbinRuleDo CodeRepository *codeRepositoryDo CodeRepositoryBranch *codeRepositoryBranchDo @@ -335,7 +328,6 @@ func (q *Query) WithContext(ctx context.Context) *queryCtx { BlobUpload: q.BlobUpload.WithContext(ctx), Builder: q.Builder.WithContext(ctx), BuilderRunner: q.BuilderRunner.WithContext(ctx), - Cache: q.Cache.WithContext(ctx), CasbinRule: q.CasbinRule.WithContext(ctx), CodeRepository: q.CodeRepository.WithContext(ctx), CodeRepositoryBranch: q.CodeRepositoryBranch.WithContext(ctx), diff --git a/pkg/modules/cacher/database/database.go b/pkg/modules/cacher/badger/badger.go similarity index 50% rename from pkg/modules/cacher/database/database.go rename to pkg/modules/cacher/badger/badger.go index 038b014c..03c413df 100644 --- a/pkg/modules/cacher/database/database.go +++ b/pkg/modules/cacher/badger/badger.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package database +package badger import ( "context" @@ -21,103 +21,92 @@ import ( "fmt" "time" - jsoniter "github.com/json-iterator/go" - "gorm.io/gorm" + "github.com/dgraph-io/badger/v4" "github.com/go-sigma/sigma/pkg/configs" - "github.com/go-sigma/sigma/pkg/dal/dao" + rBadger "github.com/go-sigma/sigma/pkg/dal/badger" "github.com/go-sigma/sigma/pkg/modules/cacher/definition" - "github.com/go-sigma/sigma/pkg/utils" - "github.com/go-sigma/sigma/pkg/utils/ptr" ) -// ValueWithTtl ... -type ValueWithTtl struct { - Value json.RawMessage - Ttl *time.Time -} - type cacher[T any] struct { - cacheService dao.CacheService - prefix string - fetcher definition.Fetcher[T] - config configs.Configuration + db *badger.DB + prefix string + fetcher definition.Fetcher[T] + config configs.Configuration } // New returns a new Cacher. func New[T any](config configs.Configuration, prefix string, fetcher definition.Fetcher[T]) (definition.Cacher[T], error) { return &cacher[T]{ - cacheService: dao.NewCacheServiceFactory().New(), - prefix: prefix, - fetcher: fetcher, - config: config, + db: rBadger.Client, + prefix: prefix, + fetcher: fetcher, + config: config, }, nil } // Set sets the value of given key if it is new to the cache. // Param val should not be nil. func (c *cacher[T]) Set(ctx context.Context, key string, val T, ttls ...time.Duration) error { - content, err := jsoniter.Marshal(val) + content, err := json.Marshal(val) if err != nil { return fmt.Errorf("marshal value failed: %w", err) } - value := ValueWithTtl{ - Value: content, - Ttl: ptr.Of(time.Now().Add(c.config.Cache.Ttl)), - } + var ttl = c.config.Cache.Badger.Ttl if len(ttls) > 0 { - value.Ttl = ptr.Of(time.Now().Add(ttls[0])) + ttl = ttls[0] } - return c.cacheService.Save(ctx, c.key(key), utils.MustMarshal(value), c.config.Cache.Database.Size, c.config.Cache.Database.Threshold) + return c.db.Update(func(txn *badger.Txn) error { + e := badger.NewEntry([]byte(key), content).WithTTL(ttl) + return txn.SetEntry(e) + }) } // Get tries to fetch a value corresponding to the given key from the cache. // If error occurs during the first time fetching, it will be cached until the // sequential fetching triggered by the refresh goroutine succeed. func (c *cacher[T]) Get(ctx context.Context, key string) (T, error) { - var result T - content, err := c.cacheService.Get(ctx, c.key(key)) + var val T + var result []byte + // var val []byte + err := c.db.View(func(txn *badger.Txn) error { + item, err := txn.Get([]byte(definition.GenKey(c.config, c.prefix, key))) + if err != nil { + return err + } + result, err = item.ValueCopy(nil) + if err != nil { + return err + } + return nil + }) if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { + if errors.Is(err, badger.ErrKeyNotFound) { if c.fetcher == nil { - return result, definition.ErrNotFound + return val, definition.ErrNotFound } - result, err = c.fetcher(key) + val, err = c.fetcher(key) if err != nil { - return result, err + return val, err } - err = c.Set(ctx, key, result, c.config.Cache.Ttl) + err = c.Set(ctx, key, val) if err != nil { - return result, fmt.Errorf("set value failed: %w", err) + return val, err } - return result, nil + return val, nil } - return result, fmt.Errorf("get value failed: %w", err) + return val, fmt.Errorf("get value failed: %w", err) } - var val ValueWithTtl - err = jsoniter.Unmarshal(content.Val, &val) + err = json.Unmarshal(result, &val) if err != nil { - return result, fmt.Errorf("unmarshal value failed: %w", err) + return val, fmt.Errorf("unmarshal value failed: %w", err) } - if val.Ttl != nil && val.Ttl.After(time.Now()) { - err = jsoniter.Unmarshal(val.Value, &result) - if err != nil { - return result, fmt.Errorf("unmarshal value failed: %w", err) - } - return result, nil - } - err = c.Del(ctx, key) - if err != nil { - return result, err - } - return result, definition.ErrNotFound + return val, nil } // Del deletes the value corresponding to the given key from the cache. func (c *cacher[T]) Del(ctx context.Context, key string) error { - return c.cacheService.Delete(ctx, c.key(key)) -} - -func (c *cacher[T]) key(key string) string { - return fmt.Sprintf("%s:%s", c.prefix, key) + return c.db.Update(func(txn *badger.Txn) error { + return txn.Delete([]byte(definition.GenKey(c.config, c.prefix, key))) + }) } diff --git a/pkg/modules/cacher/cacher.go b/pkg/modules/cacher/cacher.go index c9d88206..40a4d25a 100644 --- a/pkg/modules/cacher/cacher.go +++ b/pkg/modules/cacher/cacher.go @@ -16,7 +16,7 @@ package cacher import ( "github.com/go-sigma/sigma/pkg/configs" - "github.com/go-sigma/sigma/pkg/modules/cacher/database" + "github.com/go-sigma/sigma/pkg/modules/cacher/badger" "github.com/go-sigma/sigma/pkg/modules/cacher/definition" "github.com/go-sigma/sigma/pkg/modules/cacher/inmemory" "github.com/go-sigma/sigma/pkg/modules/cacher/redis" @@ -34,11 +34,10 @@ func New[T any](prefix string, fetcher definition.Fetcher[T]) (definition.Cacher cacher, err = redis.New[T](config, prefix, fetcher) case enums.CacherTypeInmemory: cacher, err = inmemory.New[T](config, prefix, fetcher) - case enums.CacherTypeDatabase: - cacher, err = database.New[T](config, prefix, fetcher) + case enums.CacherTypeBadger: + cacher, err = badger.New[T](config, prefix, fetcher) default: - cacher, err = database.New[T](config, prefix, fetcher) - // return nil, fmt.Errorf("Cacher %s not support", config.Cache.Type) + cacher, err = badger.New[T](config, prefix, fetcher) } return cacher, err } diff --git a/pkg/modules/cacher/definition/definition.go b/pkg/modules/cacher/definition/definition.go index 6152c0ed..7c85e66c 100644 --- a/pkg/modules/cacher/definition/definition.go +++ b/pkg/modules/cacher/definition/definition.go @@ -18,6 +18,8 @@ import ( "context" "fmt" "time" + + "github.com/go-sigma/sigma/pkg/configs" ) var ( @@ -45,3 +47,7 @@ type Cacher[T any] interface { type CacherFactory[T any] interface { New(prefix string, fetcher Fetcher[T]) (Cacher[T], error) } + +func GenKey(config configs.Configuration, prefix, key string) string { + return fmt.Sprintf("%s:%s:%s", config.Cache.Badger.Prefix, prefix, key) +} diff --git a/pkg/modules/cacher/inmemory/inmemory.go b/pkg/modules/cacher/inmemory/inmemory.go index fe481b8b..69b4a58f 100644 --- a/pkg/modules/cacher/inmemory/inmemory.go +++ b/pkg/modules/cacher/inmemory/inmemory.go @@ -23,25 +23,18 @@ import ( "github.com/go-sigma/sigma/pkg/configs" "github.com/go-sigma/sigma/pkg/modules/cacher/definition" - "github.com/go-sigma/sigma/pkg/utils/ptr" ) -// ValueWithTtl ... -type ValueWithTtl[T any] struct { - Value T - Ttl *time.Time -} - type cacher[T any] struct { config configs.Configuration - cache *lru.TwoQueueCache[string, ValueWithTtl[T]] + cache *lru.TwoQueueCache[string, T] prefix string fetcher definition.Fetcher[T] } // New returns a new Cacher. func New[T any](config configs.Configuration, prefix string, fetcher definition.Fetcher[T]) (definition.Cacher[T], error) { - cache, err := lru.New2Q[string, ValueWithTtl[T]](config.Cache.Inmemory.Size) + cache, err := lru.New2Q[string, T](config.Cache.Inmemory.Size) if err != nil { return nil, err } @@ -55,15 +48,8 @@ func New[T any](config configs.Configuration, prefix string, fetcher definition. // Set sets the value of given key if it is new to the cache. // Param val should not be nil. -func (c *cacher[T]) Set(ctx context.Context, key string, val T, ttls ...time.Duration) error { - value := ValueWithTtl[T]{ - Value: val, - Ttl: ptr.Of(time.Now().Add(c.config.Cache.Ttl)), - } - if len(ttls) > 0 { - value.Ttl = ptr.Of(time.Now().Add(ttls[0])) - } - c.cache.Add(c.key(key), value) +func (c *cacher[T]) Set(ctx context.Context, key string, val T, _ ...time.Duration) error { + c.cache.Add(definition.GenKey(c.config, c.prefix, key), val) return nil } @@ -71,40 +57,30 @@ func (c *cacher[T]) Set(ctx context.Context, key string, val T, ttls ...time.Dur // If error occurs during the first time fetching, it will be cached until the // sequential fetching triggered by the refresh goroutine succeed. func (c *cacher[T]) Get(ctx context.Context, key string) (T, error) { - result, ok := c.cache.Get(c.key(key)) + result, ok := c.cache.Get(definition.GenKey(c.config, c.prefix, key)) if !ok { if c.fetcher == nil { - return result.Value, definition.ErrNotFound + return result, definition.ErrNotFound } result, err := c.fetcher(key) if err != nil { return result, err } - err = c.Set(ctx, key, result, c.config.Cache.Ttl) + err = c.Set(ctx, key, result) if err != nil { return result, fmt.Errorf("Set value failed: %w", err) } return result, nil } - if result.Ttl == nil { - return result.Value, nil - } - if result.Ttl.After(time.Now()) { - return result.Value, nil - } - err := c.Del(ctx, key) + err := c.Set(ctx, key, result) if err != nil { - return result.Value, err + return result, fmt.Errorf("Set value failed: %w", err) } - return c.Get(ctx, key) + return result, nil } // Del deletes the value corresponding to the given key from the cache. func (c *cacher[T]) Del(ctx context.Context, key string) error { - c.cache.Remove(c.key(key)) + c.cache.Remove(definition.GenKey(c.config, c.prefix, key)) return nil } - -func (c *cacher[T]) key(key string) string { - return fmt.Sprintf("%s:%s", c.prefix, key) -} diff --git a/pkg/modules/cacher/inmemory/inmemory_test.go b/pkg/modules/cacher/inmemory/inmemory_test.go index 33c0829b..b7988811 100644 --- a/pkg/modules/cacher/inmemory/inmemory_test.go +++ b/pkg/modules/cacher/inmemory/inmemory_test.go @@ -18,7 +18,6 @@ import ( "context" "fmt" "testing" - "time" "github.com/stretchr/testify/assert" @@ -32,9 +31,9 @@ func fetcher1(key string) (string, error) { func TestNew(t *testing.T) { config := configs.Configuration{ Cache: configs.ConfigurationCache{ - Ttl: time.Second * 3, Inmemory: configs.ConfigurationCacheInmemory{ - Size: 1000, + Prefix: "sigma-cache", + Size: 1000, }, }, } @@ -68,13 +67,4 @@ func TestNew(t *testing.T) { res, err = cacher.Get(ctx, "m-test") assert.NoError(t, err) assert.Equal(t, "new-val", res) - - err = cacher.Set(ctx, "expire-key", "expire-val", time.Second) - assert.NoError(t, err) - - time.Sleep(time.Second * 3) - - res, err = cacher.Get(ctx, "expire-key") - assert.NoError(t, err) - assert.Equal(t, "new-val", res) } diff --git a/pkg/modules/cacher/redis/redis.go b/pkg/modules/cacher/redis/redis.go index 43d9f4ac..bd9be34e 100644 --- a/pkg/modules/cacher/redis/redis.go +++ b/pkg/modules/cacher/redis/redis.go @@ -54,11 +54,11 @@ func (c *cacher[T]) Set(ctx context.Context, key string, val T, ttls ...time.Dur if err != nil { return fmt.Errorf("marshal value failed: %w", err) } - var ttl = c.config.Cache.Ttl + var ttl = c.config.Cache.Redis.Ttl if len(ttls) > 0 { ttl = ttls[0] } - return c.redisCli.Set(ctx, c.key(key), content, ttl).Err() + return c.redisCli.Set(ctx, definition.GenKey(c.config, c.prefix, key), content, ttl).Err() } // Get tries to fetch a value corresponding to the given key from the cache. @@ -66,7 +66,7 @@ func (c *cacher[T]) Set(ctx context.Context, key string, val T, ttls ...time.Dur // sequential fetching triggered by the refresh goroutine succeed. func (c *cacher[T]) Get(ctx context.Context, key string) (T, error) { var result T - content, err := c.redisCli.Get(ctx, c.key(key)).Result() + content, err := c.redisCli.Get(ctx, definition.GenKey(c.config, c.prefix, key)).Result() if err != nil { if err == redis.Nil { if c.fetcher == nil { @@ -94,9 +94,5 @@ func (c *cacher[T]) Get(ctx context.Context, key string) (T, error) { // Del deletes the value corresponding to the given key from the cache. func (c *cacher[T]) Del(ctx context.Context, key string) error { - return c.redisCli.Del(ctx, c.key(key)).Err() -} - -func (c *cacher[T]) key(key string) string { - return fmt.Sprintf("%s:%s", c.prefix, key) + return c.redisCli.Del(ctx, definition.GenKey(c.config, c.prefix, key)).Err() } diff --git a/pkg/modules/locker/database/database.go b/pkg/modules/locker/badger/badger.go similarity index 83% rename from pkg/modules/locker/database/database.go rename to pkg/modules/locker/badger/badger.go index c5630e92..33dda249 100644 --- a/pkg/modules/locker/database/database.go +++ b/pkg/modules/locker/badger/badger.go @@ -12,74 +12,32 @@ // See the License for the specific language governing permissions and // limitations under the License. -package database +package badger import ( "context" "encoding/json" "fmt" - "os" - "strings" - "sync" "time" - badger "github.com/dgraph-io/badger/v4" + "github.com/dgraph-io/badger/v4" "github.com/google/uuid" "github.com/rs/zerolog/log" "github.com/go-sigma/sigma/pkg/configs" + rBadger "github.com/go-sigma/sigma/pkg/dal/badger" "github.com/go-sigma/sigma/pkg/dal/models" "github.com/go-sigma/sigma/pkg/modules/locker/definition" "github.com/go-sigma/sigma/pkg/utils" ) -type logger struct{} - -// Errorf is the error log -func (l logger) Errorf(msg string, opts ...interface{}) { - log.Error().Msg(strings.TrimSpace(fmt.Sprintf(msg, opts...))) -} - -// Warningf is the warning log -func (l logger) Warningf(msg string, opts ...interface{}) { - log.Warn().Msg(strings.TrimSpace(fmt.Sprintf(msg, opts...))) -} - -// Infof is the info log -func (l logger) Infof(msg string, opts ...interface{}) { - log.Info().Msg(strings.TrimSpace(fmt.Sprintf(msg, opts...))) -} - -// Debugf is the debug log -func (l logger) Debugf(msg string, opts ...interface{}) { - log.Debug().Msg(strings.TrimSpace(fmt.Sprintf(msg, opts...))) -} - type lockerDatabase struct { db *badger.DB } -var initOnce sync.Once - -var db *badger.DB - func New(config configs.Configuration) (definition.Locker, error) { - initOnce.Do(func() { - var err error - dir := config.Locker.Database.Path - if dir == "" { - dir, err = os.MkdirTemp("", "locker") - if err != nil { - panic("make temp dir for badger failed") - } - } - db, err = badger.Open(badger.DefaultOptions(dir).WithLogger(&logger{})) - if err != nil { - panic(fmt.Errorf("open badger database failed: %v", err)) - } - }) return &lockerDatabase{ - db: db, + db: rBadger.Client, }, nil } diff --git a/pkg/modules/locker/database/database_test.go b/pkg/modules/locker/badger/badger_test.go similarity index 85% rename from pkg/modules/locker/database/database_test.go rename to pkg/modules/locker/badger/badger_test.go index 3308eceb..896fcb61 100644 --- a/pkg/modules/locker/database/database_test.go +++ b/pkg/modules/locker/badger/badger_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package database_test +package badger_test import ( "context" @@ -27,8 +27,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/go-sigma/sigma/pkg/configs" + rBadger "github.com/go-sigma/sigma/pkg/dal/badger" "github.com/go-sigma/sigma/pkg/logger" - "github.com/go-sigma/sigma/pkg/modules/locker/database" + "github.com/go-sigma/sigma/pkg/modules/locker/badger" ) func TestDatabaseAcquire(t *testing.T) { @@ -36,10 +37,8 @@ func TestDatabaseAcquire(t *testing.T) { p, _ := os.MkdirTemp("", "badger") config := configs.Configuration{ - Locker: configs.ConfigurationLocker{ - Database: configs.ConfigurationLockerDatabase{ - Path: p, - }, + Badger: configs.ConfigurationBadger{ + Path: p, }, } defer os.RemoveAll(p) // nolint: errcheck @@ -47,7 +46,9 @@ func TestDatabaseAcquire(t *testing.T) { ctx, cancel := context.WithCancel(context.TODO()) defer cancel() - c, err := database.New(config) + assert.NoError(t, rBadger.Initialize(ctx, config)) + + c, err := badger.New(config) assert.NoError(t, err) const key = "test-redis-lock" @@ -80,10 +81,8 @@ func TestDatabaseAcquireWithRenew(t *testing.T) { p, _ := os.MkdirTemp("", "badger") config := configs.Configuration{ - Locker: configs.ConfigurationLocker{ - Database: configs.ConfigurationLockerDatabase{ - Path: p, - }, + Badger: configs.ConfigurationBadger{ + Path: p, }, } defer os.RemoveAll(p) // nolint: errcheck @@ -91,7 +90,9 @@ func TestDatabaseAcquireWithRenew(t *testing.T) { ctx, cancel := context.WithCancel(context.TODO()) defer cancel() - c, err := database.New(config) + assert.NoError(t, rBadger.Initialize(ctx, config)) + + c, err := badger.New(config) assert.NoError(t, err) const key = "test-redis-lock" diff --git a/pkg/modules/locker/locker.go b/pkg/modules/locker/locker.go index 408e4b9d..71e491cc 100644 --- a/pkg/modules/locker/locker.go +++ b/pkg/modules/locker/locker.go @@ -16,7 +16,7 @@ package locker import ( "github.com/go-sigma/sigma/pkg/configs" - "github.com/go-sigma/sigma/pkg/modules/locker/database" + "github.com/go-sigma/sigma/pkg/modules/locker/badger" "github.com/go-sigma/sigma/pkg/modules/locker/definition" "github.com/go-sigma/sigma/pkg/modules/locker/redis" "github.com/go-sigma/sigma/pkg/types/enums" @@ -29,12 +29,12 @@ var Locker definition.Locker func Initialize(config configs.Configuration) error { var err error switch config.Locker.Type { - case enums.LockerTypeDatabase: - Locker, err = database.New(config) + case enums.LockerTypeBadger: + Locker, err = badger.New(config) case enums.LockerTypeRedis: Locker, err = redis.New(config) default: - Locker, err = database.New(config) + Locker, err = badger.New(config) } return err } diff --git a/pkg/tests/middleware.go b/pkg/tests/middleware.go index 5bdd4eba..7e9f805a 100644 --- a/pkg/tests/middleware.go +++ b/pkg/tests/middleware.go @@ -15,13 +15,16 @@ package tests import ( + "context" "fmt" + "os" "strings" "testing" "github.com/spf13/viper" "github.com/go-sigma/sigma/pkg/configs" + "github.com/go-sigma/sigma/pkg/dal/badger" "github.com/go-sigma/sigma/pkg/modules/locker" "github.com/go-sigma/sigma/pkg/types/enums" ) @@ -64,7 +67,17 @@ func Initialize(t *testing.T) error { typ = enums.DatabaseSqlite3.String() } - err := locker.Initialize(configs.Configuration{}) + p, _ := os.MkdirTemp("", "badger") + err := badger.Initialize(context.Background(), configs.Configuration{ + Badger: configs.ConfigurationBadger{ + Path: p, + }, + }) + if err != nil { + return err + } + + err = locker.Initialize(configs.Configuration{}) if err != nil { return err } diff --git a/pkg/types/enums/enums.go b/pkg/types/enums/enums.go index 379bb5b9..e65126ea 100644 --- a/pkg/types/enums/enums.go +++ b/pkg/types/enums/enums.go @@ -74,7 +74,7 @@ type RedisType string // CacherType x ENUM( // inmemory, // redis, -// database, +// badger, // ) type CacherType string @@ -88,7 +88,7 @@ type WorkQueueType string // LockerType x ENUM( // redis, -// database, +// badger, // ) type LockerType string diff --git a/pkg/types/enums/enums_enum.go b/pkg/types/enums/enums_enum.go index fa4ddf49..ee0ea620 100644 --- a/pkg/types/enums/enums_enum.go +++ b/pkg/types/enums/enums_enum.go @@ -661,8 +661,8 @@ const ( CacherTypeInmemory CacherType = "inmemory" // CacherTypeRedis is a CacherType of type redis. CacherTypeRedis CacherType = "redis" - // CacherTypeDatabase is a CacherType of type database. - CacherTypeDatabase CacherType = "database" + // CacherTypeBadger is a CacherType of type badger. + CacherTypeBadger CacherType = "badger" ) var ErrInvalidCacherType = errors.New("not a valid CacherType") @@ -682,7 +682,7 @@ func (x CacherType) IsValid() bool { var _CacherTypeValue = map[string]CacherType{ "inmemory": CacherTypeInmemory, "redis": CacherTypeRedis, - "database": CacherTypeDatabase, + "badger": CacherTypeBadger, } // ParseCacherType attempts to convert a string to a CacherType. @@ -1279,8 +1279,8 @@ func (x GcTarget) Value() (driver.Value, error) { const ( // LockerTypeRedis is a LockerType of type redis. LockerTypeRedis LockerType = "redis" - // LockerTypeDatabase is a LockerType of type database. - LockerTypeDatabase LockerType = "database" + // LockerTypeBadger is a LockerType of type badger. + LockerTypeBadger LockerType = "badger" ) var ErrInvalidLockerType = errors.New("not a valid LockerType") @@ -1298,8 +1298,8 @@ func (x LockerType) IsValid() bool { } var _LockerTypeValue = map[string]LockerType{ - "redis": LockerTypeRedis, - "database": LockerTypeDatabase, + "redis": LockerTypeRedis, + "badger": LockerTypeBadger, } // ParseLockerType attempts to convert a string to a LockerType.