From 7b5123c7963c0b956e298e9e355ee45fdbd4de02 Mon Sep 17 00:00:00 2001 From: drh <> Date: Fri, 3 Nov 2023 12:09:22 +0000 Subject: [PATCH] Enhance the JSONB lookup routine with logic to apply edits. The new logic is currently unused and hence untested but does not create any regressions. FossilOrigin-Name: b12110276fc15d1b6b0cc455f89747ace7a09650d5ba433d8bb431bb4e5bc951 --- manifest | 12 +++--- manifest.uuid | 2 +- src/json.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 102 insertions(+), 14 deletions(-) diff --git a/manifest b/manifest index 869a09d63f..9aadbdf941 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\sall\sthe\slatest\strunk\sfixes\sand\senhancements\sinto\sthe\sjsonb\sbranch. -D 2023-11-03T11:35:33.434 +C Enhance\sthe\sJSONB\slookup\sroutine\swith\slogic\sto\sapply\sedits.\s\sThe\snew\slogic\sis\ncurrently\sunused\sand\shence\suntested\sbut\sdoes\snot\screate\sany\sregressions. +D 2023-11-03T12:09:22.342 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -683,7 +683,7 @@ F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51 F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6 F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71 F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276 -F src/json.c 2c0599c54aef24739015a0b7f7b569adecd760e4d06afd3d9c01c8c53713d3f0 +F src/json.c e109da4d5f29e239db837dad09a886a78b6a217592ea8c71e3b584483ce9b179 F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa F src/loadext.c 7432c944ff197046d67a1207790a1b13eec4548c85a9457eb0896bb3641dfb36 F src/main.c e1bc8864834697503d370d94613be945d05ca1c5ebdda43e7d5c8ee8c48d433c @@ -2142,8 +2142,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P a420a4f7ff76b0e9cf5f6d515ccfa31e526d58f4001a4015a367e2aa3c82091f 15b618e92a2708cc83256947736de8c494a9985a77e38bd5efc7e51e72cba344 -R 7bce1ab3b73fc9ca8b3b94f2470fc2d6 +P b089bf46374b374d02d7654c65eb3e75d1777638b398061e47644af0dab48c9b +R 02d78f9919449761c67674ad6f60265a U drh -Z 13124fe09b7592eb801d594e79ae949a +Z f8a451c94f8b9175a3b99dd9c9c86acd # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 71f504a420..c8ced97e73 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -b089bf46374b374d02d7654c65eb3e75d1777638b398061e47644af0dab48c9b \ No newline at end of file +b12110276fc15d1b6b0cc455f89747ace7a09650d5ba433d8bb431bb4e5bc951 \ No newline at end of file diff --git a/src/json.c b/src/json.c index 48d456e536..e998af5230 100644 --- a/src/json.c +++ b/src/json.c @@ -341,8 +341,19 @@ struct JsonParse { u32 nBlob; /* Bytes of aBlob[] actually used */ u32 nBlobAlloc; /* Bytes allocated to aBlob[] */ u8 *aBlob; /* BLOB representation of zJson */ + /* Search and edit information. See jsonLookupBlobStep() */ + u8 eEdit; /* Edit operation to apply */ + int delta; /* Size change due to the edit */ + u32 nIns; /* Number of bytes to insert */ + u8 *aIns; /* Content to be inserted */ }; +/* Allowed values for JsonParse.eEdit */ +#define JEDIT_DEL 0x01 /* Delete if exists */ +#define JEDIT_INS 0x02 /* Insert if not exists */ +#define JEDIT_REPL 0x04 /* Overwrite if exists */ +#define JEDIT_SET 0x08 /* Insert or overwrite */ + /* ** Maximum nesting depth of JSON for this implementation. ** @@ -720,8 +731,6 @@ static void jsonAppendNormalizedReal(JsonString *p, const char *zIn, u32 N){ } } - - /* ** Append an sqlite3_value (such as a function parameter) to the JSON ** string under construction in p. @@ -2558,6 +2567,31 @@ static int jsonBlobExpand(JsonParse *pParse, u32 N){ return 0; } +/* +** If pParse->aBlob is not previously editable (because it is taken +** from sqlite3_value_blob(), as indicated by the fact that +** pParse->nBlobAlloc==0 and pParse->nBlob>0) then make it editable +** by making a copy into space obtained from malloc. +** +** Return true on success. Return false on OOM. +*/ +static int jsonBlobMakeEditable(JsonParse *pParse){ + u8 *aOld; + u32 nSize; + if( pParse->nBlobAlloc>0 ) return 1; + aOld = pParse->aBlob; + nSize = pParse->nBlob + pParse->nIns; + if( nSize>100 ) nSize -= 100; + pParse->aBlob = 0; + if( jsonBlobExpand(pParse, nSize) ){ + return 0; + } + assert( pParse->nBlobAlloc >= pParse->nBlob + pParse->nIns ); + memcpy(pParse->aBlob, aOld, pParse->nBlob); + return 1; +} + + /* Expand pParse->aBlob and append N bytes. ** ** Return the number of errors. @@ -3767,12 +3801,24 @@ static u32 jsonbArrayCount(JsonParse *pParse, u32 iRoot){ return k; } +/* +** Edit the size of the element at iRoot by the amount in pParse->delta. +*/ +static void jsonAfterEditSizeAdjust(JsonParse *pParse, u32 iRoot){ + u32 sz; + assert( pParse->delta==0 ); + (void)jsonbPayloadSize(pParse, iRoot, &sz); + sz += pParse->delta; + jsonBlobChangePayloadSize(pParse, iRoot, sz); +} + /* ** Error returns from jsonLookupBlobStep() */ #define JSON_BLOB_ERROR 0xffffffff #define JSON_BLOB_NOTFOUND 0xfffffffe #define JSON_BLOB_PATHERROR 0xfffffffd +#define JSON_BLOB_ISERROR(x) ((x)>=JSON_BLOB_PATHERROR) /* ** Search along zPath to find the Json element specified. Return an @@ -3785,12 +3831,39 @@ static u32 jsonLookupBlobStep( u32 iRoot, /* Begin the search at this element of aBlob[] */ const char *zPath /* The path to search */ ){ - u32 i, j, k, nKey, sz, n, iEnd; + u32 i, j, k, nKey, sz, n, iEnd, rc; const char *zKey; u8 x; - if( pParse->oom ) return 0; - if( zPath[0]==0 ) return iRoot; + if( zPath[0]==0 ){ + if( pParse->eEdit && jsonBlobMakeEditable(pParse) ){ + n = jsonbPayloadSize(pParse, iRoot, &sz); + sz += n; + if( pParse->eEdit==JEDIT_DEL ){ + memmove(&pParse->aBlob[iRoot], &pParse->aBlob[iRoot+sz], + pParse->nBlob - iRoot); + pParse->nBlob -= n; + pParse->delta = -(int)n; + }else if( pParse->eEdit==JEDIT_INS ){ + /* Already exists, so json_insert() is a no-op */ + }else{ + /* json_set() or json_replace() */ + int d = (int)pParse->nIns - (int)sz; + pParse->delta = d; + if( d!=0 ){ + if( pParse->nBlob + d > pParse->nBlobAlloc ){ + jsonBlobExpand(pParse, pParse->nBlob+d); + if( pParse->oom ) return iRoot; + } + memmove(&pParse->aBlob[iRoot+pParse->nIns], + &pParse->aBlob[iRoot+sz], + pParse->nBlob - iRoot - sz); + } + memcpy(&pParse->aBlob[iRoot], pParse->aIns, pParse->nIns); + } + } + return iRoot; + } if( zPath[0]=='.' ){ x = pParse->aBlob[iRoot]; zPath++; @@ -3828,7 +3901,9 @@ static u32 jsonLookupBlobStep( if( ((pParse->aBlob[j])&0x0f)>JSONB_OBJECT ) return JSON_BLOB_ERROR; n = jsonbPayloadSize(pParse, j, &sz); if( n==0 || j+n+sz>iEnd ) return JSON_BLOB_ERROR; - return jsonLookupBlobStep(pParse, j, &zPath[i]); + rc = jsonLookupBlobStep(pParse, j, &zPath[i]); + if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot); + return rc; } j = k+sz; if( ((pParse->aBlob[j])&0x0f)>JSONB_OBJECT ) return JSON_BLOB_ERROR; @@ -3872,7 +3947,9 @@ static u32 jsonLookupBlobStep( iEnd = j+sz; while( jdelta ) jsonAfterEditSizeAdjust(pParse, iRoot); + return rc; } k--; n = jsonbPayloadSize(pParse, j, &sz); @@ -3880,9 +3957,20 @@ static u32 jsonLookupBlobStep( j += n+sz; } if( j>iEnd ) return JSON_BLOB_ERROR; + if( k>1 ) return JSON_BLOB_NOTFOUND; }else{ return JSON_BLOB_PATHERROR; } + if( pParse->eEdit==JEDIT_INS && jsonBlobMakeEditable(pParse) ){ + assert( pParse->nBlob + pParse->nIns <= pParse->nBlobAlloc ); + memmove(&pParse->aBlob[j], &pParse->aBlob[j+pParse->nIns], + pParse->nBlob - j); + memcpy(&pParse->aBlob[j], pParse->aIns, pParse->nIns); + pParse->delta = pParse->nIns; + pParse->nBlob += pParse->nIns; + jsonAfterEditSizeAdjust(pParse, iRoot); + return j; + } return JSON_BLOB_NOTFOUND; }