-C Clarify\sownership\sof\sthe\svarious\sobjects\sinvolved\sin\sparsing\sJSON.
-D 2023-07-25T20:26:47.054
+C Add\sthe\sJsonParse.zAlt\sfield\sto\sold\srevised\sJSON\stext\safter\sa\schange.\nDemonstrate\sthat\sthis\selminates\sthe\sneed\sfor\sreparsing\safter\sa\schange\nby\susing\sit\sin\sthe\sjson_remove()\sfunction.\s\sThis\sis\san\sincremental\scheck-in\ncontaining\slots\sof\scruft.
+D 2023-07-26T00:48:45.768
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6
F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71
F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276
-F src/json.c 97f8c20c1cfefbc54e2b8b2143a24aae4993c12de6e74764af443e033b01b013
+F src/json.c a7823b0593eac42a608f8439e6b1f28022ac5fce9aa81a82856e48dd07ab601d
F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
F src/loadext.c 176d6b2cb18a6ad73b133db17f6fc351c4d9a2d510deebdb76c22bde9cfd1465
F src/main.c 512b1d45bc556edf4471a845afb7ba79e64bd5b832ab222dc195c469534cd002
F src/pragma.c 37b8fb02d090262280c86e1e2654bf59d8dbfbfe8dc6733f2b968a11374c095a
F src/pragma.h e690a356c18e98414d2e870ea791c1be1545a714ba623719deb63f7f226d8bb7
F src/prepare.c d6c4354f8ea0dc06962fbabc4b68c4471a45276a2918c929be00f9f537f69eb1
-F src/printf.c 1406ade1451adfa4374d8e9bbb8606109742c1216f5dbc95e011bf721fd91365
+F src/printf.c 56e362bbefbe61b5b211f1886e3cdb9b25d8ba817c12df3a15f11b9bdc152efd
F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c
F src/resolve.c 37953a5f36c60bea413c3c04efcd433b6177009f508ef2ace0494728912fe2e9
F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97
F src/sqlite.h.in f999ef3642f381d69679b2516b430dbcb6c5a2a951b7f5e43dc4751b474a5774
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
F src/sqlite3ext.h da473ce2b3d0ae407a6300c4a164589b9a6bfdbec9462688a8593ff16f3bb6e4
-F src/sqliteInt.h 59b755dec944aa3b068e962ef53264de6fde3d6b6df2d5869ea3afdb7facdf60
+F src/sqliteInt.h 29c8e6d0b6dfa9c2c356f23ab8349d7fac3679d5f7cfc01d4ee45c9171db2960
F src/sqliteLimit.h 33b1c9baba578d34efe7dfdb43193b366111cdf41476b1e82699e14c11ee1fb6
F src/status.c 160c445d7d28c984a0eae38c144f6419311ed3eace59b44ac6dafc20db4af749
F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1
F src/vdbeapi.c dde6c4d0f87486f056b9db4d1ea185bb1d84a6839102b86e76316ba590d07cc7
F src/vdbeaux.c b5e3f7e158518b4eca6f166ac43900640a3fe9735c710e12bfa119af21059339
F src/vdbeblob.c 2516697b3ee8154eb8915f29466fb5d4f1ae39ee8b755ea909cefaf57ec5e2ce
-F src/vdbemem.c aee9ac636666616494d9a395d29efc3fe9e1404a9f043db81c82560b43b78f35
+F src/vdbemem.c 46ea371b10c3573cf9287d385411fbcc6cd42099ad3505d20e33f2f3ed91537d
F src/vdbesort.c 0d40dca073c94e158ead752ef4225f4fee22dee84145e8c00ca2309afb489015
F src/vdbetrace.c fe0bc29ebd4e02c8bc5c1945f1d2e6be5927ec12c06d89b03ef2a4def34bf823
F src/vdbevtab.c aae4bd769410eb7e1d02c42613eec961d514459b1c3c1c63cfc84e92a137daac
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 4cb15d934a85ebc290fe6dd8cd3bd47b159561ca75d72bbffef30b9ea4623b09
-R cbb913300d23d0635674a5fded1ff5ab
+P afe02a398a16d51bd7482b6fbe2fbd15d9ac4fd9cdbc9d2bf81f38b3391fc567
+R 33c717c250039ffbe53dc8d7b2094929
U drh
-Z f943de02bc9e737c29b8eb0aa52b28c4
+Z 96ad60bdf5cbde0ec2b901493a583f6e
# Remove this line to create a well-formed Fossil manifest.
u32 nNode; /* Number of slots of aNode[] used */
u32 nAlloc; /* Number of slots of aNode[] allocated */
JsonNode *aNode; /* Array of nodes containing the parse */
- char *zJson; /* Original JSON string */
+ char *zJson; /* Original JSON string (before edits) */
+ char *zAlt; /* Revised JSON after edits applied. Maybe NULL */
u32 *aUp; /* Index of parent of each node */
JsonTask *pClean; /* Cleanup operations prior to freeing this object */
u16 iDepth; /* Nesting depth */
u8 hasNonstd; /* True if input uses non-standard features like JSON5 */
u8 nJPRef; /* Number of references to this object */
u8 bOwnsJson; /* This object owns zJson and response for freeing it */
- u8 isMod; /* aNode contains edits from the original zJson */
+ u8 useMod; /* Actually use the edits contain inside aNode */
+ u8 hasMod; /* aNode contains edits from the original zJson */
int nJson; /* Length of the zJson string in bytes */
+ int nAlt; /* Length of alternative JSON string zAlt, in bytes */
u32 iErr; /* Error location in zJson[] */
u32 iSubst; /* Last known JSON_SUBST node */
u32 iHold; /* Replace cache line with the lowest iHold value */
return SQLITE_OK;
}
+/* Try to force the string to be an RCStr string, rather than a
+** static string. Return true on success. The only reason this
+** might fail is due to an OOM fault.
+*/
+static int jsonForceRCStr(JsonString *p){
+ if( p->bStatic==0 ) return 1;
+ jsonGrow(p, p->nAlloc+1);
+ return p->bStatic==0;
+}
+
/* Append N bytes from zIn onto the end of the JsonString string.
*/
static SQLITE_NOINLINE void jsonAppendExpand(
sqlite3_free(pParse->aUp);
pParse->aUp = 0;
}
+ if( pParse->zAlt ){
+ char *z = pParse->zAlt;
+ pParse->zAlt = 0;
+ sqlite3RCStrUnref(z);
+ }
if( pParse->bOwnsJson ){
/* Order operations so that if the destructor for pParse->zJson
** invokes jsonParseFree(), the recursion will terminate harmlessly */
JsonString *pOut /* Write JSON here */
){
assert( pNode!=0 );
- while( (pNode->jnFlags & JNODE_REPLACE)!=0 ){
+ while( (pNode->jnFlags & JNODE_REPLACE)!=0 && pParse->useMod ){
u32 idx = (u32)(pNode - pParse->aNode);
u32 i = pParse->iSubst;
while( 1 /*exit-by-break*/ ){
jsonAppendChar(pOut, '[');
for(;;){
while( j<=pNode->n ){
- if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
+ if( (pNode[j].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ){
jsonAppendSeparator(pOut);
jsonRenderNode(pParse, &pNode[j], pOut);
}
j += jsonNodeSize(&pNode[j]);
}
if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
+ if( pParse->useMod==0 ) break;
assert( pNode->eU==2 );
pNode = &pParse->aNode[pNode->u.iAppend];
j = 1;
jsonAppendChar(pOut, '{');
for(;;){
while( j<=pNode->n ){
- if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
+ if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ){
jsonAppendSeparator(pOut);
jsonRenderNode(pParse, &pNode[j], pOut);
jsonAppendChar(pOut, ':');
j += 1 + jsonNodeSize(&pNode[j+1]);
}
if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
+ if( pParse->useMod==0 ) break;
assert( pNode->eU==2 );
pNode = &pParse->aNode[pNode->u.iAppend];
j = 1;
static void jsonReturnJson(
JsonParse *pParse, /* The complete JSON */
JsonNode *pNode, /* Node to return */
- sqlite3_context *pCtx /* Return value for this function */
+ sqlite3_context *pCtx, /* Return value for this function */
+ int bGenerateAlt /* Also store the rendered text in zAlt */
){
JsonString s;
if( pParse->oom ){
if( pParse->nErr==0 ){
jsonInit(&s, pCtx);
jsonRenderNode(pParse, pNode, &s);
+ if( bGenerateAlt && pParse->zAlt==0 && jsonForceRCStr(&s) ){
+ jsonAppendChar(&s, 0);
+ s.nUsed--;
+ pParse->zAlt = sqlite3RCStrRef(s.zBuf);
+ pParse->nAlt = s.nUsed;
+ }
jsonResult(&s);
sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
}
}
case JSON_ARRAY:
case JSON_OBJECT: {
- jsonReturnJson(pParse, pNode, pCtx);
+ jsonReturnJson(pParse, pNode, pCtx, 0);
break;
}
}
pParse->aNode[idx].eU = 4;
pParse->aNode[idx].u.iPrev = pParse->iSubst;
pParse->iSubst = idx;
+ pParse->hasMod = 1;
+ pParse->useMod = 1;
return idx;
}
int i;
memset(pParse, 0, sizeof(*pParse));
if( zJson==0 ) return 1;
+//printf("PARSE %s\n", zJson);
pParse->zJson = zJson;
pParse->bOwnsJson = bTakeJson;
pParse->nJPRef = 1;
** and so the caller must free it.
*/
static JsonParse *jsonParseCached(
- sqlite3_context *pCtx,
- sqlite3_value *pJson,
- sqlite3_context *pErrCtx
+ sqlite3_context *pCtx, /* Context to use for cache search */
+ sqlite3_value *pJson, /* Function param containing JSON text */
+ sqlite3_context *pErrCtx, /* Write parse errors here if not NULL */
+ int bUnedited /* No prior edits allowed */
){
char *zJson = (char*)sqlite3_value_text(pJson);
int nJson = sqlite3_value_bytes(pJson);
}
if( pMatch==0
&& p->nJson==nJson
+ && (p->hasMod==0 || bUnedited==0)
&& memcmp(p->zJson,zJson,nJson)==0
){
p->nErr = 0;
+ p->useMod = 0;
+ pMatch = p;
+//printf("HIT %s at %d\n", zJson, iKey);
+ }else
+ if( pMatch==0
+ && p->zAlt!=0
+ && bUnedited==0
+ && p->nAlt==nJson
+ && memcmp(p->zAlt, zJson, nJson)==0
+ ){
+ p->nErr = 0;
+ p->useMod = 1;
pMatch = p;
+//printf("HIT %s at %d-alt\n", zJson, iKey);
}else if( p->iHold<iMinHold ){
iMinHold = p->iHold;
iMinKey = iKey;
}
}
if( pMatch ){
+ /* The input JSON text was found in the cache. Use the preexisting
+ ** parse of this JSON */
pMatch->nErr = 0;
pMatch->iHold = iMaxHold+1;
assert( pMatch->nJPRef>0 ); /* pMatch is owned by the cache */
return pMatch;
}
+
+ /* The input JSON was not found anywhere in the cache. We will need
+ ** to parse it ourselves and generate a new JsonParse object.
+ */
+//printf("MISS %s\n", zJson);
p = sqlite3_malloc64( sizeof(*p) );
if( p==0 ){
sqlite3_result_error_nomem(pCtx);
u32 i, j, nKey;
const char *zKey;
JsonNode *pRoot = &pParse->aNode[iRoot];
- while( (pRoot->jnFlags & JNODE_REPLACE)!=0 ){
+ while( (pRoot->jnFlags & JNODE_REPLACE)!=0 && pParse->useMod ){
u32 idx = (u32)(pRoot - pParse->aNode);
i = pParse->iSubst;
while( 1 /*exit-by-break*/ ){
j += jsonNodeSize(&pRoot[j]);
}
if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
+ if( pParse->useMod==0 ) break;
assert( pRoot->eU==2 );
iRoot = pRoot->u.iAppend;
pRoot = &pParse->aNode[iRoot];
if( pApnd ){
u32 iStart, iLabel;
JsonNode *pNode;
+ assert( pParse->useMod );
iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
iLabel = jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
zPath += i;
if( pRoot->eType!=JSON_ARRAY ) return 0;
for(;;){
while( j<=pBase->n ){
- if( (pBase[j].jnFlags & JNODE_REMOVE)==0 ) i++;
+ if( (pBase[j].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ) i++;
j += jsonNodeSize(&pBase[j]);
}
if( (pBase->jnFlags & JNODE_APPEND)==0 ) break;
+ if( pParse->useMod==0 ) break;
assert( pBase->eU==2 );
iBase = pBase->u.iAppend;
pBase = &pParse->aNode[iBase];
zPath += j + 1;
j = 1;
for(;;){
- while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
- if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
+ while( j<=pRoot->n
+ && (i>0 || ((pRoot[j].jnFlags & JNODE_REMOVE)!=0 && pParse->useMod))
+ ){
+ if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ) i--;
j += jsonNodeSize(&pRoot[j]);
}
if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
+ if( pParse->useMod==0 ) break;
assert( pRoot->eU==2 );
iRoot = pRoot->u.iAppend;
pRoot = &pParse->aNode[iRoot];
if( i==0 && pApnd ){
u32 iStart;
JsonNode *pNode;
+ assert( pParse->useMod );
iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
if( pParse->oom ) return 0;
u32 i;
JsonNode *pNode;
- p = jsonParseCached(ctx, argv[0], ctx);
+ p = jsonParseCached(ctx, argv[0], ctx, 0);
if( p==0 ) return;
assert( p->nNode );
if( argc==2 ){
return;
}
if( pNode->eType==JSON_ARRAY ){
- assert( (pNode->jnFlags & JNODE_APPEND)==0 );
- for(i=1; i<=pNode->n; n++){
- i += jsonNodeSize(&pNode[i]);
+ while( 1 /*exit-by-break*/ ){
+ for(i=1; i<=pNode->n; n++){
+ i += jsonNodeSize(&pNode[i]);
+ }
+ if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
+ if( p->useMod==0 ) break;
+ assert( pNode->eU==2 );
+ pNode = &p->aNode[pNode->u.iAppend];
}
}
sqlite3_result_int64(ctx, n);
JsonString jx;
if( argc<2 ) return;
- p = jsonParseCached(ctx, argv[0], ctx);
+ p = jsonParseCached(ctx, argv[0], ctx, 0);
if( p==0 ) return;
if( argc==2 ){
/* With a single PATH argument */
}
if( pNode ){
if( flags & JSON_JSON ){
- jsonReturnJson(p, pNode, ctx);
+ jsonReturnJson(p, pNode, ctx, 0);
}else{
jsonReturn(p, pNode, ctx);
sqlite3_result_subtype(ctx, 0);
jsonParseReset(&x);
return;
}
+ x.useMod = 1;
+ y.useMod = 1;
pResult = jsonMergePatch(&x, 0, y.aNode);
assert( pResult!=0 || x.oom );
if( pResult && x.oom==0 ){
jsonDebugPrintParse(&x);
jsonDebugPrintNode(pResult);
- jsonReturnJson(&x, pResult, ctx);
+ jsonReturnJson(&x, pResult, ctx, 0);
}else{
sqlite3_result_error_nomem(ctx);
}
int argc,
sqlite3_value **argv
){
- JsonParse x; /* The parse */
+ JsonParse *pParse; /* The parse */
JsonNode *pNode;
const char *zPath;
u32 i;
if( argc<1 ) return;
- if( jsonParse(&x, ctx, (char*)sqlite3_value_text(argv[0]), 0) ) return;
- assert( x.nNode );
+ pParse = jsonParseCached(ctx, argv[0], ctx, argc>1);
+ if( pParse==0 ) return;
for(i=1; i<(u32)argc; i++){
zPath = (const char*)sqlite3_value_text(argv[i]);
if( zPath==0 ) goto remove_done;
- pNode = jsonLookup(&x, zPath, 0, ctx);
- if( x.nErr ) goto remove_done;
- if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
+ pNode = jsonLookup(pParse, zPath, 0, ctx);
+ if( pParse->nErr ) goto remove_done;
+ if( pNode ){
+ pNode->jnFlags |= JNODE_REMOVE;
+ pParse->hasMod = 1;
+ pParse->useMod = 1;
+ }
}
- if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
- jsonReturnJson(&x, x.aNode, ctx);
+ if( (pParse->aNode[0].jnFlags & JNODE_REMOVE)==0 ){
+ jsonReturnJson(pParse, pParse->aNode, ctx, 1);
}
remove_done:
- jsonDebugPrintParse(&x);
- jsonParseReset(&x);
+ jsonDebugPrintParse(p);
}
/*
sqlite3_result_error_nomem(pCtx);
}
}else{
- JsonParse *pPatch = jsonParseCached(pCtx, pValue, pCtx);
+ JsonParse *pPatch = jsonParseCached(pCtx, pValue, pCtx, 1);
if( pPatch==0 ){
p->oom = 1;
break;
jsonReplaceNode(ctx, &x, (u32)(pNode - x.aNode), argv[i+1]);
}
}
- jsonReturnJson(&x, x.aNode, ctx);
+ jsonReturnJson(&x, x.aNode, ctx, 0);
replace_err:
jsonDebugPrintParse(&x);
jsonParseReset(&x);
for(i=1; i<(u32)argc; i+=2){
zPath = (const char*)sqlite3_value_text(argv[i]);
bApnd = 0;
+ x.useMod = 1;
pNode = jsonLookup(&x, zPath, &bApnd, ctx);
if( x.oom ){
sqlite3_result_error_nomem(ctx);
}
}
jsonDebugPrintParse(&x);
- jsonReturnJson(&x, x.aNode, ctx);
+ jsonReturnJson(&x, x.aNode, ctx, 0);
jsonSetDone:
jsonParseReset(&x);
const char *zPath;
JsonNode *pNode;
- p = jsonParseCached(ctx, argv[0], ctx);
+ p = jsonParseCached(ctx, argv[0], ctx, 0);
if( p==0 ) return;
if( argc==2 ){
zPath = (const char*)sqlite3_value_text(argv[1]);
JsonParse *p; /* The parse */
UNUSED_PARAMETER(argc);
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
- p = jsonParseCached(ctx, argv[0], 0);
+ p = jsonParseCached(ctx, argv[0], 0, 0);
if( p==0 || p->oom ){
sqlite3_result_error_nomem(ctx);
sqlite3_free(p);
}else{
- sqlite3_result_int(ctx, p->nErr==0 && p->hasNonstd==0);
+ sqlite3_result_int(ctx, p->nErr==0 && (p->hasNonstd==0 || p->useMod));
if( p->nErr ) jsonParseFree(p);
}
}
JsonParse *p; /* The parse */
UNUSED_PARAMETER(argc);
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
- p = jsonParseCached(ctx, argv[0], 0);
+ p = jsonParseCached(ctx, argv[0], 0, 0);
if( p==0 || p->oom ){
sqlite3_result_error_nomem(ctx);
sqlite3_free(p);