-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
88 lines (75 loc) · 2.46 KB
/
db.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
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
80
81
82
83
84
85
86
87
88
package bitempura
import (
"time"
)
// DB is a key-value database for bitemporal data.
//
// Temporal control options.
// ReadOpt's: AsOfValidTime, AsOfTransactionTime.
// WriteOpt's: WithValidTime, WithEndValidTime.
type DB interface {
// Get data by key (as of optional valid and transaction times).
Get(key string, opts ...ReadOpt) (*VersionedKV, error)
// List all data (as of optional valid and transaction times).
List(opts ...ReadOpt) ([]*VersionedKV, error)
// Set stores value (with optional start and end valid time).
Set(key string, value Value, opts ...WriteOpt) error
// Delete removes value (with optional start and end valid time).
Delete(key string, opts ...WriteOpt) error
// History returns all versioned key-values for key by descending end transaction time, descending end valid time.
History(key string) ([]*VersionedKV, error)
}
// WriteOptions is a struct for processing WriteOpt's specified on writes.
type WriteOptions struct {
ValidTime *time.Time
EndValidTime *time.Time
}
// ApplyWriteOpts applies WriteOpt's to a WriteOptions struct for usage by the DB.
func ApplyWriteOpts(opts []WriteOpt) *WriteOptions {
os := &WriteOptions{}
for _, opt := range opts {
opt(os)
}
return os
}
// WriteOpt is an option for database writes
type WriteOpt func(*WriteOptions)
// WithValidTime allows writer to configure explicit valid time. Valid times cannot be set in the future.
func WithValidTime(t time.Time) WriteOpt {
return func(os *WriteOptions) {
os.ValidTime = &t
}
}
// WithEndValidTime allows writer to configure explicit end valid time. Valid times cannot be set in the future.
func WithEndValidTime(t time.Time) WriteOpt {
return func(os *WriteOptions) {
os.EndValidTime = &t
}
}
// ReadOptions is a struct for processing ReadOpt's specified on reads.
type ReadOptions struct {
ValidTime *time.Time
TxTime *time.Time
}
// ApplyReadOpts applies ReadOpt's to a ReadOptions struct for usage by the DB.
func ApplyReadOpts(opts []ReadOpt) *ReadOptions {
os := &ReadOptions{}
for _, opt := range opts {
opt(os)
}
return os
}
// ReadOpt is an option for database reads
type ReadOpt func(*ReadOptions)
// AsOfValidTime allows reader to read as of a specified valid time
func AsOfValidTime(t time.Time) ReadOpt {
return func(os *ReadOptions) {
os.ValidTime = &t
}
}
// AsOfTransactionTime allows reader to read as of a specified transaction time
func AsOfTransactionTime(t time.Time) ReadOpt {
return func(os *ReadOptions) {
os.TxTime = &t
}
}