mirror of
https://github.com/facebook/rocksdb.git
synced 2024-12-03 14:52:53 +00:00
c73d2a9d18
Summary: The patch builds on https://github.com/facebook/rocksdb/pull/9915 and adds a new API called `PutEntity` that can be used to write a wide-column entity to the database. The new API is added to both `DB` and `WriteBatch`. Note that currently there is no way to retrieve these entities; more precisely, all read APIs (`Get`, `MultiGet`, and iterator) return `NotSupported` when they encounter a wide-column entity that is required to answer a query. Read-side support (as well as other missing functionality like `Merge`, compaction filter, and timestamp support) will be added in later PRs. Pull Request resolved: https://github.com/facebook/rocksdb/pull/10242 Test Plan: `make check` Reviewed By: riversand963 Differential Revision: D37369748 Pulled By: ltamasi fbshipit-source-id: 7f5e412359ed7a400fd80b897dae5599dbcd685d
141 lines
3.9 KiB
C++
141 lines
3.9 KiB
C++
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
#include "db/wide/wide_column_serialization.h"
|
|
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <limits>
|
|
|
|
#include "rocksdb/slice.h"
|
|
#include "util/autovector.h"
|
|
#include "util/coding.h"
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
Status WideColumnSerialization::Serialize(const WideColumns& columns,
|
|
std::string& output) {
|
|
if (columns.size() >
|
|
static_cast<size_t>(std::numeric_limits<uint32_t>::max())) {
|
|
return Status::InvalidArgument("Too many wide columns");
|
|
}
|
|
|
|
PutVarint32(&output, kCurrentVersion);
|
|
|
|
PutVarint32(&output, static_cast<uint32_t>(columns.size()));
|
|
|
|
for (size_t i = 0; i < columns.size(); ++i) {
|
|
const WideColumn& column = columns[i];
|
|
|
|
const Slice& name = column.name();
|
|
if (name.size() >
|
|
static_cast<size_t>(std::numeric_limits<uint32_t>::max())) {
|
|
return Status::InvalidArgument("Wide column name too long");
|
|
}
|
|
if (i > 0 && columns[i - 1].name().compare(name) >= 0) {
|
|
return Status::Corruption("Wide columns out of order");
|
|
}
|
|
|
|
const Slice& value = column.value();
|
|
if (value.size() >
|
|
static_cast<size_t>(std::numeric_limits<uint32_t>::max())) {
|
|
return Status::InvalidArgument("Wide column value too long");
|
|
}
|
|
|
|
PutLengthPrefixedSlice(&output, name);
|
|
PutVarint32(&output, static_cast<uint32_t>(value.size()));
|
|
}
|
|
|
|
for (const auto& column : columns) {
|
|
const Slice& value = column.value();
|
|
|
|
output.append(value.data(), value.size());
|
|
}
|
|
|
|
return Status::OK();
|
|
}
|
|
|
|
Status WideColumnSerialization::Deserialize(Slice& input,
|
|
WideColumns& columns) {
|
|
assert(columns.empty());
|
|
|
|
uint32_t version = 0;
|
|
if (!GetVarint32(&input, &version)) {
|
|
return Status::Corruption("Error decoding wide column version");
|
|
}
|
|
|
|
if (version > kCurrentVersion) {
|
|
return Status::NotSupported("Unsupported wide column version");
|
|
}
|
|
|
|
uint32_t num_columns = 0;
|
|
if (!GetVarint32(&input, &num_columns)) {
|
|
return Status::Corruption("Error decoding number of wide columns");
|
|
}
|
|
|
|
if (!num_columns) {
|
|
return Status::OK();
|
|
}
|
|
|
|
columns.reserve(num_columns);
|
|
|
|
autovector<uint32_t, 16> column_value_sizes;
|
|
column_value_sizes.reserve(num_columns);
|
|
|
|
for (uint32_t i = 0; i < num_columns; ++i) {
|
|
Slice name;
|
|
if (!GetLengthPrefixedSlice(&input, &name)) {
|
|
return Status::Corruption("Error decoding wide column name");
|
|
}
|
|
|
|
if (!columns.empty() && columns.back().name().compare(name) >= 0) {
|
|
return Status::Corruption("Wide columns out of order");
|
|
}
|
|
|
|
columns.emplace_back(name, Slice());
|
|
|
|
uint32_t value_size = 0;
|
|
if (!GetVarint32(&input, &value_size)) {
|
|
return Status::Corruption("Error decoding wide column value size");
|
|
}
|
|
|
|
column_value_sizes.emplace_back(value_size);
|
|
}
|
|
|
|
const Slice data(input);
|
|
size_t pos = 0;
|
|
|
|
for (uint32_t i = 0; i < num_columns; ++i) {
|
|
const uint32_t value_size = column_value_sizes[i];
|
|
|
|
if (pos + value_size > data.size()) {
|
|
return Status::Corruption("Error decoding wide column value payload");
|
|
}
|
|
|
|
columns[i].value() = Slice(data.data() + pos, value_size);
|
|
|
|
pos += value_size;
|
|
}
|
|
|
|
return Status::OK();
|
|
}
|
|
|
|
WideColumns::const_iterator WideColumnSerialization::Find(
|
|
const WideColumns& columns, const Slice& column_name) {
|
|
const auto it =
|
|
std::lower_bound(columns.cbegin(), columns.cend(), column_name,
|
|
[](const WideColumn& lhs, const Slice& rhs) {
|
|
return lhs.name().compare(rhs) < 0;
|
|
});
|
|
|
|
if (it == columns.cend() || it->name() != column_name) {
|
|
return columns.cend();
|
|
}
|
|
|
|
return it;
|
|
}
|
|
|
|
} // namespace ROCKSDB_NAMESPACE
|