summaryrefslogtreecommitdiff
path: root/vendor/github.com/authzed/spicedb/internal/datastore/revisions/commonrevision.go
blob: 709272818e36e025b2bd55d030ef3e3f33b75600 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package revisions

import (
	"github.com/authzed/spicedb/pkg/datastore"
	"github.com/authzed/spicedb/pkg/spiceerrors"
)

// RevisionKind is an enum of the different kinds of revisions that can be used.
type RevisionKind string

const (
	// Timestamp is a revision that is a timestamp.
	Timestamp RevisionKind = "timestamp"

	// TransactionID is a revision that is a transaction ID.
	TransactionID = "txid"

	// HybridLogicalClock is a revision that is a hybrid logical clock.
	HybridLogicalClock = "hlc"
)

// ParsingFunc is a function that can parse a string into a revision.
type ParsingFunc func(revisionStr string) (rev datastore.Revision, err error)

// RevisionParser returns a ParsingFunc for the given RevisionKind.
func RevisionParser(kind RevisionKind) ParsingFunc {
	switch kind {
	case TransactionID:
		return parseTransactionIDRevisionString

	case Timestamp:
		return parseTimestampRevisionString

	case HybridLogicalClock:
		return parseHLCRevisionString

	default:
		return func(revisionStr string) (rev datastore.Revision, err error) {
			return nil, spiceerrors.MustBugf("unknown revision kind: %v", kind)
		}
	}
}

// CommonDecoder is a revision decoder that can decode revisions of a given kind.
type CommonDecoder struct {
	Kind RevisionKind
}

func (cd CommonDecoder) RevisionFromString(s string) (datastore.Revision, error) {
	switch cd.Kind {
	case TransactionID:
		return parseTransactionIDRevisionString(s)

	case Timestamp:
		return parseTimestampRevisionString(s)

	case HybridLogicalClock:
		return parseHLCRevisionString(s)

	default:
		return nil, spiceerrors.MustBugf("unknown revision kind in decoder: %v", cd.Kind)
	}
}

// WithInexactFloat64 is an interface that can be implemented by a revision to
// provide an inexact float64 representation of the revision.
type WithInexactFloat64 interface {
	// InexactFloat64 returns a float64 that is an inexact representation of the
	// revision.
	InexactFloat64() float64
}

// WithTimestampRevision is an interface that can be implemented by a revision to
// provide a timestamp.
type WithTimestampRevision interface {
	datastore.Revision
	TimestampNanoSec() int64
	ConstructForTimestamp(timestampNanoSec int64) WithTimestampRevision
}