Skip to content

Commit

Permalink
Add ownership set serialization test
Browse files Browse the repository at this point in the history
Summary: The test didn't find any bugs (unfortunately)

Reviewed By: malanka

Differential Revision: D66579746

fbshipit-source-id: a684b40d62799a056de3710b461a6083ade6551a
  • Loading branch information
Simon Marlow authored and facebook-github-bot committed Nov 29, 2024
1 parent d0382ad commit ecc730c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 31 deletions.
31 changes: 0 additions & 31 deletions glean/rocksdb/ownership.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,37 +73,6 @@ DatabaseImpl::loadOwnershipDerivedCounters() {

namespace {

void serializeEliasFano(binary::Output& out, const OwnerSet& set) {
out.nat(set.size);
out.nat(set.numLowerBits);
out.nat(set.upperSizeBytes);
out.nat(set.skipPointers - set.data.begin());
out.nat(set.forwardPointers - set.data.begin());
out.nat(set.lower - set.data.begin());
out.nat(set.upper - set.data.begin());
out.put(set.data);
}

/// Deserialize an OwnerSet from a binary::Input. Note that the
// OwnerSet points to the contents of the binary::Input, so that
// must remain alive as long as the OwnerSet is needed.
OwnerSet deserializeEliasFano(binary::Input& in) {
OwnerSet set;
set.size = in.trustedNat();
set.numLowerBits = in.trustedNat();
set.upperSizeBytes = in.trustedNat();
auto skipPointers = in.trustedNat();
auto forwardPointers = in.trustedNat();
auto lower = in.trustedNat();
auto upper = in.trustedNat();
set.data = in.bytes();
set.skipPointers = set.data.begin() + skipPointers;
set.forwardPointers = set.data.begin() + forwardPointers;
set.lower = set.data.begin() + lower;
set.upper = set.data.begin() + upper;
return set;
}

void putOwnerSet(
ContainerImpl& container,
rocksdb::WriteBatch& batch,
Expand Down
31 changes: 31 additions & 0 deletions glean/rts/ownership.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,37 @@ namespace facebook {
namespace glean {
namespace rts {

void serializeEliasFano(binary::Output& out, const OwnerSet& set) {
out.nat(set.size);
out.nat(set.numLowerBits);
out.nat(set.upperSizeBytes);
out.nat(set.skipPointers - set.data.begin());
out.nat(set.forwardPointers - set.data.begin());
out.nat(set.lower - set.data.begin());
out.nat(set.upper - set.data.begin());
out.put(set.data);
}

/// Deserialize an OwnerSet from a binary::Input. Note that the
// OwnerSet points to the contents of the binary::Input, so that
// must remain alive as long as the OwnerSet is needed.
OwnerSet deserializeEliasFano(binary::Input& in) {
OwnerSet set;
set.size = in.trustedNat();
set.numLowerBits = in.trustedNat();
set.upperSizeBytes = in.trustedNat();
auto skipPointers = in.trustedNat();
auto forwardPointers = in.trustedNat();
auto lower = in.trustedNat();
auto upper = in.trustedNat();
set.data = in.bytes();
set.skipPointers = set.data.begin() + skipPointers;
set.forwardPointers = set.data.begin() + forwardPointers;
set.lower = set.data.begin() + lower;
set.upper = set.data.begin() + upper;
return set;
}

namespace {

/**
Expand Down
8 changes: 8 additions & 0 deletions glean/rts/ownership.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#pragma once

#include "glean/rts/binary.h"
#include "glean/rts/id.h"
#include "glean/rts/ownership/uset.h"

Expand All @@ -23,6 +24,13 @@ using UnitId = uint32_t;
using MutableOwnerSet = folly::compression::MutableEliasFanoCompressedList;
using OwnerSet = folly::compression::EliasFanoCompressedList;

void serializeEliasFano(binary::Output& out, const OwnerSet& set);

/// Deserialize an OwnerSet from a binary::Input. Note that the
// OwnerSet points to the contents of the binary::Input, so that
// must remain alive as long as the OwnerSet is needed.
OwnerSet deserializeEliasFano(binary::Input& in);

struct OwnershipSetIterator {
virtual ~OwnershipSetIterator() {}
virtual std::pair<size_t, size_t> sizes() const = 0;
Expand Down
6 changes: 6 additions & 0 deletions glean/rts/ownership/setu32.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,12 @@ class SetU32 {
}
}

static std::set<uint32_t> to(const SetU32& set) {
std::set<uint32_t> out;
set.foreach([&](uint32_t elt) { out.insert(elt); });
return out;
}

using MutableEliasFanoList =
folly::compression::MutableEliasFanoCompressedList;
using EliasFanoList = folly::compression::EliasFanoCompressedList;
Expand Down
21 changes: 21 additions & 0 deletions glean/rts/tests/OwnershipTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

#include <gtest/gtest.h>

#include <rapidcheck.h>
#include <rapidcheck/gtest.h>

using namespace facebook::glean;
using namespace facebook::glean::rts;

namespace {
Expand Down Expand Up @@ -304,3 +308,20 @@ TEST(OwnershipTest, SliceTest) {
checkVisibility(ownership, firstUsetId, numSets, {0, 1, 2}, true);
checkVisibility(ownership, firstUsetId, numSets, {0, 1, 2}, false);
}

struct SetSerializationTest : testing::Test {};

RC_GTEST_PROP(SetSerializationTest, testSerialization, ()) {
const auto set = *rc::gen::nonEmpty(rc::gen::arbitrary<std::set<uint32_t>>());
SetU32 a = SetU32::from(set);
MutableOwnerSet b = a.toEliasFano();
binary::Output o;
serializeEliasFano(o, b);
auto size = o.size();
o.expect(8);
binary::Input i(o.data(), size);
OwnerSet c = deserializeEliasFano(i);
SetU32 d = SetU32::fromEliasFano(c);
b.free();
RC_ASSERT(set == SetU32::to(d));
}

0 comments on commit ecc730c

Please sign in to comment.