<commit_before>/*
 * Copyright 2014 Cloudius Systems
 */

/*
 * 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/>.
 */

#ifndef UTILS_DATA_OUTPUT_HH_
#define UTILS_DATA_OUTPUT_HH_

#include "bytes.hh"
#include "net/byteorder.hh"

/**
 * Data output interface for Java-esqe wire serialization of
 * basic type, including strings and bytes. The latter I've opted
 * to keep for a few reasons:
 * 1.) It matches better with data_input.read<sstring>, which in turn
 *     is somewhat meaningful because it can create a string more efficiently
 *     without adding another slightly contrived abstraction (iterators?)
 * 2.) At some point this interface should maybe be extended to support stream-like
 *     underlying mediums (such as a back_insert iterator etc), at which point
 *     having to know the exact size of what it being serialized becomes less of an
 *     issue.
 */
class data_output {
public:
    data_output(char* p, char* e)
            : _ptr(p), _end(e) {
    }
    data_output(char* p, size_t n)
            : data_output(p, p + n) {
    }
    data_output(bytes& b)
            : data_output(reinterpret_cast<char*>(b.begin()), b.size()) {
    }
    data_output(bytes& b, size_t off, size_t n = bytes::npos)
            : data_output(reinterpret_cast<char*>(b.begin()) + off,
                    reinterpret_cast<char*>(b.begin()) + off + std::min(b.size() - off, n)) {
        if (off > b.size()) {
            throw std::out_of_range("Offset out of range");
        }
    }

    template<typename T>
    static inline std::enable_if_t<std::is_fundamental<T>::value, size_t> serialized_size(const T&) {
        return sizeof(T);
    }
    template<typename T>
    static inline std::enable_if_t<std::is_fundamental<T>::value, size_t> serialized_size() {
        return sizeof(T);
    }
    static inline size_t serialized_size(const sstring& s) {
        if (s.size() > std::numeric_limits<uint16_t>::max()) {
            throw std::out_of_range("String too large");
        }
        return sizeof(uint16_t) + s.size();
    }
    static inline size_t serialized_size(const bytes_view& s) {
        if (s.size() > std::numeric_limits<uint32_t>::max()) {
            throw std::out_of_range("Buffer too large");
        }
        return sizeof(uint32_t) + s.size();
    }
    static inline size_t serialized_size(const bytes& s) {
        return serialized_size(bytes_view(s));
    }

    size_t avail() const {
        return _end - _ptr;
    }
    void ensure(size_t s) const {
        if (avail() < s) {
            throw std::out_of_range("Buffer overflow");
        }
    }
    data_output& skip(size_t s) {
        ensure(s);
        _ptr += s;
        return *this;
    }

    template<typename T>
    inline std::enable_if_t<std::is_fundamental<T>::value, data_output&> write(T t);

    data_output& write(const sstring& s) {
        ensure(serialized_size(s));
        write(uint16_t(s.size()));
        _ptr = std::copy(s.begin(), s.end(), _ptr);
        return *this;
    }
    data_output& write(const bytes_view& s) {
        ensure(serialized_size(s));
        write(uint32_t(s.size()));
        _ptr = std::copy(s.begin(), s.end(), _ptr);
        return *this;
    }
    data_output& write(const bytes & s) {
        return write(bytes_view(s));
    }
    template<typename Iter>
    data_output& write(Iter s, Iter e) {
        while (s != e) {
            write(*s++);
        }
        return *this;
    }
    data_output& write(const char* s, const char* e) {
        ensure(e - s);
        _ptr = std::copy(s, e, _ptr);
        return *this;
    }
    template<typename T>
    data_output& write(T t, size_t n) {
        while (n-- > 0) {
            write(t);
        }
        return *this;
    }
private:<commit_msg>data_output: Template "blob" writers (bytes*) to allow for varying "size" type<commit_after>/*
 * Copyright 2014 Cloudius Systems
 */

/*
 * 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/>.
 */

#ifndef UTILS_DATA_OUTPUT_HH_
#define UTILS_DATA_OUTPUT_HH_

#include "bytes.hh"
#include "net/byteorder.hh"

/**
 * Data output interface for Java-esqe wire serialization of
 * basic type, including strings and bytes. The latter I've opted
 * to keep for a few reasons:
 * 1.) It matches better with data_input.read<sstring>, which in turn
 *     is somewhat meaningful because it can create a string more efficiently
 *     without adding another slightly contrived abstraction (iterators?)
 * 2.) At some point this interface should maybe be extended to support stream-like
 *     underlying mediums (such as a back_insert iterator etc), at which point
 *     having to know the exact size of what it being serialized becomes less of an
 *     issue.
 */
class data_output {
public:
    data_output(char* p, char* e)
            : _ptr(p), _end(e) {
    }
    data_output(char* p, size_t n)
            : data_output(p, p + n) {
    }
    data_output(bytes& b)
            : data_output(reinterpret_cast<char*>(b.begin()), b.size()) {
    }
    data_output(bytes& b, size_t off, size_t n = bytes::npos)
            : data_output(reinterpret_cast<char*>(b.begin()) + off,
                    reinterpret_cast<char*>(b.begin()) + off + std::min(b.size() - off, n)) {
        if (off > b.size()) {
            throw std::out_of_range("Offset out of range");
        }
    }

    template<typename T>
    static inline std::enable_if_t<std::is_fundamental<T>::value, size_t> serialized_size(const T&) {
        return sizeof(T);
    }
    template<typename T>
    static inline std::enable_if_t<std::is_fundamental<T>::value, size_t> serialized_size() {
        return sizeof(T);
    }
    template<typename SizeType = uint16_t>
    static inline size_t serialized_size(const sstring& s) {
        if (s.size() > std::numeric_limits<SizeType>::max()) {
            throw std::out_of_range("String too large");
        }
        return sizeof(SizeType) + s.size();
    }
    template<typename SizeType = uint32_t>
    static inline size_t serialized_size(const bytes_view& s) {
        if (s.size() > std::numeric_limits<SizeType>::max()) {
            throw std::out_of_range("Buffer too large");
        }
        return sizeof(SizeType) + s.size();
    }
    template<typename SizeType = uint32_t>
    static inline size_t serialized_size(const bytes& s) {
        return serialized_size<SizeType>(bytes_view(s));
    }

    size_t avail() const {
        return _end - _ptr;
    }
    void ensure(size_t s) const {
        if (avail() < s) {
            throw std::out_of_range("Buffer overflow");
        }
    }
    data_output& skip(size_t s) {
        ensure(s);
        _ptr += s;
        return *this;
    }

    template<typename T>
    inline std::enable_if_t<std::is_fundamental<T>::value, data_output&> write(T t);

    template<typename SizeType = uint16_t>
    data_output& write(const sstring& s) {
        ensure(serialized_size<SizeType>(s));
        write(SizeType(s.size()));
        _ptr = std::copy(s.begin(), s.end(), _ptr);
        return *this;
    }
    template<typename SizeType = uint32_t>
    data_output& write(const bytes_view& s) {
        ensure(serialized_size<SizeType>(s));
        write(SizeType(s.size()));
        _ptr = std::copy(s.begin(), s.end(), _ptr);
        return *this;
    }
    template<typename SizeType = uint32_t>
    data_output& write(const bytes & s) {
        return write<SizeType>(bytes_view(s));
    }
    template<typename Iter>
    data_output& write(Iter s, Iter e) {
        while (s != e) {
            write(*s++);
        }
        return *this;
    }
    data_output& write(const char* s, const char* e) {
        ensure(e - s);
        _ptr = std::copy(s, e, _ptr);
        return *this;
    }
    template<typename T>
    data_output& write(T t, size_t n) {
        while (n-- > 0) {
            write(t);
        }
        return *this;
    }
private:
{'fraction_non_alphanumeric': 0.11812493138654079, 'fraction_numerical': 0.00439126138983423, 'mean_word_length': 2.6748688987494957, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7517766', 'n_tokens_mistral': 2872, 'n_tokens_neox': 2813, 'n_words': 849}