-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssdb.go
188 lines (170 loc) · 3.46 KB
/
ssdb.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package ssdb
import (
"bytes"
"fmt"
"net"
"strconv"
)
type Client struct {
sock *net.TCPConn
recv_buf bytes.Buffer
}
func Connect(ip string, port int) (*Client, error) {
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", ip, port))
if err != nil {
return nil, err
}
sock, err := net.DialTCP("tcp", nil, addr)
if err != nil {
return nil, err
}
var c Client
c.sock = sock
return &c, nil
}
func (c *Client) Do(args ...interface{}) ([]string, error) {
err := c.send(args)
if err != nil {
return nil, err
}
resp, err := c.recv()
return resp, err
}
func (c *Client) Set(key string, val string) (interface{}, error) {
resp, err := c.Do("set", key, val)
if err != nil {
return nil, err
}
if len(resp) == 2 && resp[0] == "ok" {
return true, nil
}
return nil, fmt.Errorf("bad response")
}
// TODO: Will somebody write addition semantic methods?
func (c *Client) Get(key string) (interface{}, error) {
resp, err := c.Do("get", key)
if err != nil {
return nil, err
}
if len(resp) == 2 && resp[0] == "ok" {
return resp[1], nil
}
if resp[0] == "not_found" {
return nil, nil
}
return nil, fmt.Errorf("bad response")
}
func (c *Client) Del(key string) (interface{}, error) {
resp, err := c.Do("del", key)
if err != nil {
return nil, err
}
//response looks like this: [ok 1]
if len(resp) > 0 && resp[0] == "ok" {
return true, nil
}
return nil, fmt.Errorf("bad response:resp:%v:", resp)
}
func (c *Client) Send(args ...interface{}) error {
return c.send(args)
}
func (c *Client) send(args []interface{}) error {
var buf bytes.Buffer
for _, arg := range args {
var s string
switch arg := arg.(type) {
case string:
s = arg
case []byte:
s = string(arg)
case []string:
for _, s := range arg {
buf.WriteString(fmt.Sprintf("%d", len(s)))
buf.WriteByte('\n')
buf.WriteString(s)
buf.WriteByte('\n')
}
continue
case int:
s = fmt.Sprintf("%d", arg)
case int64:
s = fmt.Sprintf("%d", arg)
case float64:
s = fmt.Sprintf("%f", arg)
case bool:
if arg {
s = "1"
} else {
s = "0"
}
case nil:
s = ""
default:
return fmt.Errorf("bad arguments")
}
buf.WriteString(fmt.Sprintf("%d", len(s)))
buf.WriteByte('\n')
buf.WriteString(s)
buf.WriteByte('\n')
}
buf.WriteByte('\n')
_, err := c.sock.Write(buf.Bytes())
return err
}
func (c *Client) Recv() ([]string, error) {
return c.recv()
}
func (c *Client) recv() ([]string, error) {
var tmp [1]byte
for {
resp := c.parse()
if resp == nil || len(resp) > 0 {
return resp, nil
}
n, err := c.sock.Read(tmp[0:])
if err != nil {
return nil, err
}
c.recv_buf.Write(tmp[0:n])
}
}
func (c *Client) parse() []string {
resp := []string{}
buf := c.recv_buf.Bytes()
var idx, offset int
idx = 0
offset = 0
for {
idx = bytes.IndexByte(buf[offset:], '\n')
if idx == -1 {
break
}
p := buf[offset : offset+idx]
offset += idx + 1
//fmt.Printf("> [%s]\n", p);
if len(p) == 0 || (len(p) == 1 && p[0] == '\r') {
if len(resp) == 0 {
continue
} else {
c.recv_buf.Next(offset)
return resp
}
}
size, err := strconv.Atoi(string(p))
if err != nil || size < 0 {
return nil
}
if offset+size >= c.recv_buf.Len() {
break
}
v := buf[offset : offset+size]
resp = append(resp, string(v))
offset += size + 1
}
//fmt.Printf("buf.size: %d packet not ready...\n", len(buf))
return []string{}
}
// Close The Client Connection
func (c *Client) Close() error {
return c.sock.Close()
}