Skip to content

Commit 0759d47

Browse files
RONDB-768: Modified handling of SET commands
1 parent 6e2f0b5 commit 0759d47

9 files changed

+301
-301
lines changed

pink/rondis/common.cc

+16
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,19 @@ void assign_generic_err_to_response(
2525
std::cout << buf;
2626
response->assign(buf);
2727
}
28+
29+
void set_length(char *buf, Uint32 key_len)
30+
{
31+
Uint8 *ptr = (Uint8 *)buf;
32+
ptr[0] = (Uint8)(key_len & 255);
33+
ptr[1] = (Uint8)(key_len >> 8);
34+
}
35+
36+
Uint32 get_length(char *buf)
37+
{
38+
Uint8 *ptr = (Uint8 *)buf;
39+
Uint8 low = ptr[0];
40+
Uint8 high = ptr[1];
41+
Uint32 len32 = Uint32(low) + Uint32(256) * Uint32(high);
42+
return len32;
43+
}

pink/rondis/common.h

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
int write_formatted(char *buffer, int bufferSize, const char *format, ...);
1717
void assign_ndb_err_to_response(std::string *response, const char *app_str, NdbError error);
1818
void assign_generic_err_to_response(std::string *response, const char *app_str);
19+
void set_length(char* buf, Uint32 key_len);
20+
Uint32 get_length(char* buf);
1921

2022
// NDB API error messages
2123
#define FAILED_GET_DICT "Failed to get NdbDict"

pink/rondis/string/commands.cc

+60-33
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ void rondb_set(
162162
Uint32 value_len = argv[arg_index_start + 1].size();
163163
char varsize_param[EXTENSION_VALUE_LEN + 500];
164164
Uint32 num_value_rows = 0;
165+
Uint32 prev_num_rows = 0;
165166
Uint64 rondb_key = 0;
166167

167168
if (value_len > INLINE_VALUE_LEN)
@@ -191,7 +192,6 @@ void rondb_set(
191192

192193
int ret_code = 0;
193194
ret_code = create_key_row(response,
194-
ndb,
195195
tab,
196196
trans,
197197
redis_key_id,
@@ -201,8 +201,8 @@ void rondb_set(
201201
value_str,
202202
value_len,
203203
num_value_rows,
204-
Uint32(0),
205-
&varsize_param[0]);
204+
prev_num_rows,
205+
Uint32(0));
206206
if (ret_code != 0)
207207
{
208208
// Often unnecessary since it already failed to commit
@@ -226,41 +226,68 @@ void rondb_set(
226226
assign_ndb_err_to_response(response, FAILED_CREATE_TXN_OBJECT, ndb->getNdbError());
227227
return;
228228
}
229-
if (delete_and_insert_key_row(response,
230-
ndb,
231-
tab,
232-
trans,
233-
redis_key_id,
234-
rondb_key,
235-
key_str,
236-
key_len,
237-
value_str,
238-
value_len,
239-
num_value_rows,
240-
Uint32(0),
241-
&varsize_param[0]) != 0)
242-
{
243-
ndb->closeTransaction(trans);
244-
return;
245-
}
246-
}
247-
248-
if (num_value_rows == 0)
249-
{
229+
/**
230+
* We don't know the exact number of value rows, but we know that it is
231+
* at least one.
232+
*/
233+
prev_num_rows = 1;
234+
ret_code = create_key_row(response,
235+
tab,
236+
trans,
237+
redis_key_id,
238+
rondb_key,
239+
key_str,
240+
key_len,
241+
value_str,
242+
value_len,
243+
num_value_rows,
244+
prev_num_rows,
245+
Uint32(0));
246+
} else if (num_value_rows == 0) {
250247
ndb->closeTransaction(trans);
251248
response->append("+OK\r\n");
252249
return;
253250
}
254-
create_all_value_rows(response,
255-
ndb,
256-
dict,
257-
trans,
258-
rondb_key,
259-
value_str,
260-
value_len,
261-
num_value_rows,
262-
&varsize_param[0]);
251+
/**
252+
* Coming here means that we either have to add new value rows or we have
253+
* to delete previous value rows or both. Thus the transaction is still
254+
* open. We start by creating the new value rows. Next we delete the
255+
* remaining value rows from the previous instantiation of the row.
256+
*/
257+
if (num_value_rows > 0) {
258+
ret_code = create_all_value_rows(response,
259+
ndb,
260+
dict,
261+
trans,
262+
rondb_key,
263+
value_str,
264+
value_len,
265+
num_value_rows,
266+
&varsize_param[0]);
267+
}
268+
if (ret_code != 0) {
269+
return;
270+
}
271+
ret_code = delete_value_rows(response,
272+
tab,
273+
trans,
274+
rondb_key,
275+
num_value_rows,
276+
prev_num_rows);
277+
if (ret_code != 0) {
278+
return;
279+
}
280+
if (trans->execute(NdbTransaction::Commit,
281+
NdbOperation::AbortOnError) == 0 &&
282+
trans->getNdbError().code != 0)
283+
{
284+
assign_ndb_err_to_response(response,
285+
FAILED_EXEC_TXN,
286+
trans->getNdbError());
287+
return;
288+
}
263289
ndb->closeTransaction(trans);
290+
response->append("+OK\r\n");
264291
return;
265292
}
266293

pink/rondis/string/commands.h

-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
Most importantly, it writes Ndb error messages to the response string. This may
2323
however change in the future, since this causes redundancy.
2424
*/
25-
void set_length(char* buf, Uint32 key_len);
26-
Uint32 get_length(char* buf);
27-
2825
void rondb_get_command(Ndb *ndb,
2926
const pink::RedisCmdArgsType &argv,
3027
std::string *response);

0 commit comments

Comments
 (0)