-
Notifications
You must be signed in to change notification settings - Fork 246
/
stats.go
51 lines (40 loc) · 1.15 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
50
51
package memdb
import (
"context"
"fmt"
"github.com/authzed/spicedb/pkg/datastore"
)
func (mdb *memdbDatastore) Statistics(ctx context.Context) (datastore.Stats, error) {
head, err := mdb.HeadRevision(ctx)
if err != nil {
return datastore.Stats{}, fmt.Errorf("unable to compute head revision: %w", err)
}
count, err := mdb.countRelationships(ctx)
if err != nil {
return datastore.Stats{}, fmt.Errorf("unable to count relationships: %w", err)
}
objTypes, err := mdb.SnapshotReader(head).ListAllNamespaces(ctx)
if err != nil {
return datastore.Stats{}, fmt.Errorf("unable to list object types: %w", err)
}
return datastore.Stats{
UniqueID: mdb.uniqueID,
EstimatedRelationshipCount: count,
ObjectTypeStatistics: datastore.ComputeObjectTypeStats(objTypes),
}, nil
}
func (mdb *memdbDatastore) countRelationships(_ context.Context) (uint64, error) {
mdb.RLock()
defer mdb.RUnlock()
txn := mdb.db.Txn(false)
defer txn.Abort()
it, err := txn.LowerBound(tableRelationship, indexID)
if err != nil {
return 0, err
}
var count uint64
for row := it.Next(); row != nil; row = it.Next() {
count++
}
return count, nil
}