1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-09 14:21:03 +03:00

In JSON - minor code cleanup and refactoring with a small size reduction

and performance increase.

FossilOrigin-Name: 215fabda38daecdbd38b1eca5a6aafbc61b6a36a8303f1d7164d5a1138e63134
This commit is contained in:
drh
2023-12-19 21:39:58 +00:00
parent c52aebb443
commit 3262ca83a7
3 changed files with 35 additions and 37 deletions

View File

@@ -1,5 +1,5 @@
C Remove\sredundant\sconditional\sfrom\ssqlite3ExprCanBeNull(). C In\sJSON\s-\sminor\scode\scleanup\sand\srefactoring\swith\sa\ssmall\ssize\sreduction\nand\sperformance\sincrease.
D 2023-12-19T15:51:14.357 D 2023-12-19T21:39:58.157
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -696,7 +696,7 @@ F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51
F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6 F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6
F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71 F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276 F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276
F src/json.c 661b6da631185152c635161d52c274255c8de3f5769570bbbde0311cd9ad7460 F src/json.c c6fae579d681d5ab43387c8667cc6ef0c0f8769fd881fde3af6260e837cae762
F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
F src/loadext.c 7432c944ff197046d67a1207790a1b13eec4548c85a9457eb0896bb3641dfb36 F src/loadext.c 7432c944ff197046d67a1207790a1b13eec4548c85a9457eb0896bb3641dfb36
F src/main.c ce714ee501122c76eb2e69b292bebe443aba611fc3b88e6786eb910285515fe4 F src/main.c ce714ee501122c76eb2e69b292bebe443aba611fc3b88e6786eb910285515fe4
@@ -2155,8 +2155,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 36fe6a61ef8fb393281a5e15119d716521219c7b971fbfd63bdea07d27a78ac9 P 257f96a2d22c605885fa66220c28cf7dc5941c330bccee3f132b9e7b70d89d30
R 8d141472bcc17284a46ea446e12c41f3 R 1e43c523c137344cae8cdc90cc8b1706
U drh U drh
Z 7720e1377fde94e934586312bc65b10d Z 35aaca72ca93a246000bef5de54a54bb
# Remove this line to create a well-formed Fossil manifest. # Remove this line to create a well-formed Fossil manifest.

View File

@@ -1 +1 @@
257f96a2d22c605885fa66220c28cf7dc5941c330bccee3f132b9e7b70d89d30 215fabda38daecdbd38b1eca5a6aafbc61b6a36a8303f1d7164d5a1138e63134

View File

@@ -20,9 +20,9 @@
** All generated JSON text still conforms strictly to RFC-8259, but text ** All generated JSON text still conforms strictly to RFC-8259, but text
** with JSON-5 extensions is accepted as input. ** with JSON-5 extensions is accepted as input.
** **
** Beginning with version 3.45.0 (pending), these routines also accept ** Beginning with version 3.45.0 (circa 2024-01-01), these routines also
** BLOB values that have JSON encoded using a binary representation we ** accept BLOB values that have JSON encoded using a binary representation
** call JSONB. The name JSONB comes from PostgreSQL, however the on-disk ** called "JSONB". The name JSONB comes from PostgreSQL, however the on-disk
** format SQLite JSONB is completely different and incompatible with ** format SQLite JSONB is completely different and incompatible with
** PostgreSQL JSONB. ** PostgreSQL JSONB.
** **
@@ -220,6 +220,8 @@ typedef struct JsonParse JsonParse;
** * The aBlob[] array must be owned by the JsonParse object. In other ** * The aBlob[] array must be owned by the JsonParse object. In other
** words, nBlobAlloc must be non-zero. ** words, nBlobAlloc must be non-zero.
** **
** * eEdit and delta must be zero.
**
** * zJson must be an RCStr. In other words bJsonIsRCStr must be true. ** * zJson must be an RCStr. In other words bJsonIsRCStr must be true.
*/ */
struct JsonCache { struct JsonCache {
@@ -279,8 +281,8 @@ struct JsonString {
** json_replace() or json_patch() or similar). ** json_replace() or json_patch() or similar).
** **
** 4. New JSON text is generated from the aBlob[] for output. This step ** 4. New JSON text is generated from the aBlob[] for output. This step
** is skipped the function is one of the jsonb_* functions that returns ** is skipped if the function is one of the jsonb_* functions that
** JSONB instead of text JSON. ** returns JSONB instead of text JSON.
*/ */
struct JsonParse { struct JsonParse {
u8 *aBlob; /* JSONB representation of JSON value */ u8 *aBlob; /* JSONB representation of JSON value */
@@ -288,14 +290,14 @@ struct JsonParse {
u32 nBlobAlloc; /* Bytes allocated to aBlob[]. 0 if aBlob is external */ u32 nBlobAlloc; /* Bytes allocated to aBlob[]. 0 if aBlob is external */
char *zJson; /* Json text used for parsing */ char *zJson; /* Json text used for parsing */
int nJson; /* Length of the zJson string in bytes */ int nJson; /* Length of the zJson string in bytes */
u32 nJPRef; /* Number of references to this object */
u32 iErr; /* Error location in zJson[] */
u16 iDepth; /* Nesting depth */ u16 iDepth; /* Nesting depth */
u8 nErr; /* Number of errors seen */ u8 nErr; /* Number of errors seen */
u8 oom; /* Set to true if out of memory */ u8 oom; /* Set to true if out of memory */
u8 bJsonIsRCStr; /* True if zJson is an RCStr */ u8 bJsonIsRCStr; /* True if zJson is an RCStr */
u8 hasNonstd; /* True if input uses non-standard features like JSON5 */ u8 hasNonstd; /* True if input uses non-standard features like JSON5 */
u8 bReadOnly; /* Do not modify. */ u8 bReadOnly; /* Do not modify. */
u32 nJPRef; /* Number of references to this object */
u32 iErr; /* Error location in zJson[] */
/* Search and edit information. See jsonLookupStep() */ /* Search and edit information. See jsonLookupStep() */
u8 eEdit; /* Edit operation to apply */ u8 eEdit; /* Edit operation to apply */
int delta; /* Size change due to the edit */ int delta; /* Size change due to the edit */
@@ -334,7 +336,7 @@ struct JsonParse {
**************************************************************************/ **************************************************************************/
static void jsonReturnStringAsBlob(JsonString*); static void jsonReturnStringAsBlob(JsonString*);
static int jsonFuncArgMightBeBinary(sqlite3_value *pJson); static int jsonFuncArgMightBeBinary(sqlite3_value *pJson);
static u32 jsonXlateBlobToText(const JsonParse*,u32,JsonString*); static u32 jsonTranslateBlobToText(const JsonParse*,u32,JsonString*);
static void jsonReturnParse(sqlite3_context*,JsonParse*); static void jsonReturnParse(sqlite3_context*,JsonParse*);
static JsonParse *jsonParseFuncArg(sqlite3_context*,sqlite3_value*,u32); static JsonParse *jsonParseFuncArg(sqlite3_context*,sqlite3_value*,u32);
static void jsonParseFree(JsonParse*); static void jsonParseFree(JsonParse*);
@@ -709,7 +711,7 @@ static void jsonAppendSqlValue(
memset(&px, 0, sizeof(px)); memset(&px, 0, sizeof(px));
px.aBlob = (u8*)sqlite3_value_blob(pValue); px.aBlob = (u8*)sqlite3_value_blob(pValue);
px.nBlob = sqlite3_value_bytes(pValue); px.nBlob = sqlite3_value_bytes(pValue);
jsonXlateBlobToText(&px, 0, p); jsonTranslateBlobToText(&px, 0, p);
}else if( p->eErr==0 ){ }else if( p->eErr==0 ){
sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1); sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
p->eErr = JSTRING_ERR; p->eErr = JSTRING_ERR;
@@ -1226,15 +1228,11 @@ static int jsonBlobChangePayloadSize(
*/ */
static int jsonIs4HexB(const char *z, int *pOp){ static int jsonIs4HexB(const char *z, int *pOp){
if( z[0]!='u' ) return 0; if( z[0]!='u' ) return 0;
if( !sqlite3Isxdigit(z[1]) ) return 0; if( !jsonIs4Hex(&z[1]) ) return 0;
if( !sqlite3Isxdigit(z[2]) ) return 0;
if( !sqlite3Isxdigit(z[3]) ) return 0;
if( !sqlite3Isxdigit(z[4]) ) return 0;
*pOp = JSONB_TEXTJ; *pOp = JSONB_TEXTJ;
return 1; return 1;
} }
/* /*
** Check a single element of the JSONB in pParse for validity. ** Check a single element of the JSONB in pParse for validity.
** **
@@ -1451,7 +1449,7 @@ static u32 jsonbValidityCheck(
** -4 ',' seen / the index in zJson[] of the seen character ** -4 ',' seen / the index in zJson[] of the seen character
** -5 ':' seen / ** -5 ':' seen /
*/ */
static int jsonXlateTextToBlob(JsonParse *pParse, u32 i){ static int jsonTranslateTextToBlob(JsonParse *pParse, u32 i){
char c; char c;
u32 j; u32 j;
u32 iThis, iStart; u32 iThis, iStart;
@@ -1471,7 +1469,7 @@ json_parse_restart:
iStart = pParse->nBlob; iStart = pParse->nBlob;
for(j=i+1;;j++){ for(j=i+1;;j++){
u32 iBlob = pParse->nBlob; u32 iBlob = pParse->nBlob;
x = jsonXlateTextToBlob(pParse, j); x = jsonTranslateTextToBlob(pParse, j);
if( x<=0 ){ if( x<=0 ){
int op; int op;
if( x==(-2) ){ if( x==(-2) ){
@@ -1517,7 +1515,7 @@ json_parse_restart:
goto parse_object_value; goto parse_object_value;
} }
} }
x = jsonXlateTextToBlob(pParse, j); x = jsonTranslateTextToBlob(pParse, j);
if( x!=(-5) ){ if( x!=(-5) ){
if( x!=(-1) ) pParse->iErr = j; if( x!=(-1) ) pParse->iErr = j;
return -1; return -1;
@@ -1525,7 +1523,7 @@ json_parse_restart:
j = pParse->iErr+1; j = pParse->iErr+1;
} }
parse_object_value: parse_object_value:
x = jsonXlateTextToBlob(pParse, j); x = jsonTranslateTextToBlob(pParse, j);
if( x<=0 ){ if( x<=0 ){
if( x!=(-1) ) pParse->iErr = j; if( x!=(-1) ) pParse->iErr = j;
return -1; return -1;
@@ -1544,7 +1542,7 @@ json_parse_restart:
break; break;
} }
} }
x = jsonXlateTextToBlob(pParse, j); x = jsonTranslateTextToBlob(pParse, j);
if( x==(-4) ){ if( x==(-4) ){
j = pParse->iErr; j = pParse->iErr;
continue; continue;
@@ -1572,7 +1570,7 @@ json_parse_restart:
return -1; return -1;
} }
for(j=i+1;;j++){ for(j=i+1;;j++){
x = jsonXlateTextToBlob(pParse, j); x = jsonTranslateTextToBlob(pParse, j);
if( x<=0 ){ if( x<=0 ){
if( x==(-3) ){ if( x==(-3) ){
j = pParse->iErr; j = pParse->iErr;
@@ -1596,7 +1594,7 @@ json_parse_restart:
break; break;
} }
} }
x = jsonXlateTextToBlob(pParse, j); x = jsonTranslateTextToBlob(pParse, j);
if( x==(-4) ){ if( x==(-4) ){
j = pParse->iErr; j = pParse->iErr;
continue; continue;
@@ -1919,7 +1917,7 @@ static int jsonConvertTextToBlob(
){ ){
int i; int i;
const char *zJson = pParse->zJson; const char *zJson = pParse->zJson;
i = jsonXlateTextToBlob(pParse, 0); i = jsonTranslateTextToBlob(pParse, 0);
if( pParse->oom ) i = -1; if( pParse->oom ) i = -1;
if( i>0 ){ if( i>0 ){
#ifdef SQLITE_DEBUG #ifdef SQLITE_DEBUG
@@ -1964,7 +1962,7 @@ static void jsonReturnStringAsBlob(JsonString *pStr){
jsonStringTerminate(pStr); jsonStringTerminate(pStr);
px.zJson = pStr->zBuf; px.zJson = pStr->zBuf;
px.nJson = pStr->nUsed; px.nJson = pStr->nUsed;
(void)jsonXlateTextToBlob(&px, 0); (void)jsonTranslateTextToBlob(&px, 0);
if( px.oom ){ if( px.oom ){
sqlite3_free(px.aBlob); sqlite3_free(px.aBlob);
sqlite3_result_error_nomem(pStr->pCtx); sqlite3_result_error_nomem(pStr->pCtx);
@@ -2052,7 +2050,7 @@ static u32 jsonbPayloadSize(const JsonParse *pParse, u32 i, u32 *pSz){
** **
** The pOut->eErr JSTRING_OOM flag is set on a OOM. ** The pOut->eErr JSTRING_OOM flag is set on a OOM.
*/ */
static u32 jsonXlateBlobToText( static u32 jsonTranslateBlobToText(
const JsonParse *pParse, /* the complete parse of the JSON */ const JsonParse *pParse, /* the complete parse of the JSON */
u32 i, /* Start rendering at this index */ u32 i, /* Start rendering at this index */
JsonString *pOut /* Write JSON here */ JsonString *pOut /* Write JSON here */
@@ -2223,7 +2221,7 @@ static u32 jsonXlateBlobToText(
j = i+n; j = i+n;
iEnd = j+sz; iEnd = j+sz;
while( j<iEnd ){ while( j<iEnd ){
j = jsonXlateBlobToText(pParse, j, pOut); j = jsonTranslateBlobToText(pParse, j, pOut);
jsonAppendChar(pOut, ','); jsonAppendChar(pOut, ',');
} }
if( sz>0 ) pOut->nUsed--; if( sz>0 ) pOut->nUsed--;
@@ -2236,7 +2234,7 @@ static u32 jsonXlateBlobToText(
j = i+n; j = i+n;
iEnd = j+sz; iEnd = j+sz;
while( j<iEnd ){ while( j<iEnd ){
j = jsonXlateBlobToText(pParse, j, pOut); j = jsonTranslateBlobToText(pParse, j, pOut);
jsonAppendChar(pOut, (x++ & 1) ? ',' : ':'); jsonAppendChar(pOut, (x++ & 1) ? ',' : ':');
} }
if( x & 1 ) pOut->eErr |= JSTRING_MALFORMED; if( x & 1 ) pOut->eErr |= JSTRING_MALFORMED;
@@ -2818,7 +2816,7 @@ static void jsonReturnTextJsonFromBlob(
x.aBlob = (u8*)aBlob; x.aBlob = (u8*)aBlob;
x.nBlob = nBlob; x.nBlob = nBlob;
jsonStringInit(&s, ctx); jsonStringInit(&s, ctx);
jsonXlateBlobToText(&x, 0, &s); jsonTranslateBlobToText(&x, 0, &s);
jsonReturnString(&s, 0, 0); jsonReturnString(&s, 0, 0);
} }
@@ -3320,7 +3318,7 @@ static void jsonReturnParse(
JsonString s; JsonString s;
jsonStringInit(&s, ctx); jsonStringInit(&s, ctx);
p->delta = 0; p->delta = 0;
jsonXlateBlobToText(p, 0, &s); jsonTranslateBlobToText(p, 0, &s);
jsonReturnString(&s, p, ctx); jsonReturnString(&s, p, ctx);
sqlite3_result_subtype(ctx, JSON_SUBTYPE); sqlite3_result_subtype(ctx, JSON_SUBTYPE);
} }
@@ -3648,7 +3646,7 @@ static void jsonExtractFunc(
if( argc==2 ){ if( argc==2 ){
if( flags & JSON_JSON ){ if( flags & JSON_JSON ){
jsonStringInit(&jx, ctx); jsonStringInit(&jx, ctx);
jsonXlateBlobToText(p, j, &jx); jsonTranslateBlobToText(p, j, &jx);
jsonReturnString(&jx, 0, 0); jsonReturnString(&jx, 0, 0);
jsonStringReset(&jx); jsonStringReset(&jx);
assert( (flags & JSON_BLOB)==0 ); assert( (flags & JSON_BLOB)==0 );
@@ -3663,7 +3661,7 @@ static void jsonExtractFunc(
} }
}else{ }else{
jsonAppendSeparator(&jx); jsonAppendSeparator(&jx);
jsonXlateBlobToText(p, j, &jx); jsonTranslateBlobToText(p, j, &jx);
} }
}else if( j==JSON_LOOKUP_NOTFOUND ){ }else if( j==JSON_LOOKUP_NOTFOUND ){
if( argc==2 ){ if( argc==2 ){