//Len returns # of records in the cache. func (c *Cache) Len() int { var r []*record.DB err := db.DB.View(func(tx *bolt.Tx) error { var err error r, err = record.GetFromDBs(tx, c.Datfile) return err }) if err != nil { log.Print(err) return 0 } return len(r) }
//Remove Remove all files and dirs of cache. func (c *Cache) Remove() { err := db.DB.Update(func(tx *bolt.Tx) error { r, err := record.GetFromDBs(tx, c.Datfile) if err != nil { return err } for _, rr := range r { rr.Del(tx) } return db.Del(tx, "thread", []byte(c.Datfile)) }) if err != nil { log.Println(err) } }
//HasRecord return true if cache has more than one records or removed records. func (c *Cache) HasRecord() bool { var r []*record.DB err := db.DB.View(func(tx *bolt.Tx) error { var err error r, err = record.GetFromDBs(tx, c.Datfile) return err }) if err != nil { log.Print(err) return false } for _, rr := range r { if !rr.Deleted { return true } } return false }
//Velocity returns number of records in one days in the cache. func (c *Cache) Velocity() int { var r []*record.DB err := db.DB.View(func(tx *bolt.Tx) error { var err error r, err = record.GetFromDBs(tx, c.Datfile) return err }) if err != nil { log.Print(err) return 0 } cnt := 0 t := int64(time.Now().Add(-7 * 24 * time.Hour).Second()) for _, rr := range r { if rr.Stamp > t { cnt++ } } return cnt }
//Size returns sum of body char length of records in the cache. func (c *Cache) Size() int64 { if c.Len() == 0 { return 0 } var r []*record.DB err := db.DB.View(func(tx *bolt.Tx) error { var err error r, err = record.GetFromDBs(tx, c.Datfile) return err }) if err != nil { log.Print(err) return 0 } var cnt int64 for _, rr := range r { cnt += int64(len(rr.Body)) } return cnt }
//Stamp returns latest stampl of records in the cache. func (c *Cache) Stamp() int64 { var r []*record.DB err := db.DB.View(func(tx *bolt.Tx) error { var err error r, err = record.GetFromDBs(tx, c.Datfile) return err }) if err != nil { log.Print(err) return 0 } if len(r) == 0 { return 0 } var stamp int64 for i := len(r) - 1; i >= 0; i-- { if !r[i].Deleted { stamp = r[i].Stamp break } } return stamp }