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

     1  package revisions
     2  
     3  import (
     4  	"github.com/authzed/spicedb/pkg/datastore"
     5  	"github.com/authzed/spicedb/pkg/spiceerrors"
     6  )
     7  
     8  // RevisionKind is an enum of the different kinds of revisions that can be used.
     9  type RevisionKind string
    10  
    11  const (
    12  	// Timestamp is a revision that is a timestamp.
    13  	Timestamp RevisionKind = "timestamp"
    14  
    15  	// TransactionID is a revision that is a transaction ID.
    16  	TransactionID = "txid"
    17  
    18  	// HybridLogicalClock is a revision that is a hybrid logical clock.
    19  	HybridLogicalClock = "hlc"
    20  )
    21  
    22  // ParsingFunc is a function that can parse a string into a revision.
    23  type ParsingFunc func(revisionStr string) (rev datastore.Revision, err error)
    24  
    25  // RevisionParser returns a ParsingFunc for the given RevisionKind.
    26  func RevisionParser(kind RevisionKind) ParsingFunc {
    27  	switch kind {
    28  	case TransactionID:
    29  		return parseTransactionIDRevisionString
    30  
    31  	case Timestamp:
    32  		return parseTimestampRevisionString
    33  
    34  	case HybridLogicalClock:
    35  		return parseHLCRevisionString
    36  
    37  	default:
    38  		return func(revisionStr string) (rev datastore.Revision, err error) {
    39  			return nil, spiceerrors.MustBugf("unknown revision kind: %v", kind)
    40  		}
    41  	}
    42  }
    43  
    44  // CommonDecoder is a revision decoder that can decode revisions of a given kind.
    45  type CommonDecoder struct {
    46  	Kind RevisionKind
    47  }
    48  
    49  func (cd CommonDecoder) RevisionFromString(s string) (datastore.Revision, error) {
    50  	switch cd.Kind {
    51  	case TransactionID:
    52  		return parseTransactionIDRevisionString(s)
    53  
    54  	case Timestamp:
    55  		return parseTimestampRevisionString(s)
    56  
    57  	case HybridLogicalClock:
    58  		return parseHLCRevisionString(s)
    59  
    60  	default:
    61  		return nil, spiceerrors.MustBugf("unknown revision kind in decoder: %v", cd.Kind)
    62  	}
    63  }
    64  
    65  // WithInexactFloat64 is an interface that can be implemented by a revision to
    66  // provide an inexact float64 representation of the revision.
    67  type WithInexactFloat64 interface {
    68  	// InexactFloat64 returns a float64 that is an inexact representation of the
    69  	// revision.
    70  	InexactFloat64() float64
    71  }
    72  
    73  // WithTimestampRevision is an interface that can be implemented by a revision to
    74  // provide a timestamp.
    75  type WithTimestampRevision interface {
    76  	datastore.Revision
    77  	TimestampNanoSec() int64
    78  	ConstructForTimestamp(timestampNanoSec int64) WithTimestampRevision
    79  }
    80  
    81  // WithIntegerRepresentation is an interface that can be implemented by a revision to
    82  // provide an integer representation of the revision.
    83  type WithIntegerRepresentation interface {
    84  	datastore.Revision
    85  	IntegerRepresentation() (int64, uint32)
    86  }