Skip to content

Commit

Permalink
Change get slots methods to have a range.
Browse files Browse the repository at this point in the history
  • Loading branch information
kakaLQY committed Apr 26, 2017
1 parent abe17eb commit 4cb3d4d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
10 changes: 5 additions & 5 deletions action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func testNormalCloseAction(t *testing.T, db *TDB) {
db.AddAction(target, true, now)
db.AddAction(target, false, now+2)

starts, howlongs, err := db.GetAllSlots(target)
starts, howlongs, err := db.GetSlots(target, 0, 0)
assert.NoError(t, err, "get slots wrong")
assert.Len(t, starts, 1, "starts count wrong")
assert.Len(t, howlongs, 1, "howlong count wrong")
Expand All @@ -64,7 +64,7 @@ func testSingleCloseAction(t *testing.T, db *TDB) {
// single close action
db.AddAction(target, false, now)

starts, howlongs, err := db.GetAllSlots(target)
starts, howlongs, err := db.GetSlots(target, 0, 0)
assert.NoError(t, err, "get slots wrong")
assert.Len(t, starts, 1, "starts count wrong")
assert.Len(t, howlongs, 1, "howlong count wrong")
Expand All @@ -82,7 +82,7 @@ func testCloseEarlier(t *testing.T, db *TDB) {
db.AddAction(target, true, now+3)
db.AddAction(target, false, now+2)

starts, howlongs, err := db.GetAllSlots(target)
starts, howlongs, err := db.GetSlots(target, 0, 0)
assert.NoError(t, err, "get slots wrong")
assert.Len(t, starts, 1, "starts count wrong")
assert.Len(t, howlongs, 1, "howlong count wrong")
Expand All @@ -99,7 +99,7 @@ func testCloseExpiredAction(t *testing.T, db *TDB) {
db.AddAction(target, true, now)
db.AddAction(target, false, now+17)

starts, howlongs, err := db.GetAllSlots(target)
starts, howlongs, err := db.GetSlots(target, 0, 0)
assert.NoError(t, err, "get slots wrong")
assert.Len(t, starts[0], 2, "starts count wrong")
assert.Len(t, howlongs[0], 2, "howlong count wrong")
Expand All @@ -125,7 +125,7 @@ func testLaterActiveAction(t *testing.T, db *TDB) {
db.AddAction(target, true, now)
db.AddAction(target, true, now+17)

starts, howlongs, err := db.GetAllSlots(target)
starts, howlongs, err := db.GetSlots(target, 0, 0)
assert.NoError(t, err, "get slots wrong")
assert.Len(t, starts[0], 1, "starts count wrong")
assert.Len(t, howlongs[0], 1, "howlong count wrong")
Expand Down
28 changes: 23 additions & 5 deletions slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"sort"
"strings"

"math"

"github.com/tracerun/locker"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -49,15 +51,15 @@ func (db *TDB) GetTargets() []string {
return db.slot.getInfoKeys()
}

// GetAllSlots to get all the slots for a target
// GetSlots to get slots of a target in certain range
// return unix time and slots
func (db *TDB) GetAllSlots(target string) ([][]uint32, [][]uint32, error) {
func (db *TDB) GetSlots(target string, start, end uint32) ([][]uint32, [][]uint32, error) {
aliasName := string(db.slot.getInfoValue(target))
if aliasName == "" {
return nil, nil, nil
}

files, err := db.getTargetFiles(target, 0, 0)
files, err := db.getTargetFiles(target, start, end)
if err != nil {
return nil, nil, err
}
Expand All @@ -67,11 +69,27 @@ func (db *TDB) GetAllSlots(target string) ([][]uint32, [][]uint32, error) {
var g errgroup.Group
starts := make([][]uint32, len(files))
slots := make([][]uint32, len(files))

realStart := start
readEnd := end
if readEnd == 0 {
readEnd = math.MaxUint32
}

for i, f := range files {
i, f := i, f
g.Go(func() error {
var err error
starts[i], slots[i], err = readFile(aliasedHome, f)
var startsResult, slotsResult []uint32
thisStarts, thisSlots, err := readFile(aliasedHome, f)

// get the in range results
for i := 0; i < len(thisStarts); i++ {
if thisStarts[i] >= realStart && thisStarts[i] <= readEnd {
startsResult = append(startsResult, thisStarts[i])
slotsResult = append(slotsResult, thisSlots[i])
}
}
starts[i], slots[i] = startsResult, slotsResult
return err
})
}
Expand Down
18 changes: 17 additions & 1 deletion slot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,23 @@ func TestSlots(t *testing.T) {
err = db.AddSlot(target, start3, slot3)
assert.NoError(t, err, "should have no error")

starts, slots, err := db.GetAllSlots(target)
// get slots with bad range
_, _, err = db.GetSlots(target, 20, 10)
assert.Equal(t, ErrRange, err, "range should be wrong")

// get one
starts, slots, err := db.GetSlots(target, 1491134203, 0)
assert.NoError(t, err, "should have no error to get all slots")
assert.Len(t, starts, 2, "should have two files")
assert.Len(t, slots, 2, "should have two files")

assert.Len(t, starts[0], 0, "should have no slot")
assert.Len(t, slots[0], 0, "should have no slot")
assert.Len(t, starts[1], 1, "should have one slot")
assert.Len(t, slots[1], 1, "should have one slot")

// get all slots
starts, slots, err = db.GetSlots(target, 0, 0)
assert.NoError(t, err, "should have no error to get all slots")
assert.Len(t, starts, 2, "should have two files")
assert.Len(t, slots, 2, "should have two files")
Expand Down

0 comments on commit 4cb3d4d

Please sign in to comment.