notmuch: avoid stale DBs
Opening a notmuch DB gives you a snapshot of the stage at that specific time. Prior to this, we only reopened the DB upon writing. However, if say a mail sync program like offlineimap is fetching new mail, we would never pick it up. This commit caches a db for a while, so that we don't generate too much overhead and does a reconnect cycle after that. I hardcoded a value as I don't think that having an option would be beneficial. Any write operation (meaning reading mail) anyhow flushes the DB by necessity. (we need to close to commit tag changes, which changing the read state is)
This commit is contained in:
parent
72f55b857b
commit
3e7e236f50
1 changed files with 133 additions and 99 deletions
|
@ -5,15 +5,19 @@ package lib
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
notmuch "github.com/zenhack/go.notmuch"
|
notmuch "github.com/zenhack/go.notmuch"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const MAX_DB_AGE time.Duration = 10 * time.Second
|
||||||
|
|
||||||
type DB struct {
|
type DB struct {
|
||||||
path string
|
path string
|
||||||
excludedTags []string
|
excludedTags []string
|
||||||
ro *notmuch.DB
|
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
|
lastOpenTime time.Time
|
||||||
|
db *notmuch.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDB(path string, excludedTags []string,
|
func NewDB(path string, excludedTags []string,
|
||||||
|
@ -27,58 +31,79 @@ func NewDB(path string, excludedTags []string,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) Connect() error {
|
func (db *DB) Connect() error {
|
||||||
return db.connectRO()
|
// used as sanity check upon initial connect
|
||||||
|
err := db.connect(false)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// connectRW returns a writable notmuch DB, which needs to be closed to commit
|
func (db *DB) close() error {
|
||||||
// the changes and to release the DB lock
|
if db.db == nil {
|
||||||
func (db *DB) connectRW() (*notmuch.DB, error) {
|
return nil
|
||||||
rw, err := notmuch.Open(db.path, notmuch.DBReadWrite)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("could not connect to notmuch db: %v", err)
|
|
||||||
}
|
}
|
||||||
return rw, err
|
err := db.db.Close()
|
||||||
|
db.db = nil
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// connectRO connects a RO db to the worker
|
func (db *DB) connect(writable bool) error {
|
||||||
func (db *DB) connectRO() error {
|
var mode notmuch.DBMode = notmuch.DBReadOnly
|
||||||
if db.ro != nil {
|
if writable {
|
||||||
if err := db.ro.Close(); err != nil {
|
mode = notmuch.DBReadWrite
|
||||||
db.logger.Printf("connectRO: could not close the old db: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
db.ro, err = notmuch.Open(db.path, notmuch.DBReadOnly)
|
db.db, err = notmuch.Open(db.path, mode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not connect to notmuch db: %v", err)
|
return fmt.Errorf("could not connect to notmuch db: %v", err)
|
||||||
}
|
}
|
||||||
|
db.lastOpenTime = time.Now()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//withConnection calls callback on the DB object, cleaning up upon return.
|
||||||
|
//the error returned is from the connection attempt, if not successful,
|
||||||
|
//or from the callback otherwise.
|
||||||
|
func (db *DB) withConnection(writable bool, cb func(*notmuch.DB) error) error {
|
||||||
|
too_old := time.Now().After(db.lastOpenTime.Add(MAX_DB_AGE))
|
||||||
|
if db.db == nil || writable || too_old {
|
||||||
|
if cerr := db.close(); cerr != nil {
|
||||||
|
db.logger.Printf("failed to close the notmuch db: %v", cerr)
|
||||||
|
}
|
||||||
|
err := db.connect(writable)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err := cb(db.db)
|
||||||
|
if writable {
|
||||||
|
// we need to close to commit the changes, else we block others
|
||||||
|
if cerr := db.close(); cerr != nil {
|
||||||
|
db.logger.Printf("failed to close the notmuch db: %v", cerr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// ListTags lists all known tags
|
// ListTags lists all known tags
|
||||||
func (db *DB) ListTags() ([]string, error) {
|
func (db *DB) ListTags() ([]string, error) {
|
||||||
if db.ro == nil {
|
|
||||||
return nil, fmt.Errorf("not connected to the notmuch db")
|
|
||||||
}
|
|
||||||
tags, err := db.ro.Tags()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var result []string
|
var result []string
|
||||||
|
err := db.withConnection(false, func(ndb *notmuch.DB) error {
|
||||||
|
tags, err := ndb.Tags()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
var tag *notmuch.Tag
|
var tag *notmuch.Tag
|
||||||
for tags.Next(&tag) {
|
for tags.Next(&tag) {
|
||||||
result = append(result, tag.Value)
|
result = append(result, tag.Value)
|
||||||
}
|
}
|
||||||
return result, nil
|
return nil
|
||||||
|
})
|
||||||
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
//getQuery returns a query based on the provided query string.
|
//getQuery returns a query based on the provided query string.
|
||||||
//It also configures the query as specified on the worker
|
//It also configures the query as specified on the worker
|
||||||
func (db *DB) newQuery(query string) (*notmuch.Query, error) {
|
func (db *DB) newQuery(ndb *notmuch.DB, query string) (*notmuch.Query, error) {
|
||||||
if db.ro == nil {
|
q := ndb.NewQuery(query)
|
||||||
return nil, fmt.Errorf("not connected to the notmuch db")
|
|
||||||
}
|
|
||||||
q := db.ro.NewQuery(query)
|
|
||||||
q.SetExcludeScheme(notmuch.EXCLUDE_TRUE)
|
q.SetExcludeScheme(notmuch.EXCLUDE_TRUE)
|
||||||
q.SetSortScheme(notmuch.SORT_OLDEST_FIRST)
|
q.SetSortScheme(notmuch.SORT_OLDEST_FIRST)
|
||||||
for _, t := range db.excludedTags {
|
for _, t := range db.excludedTags {
|
||||||
|
@ -91,23 +116,23 @@ func (db *DB) newQuery(query string) (*notmuch.Query, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) MsgIDsFromQuery(q string) ([]string, error) {
|
func (db *DB) MsgIDsFromQuery(q string) ([]string, error) {
|
||||||
if db.ro == nil {
|
var msgIDs []string
|
||||||
return nil, fmt.Errorf("not connected to the notmuch db")
|
err := db.withConnection(false, func(ndb *notmuch.DB) error {
|
||||||
}
|
query, err := db.newQuery(ndb, q)
|
||||||
query, err := db.newQuery(q)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
msgs, err := query.Messages()
|
msgs, err := query.Messages()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
var msg *notmuch.Message
|
var msg *notmuch.Message
|
||||||
var msgIDs []string
|
|
||||||
for msgs.Next(&msg) {
|
for msgs.Next(&msg) {
|
||||||
msgIDs = append(msgIDs, msg.ID())
|
msgIDs = append(msgIDs, msg.ID())
|
||||||
}
|
}
|
||||||
return msgIDs, nil
|
return nil
|
||||||
|
})
|
||||||
|
return msgIDs, err
|
||||||
}
|
}
|
||||||
|
|
||||||
type MessageCount struct {
|
type MessageCount struct {
|
||||||
|
@ -116,60 +141,67 @@ type MessageCount struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) QueryCountMessages(q string) (MessageCount, error) {
|
func (db *DB) QueryCountMessages(q string) (MessageCount, error) {
|
||||||
query, err := db.newQuery(q)
|
var (
|
||||||
|
exists int
|
||||||
|
unread int
|
||||||
|
)
|
||||||
|
err := db.withConnection(false, func(ndb *notmuch.DB) error {
|
||||||
|
query, err := db.newQuery(ndb, q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return MessageCount{}, err
|
return err
|
||||||
}
|
}
|
||||||
exists := query.CountMessages()
|
exists = query.CountMessages()
|
||||||
query.Close()
|
query.Close()
|
||||||
uq, err := db.newQuery(fmt.Sprintf("(%v) and (tag:unread)", q))
|
uq, err := db.newQuery(ndb, fmt.Sprintf("(%v) and (tag:unread)", q))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return MessageCount{}, err
|
return err
|
||||||
}
|
}
|
||||||
defer uq.Close()
|
defer uq.Close()
|
||||||
unread := uq.CountMessages()
|
unread = uq.CountMessages()
|
||||||
|
return nil
|
||||||
|
})
|
||||||
return MessageCount{
|
return MessageCount{
|
||||||
Exists: exists,
|
Exists: exists,
|
||||||
Unread: unread,
|
Unread: unread,
|
||||||
}, nil
|
}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) MsgFilename(key string) (string, error) {
|
func (db *DB) MsgFilename(key string) (string, error) {
|
||||||
msg, err := db.ro.FindMessage(key)
|
var filename string
|
||||||
|
err := db.withConnection(false, func(ndb *notmuch.DB) error {
|
||||||
|
msg, err := ndb.FindMessage(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return err
|
||||||
}
|
}
|
||||||
defer msg.Close()
|
defer msg.Close()
|
||||||
return msg.Filename(), nil
|
filename = msg.Filename()
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return filename, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) MsgTags(key string) ([]string, error) {
|
func (db *DB) MsgTags(key string) ([]string, error) {
|
||||||
msg, err := db.ro.FindMessage(key)
|
var tags []string
|
||||||
|
err := db.withConnection(false, func(ndb *notmuch.DB) error {
|
||||||
|
msg, err := ndb.FindMessage(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
defer msg.Close()
|
defer msg.Close()
|
||||||
ts := msg.Tags()
|
ts := msg.Tags()
|
||||||
var tags []string
|
|
||||||
var tag *notmuch.Tag
|
var tag *notmuch.Tag
|
||||||
for ts.Next(&tag) {
|
for ts.Next(&tag) {
|
||||||
tags = append(tags, tag.Value)
|
tags = append(tags, tag.Value)
|
||||||
}
|
}
|
||||||
return tags, nil
|
return nil
|
||||||
|
})
|
||||||
|
return tags, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) msgModify(key string,
|
func (db *DB) msgModify(key string,
|
||||||
cb func(*notmuch.Message) error) error {
|
cb func(*notmuch.Message) error) error {
|
||||||
defer db.connectRO()
|
err := db.withConnection(true, func(ndb *notmuch.DB) error {
|
||||||
db.ro.Close()
|
msg, err := ndb.FindMessage(key)
|
||||||
|
|
||||||
rw, err := db.connectRW()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer rw.Close()
|
|
||||||
|
|
||||||
msg, err := rw.FindMessage(key)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -181,6 +213,8 @@ func (db *DB) msgModify(key string,
|
||||||
db.logger.Printf("could not sync maildir flags: %v", err)
|
db.logger.Printf("could not sync maildir flags: %v", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
})
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) MsgModifyTags(key string, add, remove []string) error {
|
func (db *DB) MsgModifyTags(key string, add, remove []string) error {
|
||||||
|
|
Loading…
Reference in a new issue