MMapping for flatbuffers
This lets us lazily load flatbuffers using mmap to both reduce memory
usage and to increase speed for use cases where we don't need to access
everything.
Change-Id: Ia271350b4c7cbb7a0a86ca094ef69c34716ba754
Signed-off-by: Austin Schuh <austin.schuh@bluerivertech.com>
diff --git a/aos/flatbuffers_test.cc b/aos/flatbuffers_test.cc
index e3030f1..f37af53 100644
--- a/aos/flatbuffers_test.cc
+++ b/aos/flatbuffers_test.cc
@@ -1,9 +1,10 @@
#include "aos/flatbuffers.h"
-#include "gtest/gtest.h"
-
+#include "absl/strings/str_cat.h"
#include "aos/json_to_flatbuffer.h"
#include "aos/json_to_flatbuffer_generated.h"
+#include "aos/testing/tmpdir.h"
+#include "gtest/gtest.h"
namespace aos {
namespace testing {
@@ -21,5 +22,38 @@
EXPECT_FALSE(empty.Verify());
}
+// Tests the ability to map a flatbuffer on disk to memory
+TEST(FlatbufferMMapTest, Verify) {
+ FlatbufferDetachedBuffer<Configuration> fb =
+ JsonToFlatbuffer<Configuration>("{\"foo_int\": 3}");
+
+ const std::string fb_path = absl::StrCat(TestTmpDir(), "/fb.bfbs");
+ WriteFlatbufferToFile(fb_path, fb);
+
+ FlatbufferMMap<Configuration> fb_mmap(fb_path);
+ EXPECT_TRUE(fb.Verify());
+ EXPECT_TRUE(fb_mmap.Verify());
+ ASSERT_EQ(fb_mmap.message().foo_int(), 3);
+
+ // Verify that copying works
+ {
+ FlatbufferMMap<Configuration> fb_mmap2(fb_path);
+ fb_mmap2 = fb_mmap;
+ EXPECT_TRUE(fb_mmap.Verify());
+ EXPECT_TRUE(fb_mmap2.Verify());
+ ASSERT_EQ(fb_mmap2.message().foo_int(), 3);
+ ASSERT_EQ(fb_mmap.message().foo_int(), 3);
+ }
+ EXPECT_TRUE(fb_mmap.Verify());
+ ASSERT_EQ(fb_mmap.message().foo_int(), 3);
+
+ // Verify that moving works
+ {
+ FlatbufferMMap<Configuration> fb_mmap3(fb_path);
+ fb_mmap3 = std::move(fb_mmap);
+ EXPECT_TRUE(fb_mmap3.Verify());
+ ASSERT_EQ(fb_mmap3.message().foo_int(), 3);
+ }
+}
} // namespace testing
} // namespace aos