1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-12 10:04:14 +02:00

Read tag size in pckReadTagNext().

Rather than reading the size everywhere it is needed, get it when the tag is read, if it exists.

This simplifies logic around consuming the data when not needed. There are more use cases for this coming up.
This commit is contained in:
David Steele 2021-10-09 12:15:19 -04:00
parent ed68792e76
commit 7ab8dcbe6e

View File

@ -246,6 +246,7 @@ struct PackRead
unsigned int tagNextId; // Next tag id
PackTypeMap tagNextTypeMap; // Next tag type map
uint64_t tagNextValue; // Next tag value
size_t tagNextSize; // Next tag size
PackTagStack tagStack; // Stack of object/array tags
};
@ -443,6 +444,28 @@ pckReadBuffer(PackRead *this, size_t size)
FUNCTION_TEST_RETURN(size);
}
/***********************************************************************************************************************************
Consume buffer left in tagNextSize
***********************************************************************************************************************************/
static void
pckReadConsumeBuffer(PackRead *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(PACK_READ, this);
FUNCTION_TEST_END();
ASSERT(this != NULL);
while (this->tagNextSize != 0)
{
size_t size = pckReadBuffer(this, this->tagNextSize);
this->bufferPos += size;
this->tagNextSize -= size;
}
FUNCTION_TEST_RETURN_VOID();
}
/***********************************************************************************************************************************
Unpack an unsigned 64-bit integer from base-128 varint encoding
***********************************************************************************************************************************/
@ -579,6 +602,15 @@ pckReadTagNext(PackRead *this)
// Increment the next tag id
this->tagNextId += this->tagStack.top->idLast + 1;
// Get tag size if it exists
if (packTypeMapData[this->tagNextTypeMap].size &&
(this->tagNextValue > 0 || !packTypeMapData[this->tagNextTypeMap].valueSingleBit))
{
this->tagNextSize = (size_t)pckReadU64Internal(this);
}
else
this->tagNextSize = 0;
// Tag was found
result = true;
}
@ -648,17 +680,7 @@ pckReadTag(PackRead *this, unsigned int *id, PackTypeMap typeMap, bool peek)
}
// Read data for the field being skipped if this is not the field requested
if (packTypeMapData[this->tagNextTypeMap].size && this->tagNextValue != 0)
{
size_t sizeExpected = (size_t)pckReadU64Internal(this);
while (sizeExpected != 0)
{
size_t sizeRead = pckReadBuffer(this, sizeExpected);
sizeExpected -= sizeRead;
this->bufferPos += sizeRead;
}
}
pckReadConsumeBuffer(this);
// Increment the last id to the id just read
this->tagStack.top->idLast = this->tagNextId;
@ -816,7 +838,7 @@ pckReadBin(PackRead *this, PckReadBinParam param)
if (pckReadTag(this, &param.id, pckTypeMapBin, false))
{
// Get the buffer size
result = bufNew((size_t)pckReadU64Internal(this));
result = bufNew(this->tagNextSize);
// Read the buffer out in chunks
while (bufUsed(result) < bufSize(result))
@ -985,7 +1007,7 @@ pckReadPack(PackRead *const this, PckReadPackParam param)
pckReadTag(this, &param.id, pckTypeMapPack, false);
// Get the pack size
Buffer *result = bufNew((size_t)pckReadU64Internal(this));
Buffer *result = bufNew(this->tagNextSize);
// Read the pack out in chunks
while (bufUsed(result) < bufSize(result))
@ -1036,7 +1058,7 @@ pckReadStr(PackRead *this, PckReadStrParam param)
if (pckReadTag(this, &param.id, pckTypeMapStr, false))
{
// Read the string size
size_t sizeExpected = (size_t)pckReadU64Internal(this);
size_t sizeExpected = (size_t)this->tagNextSize;
// Read the string out in chunks
result = strNew();