update to latest bstore (with support for an index on a []string: Message.DKIMDomains), and cyclic data types (to be used for Message.Part soon); also adds a context.Context to database operations.

This commit is contained in:
Mechiel Lukkien
2023-05-22 14:40:36 +02:00
parent f6ed860ccb
commit e81930ba20
58 changed files with 1970 additions and 1035 deletions

View File

@ -61,13 +61,13 @@ type TLSReportRecord struct {
Report tlsrpt.Report
}
func database() (rdb *bstore.DB, rerr error) {
func database(ctx context.Context) (rdb *bstore.DB, rerr error) {
mutex.Lock()
defer mutex.Unlock()
if tlsrptDB == nil {
p := mox.DataDirPath("tlsrpt.db")
os.MkdirAll(filepath.Dir(p), 0770)
db, err := bstore.Open(p, &bstore.Options{Timeout: 5 * time.Second, Perm: 0660}, TLSReportRecord{})
db, err := bstore.Open(ctx, p, &bstore.Options{Timeout: 5 * time.Second, Perm: 0660}, TLSReportRecord{})
if err != nil {
return nil, err
}
@ -78,7 +78,7 @@ func database() (rdb *bstore.DB, rerr error) {
// Init opens and possibly initializes the database.
func Init() error {
_, err := database()
_, err := database(mox.Shutdown)
return err
}
@ -106,7 +106,7 @@ func Close() {
func AddReport(ctx context.Context, verifiedFromDomain dns.Domain, mailFrom string, r *tlsrpt.Report) error {
log := xlog.WithContext(ctx)
db, err := database()
db, err := database(ctx)
if err != nil {
return err
}
@ -149,39 +149,39 @@ func AddReport(ctx context.Context, verifiedFromDomain dns.Domain, mailFrom stri
}
}
record.Domain = reportdom.Name()
return db.Insert(&record)
return db.Insert(ctx, &record)
}
// Records returns all TLS reports in the database.
func Records(ctx context.Context) ([]TLSReportRecord, error) {
db, err := database()
db, err := database(ctx)
if err != nil {
return nil, err
}
return bstore.QueryDB[TLSReportRecord](db).List()
return bstore.QueryDB[TLSReportRecord](ctx, db).List()
}
// RecordID returns the report for the ID.
func RecordID(ctx context.Context, id int64) (TLSReportRecord, error) {
db, err := database()
db, err := database(ctx)
if err != nil {
return TLSReportRecord{}, err
}
e := TLSReportRecord{ID: id}
err = db.Get(&e)
err = db.Get(ctx, &e)
return e, err
}
// RecordsPeriodDomain returns the reports overlapping start and end, for the given
// domain. If domain is empty, all records match for domain.
func RecordsPeriodDomain(ctx context.Context, start, end time.Time, domain string) ([]TLSReportRecord, error) {
db, err := database()
db, err := database(ctx)
if err != nil {
return nil, err
}
q := bstore.QueryDB[TLSReportRecord](db)
q := bstore.QueryDB[TLSReportRecord](ctx, db)
if domain != "" {
q.FilterNonzero(TLSReportRecord{Domain: domain})
}