Skip to content

Commit

Permalink
Remove optional slow & expensive function
Browse files Browse the repository at this point in the history
  • Loading branch information
glinton committed Feb 3, 2017
1 parent a372185 commit 43610a0
Showing 1 changed file with 22 additions and 51 deletions.
73 changes: 22 additions & 51 deletions drain/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,66 +267,37 @@ func (a BoltArchive) Expire() {
})
case float64, int:
records := int(saveAmt.(float64)) // assertion is slow, do it once (casting is fast)
if os.Getenv("LOGVAC_SEQUENTIAL") != "" {
// keep this around for smaller datasets. ...though since bucket.Stats() is so nonperformant maybe we should just get rid of it
a.db.Batch(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(bucketName))
if bucket == nil {
config.Log.Trace("No logs of type '%s' found", bucketName)
return fmt.Errorf("No logs of type '%s' found", bucketName)
}

// trim the bucket to size
c := bucket.Cursor()
c.First() // if we ever stop ordering by time (oldest first) we'll need to change this
a.db.Batch(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(bucketName))
if bucket == nil {
config.Log.Trace("No logs of type '%s' found", bucketName)
return fmt.Errorf("No logs of type '%s' found", bucketName)
}

// trim the bucket to size
c := bucket.Cursor()

// loop through and remove extra logs
for key_count := int(bucket.Stats().KeyN); key_count > records; key_count-- {
rSaved := 0
// loop through and remove extra logs
// if we ever stop ordering by time (oldest first) we'll need to change cursor placement
for k, v := c.Last(); k != nil && v != nil; k, v = c.Prev() {
rSaved += 1
// if the number records we've traversed is larger than our limit, delet the current record
if rSaved > records {
config.Log.Trace("Deleting extra log of type '%s'...", bucketName)
err = c.Delete()
if err != nil {
config.Log.Trace("Failed to delete extra log - %s", err)
}
c.Next()
}

config.Log.Trace("=======================================")
config.Log.Trace("= DONE CHECKING/DELETING EXPIRED LOGS =")
config.Log.Trace("=======================================")
return nil
})
} else {
a.db.Batch(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(bucketName))
if bucket == nil {
config.Log.Trace("No logs of type '%s' found", bucketName)
return fmt.Errorf("No logs of type '%s' found", bucketName)
}

// trim the bucket to size
c := bucket.Cursor()

rSaved := 0
// loop through and remove extra logs
// if we ever stop ordering by time (oldest first) we'll need to change cursor placement
for k, v := c.Last(); k != nil && v != nil; k, v = c.Prev() {
rSaved += 1
// if the number records we've traversed is larger than our limit, delet the current record
if rSaved > records {
config.Log.Trace("Deleting extra log of type '%s'...", bucketName)
err = c.Delete()
if err != nil {
config.Log.Trace("Failed to delete extra log - %s", err)
}
}
}
}

config.Log.Trace("=======================================")
config.Log.Trace("= DONE CHECKING/DELETING EXPIRED LOGS =")
config.Log.Trace("=======================================")
return nil
})
}
config.Log.Trace("=======================================")
config.Log.Trace("= DONE CHECKING/DELETING EXPIRED LOGS =")
config.Log.Trace("=======================================")
return nil
})
default:
config.Log.Fatal("Bad log-keep value")
os.Exit(1)
Expand Down

0 comments on commit 43610a0

Please sign in to comment.