memos/store/store.go

121 lines
2.4 KiB
Go
Raw Normal View History

2022-05-16 07:37:23 +08:00
package store
2022-05-22 00:59:22 +08:00
import (
2022-11-06 12:21:58 +08:00
"context"
2022-05-22 00:59:22 +08:00
"database/sql"
"fmt"
"sync"
2022-06-27 22:09:06 +08:00
"github.com/usememos/memos/server/profile"
"modernc.org/sqlite"
2022-05-22 00:59:22 +08:00
)
2022-08-24 21:53:12 +08:00
// Store provides database access to all raw objects.
2022-05-16 07:37:23 +08:00
type Store struct {
Profile *profile.Profile
db *sql.DB
systemSettingCache sync.Map // map[string]*SystemSetting
userCache sync.Map // map[int]*User
2023-06-29 22:55:03 +08:00
userSettingCache sync.Map // map[string]*UserSetting
2023-06-26 23:46:01 +08:00
idpCache sync.Map // map[int]*IdentityProvider
2022-05-16 07:37:23 +08:00
}
2022-08-24 21:53:12 +08:00
// New creates a new instance of Store.
2022-05-22 09:29:34 +08:00
func New(db *sql.DB, profile *profile.Profile) *Store {
2022-05-16 07:37:23 +08:00
return &Store{
Profile: profile,
2022-05-22 00:59:22 +08:00
db: db,
2022-05-16 07:37:23 +08:00
}
}
2022-11-06 12:21:58 +08:00
func (s *Store) GetDB() *sql.DB {
return s.db
}
func (s *Store) BackupTo(ctx context.Context, filename string) error {
conn, err := s.db.Conn(ctx)
if err != nil {
return fmt.Errorf("fail to get conn %s", err)
}
defer conn.Close()
err = conn.Raw(func(driverConn any) error {
type backuper interface {
NewBackup(string) (*sqlite.Backup, error)
}
backupConn, ok := driverConn.(backuper)
if !ok {
return fmt.Errorf("db connection is not a sqlite backuper")
}
bck, err := backupConn.NewBackup(filename)
if err != nil {
return fmt.Errorf("fail to create sqlite backup %s", err)
}
for more := true; more; {
more, err = bck.Step(-1)
if err != nil {
return fmt.Errorf("fail to execute sqlite backup %s", err)
}
}
return bck.Finish()
})
if err != nil {
return fmt.Errorf("fail to backup %s", err)
}
return nil
}
2022-11-06 12:21:58 +08:00
func (s *Store) Vacuum(ctx context.Context) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
2022-11-06 12:21:58 +08:00
}
defer tx.Rollback()
if err := s.vacuumImpl(ctx, tx); err != nil {
2022-11-06 12:21:58 +08:00
return err
}
if err := tx.Commit(); err != nil {
return err
2022-11-06 12:21:58 +08:00
}
// Vacuum sqlite database file size after deleting resource.
if _, err := s.db.Exec("VACUUM"); err != nil {
return err
}
return nil
}
func (*Store) vacuumImpl(ctx context.Context, tx *sql.Tx) error {
2022-11-06 12:21:58 +08:00
if err := vacuumMemo(ctx, tx); err != nil {
return err
}
if err := vacuumResource(ctx, tx); err != nil {
return err
}
if err := vacuumUserSetting(ctx, tx); err != nil {
return err
}
if err := vacuumMemoOrganizer(ctx, tx); err != nil {
return err
}
if err := vacuumMemoResource(ctx, tx); err != nil {
return err
}
if err := vacuumMemoRelations(ctx, tx); err != nil {
return err
}
if err := vacuumTag(ctx, tx); err != nil {
2022-11-06 12:21:58 +08:00
// Prevent revive warning.
return err
}
return nil
}