Files
scylladb/vint-serialization.hh
Jesse Haber-Kucharsky 91dab1d998 CQL native protocol: Add support for vint serialization
Version 5 of the native protocol for CQL [1] adds the `vint` and `unsigned vint`
types.

An unsigned integer encoded as a `vint` has a variable size based on the
magnitude of the value. The first byte indicates the total number of bytes.

For signed integers, a "zig-zag" encoding scheme ensures that small negative
values are encoded as short-length `vint`s (0 -> 0, -1 -> 1, 1 -> 2, 2 -> 3, -2
-> 4, etc).

[1] https://github.com/apache/cassandra/blob/trunk/doc/native_protocol_v5.spec
2017-08-10 14:11:30 -04:00

61 lines
1.6 KiB
C++

/*
* Copyright 2017 ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
//
// For reference, see https://developers.google.com/protocol-buffers/docs/encoding#varints
//
#include "bytes.hh"
#include <cstdint>
using vint_size_type = bytes::size_type;
struct unsigned_vint final {
using value_type = uint64_t;
struct deserialized_type final {
value_type value;
vint_size_type size;
};
static vint_size_type serialized_size(value_type) noexcept;
static vint_size_type serialize(value_type, bytes::iterator out);
static deserialized_type deserialize(bytes_view v);
};
struct signed_vint final {
using value_type = int64_t;
struct deserialized_type final {
value_type value;
vint_size_type size;
};
static vint_size_type serialized_size(value_type) noexcept;
static vint_size_type serialize(value_type, bytes::iterator out);
static deserialized_type deserialize(bytes_view v);
};