Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Module rewrite with custom JSONPath implementation #974

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion .github/workflows/ci-bdnbenchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
os: [ ubuntu-latest, windows-latest ]
framework: [ 'net8.0' ]
configuration: [ 'Release' ]
test: [ 'Operations.BasicOperations', 'Operations.ObjectOperations', 'Operations.HashObjectOperations', 'Cluster.ClusterMigrate', 'Cluster.ClusterOperations', 'Lua.LuaScripts', 'Lua.LuaScriptCacheOperations','Lua.LuaRunnerOperations','Operations.CustomOperations', 'Operations.RawStringOperations', 'Operations.ScriptOperations', 'Operations.ModuleOperations', 'Operations.PubSubOperations', 'Network.BasicOperations', 'Network.RawStringOperations' ]
test: [ 'Operations.BasicOperations', 'Operations.ObjectOperations', 'Operations.HashObjectOperations', 'Cluster.ClusterMigrate', 'Cluster.ClusterOperations', 'Lua.LuaScripts', 'Lua.LuaScriptCacheOperations','Lua.LuaRunnerOperations','Operations.CustomOperations', 'Operations.RawStringOperations', 'Operations.ScriptOperations', 'Operations.JsonOperations', 'Operations.ModuleOperations', 'Operations.PubSubOperations', 'Network.BasicOperations', 'Network.RawStringOperations' ]
steps:
- name: Check out code
uses: actions/checkout@v4
Expand Down
25 changes: 14 additions & 11 deletions Garnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommandInfoUpdater", "playg
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleModule", "playground\SampleModule\SampleModule.csproj", "{A8CA619E-8F13-4EF8-943F-2D5E3FEBFB3F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GarnetJSON", "playground\GarnetJSON\GarnetJSON.csproj", "{2C8F1F5D-31E5-4D00-A46E-F3B1D9BC098F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MigrateBench", "playground\MigrateBench\MigrateBench.csproj", "{6B66B394-E410-4B61-9A5A-1595FF6F5E08}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "hosting", "hosting", "{01823EA4-4446-4D66-B268-DFEE55951964}"
Expand All @@ -111,6 +109,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Garnet.resources", "libs\re
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NoOpModule", "playground\NoOpModule\NoOpModule.csproj", "{D4C9A1A0-7053-F072-21F5-4E0C5827136D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "modules", "modules", "{D8A9CE6E-91B9-4B84-B44A-2BCF1161A793}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GarnetJSON", "modules\GarnetJSON\GarnetJSON.csproj", "{1A5DF817-D0DD-4F0B-AE3A-C9CD0E03C9D5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -295,14 +297,6 @@ Global
{A8CA619E-8F13-4EF8-943F-2D5E3FEBFB3F}.Release|Any CPU.Build.0 = Release|Any CPU
{A8CA619E-8F13-4EF8-943F-2D5E3FEBFB3F}.Release|x64.ActiveCfg = Release|Any CPU
{A8CA619E-8F13-4EF8-943F-2D5E3FEBFB3F}.Release|x64.Build.0 = Release|Any CPU
{2C8F1F5D-31E5-4D00-A46E-F3B1D9BC098F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2C8F1F5D-31E5-4D00-A46E-F3B1D9BC098F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2C8F1F5D-31E5-4D00-A46E-F3B1D9BC098F}.Debug|x64.ActiveCfg = Debug|Any CPU
{2C8F1F5D-31E5-4D00-A46E-F3B1D9BC098F}.Debug|x64.Build.0 = Debug|Any CPU
{2C8F1F5D-31E5-4D00-A46E-F3B1D9BC098F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2C8F1F5D-31E5-4D00-A46E-F3B1D9BC098F}.Release|Any CPU.Build.0 = Release|Any CPU
{2C8F1F5D-31E5-4D00-A46E-F3B1D9BC098F}.Release|x64.ActiveCfg = Release|Any CPU
{2C8F1F5D-31E5-4D00-A46E-F3B1D9BC098F}.Release|x64.Build.0 = Release|Any CPU
{6B66B394-E410-4B61-9A5A-1595FF6F5E08}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6B66B394-E410-4B61-9A5A-1595FF6F5E08}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6B66B394-E410-4B61-9A5A-1595FF6F5E08}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -335,6 +329,14 @@ Global
{D4C9A1A0-7053-F072-21F5-4E0C5827136D}.Release|Any CPU.Build.0 = Release|Any CPU
{D4C9A1A0-7053-F072-21F5-4E0C5827136D}.Release|x64.ActiveCfg = Release|Any CPU
{D4C9A1A0-7053-F072-21F5-4E0C5827136D}.Release|x64.Build.0 = Release|Any CPU
{1A5DF817-D0DD-4F0B-AE3A-C9CD0E03C9D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1A5DF817-D0DD-4F0B-AE3A-C9CD0E03C9D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1A5DF817-D0DD-4F0B-AE3A-C9CD0E03C9D5}.Debug|x64.ActiveCfg = Debug|Any CPU
{1A5DF817-D0DD-4F0B-AE3A-C9CD0E03C9D5}.Debug|x64.Build.0 = Debug|Any CPU
{1A5DF817-D0DD-4F0B-AE3A-C9CD0E03C9D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1A5DF817-D0DD-4F0B-AE3A-C9CD0E03C9D5}.Release|Any CPU.Build.0 = Release|Any CPU
{1A5DF817-D0DD-4F0B-AE3A-C9CD0E03C9D5}.Release|x64.ActiveCfg = Release|Any CPU
{1A5DF817-D0DD-4F0B-AE3A-C9CD0E03C9D5}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -364,14 +366,15 @@ Global
{9F6E4734-6341-4A9C-A7FF-636A39D8BEAD} = {346A5A53-51E4-4A75-B7E6-491D950382CE}
{9BE474A2-1547-43AC-B4F2-FB48A01FA995} = {69A71E2C-00E3-42F3-854E-BE157A24834E}
{A8CA619E-8F13-4EF8-943F-2D5E3FEBFB3F} = {69A71E2C-00E3-42F3-854E-BE157A24834E}
{2C8F1F5D-31E5-4D00-A46E-F3B1D9BC098F} = {69A71E2C-00E3-42F3-854E-BE157A24834E}
{6B66B394-E410-4B61-9A5A-1595FF6F5E08} = {69A71E2C-00E3-42F3-854E-BE157A24834E}
{697766CD-2046-46D9-958A-0FD3B46C98D4} = {01823EA4-4446-4D66-B268-DFEE55951964}
{DF2DD03E-87EE-482A-9FBA-6C8FBC23BDC5} = {697766CD-2046-46D9-958A-0FD3B46C98D4}
{A48412B4-FD60-467E-A5D9-F155CAB4F907} = {147FCE31-EC09-4C90-8E4D-37CA87ED18C3}
{D4C9A1A0-7053-F072-21F5-4E0C5827136D} = {69A71E2C-00E3-42F3-854E-BE157A24834E}
{1A5DF817-D0DD-4F0B-AE3A-C9CD0E03C9D5} = {D8A9CE6E-91B9-4B84-B44A-2BCF1161A793}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
FileExplorer =
SolutionGuid = {2C02C405-4798-41CA-AF98-61EDFEF6772E}
EndGlobalSection
EndGlobal
25 changes: 25 additions & 0 deletions NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,28 @@ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

## Newtonsoft.Json

**Source**: https://github.com/JamesNK/Newtonsoft.Json

The MIT License (MIT)

Copyright (c) 2007 James Newton-King

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 1 addition & 1 deletion benchmark/BDN.benchmark/BDN.benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<ProjectReference Include="..\..\libs\host\Garnet.host.csproj" />
<ProjectReference Include="..\..\libs\common\Garnet.common.csproj" />
<ProjectReference Include="..\..\libs\server\Garnet.server.csproj" />
<ProjectReference Include="..\..\playground\GarnetJSON\GarnetJSON.csproj" />
<ProjectReference Include="..\..\modules\GarnetJSON\GarnetJSON.csproj" />
<ProjectReference Include="..\..\playground\NoOpModule\NoOpModule.csproj" />
</ItemGroup>

Expand Down
74 changes: 74 additions & 0 deletions benchmark/BDN.benchmark/Json/JsonPathQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using GarnetJSON.JSONPath;
using System.Text.Json.Nodes;

namespace BDN.benchmark.Json;

[MemoryDiagnoser]
public class JsonPathQuery
{
private JsonNode _jsonNode;

private readonly Consumer _consumer = new Consumer();

[Params(
"$.store.book[0].title",
"$.store.book[*].author",
"$.store.book[?(@.price < 10)].title",
"$.store.bicycle.color",
"$.store.book[*]", // all books
"$.store..price", // all prices using recursive descent
"$..author", // all authors using recursive descent
"$.store.book[?(@.price > 10 && @.price < 20)]", // filtered by price range
"$.store.book[?(@.category == 'fiction')]", // filtered by category
"$.store.book[-1:]", // last book
"$.store.book[:2]", // first two books
"$.store.book[?(@.author =~ /.*Waugh/)]", // regex match on author
"$..book[0,1]", // union of array indices
"$..*", // recursive descent all nodes
"$..['bicycle','price']", // recursive descent specfic node with name match
"$..[?(@.price < 10)]", // recursive descent specfic node with conditionally match
"$.store.book[?(@.author && @.title)]", // existence check
"$.store.*" // wildcard child
)]
public string JsonPath { get; set; }

[GlobalSetup]
public void Setup()
{
var jsonString = """
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
""";

_jsonNode = JsonNode.Parse(jsonString);
}

[Benchmark]
public void SelectNodes()
{
var result = _jsonNode.SelectNodes(JsonPath);
result.Consume(_consumer);
}
}
135 changes: 135 additions & 0 deletions benchmark/BDN.benchmark/Operations/JsonOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using System.Text;
using BenchmarkDotNet.Attributes;
using Embedded.server;

namespace BDN.benchmark.Operations
{
/// <summary>
/// Benchmark for ModuleOperations
/// </summary>
[MemoryDiagnoser]
public class JsonOperations : OperationsBase
{
// Existing commands
static ReadOnlySpan<byte> JSONGETCMD => "*3\r\n$8\r\nJSON.GET\r\n$2\r\nk3\r\n$1\r\n$\r\n"u8;
static ReadOnlySpan<byte> JSONSETCMD => "*4\r\n$8\r\nJSON.SET\r\n$2\r\nk3\r\n$4\r\n$.f2\r\n$1\r\n2\r\n"u8;

// New commands for different JsonPath patterns
static ReadOnlySpan<byte> JSONGET_DEEP => "*3\r\n$8\r\nJSON.GET\r\n$4\r\nbig1\r\n$12\r\n$.data[0].id\r\n"u8;
static ReadOnlySpan<byte> JSONGET_ARRAY => "*3\r\n$8\r\nJSON.GET\r\n$4\r\nbig1\r\n$13\r\n$.data[*]\r\n"u8;
static ReadOnlySpan<byte> JSONGET_ARRAY_ELEMENTS => "*3\r\n$8\r\nJSON.GET\r\n$4\r\nbig1\r\n$13\r\n$.data[*].id\r\n"u8;
static ReadOnlySpan<byte> JSONGET_FILTER => "*3\r\n$8\r\nJSON.GET\r\n$4\r\nbig1\r\n$29\r\n$.data[?(@.active==true)]\r\n"u8;
static ReadOnlySpan<byte> JSONGET_RECURSIVE => "*3\r\n$8\r\nJSON.GET\r\n$4\r\nbig1\r\n$4\r\n$..*\r\n"u8;

Request jsonGetCmd;
Request jsonSetCmd;
Request jsonGetDeepCmd;
Request jsonGetArrayCmd;
Request jsonGetArrayElementsCmd;
Request jsonGetFilterCmd;
Request jsonGetRecursiveCmd;

private static string GenerateLargeJson(int items)
{
var data = new StringBuilder();
data.Append("{\"data\":[");

for (int i = 0; i < items; i++)
{
if (i > 0) data.Append(',');
data.Append($$"""
{
"id": {{i}},
"name": "Item{{i}}",
"active": {{(i % 2 == 0).ToString().ToLower()}},
"value": {{i * 100}},
"nested": {
"level1": {
"level2": {
"value": {{i}}
}
}
}
}
""");
}

data.Append("]}");
return data.ToString();
}

private void RegisterModules()
{
server.Register.NewModule(new NoOpModule.NoOpModule(), [], out _);
server.Register.NewModule(new GarnetJSON.Module(), [], out _);
}

public override void GlobalSetup()
{
base.GlobalSetup();
RegisterModules();

SetupOperation(ref jsonGetCmd, JSONGETCMD);
SetupOperation(ref jsonSetCmd, JSONSETCMD);
SetupOperation(ref jsonGetDeepCmd, JSONGET_DEEP);
SetupOperation(ref jsonGetArrayCmd, JSONGET_ARRAY);
SetupOperation(ref jsonGetArrayElementsCmd, JSONGET_ARRAY_ELEMENTS);
SetupOperation(ref jsonGetFilterCmd, JSONGET_FILTER);
SetupOperation(ref jsonGetRecursiveCmd, JSONGET_RECURSIVE);

// Setup test data
var largeJson = GenerateLargeJson(20);
SlowConsumeMessage(Encoding.UTF8.GetBytes($"*4\r\n$8\r\nJSON.SET\r\n$4\r\nbig1\r\n$1\r\n$\r\n${largeJson.Length}\r\n{largeJson}\r\n"));

// Existing setup
SlowConsumeMessage("*4\r\n$8\r\nJSON.SET\r\n$2\r\nk3\r\n$1\r\n$\r\n$14\r\n{\"f1\":{\"a\":1}}\r\n"u8);
SlowConsumeMessage(JSONGETCMD);
SlowConsumeMessage(JSONSETCMD);
}

[Benchmark]
public void ModuleJsonGetCommand()
{
Send(jsonGetCmd);
}

[Benchmark]
public void ModuleJsonSetCommand()
{
Send(jsonSetCmd);
}

[Benchmark]
public void ModuleJsonGetDeepPath()
{
Send(jsonGetDeepCmd);
}

[Benchmark]
public void ModuleJsonGetArrayPath()
{
Send(jsonGetArrayCmd);
}

[Benchmark]
public void ModuleJsonGetArrayElementsPath()
{
Send(jsonGetArrayElementsCmd);
}

[Benchmark]
public void ModuleJsonGetFilterPath()
{
Send(jsonGetFilterCmd);
}

[Benchmark]
public void ModuleJsonGetRecursive()
{
Send(jsonGetRecursiveCmd);
}
}
}
23 changes: 0 additions & 23 deletions benchmark/BDN.benchmark/Operations/ModuleOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ public class ModuleOperations : OperationsBase
static ReadOnlySpan<byte> NOOPTXN => "*1\r\n$18\r\nNoOpModule.NOOPTXN\r\n"u8;
Request noOpTxn;

static ReadOnlySpan<byte> JSONGETCMD => "*3\r\n$8\r\nJSON.GET\r\n$2\r\nk3\r\n$1\r\n$\r\n"u8;
Request jsonGetCmd;

static ReadOnlySpan<byte> JSONSETCMD => "*4\r\n$8\r\nJSON.SET\r\n$2\r\nk3\r\n$4\r\n$.f2\r\n$1\r\n2\r\n"u8;
Request jsonSetCmd;

private void RegisterModules()
{
server.Register.NewModule(new NoOpModule.NoOpModule(), [], out _);
Expand All @@ -54,9 +48,6 @@ public override void GlobalSetup()
SetupOperation(ref noOpProc, NOOPPROC);
SetupOperation(ref noOpTxn, NOOPTXN);

SetupOperation(ref jsonGetCmd, JSONGETCMD);
SetupOperation(ref jsonSetCmd, JSONSETCMD);

SlowConsumeMessage("*3\r\n$3\r\nSET\r\n$2\r\nk1\r\n$1\r\nc\r\n"u8);
SlowConsumeMessage(NOOPCMDREAD);
SlowConsumeMessage(NOOPCMDRMW);
Expand All @@ -65,8 +56,6 @@ public override void GlobalSetup()
SlowConsumeMessage(NOOPPROC);
SlowConsumeMessage(NOOPTXN);
SlowConsumeMessage("*4\r\n$8\r\nJSON.SET\r\n$2\r\nk3\r\n$1\r\n$\r\n$14\r\n{\"f1\":{\"a\":1}}\r\n"u8);
SlowConsumeMessage(JSONGETCMD);
SlowConsumeMessage(JSONSETCMD);
}

[Benchmark]
Expand Down Expand Up @@ -104,17 +93,5 @@ public void ModuleNoOpTxn()
{
Send(noOpTxn);
}

[Benchmark]
public void ModuleJsonGetCommand()
{
Send(jsonGetCmd);
}

[Benchmark]
public void ModuleJsonSetCommand()
{
Send(jsonSetCmd);
}
}
}
Loading
Loading