github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/datastore/memdb/stats.go (about)

     1  package memdb
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/authzed/spicedb/pkg/datastore"
     8  )
     9  
    10  func (mdb *memdbDatastore) Statistics(ctx context.Context) (datastore.Stats, error) {
    11  	head, err := mdb.HeadRevision(ctx)
    12  	if err != nil {
    13  		return datastore.Stats{}, fmt.Errorf("unable to compute head revision: %w", err)
    14  	}
    15  
    16  	count, err := mdb.countRelationships(ctx)
    17  	if err != nil {
    18  		return datastore.Stats{}, fmt.Errorf("unable to count relationships: %w", err)
    19  	}
    20  
    21  	objTypes, err := mdb.SnapshotReader(head).ListAllNamespaces(ctx)
    22  	if err != nil {
    23  		return datastore.Stats{}, fmt.Errorf("unable to list object types: %w", err)
    24  	}
    25  
    26  	return datastore.Stats{
    27  		UniqueID:                   mdb.uniqueID,
    28  		EstimatedRelationshipCount: count,
    29  		ObjectTypeStatistics:       datastore.ComputeObjectTypeStats(objTypes),
    30  	}, nil
    31  }
    32  
    33  func (mdb *memdbDatastore) countRelationships(_ context.Context) (uint64, error) {
    34  	mdb.RLock()
    35  	defer mdb.RUnlock()
    36  
    37  	txn := mdb.db.Txn(false)
    38  	defer txn.Abort()
    39  
    40  	it, err := txn.LowerBound(tableRelationship, indexID)
    41  	if err != nil {
    42  		return 0, err
    43  	}
    44  
    45  	var count uint64
    46  	for row := it.Next(); row != nil; row = it.Next() {
    47  		count++
    48  	}
    49  
    50  	return count, nil
    51  }