func (s *BoltStore) CasbinStore() persist.BatchAdapter {
	return s.casbinAdapter
}
CasbinStore is as a getter for the Casbin store adapter.
package store
import (
	"encoding/binary"
	"fmt"
	"path/filepath"
	"time"
	"github.com/casbin/casbin/v2/persist"
	"github.com/speza/casbin-bolt-adapter"
	bolt "go.etcd.io/bbolt"
	"github.com/gaia-pipeline/gaia"
	"github.com/gaia-pipeline/gaia/helper/assethelper"
	"github.com/gaia-pipeline/gaia/security"
)
var (
	userBucket = []byte("Users")
	userPermsBucket = []byte("UserPermissions")
	pipelineBucket = []byte("Pipelines")
	createPipelineBucket = []byte("CreatePipelines")
	pipelineRunBucket = []byte("PipelineRun")
	settingsBucket = []byte("Settings")
	workerBucket = []byte("Worker")
	shaPairBucket = []byte("SHAPair")
)
const (
	adminUsername = "admin"
	adminPassword = "admin"
	autoUsername  = "auto"
	autoPassword  = "auto"
	boltDBFileName = "gaia.db"
)
type BoltStore struct {
	db            *bolt.DB
	casbinAdapter persist.BatchAdapter
}
type SettingsStore interface {
	SettingsPut(config *gaia.StoreConfig) error
	SettingsGet() (*gaia.StoreConfig, error)
}
type GaiaStore interface {
	SettingsStore
	Init(dataPath string) error
	Close() error
	CreatePipelinePut(createPipeline *gaia.CreatePipeline) error
	CreatePipelineGet() (listOfPipelines []gaia.CreatePipeline, err error)
	PipelinePut(pipeline *gaia.Pipeline) error
	PipelineGet(id int) (pipeline *gaia.Pipeline, err error)
	PipelineGetByName(name string) (pipline *gaia.Pipeline, err error)
	PipelineGetRunHighestID(pipeline *gaia.Pipeline) (id int, err error)
	PipelinePutRun(r *gaia.PipelineRun) error
	PipelineGetScheduled(limit int) ([]*gaia.PipelineRun, error)
	PipelineGetRunByPipelineIDAndID(pipelineid int, runid int) (*gaia.PipelineRun, error)
	PipelineGetAllRuns() ([]gaia.PipelineRun, error)
	PipelineGetAllRunsByPipelineID(pipelineID int) ([]gaia.PipelineRun, error)
	PipelineGetLatestRun(pipelineID int) (*gaia.PipelineRun, error)
	PipelineGetRunByID(runID string) (*gaia.PipelineRun, error)
	PipelineDelete(id int) error
	PipelineRunDelete(uniqueID string) error
	UserPut(u *gaia.User, encryptPassword bool) error
	UserAuth(u *gaia.User, updateLastLogin bool) (*gaia.User, error)
	UserGet(username string) (*gaia.User, error)
	UserGetAll() ([]gaia.User, error)
	UserDelete(u string) error
	UserPermissionsPut(perms *gaia.UserPermission) error
	UserPermissionsGet(username string) (*gaia.UserPermission, error)
	UserPermissionsDelete(username string) error
	WorkerPut(w *gaia.Worker) error
	WorkerGetAll() ([]*gaia.Worker, error)
	WorkerDelete(id string) error
	WorkerDeleteAll() error
	WorkerGet(id string) (*gaia.Worker, error)
	UpsertSHAPair(pair gaia.SHAPair) error
	GetSHAPair(pipelineID int) (bool, gaia.SHAPair, error)
	CasbinStore() persist.BatchAdapter
}
var _ GaiaStore = (*BoltStore)(nil)
func NewBoltStore() *BoltStore {
	s := &BoltStore{}
	return s
}
func (s *BoltStore) Init(dataPath string) error {
	path := filepath.Join(dataPath, boltDBFileName)
	db, err := bolt.Open(path, gaia.Cfg.Bolt.Mode, &bolt.Options{Timeout: 5 * time.Second})
	if err != nil {
		return err
	}
	s.db = db
	builtinPolicy, _ := assethelper.LoadRBACBuiltinPolicy()
	casbinAdapter, err := boltadapter.NewAdapter(db, "casbin-policies", builtinPolicy)
	if err != nil {
		return err
	}
	s.casbinAdapter = casbinAdapter
	return s.setupDatabase()
}
func (s *BoltStore) Close() error {
	return s.db.Close()
}
type setup struct {
	bs  *BoltStore
	err error
}
func (s *setup) update(bucketName []byte) {
	if s.err != nil {
		return
	}
	c := func(tx *bolt.Tx) error {
		_, err := tx.CreateBucketIfNotExists(bucketName)
		if err != nil {
			return fmt.Errorf("create bucket: %s", err)
		}
		return nil
	}
	s.err = s.bs.db.Update(c)
}
func (s *BoltStore) setupDatabase() error {
	setP := &setup{
		bs:  s,
		err: nil,
	}
	setP.update(userBucket)
	setP.update(userPermsBucket)
	setP.update(pipelineBucket)
	setP.update(createPipelineBucket)
	setP.update(pipelineRunBucket)
	setP.update(settingsBucket)
	setP.update(workerBucket)
	setP.update(shaPairBucket)
	if setP.err != nil {
		return setP.err
	}
	admin, err := s.UserGet(adminUsername)
	if err != nil {
		return err
	}
	if admin == nil {
		err = s.UserPut(&gaia.User{
			DisplayName: adminUsername,
			Username:    adminUsername,
			Password:    adminPassword,
		}, true)
		if err != nil {
			return err
		}
	}
	err = s.CreatePermissionsIfNotExisting()
	if err != nil {
		return err
	}
	u, err := s.UserGet(autoUsername)
	if err != nil {
		return err
	}
	if u == nil {
		triggerToken := security.GenerateRandomUUIDV5()
		auto := gaia.User{
			DisplayName:  "Auto User",
			JwtExpiry:    0,
			Password:     autoPassword,
			Tokenstring:  "",
			TriggerToken: triggerToken,
			Username:     autoUsername,
			LastLogin:    time.Now(),
		}
		err = s.UserPut(&auto, true)
		if err != nil {
			return err
		}
	}
	return nil
}
func itob(v int) []byte {
	b := make([]byte, 8)
	binary.BigEndian.PutUint64(b, uint64(v))
	return b
}