-
Notifications
You must be signed in to change notification settings - Fork 991
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: a test with flat buffers Also, add an experimental flag `--experimental_flat_json` that allows writing json objects as flat strings using flexibuffers. The experiment shows that `debug populate 100000 a 10 type json elements 30` uses almost 3 times less memory than with native jsoncons objects. Signed-off-by: Roman Gershman <roman@dragonflydb.io>
- Loading branch information
Showing
7 changed files
with
152 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
|
||
containerWorkspaceFolder=$1 | ||
git config --global --add safe.directory ${containerWorkspaceFolder}/helio | ||
mkdir -p /root/.local/share/CMakeTools |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright 2023, DragonflyDB authors. All rights reserved. | ||
// See LICENSE for licensing terms. | ||
// | ||
|
||
#include <absl/strings/escaping.h> | ||
#include <flatbuffers/flatbuffers.h> | ||
#include <flatbuffers/flexbuffers.h> | ||
#include <flatbuffers/idl.h> | ||
|
||
#include "base/gtest.h" | ||
#include "base/logging.h" | ||
|
||
using namespace std; | ||
|
||
namespace dfly { | ||
class FlatBuffersTest : public ::testing::Test { | ||
protected: | ||
}; | ||
|
||
TEST_F(FlatBuffersTest, Basic) { | ||
flexbuffers::Builder fbb; | ||
fbb.Map([&] { | ||
fbb.String("foo", "bar"); | ||
fbb.Double("bar", 1.5); | ||
fbb.Vector("strs", [&] { | ||
fbb.String("hello"); | ||
fbb.String("world"); | ||
}); | ||
}); | ||
|
||
fbb.Finish(); | ||
auto buffer = fbb.GetBuffer(); | ||
auto map = flexbuffers::GetRoot(buffer).AsMap(); | ||
EXPECT_EQ("bar", map["foo"].AsString().str()); | ||
} | ||
|
||
TEST_F(FlatBuffersTest, FlexiParser) { | ||
flatbuffers::Parser parser; | ||
const char* json = R"( | ||
{ | ||
"foo": "bar", | ||
"bar": 1.5, | ||
"strs": ["hello", "world"] | ||
} | ||
)"; | ||
flexbuffers::Builder fbb; | ||
ASSERT_TRUE(parser.ParseFlexBuffer(json, nullptr, &fbb)); | ||
fbb.Finish(); | ||
const auto& buffer = fbb.GetBuffer(); | ||
string_view buf_view{reinterpret_cast<const char*>(buffer.data()), buffer.size()}; | ||
LOG(INFO) << "Binary buffer: " << absl::CHexEscape(buf_view); | ||
|
||
auto map = flexbuffers::GetRoot(buffer).AsMap(); | ||
EXPECT_EQ("bar", map["foo"].AsString().str()); | ||
} | ||
|
||
TEST_F(FlatBuffersTest, ParseJson) { | ||
const char* schema = R"( | ||
namespace dfly; | ||
table Foo { | ||
foo: string; | ||
bar: double; | ||
strs: [string]; | ||
} | ||
root_type Foo; | ||
)"; | ||
|
||
flatbuffers::Parser parser; | ||
ASSERT_TRUE(parser.Parse(schema)); | ||
parser.Serialize(); | ||
flatbuffers::DetachedBuffer bsb = parser.builder_.Release(); | ||
|
||
// This schema will always reference bsb. | ||
auto* fbs_schema = reflection::GetSchema(bsb.data()); | ||
|
||
flatbuffers::Verifier verifier(bsb.data(), bsb.size()); | ||
ASSERT_TRUE(fbs_schema->Verify(verifier)); | ||
|
||
auto* root_table = fbs_schema->root_table(); | ||
auto* fields = root_table->fields(); | ||
auto* field_foo = fields->LookupByKey("foo"); | ||
ASSERT_EQ(field_foo->type()->base_type(), reflection::String); | ||
|
||
const char* json = R"( | ||
{ | ||
"foo": "value", | ||
"bar": 1.5, | ||
"strs": ["hello", "world"] | ||
} | ||
)"; | ||
|
||
ASSERT_TRUE(parser.Parse(json)); | ||
size_t buf_size = parser.builder_.GetSize(); | ||
|
||
ASSERT_TRUE( | ||
flatbuffers::Verify(*fbs_schema, *root_table, parser.builder_.GetBufferPointer(), buf_size)); | ||
auto* root_obj = flatbuffers::GetAnyRoot(parser.builder_.GetBufferPointer()); | ||
|
||
const flatbuffers::String* value = flatbuffers::GetFieldS(*root_obj, *field_foo); | ||
EXPECT_EQ("value", value->str()); | ||
|
||
// wrong type. | ||
ASSERT_FALSE(parser.Parse(R"({"foo": 1})")); | ||
} | ||
|
||
} // namespace dfly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters