-
Notifications
You must be signed in to change notification settings - Fork 4
/
parse.c
269 lines (241 loc) · 6.13 KB
/
parse.c
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "main.h"
#include "network.h"
#include "parse.h"
char *rdbLoadLzfStringObject(thread_contex *th) {
unsigned int len, clen;
unsigned char *c = NULL;
sds val = NULL;
if ((clen = rdbLoadLen(th)) == -1) return NULL;
if ((len = rdbLoadLen(th)) == -1) return NULL;
if ((c = malloc(clen)) == NULL) goto NULL;
if ((val = sdsnewlen(NULL,len)) == NULL) goto err;
if (read(rdb,c,clen) == 0) goto err;
if (lzf_decompress(c,clen,val,len) == 0) goto err;
zfree(c);
return createObject(REDIS_STRING,val);
err:
zfree(c);
sdsfree(val);
return NULL;
}
int ll2str(char *s, long long value) {
char *p, aux;
unsigned long long v;
size_t l;
/* Generate the string representation, this method produces
* an reversed string. */
v = (value < 0) ? -value : value;
p = s;
do {
*p++ = '0'+(v%10);
v /= 10;
} while(v);
if (value < 0) *p++ = '-';
/* Compute length and add null term. */
l = p-s;
*p = '\0';
/* Reverse the string. */
p--;
while(s < p) {
aux = *s;
*s = *p;
*p = aux;
s++;
p--;
}
return l;
}
char * rdbLoadIntegerObject(int fd, int enctype) {
unsigned char enc[4];
long long val;
if (enctype == REDIS_RDB_ENC_INT8) {
if(read(fd,enc,1) <1) return -1;
val = (signed char)enc[0];
} else if (enctype == REDIS_RDB_ENC_INT16) {
uint16_t v;
if (read(fd,enc,2) < 2) return -1;
v = enc[0]|(enc[1]<<8);
val = (int16_t)v;
} else if (enctype == REDIS_RDB_ENC_INT32) {
uint32_t v;
if (read(rdb,enc,4) < 4) return NULL;
v = enc[0]|(enc[1]<<8)|(enc[2]<<16)|(enc[3]<<24);
val = (int32_t)v;
} else {
val = 0; /* anti-warning */
//redisPanic("Unknown RDB integer encoding type");
}
char * p = malloc(21);
ll2str(p,val);
return p;
}
int rdbGetString(thread_contex *th){
if(rdbLoadLen(th) ==-1){
return -1;
}
if(th->encoded){
switch(th->key_length) {
case REDIS_RDB_ENC_INT8:
case REDIS_RDB_ENC_INT16:
case REDIS_RDB_ENC_INT32:
th->tmpStr = rdbLoadIntegerObject(th,key_length);
return 1;
case REDIS_RDB_ENC_LZF:
return rdbLoadLzfStringObject(th);
default:
//redisPanic("Unknown RDB encoding type");
}
}
}
uint32_t rdbLoadLen(thread_contex* th){
unsigned char buf[2];
uint32_t len;
int type;
int fd = th->fd;
th->encoded = 0;
int n = read(fd,buf,1);
if(n == 0){
th->close =1;
return -1;
}
type = (buf[0]&0xC0)>>6;
if (type == REDIS_RDB_ENCVAL) {
/* Read a 6 bit encoding type. */
th->encoded = 1;
th->key_length = buf[0]&0x3F;
return 1;
} else if (type == REDIS_RDB_6BITLEN) {
/* Read a 6 bit len. */
th->key_length = buf[0]&0x3F;
return 1;
} else if (type == REDIS_RDB_14BITLEN) {
/* Read a 14 bit len. */
n = read(fd,buf+1,1);
if(n == 0){
th->close =1;
return -1;
}
th->key_length = ((buf[0]&0x3F)<<8)|buf[1];
return 1;
} else {
/* Read a 32 bit len. */
n = read(rdb,&len,4);
if(n == 0){
th->close =1;
return -1;
}
th->key_length =ntohl(len);
return 1;
}
}
int rdbLoadType(thread_contex *th){
int n = read(th->fd,&th->type,1);
if(n < 0){
return -1;
}
if(n == 0){
th->close = 1;
return -1;
}
}
time_t rdbLoadTime(thread_contex *th) {
int n = read(th->fd,&th->expiretime,4);
if(n < 0){
return -1;
}
if(n == 0){
th->close = 1;
return -1;
}
return 1;
}
long long rdbLoadMillisecondTime(thread_contex *th){
int n = read(th->fd,&th->expiretimeM,8);
if(n < 0){
return -1;
}
if(n == 0){
th->close = 1;
return -1;
}
return 1;
}
void parseRdb(void *data){
event *ev = data;
thread_contex * th = ev->contex;
int fd = ev->fd;
int type;
int n;
char buf[1024];
long expiretime;
//buf_t * buf;
printf("%d\n",th->state );
while(1){
switch(th->state){
case NEED_HEAD:
n = readFromServer(fd,buf,9);
if(n < 9){
return;
}
buf[9] = '\0';
printf("%s\n",buf);
th->state = NEED_TYPE;
continue;
break;
case NEED_TYPE:
// printf("ssss\n");
if(rdbLoadType(th) == -1){
if(th->close)
goto eoferr;
return;
}
printf("type is %d\n",th->type);
if(th->type == REDIS_RDB_OPCODE_EXPIRETIME){
if(rdbLoadTime(th) == -1){
if(th->close)
goto eoferr;
th->state = NEED_EXPIRE;
return;
}
th->state = NEED_TYPE;
}else if(th->type == REDIS_RDB_OPCODE_EXPIRETIME_MS){
if(rdbLoadMillisecondTime(th) == -1){
if(th->close)
goto eoferr;
th->state = NEED_EXPIREM;
return;
}
th->state = NEED_TYPE;
}else if(th->type == REDIS_RDB_OPCODE_EOF){
//th->state = NEED_TYPE;
} /* can't appear */
else if(th->type == REDIS_RDB_OPCODE_SELECTDB) {
//printf("come h\n");
th->state = NEED_TYPE;
}else{
//printf("sdfsfs\n");
th->state = NEED_KEY;
}
break;
case NEED_KEY_LENGTH:
if(rdbLoadLen(th) == -1){
}
if(th->encoded){
//key已获取
if(th->key_length == REDIS_RDB_ENC_INT8 || th->key_length == REDIS_RDB_ENC_INT16|| th->key_length == REDIS_RDB_ENC_INT32){
th->value = rdbLoadIntegerObject(th,key_length);
th->state = NEED_FLUSH;
continue;
}else{
}
}
//printf("sdfsadf\n");
//readLineFromServer(fd,buf,1024);
}
}
eoferr:
return;
}