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

     1  package revisions
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/authzed/spicedb/pkg/datastore"
     8  )
     9  
    10  // TransactionIDRevision is a revision that is a transaction ID.
    11  type TransactionIDRevision uint64
    12  
    13  // NewForTransactionID creates a new revision for the given transaction ID.
    14  func NewForTransactionID(transactionID uint64) TransactionIDRevision {
    15  	return TransactionIDRevision(transactionID)
    16  }
    17  
    18  // parseTransactionIDRevisionString parses a string into a transaction ID revision.
    19  func parseTransactionIDRevisionString(revisionStr string) (rev datastore.Revision, err error) {
    20  	parsed, err := strconv.ParseUint(revisionStr, 10, 64)
    21  	if err != nil {
    22  		return nil, fmt.Errorf("invalid integer revision: %w", err)
    23  	}
    24  
    25  	return TransactionIDRevision(parsed), nil
    26  }
    27  
    28  func (ir TransactionIDRevision) Equal(other datastore.Revision) bool {
    29  	return uint64(ir) == uint64(other.(TransactionIDRevision))
    30  }
    31  
    32  func (ir TransactionIDRevision) GreaterThan(other datastore.Revision) bool {
    33  	return uint64(ir) > uint64(other.(TransactionIDRevision))
    34  }
    35  
    36  func (ir TransactionIDRevision) LessThan(other datastore.Revision) bool {
    37  	return uint64(ir) < uint64(other.(TransactionIDRevision))
    38  }
    39  
    40  func (ir TransactionIDRevision) TransactionID() uint64 {
    41  	return uint64(ir)
    42  }
    43  
    44  func (ir TransactionIDRevision) String() string {
    45  	return strconv.FormatInt(int64(ir), 10)
    46  }
    47  
    48  func (ir TransactionIDRevision) WithInexactFloat64() float64 {
    49  	return float64(ir)
    50  }
    51  
    52  func (ir TransactionIDRevision) IntegerRepresentation() (int64, uint32) {
    53  	return int64(ir), 0
    54  }
    55  
    56  var _ datastore.Revision = TransactionIDRevision(0)
    57  
    58  // TransactionIDKeyFunc is used to create keys for transaction IDs.
    59  func TransactionIDKeyFunc(r TransactionIDRevision) uint64 {
    60  	return uint64(r)
    61  }
    62  
    63  // TransactionIDKeyLessThanFunc is used to create keys for transaction IDs.
    64  func TransactionIDKeyLessThanFunc(l, r uint64) bool {
    65  	return l < r
    66  }