|
|
|
@ -23,10 +23,11 @@ import ( |
|
|
|
|
|
|
|
var conf = &config.Config |
|
|
|
|
|
|
|
type BoltdbStore struct { |
|
|
|
// Store implements storage.Store, and storage with boltdb
|
|
|
|
type Store struct { |
|
|
|
} |
|
|
|
|
|
|
|
var boltDb *bolt.DB = nil |
|
|
|
var boltDb *bolt.DB |
|
|
|
var boltOnce sync.Once |
|
|
|
|
|
|
|
func boltGet() *bolt.DB { |
|
|
|
@ -110,7 +111,7 @@ func cleanupGlobalWithGids(t *bolt.Tx, gids map[string]struct{}) { |
|
|
|
logger.Debugf("Start to cleanup %d gids", len(gids)) |
|
|
|
for gid := range gids { |
|
|
|
logger.Debugf("Start to delete gid: %s", gid) |
|
|
|
bucket.Delete([]byte(gid)) |
|
|
|
dtmimp.E2P(bucket.Delete([]byte(gid))) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -139,7 +140,7 @@ func cleanupBranchWithGids(t *bolt.Tx, gids map[string]struct{}) { |
|
|
|
logger.Debugf("Start to cleanup %d branches", len(branchKeys)) |
|
|
|
for _, key := range branchKeys { |
|
|
|
logger.Debugf("Start to delete branch: %s", key) |
|
|
|
bucket.Delete([]byte(key)) |
|
|
|
dtmimp.E2P(bucket.Delete([]byte(key))) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -165,7 +166,7 @@ func cleanupIndexWithGids(t *bolt.Tx, gids map[string]struct{}) { |
|
|
|
logger.Debugf("Start to cleanup %d indexes", len(indexKeys)) |
|
|
|
for _, key := range indexKeys { |
|
|
|
logger.Debugf("Start to delete index: %s", key) |
|
|
|
bucket.Delete([]byte(key)) |
|
|
|
dtmimp.E2P(bucket.Delete([]byte(key))) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -232,19 +233,25 @@ func tPutIndex(t *bolt.Tx, unix int64, gid string) { |
|
|
|
dtmimp.E2P(err) |
|
|
|
} |
|
|
|
|
|
|
|
func (s *BoltdbStore) Ping() error { |
|
|
|
// Ping execs ping cmd to boltdb
|
|
|
|
func (s *Store) Ping() error { |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func (s *BoltdbStore) PopulateData(skipDrop bool) { |
|
|
|
// PopulateData populates data to boltdb
|
|
|
|
func (s *Store) PopulateData(skipDrop bool) { |
|
|
|
if !skipDrop { |
|
|
|
err := boltGet().Update(func(t *bolt.Tx) error { |
|
|
|
t.DeleteBucket(bucketIndex) |
|
|
|
t.DeleteBucket(bucketBranches) |
|
|
|
t.DeleteBucket(bucketGlobal) |
|
|
|
t.CreateBucket(bucketIndex) |
|
|
|
t.CreateBucket(bucketBranches) |
|
|
|
t.CreateBucket(bucketGlobal) |
|
|
|
dtmimp.E2P(t.DeleteBucket(bucketIndex)) |
|
|
|
dtmimp.E2P(t.DeleteBucket(bucketBranches)) |
|
|
|
dtmimp.E2P(t.DeleteBucket(bucketGlobal)) |
|
|
|
_, err := t.CreateBucket(bucketIndex) |
|
|
|
dtmimp.E2P(err) |
|
|
|
_, err = t.CreateBucket(bucketBranches) |
|
|
|
dtmimp.E2P(err) |
|
|
|
_, err = t.CreateBucket(bucketGlobal) |
|
|
|
dtmimp.E2P(err) |
|
|
|
|
|
|
|
return nil |
|
|
|
}) |
|
|
|
dtmimp.E2P(err) |
|
|
|
@ -252,7 +259,8 @@ func (s *BoltdbStore) PopulateData(skipDrop bool) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (s *BoltdbStore) FindTransGlobalStore(gid string) (trans *storage.TransGlobalStore) { |
|
|
|
// FindTransGlobalStore finds GlobalTrans data by gid
|
|
|
|
func (s *Store) FindTransGlobalStore(gid string) (trans *storage.TransGlobalStore) { |
|
|
|
err := boltGet().View(func(t *bolt.Tx) error { |
|
|
|
trans = tGetGlobal(t, gid) |
|
|
|
return nil |
|
|
|
@ -261,7 +269,8 @@ func (s *BoltdbStore) FindTransGlobalStore(gid string) (trans *storage.TransGlob |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
func (s *BoltdbStore) ScanTransGlobalStores(position *string, limit int64) []storage.TransGlobalStore { |
|
|
|
// ScanTransGlobalStores lists GlobalTrans data
|
|
|
|
func (s *Store) ScanTransGlobalStores(position *string, limit int64) []storage.TransGlobalStore { |
|
|
|
globals := []storage.TransGlobalStore{} |
|
|
|
err := boltGet().View(func(t *bolt.Tx) error { |
|
|
|
cursor := t.Bucket(bucketGlobal).Cursor() |
|
|
|
@ -287,8 +296,9 @@ func (s *BoltdbStore) ScanTransGlobalStores(position *string, limit int64) []sto |
|
|
|
return globals |
|
|
|
} |
|
|
|
|
|
|
|
func (s *BoltdbStore) FindBranches(gid string) []storage.TransBranchStore { |
|
|
|
var branches []storage.TransBranchStore = nil |
|
|
|
// FindBranches finds Branch data by gid
|
|
|
|
func (s *Store) FindBranches(gid string) []storage.TransBranchStore { |
|
|
|
var branches []storage.TransBranchStore |
|
|
|
err := boltGet().View(func(t *bolt.Tx) error { |
|
|
|
branches = tGetBranches(t, gid) |
|
|
|
return nil |
|
|
|
@ -297,11 +307,13 @@ func (s *BoltdbStore) FindBranches(gid string) []storage.TransBranchStore { |
|
|
|
return branches |
|
|
|
} |
|
|
|
|
|
|
|
func (s *BoltdbStore) UpdateBranches(branches []storage.TransBranchStore, updates []string) (int, error) { |
|
|
|
// UpdateBranches update branches info
|
|
|
|
func (s *Store) UpdateBranches(branches []storage.TransBranchStore, updates []string) (int, error) { |
|
|
|
return 0, nil // not implemented
|
|
|
|
} |
|
|
|
|
|
|
|
func (s *BoltdbStore) LockGlobalSaveBranches(gid string, status string, branches []storage.TransBranchStore, branchStart int) { |
|
|
|
// LockGlobalSaveBranches creates branches
|
|
|
|
func (s *Store) LockGlobalSaveBranches(gid string, status string, branches []storage.TransBranchStore, branchStart int) { |
|
|
|
err := boltGet().Update(func(t *bolt.Tx) error { |
|
|
|
g := tGetGlobal(t, gid) |
|
|
|
if g == nil { |
|
|
|
@ -316,7 +328,8 @@ func (s *BoltdbStore) LockGlobalSaveBranches(gid string, status string, branches |
|
|
|
dtmimp.E2P(err) |
|
|
|
} |
|
|
|
|
|
|
|
func (s *BoltdbStore) MaySaveNewTrans(global *storage.TransGlobalStore, branches []storage.TransBranchStore) error { |
|
|
|
// MaySaveNewTrans creates a new trans
|
|
|
|
func (s *Store) MaySaveNewTrans(global *storage.TransGlobalStore, branches []storage.TransBranchStore) error { |
|
|
|
return boltGet().Update(func(t *bolt.Tx) error { |
|
|
|
g := tGetGlobal(t, global.Gid) |
|
|
|
if g != nil { |
|
|
|
@ -329,7 +342,8 @@ func (s *BoltdbStore) MaySaveNewTrans(global *storage.TransGlobalStore, branches |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
func (s *BoltdbStore) ChangeGlobalStatus(global *storage.TransGlobalStore, newStatus string, updates []string, finished bool) { |
|
|
|
// ChangeGlobalStatus changes global trans status
|
|
|
|
func (s *Store) ChangeGlobalStatus(global *storage.TransGlobalStore, newStatus string, updates []string, finished bool) { |
|
|
|
old := global.Status |
|
|
|
global.Status = newStatus |
|
|
|
err := boltGet().Update(func(t *bolt.Tx) error { |
|
|
|
@ -346,7 +360,8 @@ func (s *BoltdbStore) ChangeGlobalStatus(global *storage.TransGlobalStore, newSt |
|
|
|
dtmimp.E2P(err) |
|
|
|
} |
|
|
|
|
|
|
|
func (s *BoltdbStore) TouchCronTime(global *storage.TransGlobalStore, nextCronInterval int64) { |
|
|
|
// TouchCronTime updates cronTime
|
|
|
|
func (s *Store) TouchCronTime(global *storage.TransGlobalStore, nextCronInterval int64) { |
|
|
|
oldUnix := global.NextCronTime.Unix() |
|
|
|
global.NextCronTime = dtmutil.GetNextTime(nextCronInterval) |
|
|
|
global.UpdateTime = dtmutil.GetNextTime(0) |
|
|
|
@ -364,8 +379,9 @@ func (s *BoltdbStore) TouchCronTime(global *storage.TransGlobalStore, nextCronIn |
|
|
|
dtmimp.E2P(err) |
|
|
|
} |
|
|
|
|
|
|
|
func (s *BoltdbStore) LockOneGlobalTrans(expireIn time.Duration) *storage.TransGlobalStore { |
|
|
|
var trans *storage.TransGlobalStore = nil |
|
|
|
// LockOneGlobalTrans finds GlobalTrans
|
|
|
|
func (s *Store) LockOneGlobalTrans(expireIn time.Duration) *storage.TransGlobalStore { |
|
|
|
var trans *storage.TransGlobalStore |
|
|
|
min := fmt.Sprintf("%d", time.Now().Add(expireIn).Unix()) |
|
|
|
next := time.Now().Add(time.Duration(conf.RetryInterval) * time.Second) |
|
|
|
err := boltGet().Update(func(t *bolt.Tx) error { |
|
|
|
|