diff --git a/action.go b/action.go index 895bef6..59d9862 100644 --- a/action.go +++ b/action.go @@ -2,6 +2,7 @@ package tdb import ( "path/filepath" + "time" ) const ( @@ -44,6 +45,35 @@ func (db *TDB) GetActions() ([]string, []uint32, []uint32, error) { return targets, starts, lasts, nil } +// CheckExpirations to check expired actions +func (db *TDB) CheckExpirations() error { + targets, starts, lasts, err := db.GetActions() + if err != nil { + return err + } + + changed := false + now := uint32(time.Now().Unix()) + + db.action.contentLock.Lock() + for i := 0; i < len(lasts); i++ { + if now-lasts[i] > actionExp { + changed = true + if err := db.AddSlot(targets[i], starts[i], lasts[i]-starts[i]); err != nil { + db.action.contentLock.Unlock() + return err + } + delete(db.action.content, targets[i]) + } + } + db.action.contentLock.Unlock() + + if changed { + return db.action.writeToDisk() + } + return nil +} + func handleAction(db *TDB, target string, active bool, ts uint32) error { actions := db.action.content v := actions[target] diff --git a/action_test.go b/action_test.go index b551279..8261a57 100644 --- a/action_test.go +++ b/action_test.go @@ -33,6 +33,8 @@ func TestActions(t *testing.T) { assert.Len(t, targets, 1, "targets count wrong") assert.Len(t, allStarts, 1, "starts count wrong") assert.Len(t, allLasts, 1, "lasts count wrong") + + testCheckEXP(t, db) } func testNormalCloseAction(t *testing.T, db *TDB) { @@ -134,3 +136,28 @@ func testLaterActiveAction(t *testing.T, db *TDB) { assert.Len(t, allStarts, 1, "starts count wrong") assert.Len(t, allLasts, 1, "lasts count wrong") } + +func testCheckEXP(t *testing.T, db *TDB) { + var keys []string + for k := range db.action.content { + keys = append(keys, k) + } + for i := 0; i < len(keys); i++ { + delete(db.action.content, keys[i]) + } + + target := string(randBytes(6)) + now := uint32(time.Now().Unix()) + + err := db.AddAction(target, true, now-actionExp-2) + assert.NoError(t, err, "error add an action") + + err = db.CheckExpirations() + assert.NoError(t, err, "error check expiration") + + targets, allStarts, allLasts, err := db.GetActions() + assert.NoError(t, err, "get all actions wrong") + assert.Len(t, targets, 0, "targets count wrong") + assert.Len(t, allStarts, 0, "starts count wrong") + assert.Len(t, allLasts, 0, "lasts count wrong") +} diff --git a/glide.lock b/glide.lock index 634ca63..cde107f 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ hash: 9a15fec9a582da416c578cf88651edde1ee74b87efe6cd4e1b4716f73b7b7e99 -updated: 2017-04-13T22:48:51.355626295+08:00 +updated: 2017-04-24T12:20:28.2983702+08:00 imports: - name: github.com/drkaka/ulid version: a4078d88d430f7f5c443e60820664d0eef7a1cc8 @@ -14,7 +14,7 @@ imports: subpackages: - context - name: golang.org/x/sync - version: 5a06fca2c336a4b2b2fcb45702e8c47621b2aa2c + version: de49d9dcd27d4f764488181bea099dfe6179bcf0 subpackages: - errgroup testImports: @@ -23,7 +23,7 @@ testImports: subpackages: - spew - name: github.com/mattn/goveralls - version: a99c5ee06aeeca2a2befc7e90b99061b1180850c + version: 9d621f6940639bf287ecbb6f585a2c26eeba3bbe - name: github.com/pborman/uuid version: 1b00554d822231195d1babd97ff4a781231955c9 - name: github.com/pmezard/go-difflib @@ -35,6 +35,6 @@ testImports: subpackages: - assert - name: golang.org/x/tools - version: 9bf174b4d3fab31065b140e4a69c6e6b17746806 + version: c853fd576988aeebd3665f6224972f1379960013 subpackages: - cover