2
0
mirror of https://github.com/carlostrub/sisyphus synced 2024-10-31 09:20:15 +00:00
sisyphus/database.go

85 lines
1.9 KiB
Go
Raw Normal View History

2017-04-15 20:23:26 +00:00
package sisyphus
2017-03-11 20:02:17 +00:00
import (
"log"
2017-03-11 20:02:17 +00:00
"github.com/boltdb/bolt"
)
2017-05-08 03:29:25 +00:00
// openDB creates and opens a new database and its respective buckets (if required)
func openDB(m Maildir) (db *bolt.DB, err error) {
2017-03-11 20:02:17 +00:00
2017-05-08 03:29:25 +00:00
log.Println("loading database for " + string(m))
2017-03-11 20:02:17 +00:00
// Open the sisyphus.db data file in your current directory.
// It will be created if it doesn't exist.
2017-05-08 03:29:25 +00:00
db, err = bolt.Open(string(m)+"/sisyphus.db", 0600, nil)
2017-03-11 20:02:17 +00:00
if err != nil {
return db, err
}
// Create DB bucket for the map of processed e-mail IDs
err = db.Update(func(tx *bolt.Tx) error {
2017-05-08 03:29:25 +00:00
_, err := tx.CreateBucketIfNotExists([]byte("Statistics"))
2017-04-26 11:20:21 +00:00
return err
})
if err != nil {
return db, err
}
2017-03-11 20:02:17 +00:00
// Create DB bucket for word lists
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("Wordlists"))
2017-04-26 11:20:21 +00:00
return err
2017-03-11 20:02:17 +00:00
})
if err != nil {
return db, err
}
// Create DB bucket for Junk inside bucket Wordlists
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Wordlists"))
_, err := b.CreateBucketIfNotExists([]byte("Junk"))
2017-04-26 11:20:21 +00:00
return err
2017-03-11 20:02:17 +00:00
})
if err != nil {
return db, err
}
// Create DB bucket for Good inside bucket Wordlists
2017-03-11 20:02:17 +00:00
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Wordlists"))
_, err := b.CreateBucketIfNotExists([]byte("Good"))
2017-04-26 11:20:21 +00:00
return err
2017-03-11 20:02:17 +00:00
})
2017-03-14 20:27:05 +00:00
return db, err
2017-03-11 20:02:17 +00:00
}
2017-05-08 03:29:25 +00:00
// LoadDatabases loads all databases from a given slice of Maildirs
func LoadDatabases(d []Maildir) (databases map[Maildir]*bolt.DB, err error) {
databases = make(map[Maildir]*bolt.DB)
for _, val := range d {
databases[val], err = openDB(val)
if err != nil {
return databases, err
}
}
log.Println("all databases loaded")
2017-05-08 03:29:25 +00:00
return databases, nil
}
// CloseDatabases closes all databases from a given slice of Maildirs
func CloseDatabases(databases map[Maildir]*bolt.DB) {
for key, val := range databases {
2017-05-08 03:29:25 +00:00
err := val.Close()
if err != nil {
log.Println(err)
}
log.Println("database " + string(key) + "/sisyphus.db closed")
2017-05-08 03:29:25 +00:00
}
return
}