<commit_before>#include <utility>
#include <ostream>
#include <iomanip>

#include <tightdb/terminate.hpp>
#include <tightdb/safe_int_ops.hpp>
#include <tightdb/string_buffer.hpp>
#include <tightdb/unique_ptr.hpp>
#include <tightdb/table.hpp>
#include <tightdb/group_shared.hpp>
#include <tightdb/replication.hpp>

using namespace std;
using namespace tightdb;

namespace {

const size_t init_subtab_path_buf_levels = 2; // 2 table levels (soft limit)
const size_t init_subtab_path_buf_size = 2*init_subtab_path_buf_levels - 1;

} // anonymous namespace


namespace tightdb {


Replication::Replication(): m_selected_table(0), m_selected_spec(0)
{
    m_subtab_path_buf.set_size(init_subtab_path_buf_size); // Throws
}


Group& Replication::get_group(SharedGroup& sg) TIGHTDB_NOEXCEPT
{
    return sg.m_group;
}


Replication::database_version_type
Replication::get_current_version(SharedGroup& sg) TIGHTDB_NOEXCEPT
{
    return sg.get_current_version();
}


void Replication::commit_foreign_transact_log(SharedGroup& sg, database_version_type new_version)
{
    sg.low_level_commit(new_version);
}


void Replication::select_table(const Table* table)
{
    size_t* begin;
    size_t* end;
    for (;;) {
        begin = m_subtab_path_buf.m_data.get();
        end = table->record_subtable_path(begin, begin+m_subtab_path_buf.m_size);
        if (end)
            break;
        size_t new_size = m_subtab_path_buf.m_size;
        if (int_multiply_with_overflow_detect(new_size, 2))
            throw runtime_error("To many subtable nesting levels");
        m_subtab_path_buf.set_size(new_size); // Throws
    }
    char* buf;
    const int max_elems_per_chunk = 8; // FIXME: Use smaller number when compiling in debug mode
    transact_log_reserve(&buf, 1 + (1+max_elems_per_chunk)*max_enc_bytes_per_int); // Throws
    *buf++ = 'T';
    TIGHTDB_ASSERT(1 <= end - begin);
    const ptrdiff_t level = (end - begin) / 2;
    buf = encode_int(buf, level);
    for (;;) {
        for (int i=0; i<max_elems_per_chunk; ++i) {
            buf = encode_int(buf, *--end);
            if (begin == end)
                goto good;
        }
        transact_log_advance(buf);
        transact_log_reserve(&buf, max_elems_per_chunk*max_enc_bytes_per_int); // Throws
    }

good:
    transact_log_advance(buf);
    m_selected_spec  = 0;
    m_selected_table = table;
}


void Replication::select_spec(const Table* table, const Spec* spec)
{
    check_table(table);
    size_t* begin;
    size_t* end;
    for (;;) {
        begin = m_subtab_path_buf.m_data.get();
        end = table->record_subspec_path(spec, begin, begin+m_subtab_path_buf.m_size);
        if (end) break;
        size_t new_size = m_subtab_path_buf.m_size;
        if (int_multiply_with_overflow_detect(new_size, 2))
            throw runtime_error("To many subspec nesting levels");
        m_subtab_path_buf.set_size(new_size); // Throws
    }
    char* buf;
    const int max_elems_per_chunk = 8; // FIXME: Use smaller number when compiling in debug mode
    transact_log_reserve(&buf, 1 + (1+max_elems_per_chunk)*max_enc_bytes_per_int); // Throws
    *buf++ = 'S';
    const ptrdiff_t level = end - begin;
    buf = encode_int(buf, level);
    if (begin == end)
        goto good;
    for (;;) {
        for (int i=0; i<max_elems_per_chunk; ++i) {
            buf = encode_int(buf, *--end);
            if (begin == end)
                goto good;
        }
        transact_log_advance(buf);
        transact_log_reserve(&buf, max_elems_per_chunk*max_enc_bytes_per_int); // Throws
    }

good:
    transact_log_advance(buf);
    m_selected_spec = spec;
}<commit_msg>Inappropriate use of 'ptrdiff_t'. Now using 'size_t' instead<commit_after>#include <utility>
#include <ostream>
#include <iomanip>

#include <tightdb/terminate.hpp>
#include <tightdb/safe_int_ops.hpp>
#include <tightdb/string_buffer.hpp>
#include <tightdb/unique_ptr.hpp>
#include <tightdb/table.hpp>
#include <tightdb/group_shared.hpp>
#include <tightdb/replication.hpp>

using namespace std;
using namespace tightdb;

namespace {

const size_t init_subtab_path_buf_levels = 2; // 2 table levels (soft limit)
const size_t init_subtab_path_buf_size = 2*init_subtab_path_buf_levels - 1;

} // anonymous namespace


namespace tightdb {


Replication::Replication(): m_selected_table(0), m_selected_spec(0)
{
    m_subtab_path_buf.set_size(init_subtab_path_buf_size); // Throws
}


Group& Replication::get_group(SharedGroup& sg) TIGHTDB_NOEXCEPT
{
    return sg.m_group;
}


Replication::database_version_type
Replication::get_current_version(SharedGroup& sg) TIGHTDB_NOEXCEPT
{
    return sg.get_current_version();
}


void Replication::commit_foreign_transact_log(SharedGroup& sg, database_version_type new_version)
{
    sg.low_level_commit(new_version);
}


void Replication::select_table(const Table* table)
{
    size_t* begin;
    size_t* end;
    for (;;) {
        begin = m_subtab_path_buf.m_data.get();
        end = table->record_subtable_path(begin, begin+m_subtab_path_buf.m_size);
        if (end)
            break;
        size_t new_size = m_subtab_path_buf.m_size;
        if (int_multiply_with_overflow_detect(new_size, 2))
            throw runtime_error("To many subtable nesting levels");
        m_subtab_path_buf.set_size(new_size); // Throws
    }
    char* buf;
    const int max_elems_per_chunk = 8; // FIXME: Use smaller number when compiling in debug mode
    transact_log_reserve(&buf, 1 + (1+max_elems_per_chunk)*max_enc_bytes_per_int); // Throws
    *buf++ = 'T';
    TIGHTDB_ASSERT(1 <= end - begin);
    const size_t level = (end - begin) / 2;
    buf = encode_int(buf, level);
    for (;;) {
        for (int i=0; i<max_elems_per_chunk; ++i) {
            buf = encode_int(buf, *--end);
            if (begin == end)
                goto good;
        }
        transact_log_advance(buf);
        transact_log_reserve(&buf, max_elems_per_chunk*max_enc_bytes_per_int); // Throws
    }

good:
    transact_log_advance(buf);
    m_selected_spec  = 0;
    m_selected_table = table;
}


void Replication::select_spec(const Table* table, const Spec* spec)
{
    check_table(table);
    size_t* begin;
    size_t* end;
    for (;;) {
        begin = m_subtab_path_buf.m_data.get();
        end = table->record_subspec_path(spec, begin, begin+m_subtab_path_buf.m_size);
        if (end) break;
        size_t new_size = m_subtab_path_buf.m_size;
        if (int_multiply_with_overflow_detect(new_size, 2))
            throw runtime_error("To many subspec nesting levels");
        m_subtab_path_buf.set_size(new_size); // Throws
    }
    char* buf;
    const int max_elems_per_chunk = 8; // FIXME: Use smaller number when compiling in debug mode
    transact_log_reserve(&buf, 1 + (1+max_elems_per_chunk)*max_enc_bytes_per_int); // Throws
    *buf++ = 'S';
    const size_t level = end - begin;
    buf = encode_int(buf, level);
    if (begin == end)
        goto good;
    for (;;) {
        for (int i=0; i<max_elems_per_chunk; ++i) {
            buf = encode_int(buf, *--end);
            if (begin == end)
                goto good;
        }
        transact_log_advance(buf);
        transact_log_reserve(&buf, max_elems_per_chunk*max_enc_bytes_per_int); // Throws
    }

good:
    transact_log_advance(buf);
    m_selected_spec = spec;
}
{'fraction_non_alphanumeric': 0.11220715166461159, 'fraction_numerical': 0.005206192629127277, 'mean_word_length': 3.363419007770472, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '787354', 'n_tokens_mistral': 2687, 'n_tokens_neox': 2602, 'n_words': 503}