2022-05-16 02:37:23 +03:00
|
|
|
package store
|
|
|
|
|
2022-05-21 19:59:22 +03:00
|
|
|
import (
|
2022-11-06 07:21:58 +03:00
|
|
|
"context"
|
2022-05-21 19:59:22 +03:00
|
|
|
"database/sql"
|
2023-02-11 09:19:26 +03:00
|
|
|
"sync"
|
2022-06-27 17:09:06 +03:00
|
|
|
|
|
|
|
"github.com/usememos/memos/server/profile"
|
2022-05-21 19:59:22 +03:00
|
|
|
)
|
|
|
|
|
2022-08-24 16:53:12 +03:00
|
|
|
// Store provides database access to all raw objects.
|
2022-05-16 02:37:23 +03:00
|
|
|
type Store struct {
|
2022-05-21 19:59:22 +03:00
|
|
|
db *sql.DB
|
2022-05-22 04:29:34 +03:00
|
|
|
profile *profile.Profile
|
2023-02-11 09:19:26 +03:00
|
|
|
|
2023-04-27 19:02:54 +03:00
|
|
|
systemSettingCache sync.Map // map[string]*systemSettingRaw
|
|
|
|
userCache sync.Map // map[int]*userRaw
|
|
|
|
userSettingCache sync.Map // map[string]*userSettingRaw
|
|
|
|
memoCache sync.Map // map[int]*memoRaw
|
|
|
|
shortcutCache sync.Map // map[int]*shortcutRaw
|
|
|
|
idpCache sync.Map // map[int]*identityProviderMessage
|
2022-05-16 02:37:23 +03:00
|
|
|
}
|
|
|
|
|
2022-08-24 16:53:12 +03:00
|
|
|
// New creates a new instance of Store.
|
2022-05-22 04:29:34 +03:00
|
|
|
func New(db *sql.DB, profile *profile.Profile) *Store {
|
2022-05-16 02:37:23 +03:00
|
|
|
return &Store{
|
2022-05-21 19:59:22 +03:00
|
|
|
db: db,
|
|
|
|
profile: profile,
|
2022-05-16 02:37:23 +03:00
|
|
|
}
|
|
|
|
}
|
2022-11-06 07:21:58 +03: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()
|
|
|
|
|
|
|
|
if err := vacuum(ctx, tx); err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-26 09:23:29 +03:00
|
|
|
// Exec vacuum records in a transaction.
|
2022-11-06 07:21:58 +03:00
|
|
|
func vacuum(ctx context.Context, tx *sql.Tx) error {
|
|
|
|
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 14:22:32 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-04-25 18:27:38 +03:00
|
|
|
if err := vacuumMemoRelations(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-21 14:22:32 +03:00
|
|
|
if err := vacuumTag(ctx, tx); err != nil {
|
2022-11-06 07:21:58 +03:00
|
|
|
// Prevent revive warning.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|