What type of data structure can help compress JSON patches?
We can compress JSON patches by removing redundant patches. Redundant patches are patches which are fully replaced later, either by a patch with the same path, or with a patch with a shorter prefix.
This is like Last Writer Wins with Highest Ancestors.
As an example, take the following list
[
{
"op": "replace",
"path": "/layouts/13da6470-dbec-57e7-b412-0d75110b145d/x",
"value": 792
},
{
"op": "replace",
"path": "/layouts/13da6470-dbec-57e7-b412-0d75110b145d/y",
"value": 264
},
{
"op": "replace",
"path": "/layouts/13da6470-dbec-57e7-b412-0d75110b145d/x",
"value": 784
},
{
"op": "replace",
"path": "/layouts/13da6470-dbec-57e7-b412-0d75110b145d/y",
"value": 296
},
{
"op": "replace",
"path": "/layouts/13da6470-dbec-57e7-b412-0d75110b145d/x",
"value": 760
},
{
"op": "remove",
"path": "/layouts/13da6470-dbec-57e7-b412-0d75110b145d"
},
]
This can be compressed to the following since "/layouts/13da6470-dbec-57e7-b412-0d75110b145d"
is a prefix key for patches before it.
[
{
"op": "remove",
"path": "/layouts/13da6470-dbec-57e7-b412-0d75110b145d"
},
]
On the other hand, if we had a different list,
[
{
"op": "add",
"path": "/layouts/13da6470-dbec-57e7-b412-0d75110b145d",
"value": { x: 0, y: 0 }
},
{
"op": "replace",
"path": "/layouts/13da6470-dbec-57e7-b412-0d75110b145d/x",
"value": 792
},
{
"op": "replace",
"path": "/layouts/13da6470-dbec-57e7-b412-0d75110b145d/y",
"value": 264
},
{
"op": "replace",
"path": "/layouts/13da6470-dbec-57e7-b412-0d75110b145d/x",
"value": 784
},
{
"op": "replace",
"path": "/layouts/13da6470-dbec-57e7-b412-0d75110b145d/y",
"value": 296
},
{
"op": "replace",
"path": "/layouts/13da6470-dbec-57e7-b412-0d75110b145d/x",
"value": 760
},
]
it could be compressed to
[
{
"op": "add",
"path": "/layouts/13da6470-dbec-57e7-b412-0d75110b145d",
"value": { x: 760, y: 296 }
},
]
I've thought of using a trie where each path segment is treated as a character, but in the same thought, could I use some type of radix sort by path segment + patch index too?