1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-11 01:42:22 +03:00

More aggressive use of jsonBlobEdit(). Improvements to the MergePatch

implementation sketch.

FossilOrigin-Name: fbca9570fd2e1465739e4d3a8d9bb40fad594fd78ab49b2cb34efa27ebdd8361
This commit is contained in:
drh
2023-11-28 00:27:58 +00:00
parent eb04a0bb7b
commit f46f89df97
3 changed files with 31 additions and 24 deletions

View File

@@ -1,5 +1,5 @@
C Add\suntested\s(#ifdefed-out)\scode\sfor\sthe\sMergePatch\salgorithm\sagainst\sJSONB.\nAdd\s(and\stest)\sthe\sjsonBlobEdit()\sroutine\sthat\sis\sneeded\sby\sthe\snew\sMergePatch.
D 2023-11-27T23:46:12.954
C More\saggressive\suse\sof\sjsonBlobEdit().\s\sImprovements\sto\sthe\sMergePatch\nimplementation\ssketch.
D 2023-11-28T00:27:58.169
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -688,7 +688,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 7d6387920736cd9b78a4211771265c4c574284613d8e63f43b3ce7bc160da498
F src/json.c a91647a3c56298ff94bb2d5a989e8e329c4435e3a0ae9760b04b46c7b2083541
F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa
F src/loadext.c 7432c944ff197046d67a1207790a1b13eec4548c85a9457eb0896bb3641dfb36
F src/main.c 1b89f3de98d1b59fec5bac1d66d6ece21f703821b8eaa0d53d9604c35309f6f9
@@ -2145,8 +2145,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 af267868562e0799ad691dccad05f17afbc34d609eede8c55f57d209290246ef
R 47ffcc493583e45ee7703e45cf1805e3
P 4d353387fc10e1038cfdd86e66007bf728c231a928e588897bbee0fbfe76f225
R 42f4ec3bb9c19f358b256294966169e5
U drh
Z 2cedbcb9affb9f675b8d832bfea283a4
Z 551901455bde8067c499befcab52664b
# Remove this line to create a well-formed Fossil manifest.

View File

@@ -1 +1 @@
4d353387fc10e1038cfdd86e66007bf728c231a928e588897bbee0fbfe76f225
fbca9570fd2e1465739e4d3a8d9bb40fad594fd78ab49b2cb34efa27ebdd8361

View File

@@ -3809,8 +3809,8 @@ static void jsonAfterEditSizeAdjust(JsonParse *pParse, u32 iRoot){
** nDel may be zero, in which case no bytes are removed. But iDel is
** still important as new bytes will be insert beginning at iDel.
**
** nIns may be zero, in which case no new bytes are inserted. aIns might
** be a NULL pointer in this case.
** aIns may be zero, in which case space is created to hold nIns bytes
** beginning at iDel, but that space is uninitialized.
**
** Set pParse->oom if an OOM occurs.
*/
@@ -3833,7 +3833,7 @@ static void jsonBlobEdit(
pParse->nBlob += d;
pParse->delta += d;
}
if( nIns ) memcpy(&pParse->aBlob[iDel], aIns, nIns);
if( nIns && aIns ) memcpy(&pParse->aBlob[iDel], aIns, nIns);
}
/*
@@ -3940,19 +3940,14 @@ static u32 jsonLookupBlobStep(
ix.nBlobAlloc = sizeof(aLabel);
jsonBlobAppendNodeType(&ix,JSONB_TEXTRAW, nKey);
if( jsonBlobMakeEditable(pParse, ix.nBlob+nKey) ){
/* This is similar to jsonBlobEdit() except that the inserted
** bytes come from two different places, ix.aBlob and pParse->aBlob. */
nIns = ix.nBlob + nKey + pParse->nIns;
jsonBlobEdit(pParse, j, 0, 0, nIns);
assert( pParse->nBlob + pParse->nIns <= pParse->nBlobAlloc );
memmove(&pParse->aBlob[j+nIns], &pParse->aBlob[j],
pParse->nBlob - j);
memcpy(&pParse->aBlob[j], ix.aBlob, ix.nBlob);
k = j + ix.nBlob;
memcpy(&pParse->aBlob[k], zKey, nKey);
k += nKey;
memcpy(&pParse->aBlob[k], pParse->aIns, pParse->nIns);
pParse->delta = nIns;
pParse->nBlob += nIns;
jsonAfterEditSizeAdjust(pParse, iRoot);
}
return j;
@@ -5030,13 +5025,13 @@ static JsonNode *jsonMergePatch(
** else:
** return Patch
**
** Here is the same algorithm restrictured to show the actual
** Here is an equivalent algorithm restructured to show the actual
** implementation:
**
** 01 define MergePatch(Target, Patch):
** 02 if Patch is not an Object:
** 03 return Patch
** 04 else: // if Patch is an Object:
** 04 else: // if Patch is an Object
** 05 if Target is not an Object:
** 06 Target = {}
** 07 for each Name/Value pair in Patch:
@@ -5046,8 +5041,11 @@ static JsonNode *jsonMergePatch(
** 11 else
** 12 Target[name] = MergePatch(Target[Name], Value)
** 13 else if Value is not NULL:
** 14 Target[name] = RemoveNullVAlues(Value)
** 15 return Target
** 14 if Value is not an Object:
** 15 Target[name] = Value
** 16 else:
** 17 Target[name] = MergePatch('{}',value)
** 18 return Target
** |
** ^---- Line numbers referenced in comments in the implementation
*/
@@ -5161,10 +5159,19 @@ static int jsonMergePatchBlob(
}
}else if( x>0 ){ /* Algorithm line 13 */
/* No match and patch value is not NULL */
if( pPatch->aBlob[iPValue] & 0x0f)!=JSONB_OBJECT ){
jsonBlobEdit(pTarget, iTEnd, 0,
pPatch->aBlob+iPValue, szPValue+nPValue);
pPatch->aBlob+iPValue, szPValue+nPValue, 0, 0);
if( pTarget->oom ) return JSON_MERGE_OOM;
jsonBlobRemoveNullsFromObject(pTarget, iTEnd);
}else{
u32 szNew = szPLabel+nPLabel;
jsonBlobEdit(pTarget, iTEnd, 0, 0, szNew+1);
if( pTarget->oom ) return JSON_MERGE_OOM;
memcpy(&pTarget->aBlob[iTEnd], &pPatch->aBlob[iPLabel], szNew);
pTarget->aBlob[iTEnd+szNew] = 0x00;
rc = jsonMergePatch(pTarget, iTEnd+szNew, pPatch, iPValue);
if( rc ) return rc;
}
}
}
jsonAfterEditSizeAdjust(pTarget, iTarget);