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"
|
2023-02-11 14:19:26 +08:00
|
|
|
"sync"
|
2022-06-27 22:09:06 +08:00
|
|
|
|
|
|
|
"github.com/usememos/memos/server/profile"
|
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 {
|
2023-05-09 09:02:59 +08:00
|
|
|
Profile *profile.Profile
|
|
|
|
db *sql.DB
|
2023-04-28 00:02:54 +08:00
|
|
|
systemSettingCache sync.Map // map[string]*systemSettingRaw
|
|
|
|
userCache sync.Map // map[int]*userRaw
|
|
|
|
userSettingCache sync.Map // map[string]*userSettingRaw
|
|
|
|
shortcutCache sync.Map // map[int]*shortcutRaw
|
|
|
|
idpCache sync.Map // map[int]*identityProviderMessage
|
2023-05-26 00:38:27 +08:00
|
|
|
resourceCache sync.Map // map[int]*resourceRaw
|
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{
|
2023-05-09 09:02:59 +08:00
|
|
|
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
|
|
|
|
2023-06-17 21:25:46 +08:00
|
|
|
func (s *Store) GetDB() *sql.DB {
|
|
|
|
return s.db
|
|
|
|
}
|
|
|
|
|
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 FormatError(err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-05-09 09:02:59 +08:00
|
|
|
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 FormatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vacuum sqlite database file size after deleting resource.
|
|
|
|
if _, err := s.db.Exec("VACUUM"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-20 10:16:19 +08:00
|
|
|
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 := vacuumShortcut(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 {
|
2022-12-21 19:22:32 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-05-20 10:16:19 +08:00
|
|
|
if err := vacuumMemoRelations(ctx, tx); err != nil {
|
|
|
|
return err
|
2023-04-25 23:27:38 +08:00
|
|
|
}
|
2022-12-21 19:22:32 +08:00
|
|
|
if err := vacuumTag(ctx, tx); err != nil {
|
2022-11-06 12:21:58 +08:00
|
|
|
// Prevent revive warning.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|