-
Notifications
You must be signed in to change notification settings - Fork 387
/
stats.go
49 lines (40 loc) · 1.43 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package metabase
import (
"context"
"database/sql"
"errors"
"time"
"storj.io/private/dbutil"
)
const statsUpToDateThreshold = 8 * time.Hour
// GetTableStats contains arguments necessary for getting table statistics.
type GetTableStats struct {
AsOfSystemInterval time.Duration
}
// TableStats contains information about the metabase status.
type TableStats struct {
SegmentCount int64
}
// GetTableStats gathers information about the metabase tables, currently only "segments" table.
func (db *DB) GetTableStats(ctx context.Context, opts GetTableStats) (result TableStats, err error) {
defer mon.Task()(&ctx)(&err)
// if it's cockroach and statistics are up to date we will use them to get segments count
if db.impl == dbutil.Cockroach {
var created time.Time
err := db.db.QueryRowContext(ctx, `WITH stats AS (SHOW STATISTICS FOR TABLE segments) SELECT row_count, created FROM stats ORDER BY created DESC LIMIT 1`).
Scan(&result.SegmentCount, &created)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return TableStats{}, err
}
if !created.IsZero() && statsUpToDateThreshold > time.Since(created) {
return result, nil
}
}
err = db.db.QueryRowContext(ctx, `SELECT count(*) FROM segments `+db.impl.AsOfSystemInterval(opts.AsOfSystemInterval)).Scan(&result.SegmentCount)
if err != nil {
return TableStats{}, err
}
return result, nil
}