hirlite 0.0.2 → 0.0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/ext/hirlite_ext/extconf.rb +2 -2
  3. data/lib/hirlite/version.rb +1 -1
  4. data/vendor/rlite/deps/lua/Makefile +128 -0
  5. data/vendor/rlite/deps/lua/src/fpconv.c +205 -0
  6. data/vendor/rlite/deps/lua/src/fpconv.h +22 -0
  7. data/vendor/rlite/deps/lua/src/lapi.c +1087 -0
  8. data/vendor/rlite/deps/lua/src/lapi.h +16 -0
  9. data/vendor/rlite/deps/lua/src/lauxlib.c +652 -0
  10. data/vendor/rlite/deps/lua/src/lauxlib.h +174 -0
  11. data/vendor/rlite/deps/lua/src/lbaselib.c +653 -0
  12. data/vendor/rlite/deps/lua/src/lcode.c +831 -0
  13. data/vendor/rlite/deps/lua/src/lcode.h +76 -0
  14. data/vendor/rlite/deps/lua/src/ldblib.c +398 -0
  15. data/vendor/rlite/deps/lua/src/ldebug.c +638 -0
  16. data/vendor/rlite/deps/lua/src/ldebug.h +33 -0
  17. data/vendor/rlite/deps/lua/src/ldo.c +519 -0
  18. data/vendor/rlite/deps/lua/src/ldo.h +57 -0
  19. data/vendor/rlite/deps/lua/src/ldump.c +164 -0
  20. data/vendor/rlite/deps/lua/src/lfunc.c +174 -0
  21. data/vendor/rlite/deps/lua/src/lfunc.h +34 -0
  22. data/vendor/rlite/deps/lua/src/lgc.c +710 -0
  23. data/vendor/rlite/deps/lua/src/lgc.h +110 -0
  24. data/vendor/rlite/deps/lua/src/linit.c +38 -0
  25. data/vendor/rlite/deps/lua/src/liolib.c +556 -0
  26. data/vendor/rlite/deps/lua/src/llex.c +463 -0
  27. data/vendor/rlite/deps/lua/src/llex.h +81 -0
  28. data/vendor/rlite/deps/lua/src/llimits.h +128 -0
  29. data/vendor/rlite/deps/lua/src/lmathlib.c +263 -0
  30. data/vendor/rlite/deps/lua/src/lmem.c +86 -0
  31. data/vendor/rlite/deps/lua/src/lmem.h +49 -0
  32. data/vendor/rlite/deps/lua/src/loadlib.c +666 -0
  33. data/vendor/rlite/deps/lua/src/lobject.c +214 -0
  34. data/vendor/rlite/deps/lua/src/lobject.h +381 -0
  35. data/vendor/rlite/deps/lua/src/lopcodes.c +102 -0
  36. data/vendor/rlite/deps/lua/src/lopcodes.h +268 -0
  37. data/vendor/rlite/deps/lua/src/loslib.c +243 -0
  38. data/vendor/rlite/deps/lua/src/lparser.c +1339 -0
  39. data/vendor/rlite/deps/lua/src/lparser.h +82 -0
  40. data/vendor/rlite/deps/lua/src/lstate.c +214 -0
  41. data/vendor/rlite/deps/lua/src/lstate.h +169 -0
  42. data/vendor/rlite/deps/lua/src/lstring.c +111 -0
  43. data/vendor/rlite/deps/lua/src/lstring.h +31 -0
  44. data/vendor/rlite/deps/lua/src/lstrlib.c +871 -0
  45. data/vendor/rlite/deps/lua/src/ltable.c +588 -0
  46. data/vendor/rlite/deps/lua/src/ltable.h +40 -0
  47. data/vendor/rlite/deps/lua/src/ltablib.c +287 -0
  48. data/vendor/rlite/deps/lua/src/ltm.c +75 -0
  49. data/vendor/rlite/deps/lua/src/ltm.h +54 -0
  50. data/vendor/rlite/deps/lua/src/lua.c +392 -0
  51. data/vendor/rlite/deps/lua/src/lua.h +388 -0
  52. data/vendor/rlite/deps/lua/src/lua_bit.c +189 -0
  53. data/vendor/rlite/deps/lua/src/lua_cjson.c +1427 -0
  54. data/vendor/rlite/deps/lua/src/lua_cmsgpack.c +957 -0
  55. data/vendor/rlite/deps/lua/src/lua_struct.c +421 -0
  56. data/vendor/rlite/deps/lua/src/luac.c +200 -0
  57. data/vendor/rlite/deps/lua/src/luaconf.h +763 -0
  58. data/vendor/rlite/deps/lua/src/lualib.h +53 -0
  59. data/vendor/rlite/deps/lua/src/lundump.c +227 -0
  60. data/vendor/rlite/deps/lua/src/lundump.h +36 -0
  61. data/vendor/rlite/deps/lua/src/lvm.c +767 -0
  62. data/vendor/rlite/deps/lua/src/lvm.h +36 -0
  63. data/vendor/rlite/deps/lua/src/lzio.c +82 -0
  64. data/vendor/rlite/deps/lua/src/lzio.h +67 -0
  65. data/vendor/rlite/deps/lua/src/print.c +227 -0
  66. data/vendor/rlite/deps/lua/src/strbuf.c +251 -0
  67. data/vendor/rlite/deps/lua/src/strbuf.h +154 -0
  68. data/vendor/rlite/src/Makefile +97 -25
  69. data/vendor/rlite/{deps → src}/crc64.c +0 -0
  70. data/vendor/rlite/{deps → src}/crc64.h +0 -0
  71. data/vendor/rlite/src/dump.c +2 -2
  72. data/vendor/rlite/{deps → src}/endianconv.h +0 -0
  73. data/vendor/rlite/src/hirlite.c +134 -90
  74. data/vendor/rlite/src/hirlite.h +34 -1
  75. data/vendor/rlite/{deps → src}/hyperloglog.c +0 -0
  76. data/vendor/rlite/{deps → src}/hyperloglog.h +0 -0
  77. data/vendor/rlite/{deps → src}/lzf.h +0 -0
  78. data/vendor/rlite/{deps → src}/lzfP.h +0 -0
  79. data/vendor/rlite/{deps → src}/lzf_c.c +0 -0
  80. data/vendor/rlite/{deps → src}/lzf_d.c +0 -0
  81. data/vendor/rlite/src/page_btree.c +10 -10
  82. data/vendor/rlite/src/page_btree.h +2 -2
  83. data/vendor/rlite/src/page_list.c +3 -3
  84. data/vendor/rlite/src/page_list.h +1 -1
  85. data/vendor/rlite/src/page_multi_string.c +1 -1
  86. data/vendor/rlite/src/page_skiplist.c +1 -1
  87. data/vendor/rlite/src/page_skiplist.h +1 -1
  88. data/vendor/rlite/src/rand.c +93 -0
  89. data/vendor/rlite/src/rand.h +38 -0
  90. data/vendor/rlite/src/restore.c +286 -155
  91. data/vendor/rlite/src/restore.h +6 -0
  92. data/vendor/rlite/src/rlite.c +30 -30
  93. data/vendor/rlite/src/rlite.h +1 -1
  94. data/vendor/rlite/src/scripting.c +1138 -0
  95. data/vendor/rlite/src/scripting.h +10 -0
  96. data/vendor/rlite/{deps → src}/sha1.c +0 -0
  97. data/vendor/rlite/{deps → src}/sha1.h +0 -0
  98. data/vendor/rlite/src/solarisfixes.h +54 -0
  99. data/vendor/rlite/src/sort.c +4 -2
  100. data/vendor/rlite/src/sort.h +1 -1
  101. data/vendor/rlite/src/type_string.c +1 -1
  102. data/vendor/rlite/src/util.c +4 -4
  103. data/vendor/rlite/src/util.h +3 -3
  104. data/vendor/rlite/{deps → src}/utilfromredis.c +0 -0
  105. data/vendor/rlite/{deps → src}/utilfromredis.h +0 -0
  106. metadata +84 -15
@@ -2,9 +2,54 @@
2
2
  #include "page_key.h"
3
3
  #include "rlite.h"
4
4
  #include "util.h"
5
- #include "../deps/crc64.h"
6
- #include "../deps/endianconv.h"
7
- #include "../deps/lzf.h"
5
+ #include "crc64.h"
6
+ #include "endianconv.h"
7
+ #include "lzf.h"
8
+
9
+ struct stringwithlength {
10
+ unsigned char *string;
11
+ long stringlen;
12
+ };
13
+
14
+ int ucread(struct rl_restore_streamer *streamer, unsigned char *str, long len) {
15
+ struct stringwithlength *data = streamer->context;
16
+ if (len > data->stringlen) {
17
+ return RL_UNEXPECTED;
18
+ }
19
+ if (str) {
20
+ memcpy(str, data->string, len);
21
+ }
22
+ data->string += len;
23
+ data->stringlen -= len;
24
+ return RL_OK;
25
+ }
26
+
27
+ static rl_restore_streamer* init_string_streamer(unsigned char *data, long datalen)
28
+ {
29
+ rl_restore_streamer *streamer = rl_malloc(sizeof(*streamer));
30
+ if (!streamer) {
31
+ return NULL;
32
+ }
33
+ struct stringwithlength *s = rl_malloc(sizeof(*s));
34
+ if (!s) {
35
+ rl_free(streamer);
36
+ return NULL;
37
+ }
38
+ s->string = data;
39
+ s->stringlen = datalen;
40
+ streamer->context = s;
41
+ streamer->read = &ucread;
42
+ return streamer;
43
+ }
44
+
45
+ static void free_string_streamer(rl_restore_streamer *streamer) {
46
+ rl_free(streamer->context);
47
+ rl_free(streamer);
48
+ }
49
+
50
+ static int read(rl_restore_streamer *streamer, unsigned char *target, long len) {
51
+ return streamer->read(streamer, target, len);
52
+ }
8
53
 
9
54
  static int verify(unsigned char *data, long datalen) {
10
55
  unsigned char *footer;
@@ -25,17 +70,12 @@ static int verify(unsigned char *data, long datalen) {
25
70
  return (memcmp(&crc,footer+2,8) == 0) ? RL_OK : RL_INVALID_PARAMETERS;
26
71
  }
27
72
 
28
- static unsigned char *read_signed_short(unsigned char *data, long *value) {
29
- *value = (char)data[0] | (char)(data[1] << 8);
30
- return data + 2;
31
- }
32
-
33
- static unsigned char *read_signed_int(unsigned char *data, long *value) {
73
+ static int read_signed_int(unsigned char *data, long *value) {
34
74
  *value = (long)data[0] | (long)(data[1] << 8) | (long)(data[2] << 16) | (long)(data[3] << 24);
35
- return data + 4;
75
+ return RL_OK;
36
76
  }
37
77
 
38
- static unsigned char *read_signed_long(unsigned char *data, long *value) {
78
+ static int read_signed_long(unsigned char *data, long *value) {
39
79
  long tmp;
40
80
  *value = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
41
81
  tmp = data[4];
@@ -46,184 +86,242 @@ static unsigned char *read_signed_long(unsigned char *data, long *value) {
46
86
  *value |= tmp << 48;
47
87
  tmp = data[7];
48
88
  *value |= tmp << 56;
49
- return data + 8;
89
+ return RL_OK;
50
90
  }
51
91
 
52
- static unsigned char *read_unsigned_short(unsigned char *data, unsigned long *value) {
53
- *value = data[0] | (data[1] << 8);
54
- return data + 2;
92
+ static int read_unsigned_short(rl_restore_streamer *streamer, unsigned long *value) {
93
+ int retval;
94
+ unsigned char ucint;
95
+ long val;
96
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
97
+ val = ucint;
98
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
99
+ val |= ucint << 8;
100
+ *value = val;
101
+ cleanup:
102
+ return retval;
55
103
  }
56
104
 
57
- static unsigned char *read_unsigned_int(unsigned char *data, unsigned long *value) {
58
- *value = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
59
- return data + 4;
105
+ static int read_unsigned_int(rl_restore_streamer *streamer, unsigned long *value) {
106
+ int retval;
107
+ unsigned char ucint;
108
+ long val;
109
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
110
+ val = ucint;
111
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
112
+ val |= ucint << 8;
113
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
114
+ val |= ucint << 16;
115
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
116
+ val |= ucint << 24;
117
+ *value = val;
118
+ cleanup:
119
+ return retval;
60
120
  }
61
121
 
62
- static unsigned char *read_unsigned_long(unsigned char *data, unsigned long *value) {
63
- unsigned long tmp;
64
- *value = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
65
- tmp = data[4];
66
- *value |= tmp << 32;
67
- tmp = data[5];
68
- *value |= tmp << 40;
69
- tmp = data[6];
70
- *value |= tmp << 48;
71
- tmp = data[7];
72
- *value |= tmp << 56;
73
- return data + 8;
122
+ static int read_unsigned_long(rl_restore_streamer *streamer, unsigned long *value) {
123
+ int retval;
124
+ unsigned char ucint;
125
+ unsigned long val;
126
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
127
+ val = ucint;
128
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
129
+ val |= ucint << 8;
130
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
131
+ val |= ucint << 16;
132
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
133
+ val |= ucint << 24;
134
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
135
+ val |= (unsigned long)ucint << 32;
136
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
137
+ val |= (unsigned long)ucint << 40;
138
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
139
+ val |= (unsigned long)ucint << 48;
140
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
141
+ val |= (unsigned long)ucint << 56;
142
+ *value = val;
143
+ cleanup:
144
+ return retval;
74
145
  }
75
146
 
76
- static unsigned char *read_length_with_encoding(unsigned char *f, long *length, int *is_encoded)
147
+ static int read_length_with_encoding(rl_restore_streamer *streamer, long *length, int *is_encoded)
77
148
  {
78
- int enc_type = (f[0] & 0xC0) >> 6;
149
+ int retval;
150
+ unsigned char f;
151
+ unsigned char f4[4];
152
+ RL_CALL(read, RL_OK, streamer, &f, 1);
153
+ int enc_type = (f & 0xC0) >> 6;
79
154
  if (enc_type == REDIS_RDB_ENCVAL) {
80
155
  if (is_encoded) {
81
156
  *is_encoded = 1;
82
157
  }
83
- *length = f[0] & 0x3F;
84
- return f + 1;
158
+ *length = f & 0x3F;
159
+ return RL_OK;
85
160
  } else if (enc_type == REDIS_RDB_6BITLEN) {
86
161
  if (is_encoded) {
87
162
  *is_encoded = 0;
88
163
  }
89
- *length = f[0] & 0x3F;
90
- return f + 1;
164
+ *length = f & 0x3F;
165
+ return RL_OK;
91
166
  } else if (enc_type == REDIS_RDB_14BITLEN) {
92
167
  if (is_encoded) {
93
168
  *is_encoded = 0;
94
169
  }
95
- *length = ((f[0] & 0x3F) << 8 ) | f[1];
96
- return f + 2;
170
+ *length = ((f & 0x3F) << 8 );
171
+ RL_CALL(read, RL_OK, streamer, &f, 1);
172
+ *length |= f;
173
+ return RL_OK;
97
174
  } else {
98
175
  if (is_encoded) {
99
176
  *is_encoded = 0;
100
177
  }
101
- *length = ntohl(*(uint32_t*)(f + 1));
102
- return f + 5;
178
+ RL_CALL(read, RL_OK, streamer, f4, 4);
179
+ *length = ntohl(*(uint32_t*)f4);
180
+ return RL_OK;
103
181
  }
182
+ cleanup:
183
+ return retval;
104
184
  }
105
185
 
106
- static unsigned char *read_ziplist_entry(unsigned char *data, unsigned char **_entry, long *_length)
186
+ static int read_ziplist_entry(rl_restore_streamer *streamer, unsigned long prev_length, unsigned char **_entry, long *_length)
107
187
  {
188
+ int retval = RL_OK;
108
189
  long length = 0;
109
190
  unsigned char *entry;
110
- unsigned long prev_length = data[0];
111
- data++;
112
191
  if (prev_length == 254) {
113
- data = read_unsigned_int(data, &prev_length);
192
+ RL_CALL(read_unsigned_int, RL_OK, streamer, &prev_length)
114
193
  }
115
- unsigned long entry_header = data[0];
194
+ unsigned char ucaux;
195
+ unsigned long entry_header;
196
+ RL_CALL(read, RL_OK, streamer, &ucaux, 1);
197
+ entry_header = ucaux;
116
198
  if ((entry_header >> 6) == 0) {
117
199
  length = entry_header & 0x3F;
118
- data++;
119
200
  }
120
201
  else if ((entry_header >> 6) == 1) {
121
- length = ((entry_header & 0x3F) << 8) | data[0];
122
- data++;
202
+ length = ((entry_header & 0x3F) << 8);
203
+ RL_CALL(read, RL_OK, streamer, &ucaux, 1);
204
+ length |= ucaux;
123
205
  }
124
206
  else if ((entry_header >> 6) == 2) {
125
207
  // TODO: length = read_big_endian_unsigned_int(f)
126
- data = NULL;
208
+ retval = RL_NOT_IMPLEMENTED;
209
+ goto cleanup;
127
210
  } else if ((entry_header >> 4) == 12) {
128
- data = read_signed_short(data, &length);
211
+ length = entry_header;
212
+ RL_CALL(read, RL_OK, streamer, &ucaux, 1);
213
+ length |= ucaux << 8;
129
214
  } else if ((entry_header >> 4) == 13) {
130
- data = read_signed_int(data, &length);
215
+ unsigned char b[4];
216
+ b[0] = entry_header;
217
+ RL_CALL(read, RL_OK, streamer, &b[1], 3);
218
+ RL_CALL(read_signed_int, RL_OK, b, &length);
131
219
  } else if ((entry_header >> 4) == 14) {
132
- data = read_signed_long(data, &length);
220
+ unsigned char b[8];
221
+ b[0] = entry_header;
222
+ RL_CALL(read, RL_OK, streamer, &b[1], 7);
223
+ RL_CALL(read_signed_long, RL_OK, b, &length);
133
224
  } else if (entry_header == 240) {
134
225
  unsigned char tmp[5];
135
226
  tmp[0] = tmp[4] = 0;
136
- memcpy(&tmp[1], data, 3);
137
- data = read_signed_int(data, &length);
227
+ tmp[1] = entry_header;
228
+ RL_CALL(read, RL_OK, streamer, &tmp[2], 2);
229
+ RL_CALL(read_signed_int, RL_OK, tmp, &length);
138
230
  } else if (entry_header == 254) {
139
- length = data[0];
140
- data++;
231
+ RL_CALL(read, RL_OK, streamer, &ucaux, 1);
232
+ length = ucaux;
141
233
  } else if (entry_header >= 241 && entry_header <= 253) {
142
- entry = rl_malloc(sizeof(unsigned char) * 2);
143
- if (!entry) {
144
- return NULL;
145
- }
234
+ RL_MALLOC(entry, sizeof(unsigned char) * 2);
146
235
  length = snprintf((char *)entry, 2, "%lu", entry_header - 241);
147
- data++;
148
236
  goto ret;
149
237
  }
150
- entry = rl_malloc(sizeof(unsigned char) * length);
151
- if (!entry) {
152
- return NULL;
153
- }
154
- memcpy(entry, data, length);
155
- data += length;
238
+ RL_MALLOC(entry, sizeof(unsigned char) * length);
239
+ RL_CALL(read, RL_OK, streamer, entry, length);
156
240
  ret:
157
241
  *_entry = entry;
158
242
  *_length = length;
159
- return data;
243
+ cleanup:
244
+ return retval;
160
245
  }
161
246
 
162
- static int read_string(unsigned char *data, unsigned char **str, long *strlen, unsigned char **newdata)
247
+ static int read_string(rl_restore_streamer *streamer, unsigned char **str, long *strlen)
163
248
  {
164
249
  int retval = RL_OK;
165
250
  long length, strdatalen, cdatalen;
166
251
  unsigned char *strdata;
167
252
  int is_encoded;
168
- data = read_length_with_encoding(data, &length, &is_encoded);
253
+ unsigned char ucint;
254
+ int16_t i16int;
255
+ int32_t i32int;
256
+ RL_CALL(read_length_with_encoding, RL_OK, streamer, &length, &is_encoded);
169
257
  if (is_encoded && length == REDIS_RDB_ENC_INT8) {
170
258
  strdatalen = 5;
171
259
  RL_MALLOC(strdata, strdatalen * sizeof(unsigned char));
172
- strdatalen = snprintf((char *)strdata, strdatalen, "%d", (signed char)data[0]);
260
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
261
+ strdatalen = snprintf((char *)strdata, strdatalen, "%d", (signed char)ucint);
173
262
  if (strdatalen < 0) {
174
263
  retval = RL_UNEXPECTED;
175
264
  goto cleanup;
176
265
  }
177
- data++;
178
266
  } else if (is_encoded && length == REDIS_RDB_ENC_INT16) {
179
267
  strdatalen = 7;
180
- strdata = rl_malloc(strdatalen * sizeof(unsigned char));
181
- strdatalen = snprintf((char *)strdata, strdatalen, "%d", (int16_t)(data[0] | (data[1] << 8)));
268
+ RL_MALLOC(strdata, strdatalen * sizeof(unsigned char));
269
+
270
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
271
+ i16int = ucint;
272
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
273
+ i16int |= ucint << 8;
274
+
275
+ strdatalen = snprintf((char *)strdata, strdatalen, "%d", i16int);
182
276
  if (strdatalen < 0) {
183
277
  retval = RL_UNEXPECTED;
184
278
  goto cleanup;
185
279
  }
186
- data += 2;
187
280
  } else if (is_encoded && length == REDIS_RDB_ENC_INT32) {
188
281
  strdatalen = 12;
189
- strdata = rl_malloc(strdatalen * sizeof(unsigned char));
190
- strdatalen = snprintf((char *)strdata, strdatalen, "%d", (int32_t)(data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24)));
282
+ RL_MALLOC(strdata, strdatalen * sizeof(unsigned char));
283
+
284
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
285
+ i32int = ucint;
286
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
287
+ i32int |= ucint << 8;
288
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
289
+ i32int |= ucint << 16;
290
+ RL_CALL(read, RL_OK, streamer, &ucint, 1);
291
+ i32int |= ucint << 24;
292
+
293
+ strdatalen = snprintf((char *)strdata, strdatalen, "%d", i32int);
191
294
  if (strdatalen < 0) {
192
295
  retval = RL_UNEXPECTED;
193
296
  goto cleanup;
194
297
  }
195
- data += 4;
196
298
  } else if (is_encoded && length == REDIS_RDB_ENC_LZF) {
197
- data = read_length_with_encoding(data, &cdatalen, NULL);
198
- data = read_length_with_encoding(data, &strdatalen, NULL);
299
+ RL_CALL(read_length_with_encoding, RL_OK, streamer, &cdatalen, NULL);
300
+ RL_CALL(read_length_with_encoding, RL_OK, streamer, &strdatalen, NULL);
199
301
  strdata = rl_malloc(strdatalen * sizeof(unsigned char));
200
- rl_lzf_decompress(data, cdatalen, strdata, strdatalen);
201
- data += cdatalen;
302
+ unsigned char *cdata = malloc(sizeof(unsigned char) * cdatalen);
303
+ RL_CALL(read, RL_OK, streamer, cdata, cdatalen);
304
+ rl_lzf_decompress(cdata, cdatalen, strdata, strdatalen);
305
+ free(cdata);
202
306
  } else if (!is_encoded) {
203
307
  strdatalen = length;
204
308
  strdata = rl_malloc(strdatalen * sizeof(unsigned char));
205
- memcpy(strdata, data, strdatalen);
206
- data += strdatalen;
309
+ RL_CALL(read, RL_OK, streamer, strdata, strdatalen);
207
310
  } else {
208
311
  retval = RL_NOT_IMPLEMENTED;
209
312
  goto cleanup;
210
313
  }
211
314
  *str = strdata;
212
315
  *strlen = strdatalen;
213
- if (newdata) {
214
- *newdata = data;
215
- }
216
316
  cleanup:
217
317
  return retval;
218
318
  }
219
319
 
220
- int rl_restore(struct rlite *db, const unsigned char *key, long keylen, unsigned long long expires, unsigned char *_data, long datalen)
221
- {
320
+ int rl_restore_stream(struct rlite *db, const unsigned char *key, long keylen, unsigned long long expires, rl_restore_streamer *streamer) {
222
321
  int retval;
223
- unsigned char type;
322
+ unsigned char type, ucaux;
224
323
  long i, length, length2;
225
324
  unsigned char *strdata = NULL, *strdata2 = NULL, *strdata3 = NULL;
226
- unsigned char *data = _data, *tmpdata;
227
325
  long strdatalen = 0, strdata2len, strdata3len;
228
326
  unsigned long j, encoding, numentries, ulvalue;
229
327
  void **tmp = NULL;
@@ -231,60 +329,66 @@ int rl_restore(struct rlite *db, const unsigned char *key, long keylen, unsigned
231
329
  double d;
232
330
 
233
331
  RL_CALL(rl_key_get, RL_NOT_FOUND, db, key, keylen, NULL, NULL, NULL, NULL, NULL);
234
- RL_CALL(verify, RL_OK, data, datalen);
235
-
236
- type = data[0];
237
- data++;
332
+ RL_CALL(read, RL_OK, streamer, &type, 1);
238
333
 
239
334
  if (type == REDIS_RDB_TYPE_STRING) {
240
- RL_CALL(read_string, RL_OK, data, &strdata, &strdatalen, NULL);
241
- RL_CALL(rl_set, RL_OK, db, key, keylen, strdata, strdatalen, 1, expires);
335
+ RL_CALL(read_string, RL_OK, streamer, &strdata, &strdatalen);
336
+ if (key) {
337
+ RL_CALL(rl_set, RL_OK, db, key, keylen, strdata, strdatalen, 1, expires);
338
+ }
242
339
  }
243
340
  else if (type == REDIS_RDB_TYPE_LIST) {
244
- data = read_length_with_encoding(data, &length, NULL);
341
+ RL_CALL(read_length_with_encoding, RL_OK, streamer, &length, NULL);
245
342
  for (i = 0; i < length; i++) {
246
- RL_CALL(read_string, RL_OK, data, &strdata, &strdatalen, &data);
247
- RL_CALL(rl_push, RL_OK, db, key, keylen, 1, 0, 1, &strdata, &strdatalen, NULL);
343
+ RL_CALL(read_string, RL_OK, streamer, &strdata, &strdatalen);
344
+ if (key) {
345
+ RL_CALL(rl_push, RL_OK, db, key, keylen, 1, 0, 1, &strdata, &strdatalen, NULL);
346
+ }
248
347
  rl_free(strdata);
249
348
  strdata = NULL;
250
349
  }
251
350
  }
252
351
  else if (type == REDIS_RDB_TYPE_SET) {
253
- data = read_length_with_encoding(data, &length, NULL);
352
+ RL_CALL(read_length_with_encoding, RL_OK, streamer, &length, NULL);
254
353
  for (i = 0; i < length; i++) {
255
- RL_CALL(read_string, RL_OK, data, &strdata, &strdatalen, &data);
256
- RL_CALL(rl_sadd, RL_OK, db, key, keylen, 1, &strdata, &strdatalen, NULL);
354
+ RL_CALL(read_string, RL_OK, streamer, &strdata, &strdatalen);
355
+ if (key) {
356
+ RL_CALL(rl_sadd, RL_OK, db, key, keylen, 1, &strdata, &strdatalen, NULL);
357
+ }
257
358
  rl_free(strdata);
258
359
  strdata = NULL;
259
360
  }
260
361
  }
261
362
  else if (type == REDIS_RDB_TYPE_ZSET) {
262
- data = read_length_with_encoding(data, &length, NULL);
363
+ RL_CALL(read_length_with_encoding, RL_OK, streamer, &length, NULL);
263
364
  for (i = 0; i < length; i++) {
264
- RL_CALL(read_string, RL_OK, data, &strdata, &strdatalen, &data);
265
- length2 = data[0];
266
- data++;
365
+ RL_CALL(read_string, RL_OK, streamer, &strdata, &strdatalen);
366
+ RL_CALL(read, RL_OK, streamer, &ucaux, 1);
367
+ length2 = ucaux;
267
368
  if (length2 > 40 || length2 < 1) {
268
369
  retval = RL_UNEXPECTED;
269
370
  goto cleanup;
270
371
  }
271
- memcpy(f, data, length2);
272
- data += length2;
372
+ RL_CALL(read, RL_OK, streamer, (unsigned char *)f, length2);
273
373
  f[length2] = 0;
274
374
  d = strtold(f, NULL);
275
375
 
276
- RL_CALL(rl_zadd, RL_OK, db, key, keylen, d, strdata, strdatalen);
376
+ if (key) {
377
+ RL_CALL(rl_zadd, RL_OK, db, key, keylen, d, strdata, strdatalen);
378
+ }
277
379
  rl_free(strdata);
278
380
  strdata = NULL;
279
381
  }
280
382
  }
281
383
  else if (type == REDIS_RDB_TYPE_HASH) {
282
- data = read_length_with_encoding(data, &length, NULL);
384
+ RL_CALL(read_length_with_encoding, RL_OK, streamer, &length, NULL);
283
385
  for (i = 0; i < length; i++) {
284
- RL_CALL(read_string, RL_OK, data, &strdata, &strdatalen, &data);
285
- RL_CALL(read_string, RL_OK, data, &strdata2, &length2, &data);
386
+ RL_CALL(read_string, RL_OK, streamer, &strdata, &strdatalen);
387
+ RL_CALL(read_string, RL_OK, streamer, &strdata2, &length2);
286
388
 
287
- RL_CALL(rl_hset, RL_OK, db, key, keylen, strdata, strdatalen, strdata2, length2, NULL, 0);
389
+ if (key) {
390
+ RL_CALL(rl_hset, RL_OK, db, key, keylen, strdata, strdatalen, strdata2, length2, NULL, 0);
391
+ }
288
392
  rl_free(strdata);
289
393
  strdata = NULL;
290
394
  rl_free(strdata2);
@@ -296,26 +400,30 @@ int rl_restore(struct rlite *db, const unsigned char *key, long keylen, unsigned
296
400
  goto cleanup;
297
401
  }
298
402
  else if (type == REDIS_RDB_TYPE_LIST_ZIPLIST) {
299
- RL_CALL(read_string, RL_OK, data, &strdata, &strdatalen, &data);
300
- tmpdata = strdata + 10;
301
- while (*tmpdata != 255) {
302
- tmpdata = read_ziplist_entry(tmpdata, &strdata2, &strdata2len);
303
- if (!tmpdata) {
304
- retval = RL_UNEXPECTED;
305
- goto cleanup;
403
+ RL_CALL(read_string, RL_OK, streamer, &strdata, &strdatalen);
404
+ rl_restore_streamer *substreamer = init_string_streamer(strdata, strdatalen);
405
+ RL_CALL(read, RL_OK, substreamer, NULL, 10);
406
+ while (1) {
407
+ RL_CALL(read, RL_OK, substreamer, &ucaux, 1);
408
+ if (ucaux == 255) {
409
+ break;
410
+ }
411
+ RL_CALL(read_ziplist_entry, RL_OK, substreamer, ucaux, &strdata2, &strdata2len);
412
+ if (key) {
413
+ RL_CALL(rl_push, RL_OK, db, key, keylen, 1, 0, 1, &strdata2, &strdata2len, NULL);
306
414
  }
307
- RL_CALL(rl_push, RL_OK, db, key, keylen, 1, 0, 1, &strdata2, &strdata2len, NULL);
308
415
  rl_free(strdata2);
309
416
  strdata2 = NULL;
310
417
  }
418
+ free_string_streamer(substreamer);
311
419
  rl_free(strdata);
312
420
  strdata = NULL;
313
421
  }
314
422
  else if (type == REDIS_RDB_TYPE_SET_INTSET) {
315
- RL_CALL(read_string, RL_OK, data, &strdata, &strdatalen, &data);
316
- tmpdata = strdata;
317
- tmpdata = read_unsigned_int(tmpdata, &encoding);
318
- tmpdata = read_unsigned_int(tmpdata, &numentries);
423
+ RL_CALL(read_string, RL_OK, streamer, &strdata, &strdatalen);
424
+ rl_restore_streamer *substreamer = init_string_streamer(strdata, strdatalen);
425
+ RL_CALL(read_unsigned_int, RL_OK, substreamer, &encoding);
426
+ RL_CALL(read_unsigned_int, RL_OK, substreamer, &numentries);
319
427
  if (encoding != 2 && encoding != 4 && encoding != 8) {
320
428
  retval = RL_UNEXPECTED;
321
429
  goto cleanup;
@@ -323,31 +431,32 @@ int rl_restore(struct rlite *db, const unsigned char *key, long keylen, unsigned
323
431
  RL_MALLOC(tmp, sizeof(void *));
324
432
  for (j = 0; j < numentries; j++) {
325
433
  if (encoding == 8) {
326
- tmpdata = read_unsigned_long(tmpdata, &ulvalue);
434
+ RL_CALL(read_unsigned_long, RL_OK, substreamer, &ulvalue);
327
435
  } else if (encoding == 4) {
328
- tmpdata = read_unsigned_int(tmpdata, &ulvalue);
436
+ RL_CALL(read_unsigned_int, RL_OK, substreamer, &ulvalue);
329
437
  } else if (encoding == 2) {
330
- tmpdata = read_unsigned_short(tmpdata, &ulvalue);
438
+ RL_CALL(read_unsigned_short, RL_OK, substreamer, &ulvalue);
331
439
  }
332
440
  length2 = snprintf(f, 40, "%lu", ulvalue);
333
441
  tmp[0] = f;
334
- RL_CALL(rl_sadd, RL_OK, db, key, keylen, 1, (unsigned char **)tmp, &length2, NULL);
442
+ if (key) {
443
+ RL_CALL(rl_sadd, RL_OK, db, key, keylen, 1, (unsigned char **)tmp, &length2, NULL);
444
+ }
335
445
  }
446
+ free_string_streamer(substreamer);
336
447
  }
337
448
  else if (type == REDIS_RDB_TYPE_ZSET_ZIPLIST) {
338
- RL_CALL(read_string, RL_OK, data, &strdata, &strdatalen, &data);
339
- tmpdata = strdata + 10;
340
- while (*tmpdata != 255) {
341
- tmpdata = read_ziplist_entry(tmpdata, &strdata2, &strdata2len);
342
- if (!tmpdata) {
343
- retval = RL_UNEXPECTED;
344
- goto cleanup;
345
- }
346
- tmpdata = read_ziplist_entry(tmpdata, &strdata3, &strdata3len);
347
- if (!tmpdata) {
348
- retval = RL_UNEXPECTED;
349
- goto cleanup;
449
+ RL_CALL(read_string, RL_OK, streamer, &strdata, &strdatalen);
450
+ rl_restore_streamer *substreamer = init_string_streamer(strdata, strdatalen);
451
+ RL_CALL(read, RL_OK, substreamer, NULL, 10);
452
+ while (1) {
453
+ RL_CALL(read, RL_OK, substreamer, &ucaux, 1);
454
+ if (ucaux == 255) {
455
+ break;
350
456
  }
457
+ RL_CALL(read_ziplist_entry, RL_OK, substreamer, ucaux, &strdata2, &strdata2len);
458
+ RL_CALL(read, RL_OK, substreamer, NULL, 1);
459
+ RL_CALL(read_ziplist_entry, RL_OK, substreamer, ucaux, &strdata3, &strdata3len);
351
460
 
352
461
  if (strdata3len > 40 || strdata3len < 1) {
353
462
  retval = RL_UNEXPECTED;
@@ -356,35 +465,39 @@ int rl_restore(struct rlite *db, const unsigned char *key, long keylen, unsigned
356
465
  memcpy(f, strdata3, strdata3len);
357
466
  f[strdata3len] = 0;
358
467
  d = strtold(f, NULL);
359
- RL_CALL(rl_zadd, RL_OK, db, key, keylen, d, strdata2, strdata2len);
468
+ if (key) {
469
+ RL_CALL(rl_zadd, RL_OK, db, key, keylen, d, strdata2, strdata2len);
470
+ }
360
471
  rl_free(strdata2);
361
472
  strdata2 = NULL;
362
473
  rl_free(strdata3);
363
474
  strdata3 = NULL;
364
475
  }
476
+ free_string_streamer(substreamer);
365
477
  rl_free(strdata);
366
478
  strdata = NULL;
367
479
  }
368
480
  else if (type == REDIS_RDB_TYPE_HASH_ZIPLIST) {
369
- RL_CALL(read_string, RL_OK, data, &strdata, &strdatalen, &data);
370
- tmpdata = strdata + 10;
371
- while (*tmpdata != 255) {
372
- tmpdata = read_ziplist_entry(tmpdata, &strdata2, &strdata2len);
373
- if (!tmpdata) {
374
- retval = RL_UNEXPECTED;
375
- goto cleanup;
481
+ RL_CALL(read_string, RL_OK, streamer, &strdata, &strdatalen);
482
+ rl_restore_streamer *substreamer = init_string_streamer(strdata, strdatalen);
483
+ RL_CALL(read, RL_OK, substreamer, NULL, 10);
484
+ while (1) {
485
+ RL_CALL(read, RL_OK, substreamer, &ucaux, 1);
486
+ if (ucaux == 255) {
487
+ break;
376
488
  }
377
- tmpdata = read_ziplist_entry(tmpdata, &strdata3, &strdata3len);
378
- if (!tmpdata) {
379
- retval = RL_UNEXPECTED;
380
- goto cleanup;
489
+ RL_CALL(read_ziplist_entry, RL_OK, substreamer, ucaux, &strdata2, &strdata2len);
490
+ RL_CALL(read, RL_OK, substreamer, NULL, 1);
491
+ RL_CALL(read_ziplist_entry, RL_OK, substreamer, ucaux, &strdata3, &strdata3len);
492
+ if (key) {
493
+ RL_CALL(rl_hset, RL_OK, db, key, keylen, strdata2, strdata2len, strdata3, strdata3len, NULL, 0);
381
494
  }
382
- RL_CALL(rl_hset, RL_OK, db, key, keylen, strdata2, strdata2len, strdata3, strdata3len, NULL, 0);
383
495
  rl_free(strdata2);
384
496
  strdata2 = NULL;
385
497
  rl_free(strdata3);
386
498
  strdata3 = NULL;
387
499
  }
500
+ free_string_streamer(substreamer);
388
501
  rl_free(strdata);
389
502
  strdata = NULL;
390
503
  } else {
@@ -399,3 +512,21 @@ cleanup:
399
512
  rl_free(strdata3);
400
513
  return retval;
401
514
  }
515
+
516
+ int rl_restore(struct rlite *db, const unsigned char *key, long keylen, unsigned long long expires, unsigned char *data, long datalen)
517
+ {
518
+ int retval;
519
+ rl_restore_streamer* streamer = NULL;
520
+ RL_CALL(verify, RL_OK, data, datalen);
521
+ streamer = init_string_streamer(data, datalen);
522
+ if (!streamer) {
523
+ retval = RL_OUT_OF_MEMORY;
524
+ goto cleanup;
525
+ }
526
+ RL_CALL(rl_restore_stream, RL_OK, db, key, keylen, expires, streamer);
527
+ cleanup:
528
+ if (streamer) {
529
+ free_string_streamer(streamer);
530
+ }
531
+ return retval;
532
+ }
@@ -1,3 +1,9 @@
1
1
  #include "rlite.h"
2
2
 
3
+ typedef struct rl_restore_streamer {
4
+ void *context;
5
+ int (*read)(struct rl_restore_streamer *streamer, unsigned char *str, long len);
6
+ } rl_restore_streamer;
7
+
3
8
  int rl_restore(struct rlite *db, const unsigned char *key, long keylen, unsigned long long expires, unsigned char *data, long datalen);
9
+ int rl_restore_stream(struct rlite *db, const unsigned char *key, long keylen, unsigned long long expires, rl_restore_streamer *streamer);