Skip to content

Commit c1edece

Browse files
committed
Call wal_checkpoint during startup/compact for sqlite
Signed-off-by: Brad Davidson <[email protected]>
1 parent 92fcaea commit c1edece

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

pkg/drivers/generic/generic.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ type Generic struct {
9494
DeleteSQL string
9595
CompactSQL string
9696
UpdateCompactSQL string
97+
PostCompactSQL string
9798
InsertSQL string
9899
FillSQL string
99100
InsertLastInsertIDSQL string
@@ -297,6 +298,15 @@ func (d *Generic) Compact(ctx context.Context, revision int64) (int64, error) {
297298
return res.RowsAffected()
298299
}
299300

301+
func (d *Generic) PostCompact(ctx context.Context) error {
302+
logrus.Trace("POSTCOMPACT")
303+
if d.PostCompactSQL != "" {
304+
_, err := d.execute(ctx, d.PostCompactSQL)
305+
return err
306+
}
307+
return nil
308+
}
309+
300310
func (d *Generic) GetRevision(ctx context.Context, revision int64) (*sql.Rows, error) {
301311
return d.query(ctx, d.GetRevisionSQL, revision)
302312
}

pkg/drivers/sqlite/sqlite.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var (
3939
`CREATE INDEX IF NOT EXISTS kine_id_deleted_index ON kine (id,deleted)`,
4040
`CREATE INDEX IF NOT EXISTS kine_prev_revision_index ON kine (prev_revision)`,
4141
`CREATE UNIQUE INDEX IF NOT EXISTS kine_name_prev_revision_uindex ON kine (name, prev_revision)`,
42+
`PRAGMA wal_checkpoint(TRUNCATE)`,
4243
}
4344
)
4445

@@ -79,6 +80,7 @@ func NewVariant(ctx context.Context, driverName, dataSourceName string, connPool
7980
kd.deleted != 0 AND
8081
kd.id <= ?
8182
)`
83+
dialect.PostCompactSQL = `PRAGMA wal_checkpoint(FULL)`
8284
dialect.TranslateErr = func(err error) error {
8385
if err, ok := err.(sqlite3.Error); ok && err.ExtendedCode == sqlite3.ErrConstraintUnique {
8486
return server.ErrKeyExists

pkg/logstructured/sqllog/sql.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ outer:
155155
}
156156
}
157157

158+
if err := s.postCompact(); err != nil {
159+
logrus.Errorf("Post-compact operations failed: %v", err)
160+
}
161+
158162
// Record the final results for the outer loop
159163
compactRev = compactedRev
160164
targetCompactRev = currentRev
@@ -218,6 +222,11 @@ func (s *SQLLog) compact(compactRev int64, targetCompactRev int64) (int64, int64
218222
return targetCompactRev, currentRev, nil
219223
}
220224

225+
// postCompact executes any post-compact database cleanup - vacuuming, WAL truncate, etc.
226+
func (s *SQLLog) postCompact() error {
227+
return s.d.PostCompact(s.ctx)
228+
}
229+
221230
func (s *SQLLog) CurrentRevision(ctx context.Context) (int64, error) {
222231
return s.d.CurrentRevision(ctx)
223232
}

pkg/server/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Dialect interface {
3636
GetCompactRevision(ctx context.Context) (int64, error)
3737
SetCompactRevision(ctx context.Context, revision int64) error
3838
Compact(ctx context.Context, revision int64) (int64, error)
39+
PostCompact(ctx context.Context) error
3940
Fill(ctx context.Context, revision int64) error
4041
IsFill(key string) bool
4142
BeginTx(ctx context.Context, opts *sql.TxOptions) (Transaction, error)

0 commit comments

Comments
 (0)