/
basic.go
69 lines (54 loc) · 1.32 KB
/
basic.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
package kvproc
import (
"time"
"github.com/couchbaselabs/gocaves/mock/mockdb"
)
// Engine represents a specific engine.
type Engine struct {
db *mockdb.Bucket
vbOwnership []int
}
// New creates a new crudproc engine using a mockdb and a list of what replicas
// are owned by this particular engine.
func New(db *mockdb.Bucket, vbOwnership []int) *Engine {
return &Engine{
db: db,
vbOwnership: vbOwnership,
}
}
func (e *Engine) findReplicaIdx(vbIdx uint) int {
if vbIdx >= uint(len(e.vbOwnership)) {
return -1
}
return e.vbOwnership[vbIdx]
}
func (e *Engine) confirmIsMaster(vbIdx uint) error {
repIdx := e.findReplicaIdx(vbIdx)
if repIdx != 0 {
return ErrNotMyVbucket
}
return nil
}
func (e *Engine) docIsLocked(doc *mockdb.Document) bool {
if doc == nil {
return false
}
if doc.LockExpiry.IsZero() {
return false
}
return e.db.Chrono().Now().Before(doc.LockExpiry)
}
func (e *Engine) parseExpiry(expiry uint32) time.Time {
if expiry == 0 {
return time.Time{}
}
// TODO(brett19): Check if this is the right edge for expiry.
if expiry > 30*24*60*60 {
return time.Unix(int64(expiry), 0).Add(e.db.Chrono().TimeShift())
}
expiryDura := time.Duration(expiry) * time.Second
return e.db.Chrono().Now().Add(expiryDura)
}
func (e *Engine) HLC() time.Time {
return e.db.Chrono().Now()
}