-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
148 lines (109 loc) · 2.55 KB
/
command.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
143
144
145
146
147
package Orangedb
import (
"github.com/laohanlinux/go-logger/logger"
)
type CmdType uint8
const (
// LogCommand is applied to a user FSM.
ApplyCommand CmdType = iota
BatchSubCommand
)
type applyCommand struct {
Op string `json:"op,omitempty"`
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
type batchSubCommand struct {
BatchTxn *BatchTxn
}
type Command struct {
// Type holds the type of the log entry.
Type CmdType
// Data holds the log entry's type-specific data.
Data []byte
}
type CommandFunc func(c *RespClient) error
var txnPushFuncMap = map[string]CommandFunc{}
func DoGet(c *RespClient) error {
if len(c.Args) < 1 {
return ErrInvalidArgument
}
txn := NewTxn(c)
txn.AddReadSet(string(c.Args[0]))
logger.Debugf("get a key [%s]", string(c.Args[0]))
c.Service.PushTxnTask(txn)
return nil
}
func DoSet(c *RespClient) error {
if len(c.Args) < 1 {
return ErrInvalidArgument
}
txn := NewTxn(c)
txn.AddWriteSet(string(c.Args[0]))
logger.Debugf("set a key", string(c.Args[0]))
c.Service.PushTxnTask(txn)
c.respWriter.writeStatus(ResponseOK)
c.respWriter.flush()
return nil
}
func DoDel(c *RespClient) error {
if len(c.Args) < 1 {
return ErrInvalidArgument
}
txn := NewTxn(c)
txn.AddWriteSet(string(c.Args[0]))
logger.Debugf("delete a key", string(c.Args[0]))
c.Service.PushTxnTask(txn)
c.respWriter.writeStatus(ResponseOK)
c.respWriter.flush()
return nil
}
func DoMget(c *RespClient) error{
if len(c.Args) < 1 {
return ErrInvalidArgument
}
txn := NewTxn(c)
for _,arg := range c.Args{
txn.AddReadSet(string(arg))
}
logger.Debugf("get keys array [%v]",txn.ReadSet)
c.Service.PushTxnTask(txn)
return nil
}
func DoMset(c *RespClient) error{
if len(c.Args) < 2 {
return ErrInvalidArgument
}
txn := NewTxn(c)
for index:=0; index <len(c.Args);index+=2{
txn.AddWriteSet(string(c.Args[index]))
}
logger.Debugf("get set key array [%v]",txn.WriteSet)
c.Service.PushTxnTask(txn)
c.respWriter.writeStatus(ResponseOK)
c.respWriter.flush()
return nil
}
/*@berif 注册各种回调函数*/
func Register() {
txnPushFuncMap["get"] = DoGet
txnPushFuncMap["set"] = DoSet
txnPushFuncMap["del"] = DoDel
txnPushFuncMap["mget"] = DoMget
txnPushFuncMap["mset"] = DoMset
}
type TransBatchTxnRequest struct {
SourceAddr string
TransBatch *BatchTxn
}
type TransBatchTxnResponse struct {
Success bool
}
type RetReadResultRequest struct {
TxnId int
ReadKey [] string
ReadResult [] string //和 readkey 一一对应
}
type RetReadResultResponse struct {
Ok bool
}