-
Notifications
You must be signed in to change notification settings - Fork 2
/
option.go
142 lines (112 loc) · 2.1 KB
/
option.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package mossdb
import (
"time"
)
type OpType uint16
const (
GetOp OpType = iota + 1
ModifyOp
DeleteOp
WatchOP
ListOp
)
var opMap = map[OpType]string{
GetOp: "GET",
ModifyOp: "MODIFY",
DeleteOp: "DELETE",
WatchOP: "WATCH",
}
func (op OpType) String() string {
return opMap[op]
}
type Op struct {
op OpType
key string
end string
val Val
ttl int64
prefix bool
all bool
msg string
createdNotify bool
updateNotify bool
deleteNotify bool
eventBuffSize int
}
func NewOption(op OpType, key string, val Val, opts ...Option) *Op {
opt := &Op{key: key, op: op, val: val}
for _, f := range opts {
f(opt)
}
return opt
}
func getOption(key string, opts ...Option) Op {
return *NewOption(GetOp, key, nil, opts...)
}
func listOption(opts ...Option) Op {
return *NewOption(ListOp, "", nil, opts...)
}
func setOption(key string, val Val, opts ...Option) *Op {
return NewOption(ModifyOp, key, val, opts...)
}
func deleteOption(key string, opts ...Option) *Op {
return NewOption(DeleteOp, key, nil, opts...)
}
func watchOption(key string, opts ...Option) *Op {
return NewOption(WatchOP, key, nil, opts...)
}
func (op *Op) IsGet() bool {
return op.op == GetOp
}
func (op *Op) IsMutate() bool {
return op.op == ModifyOp || op.op == DeleteOp
}
type Option func(*Op)
func WithTTL(ttl time.Duration) Option {
return func(opt *Op) {
opt.ttl = time.Now().Add(ttl).UnixNano()
}
}
func WithPrefix() Option {
return func(opt *Op) {
opt.prefix = true
}
}
func WithPrefixKey(key string) Option {
return func(opt *Op) {
opt.key = key
opt.prefix = true
}
}
func WithAll() Option {
return func(opt *Op) {
opt.all = true
}
}
func WithCreateNotify() Option {
return func(opt *Op) {
opt.createdNotify = true
}
}
func WithUpdateNotify() Option {
return func(opt *Op) {
opt.updateNotify = true
}
}
func WithDeleteNotify() Option {
return func(opt *Op) {
opt.deleteNotify = true
}
}
func WithMsg(msg string) Option {
return func(opt *Op) {
opt.msg = msg
}
}
func WithWatchEventBuffSize(size int) Option {
return func(opt *Op) {
if size > 0 {
opt.eventBuffSize = size
}
}
}