1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-01-18 04:58:51 +02:00

Always advance id when pack field is NULL.

This was done in the internal versions but not the user-facing function. That meant the field had to be explicitly read after determining it was NULL, which is wasteful.

Since there is only one behavior now, remove pckReadDefaultNull() and move the logic to pckReadNullInternal().
This commit is contained in:
David Steele 2020-12-16 09:59:48 -05:00
parent 39963f6aa5
commit 558ff1e555
3 changed files with 22 additions and 39 deletions

View File

@ -592,9 +592,18 @@ pckReadNullInternal(PackRead *this, unsigned int *id)
ASSERT(this != NULL);
ASSERT(id != NULL);
// Read tag at specified id
pckReadTag(this, id, pckTypeUnknown, true);
FUNCTION_TEST_RETURN(*id < this->tagNextId);
// If the field is NULL then set idLast (to avoid rechecking the same id on the next call) and return true
if (*id < this->tagNextId)
{
this->tagStackTop->idLast = *id;
FUNCTION_TEST_RETURN(true);
}
// The field is not NULL
FUNCTION_TEST_RETURN(false);
}
bool
@ -610,31 +619,6 @@ pckReadNull(PackRead *this, PackIdParam param)
FUNCTION_TEST_RETURN(pckReadNullInternal(this, &param.id));
}
/***********************************************************************************************************************************
Helper function to determine whether a default should be returned when the field is NULL (missing)
***********************************************************************************************************************************/
static inline bool
pckReadDefaultNull(PackRead *this, unsigned int *id)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(PACK_READ, this);
FUNCTION_TEST_PARAM_P(UINT, id);
FUNCTION_TEST_END();
ASSERT(this != NULL);
ASSERT(id != NULL);
// If the field is NULL then set idLast (to avoid rechecking the same id on the next call) and return true
if (pckReadNullInternal(this, id))
{
this->tagStackTop->idLast = *id;
FUNCTION_TEST_RETURN(true);
}
// The field is not NULL
FUNCTION_TEST_RETURN(false);
}
/**********************************************************************************************************************************/
PackType
pckReadType(PackRead *this)
@ -705,7 +689,7 @@ pckReadBin(PackRead *this, PckReadBinParam param)
ASSERT(this != NULL);
if (pckReadDefaultNull(this, &param.id))
if (pckReadNullInternal(this, &param.id))
FUNCTION_TEST_RETURN(NULL);
Buffer *result = NULL;
@ -743,7 +727,7 @@ pckReadBool(PackRead *this, PckReadBoolParam param)
ASSERT(this != NULL);
if (pckReadDefaultNull(this, &param.id))
if (pckReadNullInternal(this, &param.id))
FUNCTION_TEST_RETURN(param.defaultValue);
FUNCTION_TEST_RETURN(pckReadTag(this, &param.id, pckTypeBool, false));
@ -761,7 +745,7 @@ pckReadI32(PackRead *this, PckReadInt32Param param)
ASSERT(this != NULL);
if (pckReadDefaultNull(this, &param.id))
if (pckReadNullInternal(this, &param.id))
FUNCTION_TEST_RETURN(param.defaultValue);
FUNCTION_TEST_RETURN(cvtInt32FromZigZag((uint32_t)pckReadTag(this, &param.id, pckTypeI32, false)));
@ -779,7 +763,7 @@ pckReadI64(PackRead *this, PckReadInt64Param param)
ASSERT(this != NULL);
if (pckReadDefaultNull(this, &param.id))
if (pckReadNullInternal(this, &param.id))
FUNCTION_TEST_RETURN(param.defaultValue);
FUNCTION_TEST_RETURN(cvtInt64FromZigZag(pckReadTag(this, &param.id, pckTypeI64, false)));
@ -842,7 +826,7 @@ pckReadPtr(PackRead *this, PckReadPtrParam param)
ASSERT(this != NULL);
if (pckReadDefaultNull(this, &param.id))
if (pckReadNullInternal(this, &param.id))
FUNCTION_TEST_RETURN(NULL);
FUNCTION_TEST_RETURN((void *)(uintptr_t)pckReadTag(this, &param.id, pckTypePtr, false));
@ -860,7 +844,7 @@ pckReadStr(PackRead *this, PckReadStrParam param)
ASSERT(this != NULL);
if (pckReadDefaultNull(this, &param.id))
if (pckReadNullInternal(this, &param.id))
FUNCTION_TEST_RETURN(strDup(param.defaultValue));
String *result = NULL;
@ -900,7 +884,7 @@ pckReadTime(PackRead *this, PckReadTimeParam param)
ASSERT(this != NULL);
if (pckReadDefaultNull(this, &param.id))
if (pckReadNullInternal(this, &param.id))
FUNCTION_TEST_RETURN(param.defaultValue);
FUNCTION_TEST_RETURN((time_t)cvtInt64FromZigZag(pckReadTag(this, &param.id, pckTypeTime, false)));
@ -918,7 +902,7 @@ pckReadU32(PackRead *this, PckReadUInt32Param param)
ASSERT(this != NULL);
if (pckReadDefaultNull(this, &param.id))
if (pckReadNullInternal(this, &param.id))
FUNCTION_TEST_RETURN(param.defaultValue);
FUNCTION_TEST_RETURN((uint32_t)pckReadTag(this, &param.id, pckTypeU32, false));
@ -936,7 +920,7 @@ pckReadU64(PackRead *this, PckReadUInt64Param param)
ASSERT(this != NULL);
if (pckReadDefaultNull(this, &param.id))
if (pckReadNullInternal(this, &param.id))
FUNCTION_TEST_RETURN(param.defaultValue);
FUNCTION_TEST_RETURN(pckReadTag(this, &param.id, pckTypeU64, false));

View File

@ -150,7 +150,8 @@ unsigned int pckReadId(PackRead *this);
// Current field type. Set after a call to pckReadNext().
PackType pckReadType(PackRead *this);
// Is the field NULL?
// Is the field NULL? If the field is NULL the id will be advanced so the field does not need to be read explictly. If the field is
// not NULL then the id is not advanced since a subsequent read is expected.
#define pckReadNullP(this, ...) \
pckReadNull(this, (PackIdParam){VAR_PARAM_INIT, __VA_ARGS__})

View File

@ -198,7 +198,6 @@ testRun(void)
TEST_ERROR(pckReadU32P(packRead, .id = 7), FormatError, "field 7 is type 'u64' but expected 'u32'");
TEST_RESULT_UINT(pckReadU64P(packRead, .id = 7), 0xFFFFFFFFFFFFFFFF, "read max u64");
TEST_RESULT_BOOL(pckReadNullP(packRead, .id = 9), true, "field 9 is null");
TEST_RESULT_UINT(pckReadU64P(packRead, .id = 9), 0, "field 9 default is 0");
TEST_RESULT_BOOL(pckReadNullP(packRead, .id = 10), false, "field 10 is not null");
TEST_RESULT_UINT(pckReadU64P(packRead, .id = 10), 1, "read 1");
TEST_RESULT_UINT(pckReadU32P(packRead, .id = 12), 127, "read 127 (skip field 11)");
@ -212,7 +211,6 @@ testRun(void)
TEST_ERROR(pckReadArrayEndP(packRead), FormatError, "not in array");
TEST_RESULT_BOOL(pckReadBoolP(packRead), true, "read true");
TEST_RESULT_BOOL(pckReadBoolP(packRead), false, "read false");
TEST_RESULT_BOOL(pckReadNullP(packRead), true, "field 3 is null");
TEST_RESULT_BOOL(pckReadBoolP(packRead), false, "field 3 default is false");
TEST_RESULT_BOOL(pckReadNullP(packRead, .id = 4), true, "field 4 is null");
TEST_RESULT_BOOL(pckReadBoolP(packRead), false, "read default false");
@ -265,7 +263,7 @@ testRun(void)
TEST_RESULT_UINT(bufSize(pckReadBinP(packRead)), 0, "read bin zero length");
TEST_RESULT_BOOL(pckReadNullP(packRead, .id = 999), true, "field 999 is null");
TEST_RESULT_UINT(pckReadU64P(packRead, .id = 999), 0, "field 999 default is 0");
TEST_RESULT_UINT(pckReadU64P(packRead, .id = 1000), 0, "field 1000 default is 0");
TEST_RESULT_VOID(pckReadEndP(packRead), "end");
TEST_RESULT_VOID(pckReadFree(packRead), "free");