Emit the commands needed to rebuild a stream object. * The function returns 0 on error, 1 on success. */
| 1383 | /* Emit the commands needed to rebuild a stream object. |
| 1384 | * The function returns 0 on error, 1 on success. */ |
| 1385 | int rewriteStreamObject(rio *r, robj *key, robj *o) { |
| 1386 | stream *s = (stream*)ptrFromObj(o); |
| 1387 | streamIterator si; |
| 1388 | streamIteratorStart(&si,s,NULL,NULL,0); |
| 1389 | streamID id; |
| 1390 | int64_t numfields; |
| 1391 | |
| 1392 | if (s->length) { |
| 1393 | /* Reconstruct the stream data using XADD commands. */ |
| 1394 | while(streamIteratorGetID(&si,&id,&numfields)) { |
| 1395 | /* Emit a two elements array for each item. The first is |
| 1396 | * the ID, the second is an array of field-value pairs. */ |
| 1397 | |
| 1398 | /* Emit the XADD <key> <id> ...fields... command. */ |
| 1399 | if (!rioWriteBulkCount(r,'*',3+numfields*2) || |
| 1400 | !rioWriteBulkString(r,"XADD",4) || |
| 1401 | !rioWriteBulkObject(r,key) || |
| 1402 | !rioWriteBulkStreamID(r,&id)) |
| 1403 | { |
| 1404 | streamIteratorStop(&si); |
| 1405 | return 0; |
| 1406 | } |
| 1407 | while(numfields--) { |
| 1408 | unsigned char *field, *value; |
| 1409 | int64_t field_len, value_len; |
| 1410 | streamIteratorGetField(&si,&field,&value,&field_len,&value_len); |
| 1411 | if (!rioWriteBulkString(r,(char*)field,field_len) || |
| 1412 | !rioWriteBulkString(r,(char*)value,value_len)) |
| 1413 | { |
| 1414 | streamIteratorStop(&si); |
| 1415 | return 0; |
| 1416 | } |
| 1417 | } |
| 1418 | } |
| 1419 | } else { |
| 1420 | /* Use the XADD MAXLEN 0 trick to generate an empty stream if |
| 1421 | * the key we are serializing is an empty string, which is possible |
| 1422 | * for the Stream type. */ |
| 1423 | id.ms = 0; id.seq = 1; |
| 1424 | if (!rioWriteBulkCount(r,'*',7) || |
| 1425 | !rioWriteBulkString(r,"XADD",4) || |
| 1426 | !rioWriteBulkObject(r,key) || |
| 1427 | !rioWriteBulkString(r,"MAXLEN",6) || |
| 1428 | !rioWriteBulkString(r,"0",1) || |
| 1429 | !rioWriteBulkStreamID(r,&id) || |
| 1430 | !rioWriteBulkString(r,"x",1) || |
| 1431 | !rioWriteBulkString(r,"y",1)) |
| 1432 | { |
| 1433 | streamIteratorStop(&si); |
| 1434 | return 0; |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | /* Append XSETID after XADD, make sure lastid is correct, |
| 1439 | * in case of XDEL lastid. */ |
| 1440 | if (!rioWriteBulkCount(r,'*',3) || |
| 1441 | !rioWriteBulkString(r,"XSETID",6) || |
| 1442 | !rioWriteBulkObject(r,key) || |
no test coverage detected