Skip to content

Commit

Permalink
json: Introduce chunked_buffer
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Pope <ben@redpanda.com>
  • Loading branch information
BenPope committed Jul 4, 2024
1 parent d74ec2a commit f17567f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/v/json/chunked_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2024 Redpanda Data, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0

#pragma once

#include "bytes/iobuf.h"
#include "json/encodings.h"

namespace json {

namespace impl {

/**
* \brief An in-memory output stream with non-contiguous memory allocation.
*/
template<typename Encoding>
struct generic_chunked_buffer {
using Ch = Encoding::Ch;

/**
* \defgroup Implement rapidjson::Stream
*/
/**@{*/

void Put(Ch c) { _impl.append(&c, sizeof(Ch)); }
void Flush() {}

//! Get the size of string in bytes in the string buffer.
size_t GetSize() const { return _impl.size_bytes(); }

//! Get the length of string in Ch in the string buffer.
size_t GetLength() const { return _impl.size_bytes() / sizeof(Ch); }

void Reserve(size_t s) { _impl.reserve(s); }

void Clear() { _impl.clear(); }

/**@}*/

iobuf as_iobuf() && { return std::move(_impl); }

private:
iobuf _impl;
};

} // namespace impl

template<typename Encoding>
using generic_chunked_buffer = impl::generic_chunked_buffer<Encoding>;

using chunked_buffer = generic_chunked_buffer<UTF8<>>;

} // namespace json
40 changes: 40 additions & 0 deletions src/v/json/json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "json/json.h"

#include "json/chunked_buffer.h"
#include "json/stringbuffer.h"

namespace json {
Expand Down Expand Up @@ -175,4 +176,43 @@ template void rjson_serialize<json::StringBuffer>(
template void rjson_serialize<json::StringBuffer>(
json::Writer<json::StringBuffer>& w, const std::filesystem::path& path);

template void
rjson_serialize<chunked_buffer>(json::Writer<chunked_buffer>& w, short v);

template void
rjson_serialize<chunked_buffer>(json::Writer<chunked_buffer>& w, bool v);

template void
rjson_serialize<chunked_buffer>(json::Writer<chunked_buffer>& w, long long v);

template void
rjson_serialize<chunked_buffer>(json::Writer<chunked_buffer>& w, int v);

template void rjson_serialize<chunked_buffer>(
json::Writer<chunked_buffer>& w, unsigned int v);

template void
rjson_serialize<chunked_buffer>(json::Writer<chunked_buffer>& w, long v);

template void rjson_serialize<chunked_buffer>(
json::Writer<chunked_buffer>& w, unsigned long v);

template void
rjson_serialize<chunked_buffer>(json::Writer<chunked_buffer>& w, double v);

template void rjson_serialize<chunked_buffer>(
json::Writer<chunked_buffer>& w, std::string_view s);

template void rjson_serialize<chunked_buffer>(
json::Writer<chunked_buffer>& w, const net::unresolved_address& v);

template void rjson_serialize<chunked_buffer>(
json::Writer<chunked_buffer>& w, const std::chrono::milliseconds& v);

template void rjson_serialize<chunked_buffer>(
json::Writer<chunked_buffer>& w, const std::chrono::seconds& v);

template void rjson_serialize<chunked_buffer>(
json::Writer<chunked_buffer>& w, const std::filesystem::path& path);

} // namespace json

0 comments on commit f17567f

Please sign in to comment.