feat(storage): implement original V1 durable Storage engine, tools, and async adapter
This commit is contained in:
1 parent
427cc9a843
commit
fdf0da5ca8
21 files changed
+6966
No files matched your search
@@ -0,0 +1,122 @@
|
||||
#include <Void/Storage/Batch.hpp>
|
||||
|
||||
#include <set>
|
||||
|
||||
namespace Void::Storage
|
||||
{
|
||||
Batch& Batch::put(ByteView key, ByteView value)
|
||||
{
|
||||
Mutation m;
|
||||
m.kind = MutationKind::Put;
|
||||
m.key.assign(key.begin(), key.end());
|
||||
m.value.assign(value.begin(), value.end());
|
||||
m_mutations.push_back(std::move(m));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Batch& Batch::erase(ByteView key)
|
||||
{
|
||||
Mutation m;
|
||||
m.kind = MutationKind::Erase;
|
||||
m.key.assign(key.begin(), key.end());
|
||||
m_mutations.push_back(std::move(m));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Batch& Batch::require_absent(ByteView key)
|
||||
{
|
||||
Precondition p;
|
||||
p.kind = PreconditionKind::Absent;
|
||||
p.key.assign(key.begin(), key.end());
|
||||
m_preconditions.push_back(std::move(p));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Batch& Batch::require_version(ByteView key, Sequence expected_version)
|
||||
{
|
||||
Precondition p;
|
||||
p.kind = PreconditionKind::VersionEquals;
|
||||
p.key.assign(key.begin(), key.end());
|
||||
p.expected_version = expected_version;
|
||||
m_preconditions.push_back(std::move(p));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Batch& Batch::require_value(ByteView key, ByteView expected_value)
|
||||
{
|
||||
Precondition p;
|
||||
p.kind = PreconditionKind::ValueEquals;
|
||||
p.key.assign(key.begin(), key.end());
|
||||
p.expected_value.assign(expected_value.begin(), expected_value.end());
|
||||
m_preconditions.push_back(std::move(p));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Result<void> Batch::validate(const Limits& limits) const
|
||||
{
|
||||
if (m_mutations.size() > limits.max_batch_mutations) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::invalid_argument,
|
||||
"Storage::Batch: mutation count exceeds max_batch_mutations"));
|
||||
}
|
||||
if (m_preconditions.size() > limits.max_batch_conditions) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::invalid_argument,
|
||||
"Storage::Batch: precondition count exceeds max_batch_conditions"));
|
||||
}
|
||||
|
||||
std::set<ByteView, ByteLess> seen_mut_keys;
|
||||
std::size_t total_op_bytes = 0;
|
||||
for (const auto& m : m_mutations) {
|
||||
if (m.key.size() > limits.max_key_bytes) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::invalid_argument,
|
||||
"Storage::Batch: mutation key exceeds max_key_bytes"));
|
||||
}
|
||||
if (m.value.size() > limits.max_value_bytes) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::invalid_argument,
|
||||
"Storage::Batch: mutation value exceeds max_value_bytes"));
|
||||
}
|
||||
if (m.kind == MutationKind::Erase && !m.value.empty()) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::invalid_argument,
|
||||
"Storage::Batch: erase mutation must have empty value"));
|
||||
}
|
||||
if (!seen_mut_keys.insert(ByteView{m.key}).second) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::invalid_argument,
|
||||
"Storage::Batch: duplicate mutation key in batch"));
|
||||
}
|
||||
const std::size_t pair_bytes = m.key.size() + m.value.size();
|
||||
if (total_op_bytes > limits.max_batch_encoded_bytes - pair_bytes) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::quota_exceeded,
|
||||
"Storage::Batch: mutation payload exceeds max_batch_encoded_bytes"));
|
||||
}
|
||||
total_op_bytes += pair_bytes;
|
||||
}
|
||||
|
||||
std::set<ByteView, ByteLess> seen_cond_keys;
|
||||
for (const auto& p : m_preconditions) {
|
||||
if (p.key.size() > limits.max_key_bytes) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::invalid_argument,
|
||||
"Storage::Batch: precondition key exceeds max_key_bytes"));
|
||||
}
|
||||
if (p.expected_value.size() > limits.max_value_bytes) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::invalid_argument,
|
||||
"Storage::Batch: precondition expected_value exceeds max_value_bytes"));
|
||||
}
|
||||
if (!seen_cond_keys.insert(ByteView{p.key}).second) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::invalid_argument,
|
||||
"Storage::Batch: duplicate precondition for same key"));
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace Void::Storage
|
||||
Reference in new issue
Block a user