Skip to content

Commit 62ee098

Browse files
committed
Refactor struct RdbxRespWriterStartCmd init to use compound literal syntax
1 parent 92ec828 commit 62ee098

File tree

3 files changed

+23
-80
lines changed

3 files changed

+23
-80
lines changed

api/librdb-ext-api.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ _LIBRDB_API RdbxToResp *RDBX_createHandlersToResp(RdbParser *, RdbxToRespConf *)
199199
* <user-defined-writer>
200200
****************************************************************/
201201

202-
/* On start command pass command info. NULL otherwise. */
202+
/* As streaming RESP protocol, when starting a new command, provide details
203+
* about the command. Otherwise, pass NULL. This information will be used to log
204+
* and report the command in case of a failure from Redis server. */
203205
typedef struct RdbxRespWriterStartCmd {
204206
/* Redis Command name (Ex: "SET", "RESTORE"). Owned by the caller. It is
205207
* constant static string and Valid for ref behind the duration of the call. */

src/ext/handlersToResp.c

+18-71
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,7 @@ static inline RdbRes onWriteNewCmdDbg(RdbxToResp *ctx) {
225225
if (ctx->debug.flags & RFLAG_ENUM_CMD_ID) {
226226
char keyLenStr[32], cmdIdLenStr[32], cmdIdStr[32];
227227

228-
RdbxRespWriterStartCmd startCmd;
229-
startCmd.cmd = "SET";
230-
startCmd.key = KEY_CMD_ID_DBG;
231-
startCmd.restoreSize = 0;
228+
RdbxRespWriterStartCmd startCmd = {"SET", KEY_CMD_ID_DBG, 0};
232229

233230
struct iovec iov[7];
234231
/* write SET */
@@ -297,10 +294,7 @@ static inline RdbRes sendFirstRestoreFrag(RdbxToResp *ctx, RdbBulk frag, size_t
297294
if (ctx->keyCtx.delBeforeWrite == DEL_KEY_BEFORE_BY_RESTORE_REPLACE)
298295
extra_args++;
299296

300-
RdbxRespWriterStartCmd startCmd;
301-
startCmd.cmd = "RESTORE";
302-
startCmd.key = ctx->keyCtx.key;
303-
startCmd.restoreSize = ctx->restoreCtx.restoreSize;
297+
RdbxRespWriterStartCmd startCmd = {"RESTORE", ctx->keyCtx.key, ctx->restoreCtx.restoreSize};
304298

305299
/* writev RESTORE */
306300
char cmd[64];
@@ -328,10 +322,7 @@ static inline RdbRes sendFirstRestoreFragModuleAux(RdbxToResp *ctx, RdbBulk frag
328322
struct iovec iov[3];
329323
char lenStr[32];
330324

331-
RdbxRespWriterStartCmd startCmd;
332-
startCmd.cmd = "RESTOREMODAUX";
333-
startCmd.key = "";
334-
startCmd.restoreSize = ctx->restoreCtx.restoreSize;
325+
RdbxRespWriterStartCmd startCmd = {"RESTOREMODAUX", "", ctx->restoreCtx.restoreSize};
335326

336327
/* writev RESTOREMODAUX */
337328
iov[0].iov_base = ctx->restoreCtx.moduleAux.cmdPrefix;
@@ -357,10 +348,7 @@ static RdbRes toRespNewDb(RdbParser *p, void *userData, int dbid) {
357348

358349
int cnt = ll2string(dbidStr, sizeof(dbidStr), dbid);
359350

360-
RdbxRespWriterStartCmd startCmd;
361-
startCmd.cmd = "SELECT";
362-
startCmd.key = "";
363-
startCmd.restoreSize = 0;
351+
RdbxRespWriterStartCmd startCmd = {"SELECT", "", 0};
364352

365353
IOV_CONST(&iov[0], "*2\r\n$6\r\nSELECT");
366354
IOV_LENGTH(&iov[1], cnt, cntStr);
@@ -398,10 +386,7 @@ static RdbRes toRespNewKey(RdbParser *p, void *userData, RdbBulk key, RdbKeyInfo
398386
struct iovec iov[4];
399387
char keyLenStr[32];
400388

401-
RdbxRespWriterStartCmd startCmd;
402-
startCmd.cmd = "DEL";
403-
startCmd.key = ctx->keyCtx.key;
404-
startCmd.restoreSize = 0;
389+
RdbxRespWriterStartCmd startCmd = {"DEL", ctx->keyCtx.key, 0};
405390

406391
IOV_CONST(&iov[0], "*2\r\n$3\r\nDEL");
407392
IOV_LENGTH(&iov[1], ctx->keyCtx.keyLen, keyLenStr);
@@ -420,10 +405,7 @@ static RdbRes toRespEndKey(RdbParser *p, void *userData) {
420405
/* key is in db. Set its expiration time */
421406
if (ctx->keyCtx.info.expiretime != -1) {
422407
struct iovec iov[6];
423-
RdbxRespWriterStartCmd startCmd;
424-
startCmd.cmd = "PEXPIREAT";
425-
startCmd.key = ctx->keyCtx.key;
426-
startCmd.restoreSize = 0;
408+
RdbxRespWriterStartCmd startCmd = {"PEXPIREAT", ctx->keyCtx.key, 0};
427409

428410
char keyLenStr[32], expireLenStr[32], expireStr[32];
429411
/* PEXPIREAT */
@@ -454,10 +436,7 @@ static RdbRes toRespString(RdbParser *p, void *userData, RdbBulk string) {
454436

455437
struct iovec iov[7];
456438

457-
RdbxRespWriterStartCmd startCmd;
458-
startCmd.cmd = "SET";
459-
startCmd.key = ctx->keyCtx.key;
460-
startCmd.restoreSize = 0;
439+
RdbxRespWriterStartCmd startCmd = {"SET", ctx->keyCtx.key, 0};
461440

462441
/* write SET */
463442
IOV_CONST(&iov[0], "*3\r\n$3\r\nSET");
@@ -480,10 +459,7 @@ static RdbRes toRespList(RdbParser *p, void *userData, RdbBulk item) {
480459
char keyLenStr[32], valLenStr[32];
481460
int valLen = RDB_bulkLen(p, item);
482461

483-
RdbxRespWriterStartCmd startCmd;
484-
startCmd.cmd = "RPUSH";
485-
startCmd.key = ctx->keyCtx.key;
486-
startCmd.restoreSize = 0;
462+
RdbxRespWriterStartCmd startCmd = {"RPUSH", ctx->keyCtx.key, 0};
487463

488464
/* write RPUSH */
489465
IOV_CONST(&iov[0], "*3\r\n$5\r\nRPUSH");
@@ -508,10 +484,7 @@ static RdbRes toRespHash(RdbParser *p, void *userData, RdbBulk field, RdbBulk va
508484
int fieldLen = RDB_bulkLen(p, field);
509485
int valueLen = RDB_bulkLen(p, value);
510486

511-
RdbxRespWriterStartCmd hsetCmd;
512-
hsetCmd.cmd = "HSET";
513-
hsetCmd.key = ctx->keyCtx.key;
514-
hsetCmd.restoreSize = 0;
487+
RdbxRespWriterStartCmd hsetCmd = {"HSET", ctx->keyCtx.key, 0};
515488

516489
/* write RPUSH */
517490
IOV_CONST(&iov[0], "*4\r\n$4\r\nHSET");
@@ -529,10 +502,7 @@ static RdbRes toRespHash(RdbParser *p, void *userData, RdbBulk field, RdbBulk va
529502

530503
if (expireAt == -1) return RDB_OK;
531504

532-
RdbxRespWriterStartCmd hpexpireatCmd;
533-
hpexpireatCmd.cmd = "HPEXPIREAT";
534-
hpexpireatCmd.key = ctx->keyCtx.key;
535-
hpexpireatCmd.restoreSize = 0;
505+
RdbxRespWriterStartCmd hpexpireatCmd = {"HPEXPIREAT", ctx->keyCtx.key, 0};
536506

537507
/* write HPEXPIREAT */
538508
IOV_CONST(&iov[0], "*6\r\n$10\r\nHPEXPIREAT");
@@ -556,10 +526,7 @@ static RdbRes toRespSet(RdbParser *p, void *userData, RdbBulk member) {
556526

557527
int valLen = RDB_bulkLen(p, member);
558528

559-
RdbxRespWriterStartCmd startCmd;
560-
startCmd.cmd = "SADD";
561-
startCmd.key = ctx->keyCtx.key;
562-
startCmd.restoreSize = 0;
529+
RdbxRespWriterStartCmd startCmd = {"SADD", ctx->keyCtx.key, 0};
563530

564531
/* write RPUSH */
565532
IOV_CONST(&iov[0], "*3\r\n$4\r\nSADD");
@@ -580,10 +547,7 @@ static RdbRes toRespZset(RdbParser *p, void *userData, RdbBulk member, double sc
580547

581548
int valLen = RDB_bulkLen(p, member);
582549

583-
RdbxRespWriterStartCmd startCmd;
584-
startCmd.cmd = "ZADD";
585-
startCmd.key = ctx->keyCtx.key;
586-
startCmd.restoreSize = 0;
550+
RdbxRespWriterStartCmd startCmd = {"ZADD", ctx->keyCtx.key, 0};
587551

588552
/* write ZADD */
589553
IOV_CONST(&iov[0], "*4\r\n$4\r\nZADD");
@@ -628,10 +592,7 @@ static RdbRes toRespFunction(RdbParser *p, void *userData, RdbBulk func) {
628592

629593
int funcLen = RDB_bulkLen(p, func);
630594

631-
RdbxRespWriterStartCmd startCmd;
632-
startCmd.cmd = "FUNCTION";
633-
startCmd.key = "";
634-
startCmd.restoreSize = 0;
595+
RdbxRespWriterStartCmd startCmd = {"FUNCTION", "", 0};
635596

636597
if (ctx->conf.funcLibReplaceIfExist)
637598
IOV_CONST(&iov[0], "*4\r\n$8\r\nFUNCTION\r\n$4\r\nLOAD\r\n$7\r\nREPLACE");
@@ -658,10 +619,7 @@ static RdbRes toRespStreamMetaData(RdbParser *p, void *userData, RdbStreamMeta *
658619
* for the Stream type. (We don't use the MAXLEN 0 trick from aof.c
659620
* because of Redis Enterprise CRDT compatibility issues - Can't XSETID "back") */
660621

661-
RdbxRespWriterStartCmd startCmd;
662-
startCmd.cmd = "XGROUP CREATE";
663-
startCmd.key = ctx->keyCtx.key;
664-
startCmd.restoreSize = 0;
622+
RdbxRespWriterStartCmd startCmd = {"XGROUP CREATE", ctx->keyCtx.key, 0};
665623

666624
IOV_CONST(&iov[0], "*6\r\n$6\r\nXGROUP\r\n$6\r\nCREATE");
667625
IOV_LENGTH(&iov[1], ctx->keyCtx.keyLen, keyLenStr);
@@ -686,10 +644,7 @@ static RdbRes toRespStreamMetaData(RdbParser *p, void *userData, RdbStreamMeta *
686644
int idLen = snprintf(idStr, sizeof(idStr), "%lu-%lu",meta->lastID.ms,meta->lastID.seq);
687645
int maxDelEntryIdLen = snprintf(maxDelEntryId, sizeof(maxDelEntryId), "%lu-%lu", meta->maxDelEntryID.ms, meta->maxDelEntryID.seq);
688646

689-
RdbxRespWriterStartCmd startCmd;
690-
startCmd.cmd = "XSETID";
691-
startCmd.key = ctx->keyCtx.key;
692-
startCmd.restoreSize = 0;
647+
RdbxRespWriterStartCmd startCmd = {"XSETID", ctx->keyCtx.key, 0};
693648

694649
if ((ctx->keyCtx.info.opcode >= _RDB_TYPE_STREAM_LISTPACKS_2) && (ctx->targetRedisVerVal >= VER_VAL(7, 0))) {
695650
IOV_CONST(&iov[0], "*7\r\n$6\r\nXSETID");
@@ -727,9 +682,7 @@ static RdbRes toRespStreamItem(RdbParser *p, void *userData, RdbStreamID *id, Rd
727682

728683
/* Start of (another) stream item? */
729684
if ((ctx->streamCtx.xaddStartEndCounter % 2) == 0) {
730-
startCmd.cmd = "XADD";
731-
startCmd.key = ctx->keyCtx.key;
732-
startCmd.restoreSize = 0;
685+
startCmd = (RdbxRespWriterStartCmd) {"XADD", ctx->keyCtx.key, 0};
733686
startCmdRef = &startCmd;
734687

735688
/* writev XADD */
@@ -780,10 +733,7 @@ static RdbRes toRespStreamNewCGroup(RdbParser *p, void *userData, RdbBulk grpNam
780733

781734
int idLen = snprintf(idStr, sizeof(idStr), "%lu-%lu",meta->lastId.ms,meta->lastId.seq);
782735

783-
RdbxRespWriterStartCmd startCmd;
784-
startCmd.cmd = "XGROUP";
785-
startCmd.key = ctx->keyCtx.key;
786-
startCmd.restoreSize = 0;
736+
RdbxRespWriterStartCmd startCmd = { "XGROUP", ctx->keyCtx.key, 0};
787737

788738
/* writev XGROUP */
789739
if ( (meta->entriesRead>=0) && (ctx->targetRedisVerVal >= VER_VAL(7, 0))) {
@@ -863,10 +813,7 @@ static RdbRes toRespStreamConsumerPendingEntry(RdbParser *p, void *userData, Rdb
863813
return (RdbRes) RDBX_ERR_STREAM_INTEG_CHECK;
864814
}
865815

866-
RdbxRespWriterStartCmd startCmd;
867-
startCmd.cmd = "XCLAIM";
868-
startCmd.key = ctx->keyCtx.key;
869-
startCmd.restoreSize = 0;
816+
RdbxRespWriterStartCmd startCmd = {"XCLAIM", ctx->keyCtx.key, 0};
870817

871818
/* writev XCLAIM */
872819
IOV_CONST(&iov[iovs++], "*12\r\n$6\r\nXCLAIM");

src/ext/respToRedisLoader.c

+2-8
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,7 @@ static RdbRes redisAuthCustomized(RdbxRespToRedisLoader *ctx, RdbxRedisAuth *aut
243243

244244
char prefix[32];
245245

246-
RdbxRespWriterStartCmd startCmd;
247-
startCmd.cmd = "<AUTH_CUSTOMIZED_CMD>";
248-
startCmd.key = "";
249-
startCmd.restoreSize = 0;
246+
RdbxRespWriterStartCmd startCmd = {"<AUTH_CUSTOMIZED_CMD>", "", 0};
250247

251248
/* allocate iovec (2 for header and trailer. 3 for each argument) */
252249
struct iovec *iov = (struct iovec *)malloc((auth->cmd.argc * 3 + 2) * sizeof(struct iovec));
@@ -293,10 +290,7 @@ static RdbRes redisAuth(RdbxRespToRedisLoader *ctx, RdbxRedisAuth *auth) {
293290

294291
/* AUTH [username] password */
295292

296-
RdbxRespWriterStartCmd startCmd;
297-
startCmd.cmd = "AUTH";
298-
startCmd.key = "";
299-
startCmd.restoreSize = 0;
293+
RdbxRespWriterStartCmd startCmd = {"AUTH", "", 0};
300294

301295
struct iovec iov[10];
302296
if (auth->user) {

0 commit comments

Comments
 (0)