<commit_before>// Copyright 2010-2014 RethinkDB, all rights reserved.
#include "btree/secondary_operations.hpp"

#include "btree/operations.hpp"
#include "buffer_cache/alt/alt.hpp"
#include "buffer_cache/alt/blob.hpp"
#include "buffer_cache/alt/serialize_onto_blob.hpp"
#include "containers/archive/vector_stream.hpp"

RDB_IMPL_SERIALIZABLE_5(secondary_index_t, superblock, opaque_definition,
                        post_construction_complete, being_deleted, id);

RDB_IMPL_SERIALIZABLE_2(sindex_name_t, name, being_deleted);

struct btree_sindex_block_t {
    static const int SINDEX_BLOB_MAXREFLEN = 4076;

    block_magic_t magic;
    char sindex_blob[SINDEX_BLOB_MAXREFLEN];
} __attribute__((__packed__));

// RSI: Can this be in the .cc file?
template <cluster_version_t W>
struct btree_sindex_block_magic_t { static const block_magic_t value; };

cluster_version_t sindex_block_version(const btree_sindex_block_t *data);
void sindex_block_initialize(btree_sindex_block_t *data);

template <>
const block_magic_t
btree_sindex_block_magic_t<cluster_version_t::v1_13_is_latest>::value
    = { { 's', 'i', 'n', 'd' } };

cluster_version_t sindex_block_version(const btree_sindex_block_t *data) {
    if (data->magic
        == btree_sindex_block_magic_t<cluster_version_t::v1_13_is_latest>::value) {
        return cluster_version_t::v1_13_is_latest;
    } else {
        crash("Unexpected magic in btree_sindex_block_t.");
    }
}

void sindex_block_initialize(btree_sindex_block_t *data) {
    data->magic = btree_sindex_block_magic_t<cluster_version_t::ONLY_VERSION>::value;
    memset(data->sindex_blob, 0, btree_sindex_block_t::SINDEX_BLOB_MAXREFLEN);
}

void get_secondary_indexes_internal(
        buf_lock_t *sindex_block,
        std::map<sindex_name_t, secondary_index_t> *sindexes_out) {
    buf_read_t read(sindex_block);
    const btree_sindex_block_t *data
        = static_cast<const btree_sindex_block_t *>(read.get_data_read());

    blob_t sindex_blob(sindex_block->cache()->max_block_size(),
                       const_cast<char *>(data->sindex_blob),
                       btree_sindex_block_t::SINDEX_BLOB_MAXREFLEN);
    deserialize_for_version_from_blob(sindex_block_version(data),
                                      buf_parent_t(sindex_block), &sindex_blob, sindexes_out);
}

void set_secondary_indexes_internal(
        buf_lock_t *sindex_block,
        const std::map<sindex_name_t, secondary_index_t> &sindexes) {
    buf_write_t write(sindex_block);
    btree_sindex_block_t *data
        = static_cast<btree_sindex_block_t *>(write.get_data_write());

    blob_t sindex_blob(sindex_block->cache()->max_block_size(),
                       data->sindex_blob,
                       btree_sindex_block_t::SINDEX_BLOB_MAXREFLEN);
    serialize_for_version_onto_blob(sindex_block_version(data),
                                    buf_parent_t(sindex_block), &sindex_blob, sindexes);
}

void initialize_secondary_indexes(buf_lock_t *sindex_block) {
    buf_write_t write(sindex_block);
    btree_sindex_block_t *data
        = static_cast<btree_sindex_block_t *>(write.get_data_write());
    sindex_block_initialize(data);

    set_secondary_indexes_internal(sindex_block,
                                   std::map<sindex_name_t, secondary_index_t>());
}

bool get_secondary_index(buf_lock_t *sindex_block, const sindex_name_t &name,
                         secondary_index_t *sindex_out) {
    std::map<sindex_name_t, secondary_index_t> sindex_map;

    get_secondary_indexes_internal(sindex_block, &sindex_map);

    auto it = sindex_map.find(name);
    if (it != sindex_map.end()) {
        *sindex_out = it->second;
        return true;
    } else {
        return false;
    }
}

bool get_secondary_index(buf_lock_t *sindex_block, uuid_u id,
                         secondary_index_t *sindex_out) {
    std::map<sindex_name_t, secondary_index_t> sindex_map;

    get_secondary_indexes_internal(sindex_block, &sindex_map);
    for (auto it = sindex_map.begin(); it != sindex_map.end(); ++it) {
        if (it->second.id == id) {
            *sindex_out = it->second;
            return true;
        }
    }<commit_msg>Removed pointless sindex_block_initialize function.<commit_after>// Copyright 2010-2014 RethinkDB, all rights reserved.
#include "btree/secondary_operations.hpp"

#include "btree/operations.hpp"
#include "buffer_cache/alt/alt.hpp"
#include "buffer_cache/alt/blob.hpp"
#include "buffer_cache/alt/serialize_onto_blob.hpp"
#include "containers/archive/vector_stream.hpp"

RDB_IMPL_SERIALIZABLE_5(secondary_index_t, superblock, opaque_definition,
                        post_construction_complete, being_deleted, id);

RDB_IMPL_SERIALIZABLE_2(sindex_name_t, name, being_deleted);

struct btree_sindex_block_t {
    static const int SINDEX_BLOB_MAXREFLEN = 4076;

    block_magic_t magic;
    char sindex_blob[SINDEX_BLOB_MAXREFLEN];
} __attribute__((__packed__));

// RSI: Can this be in the .cc file?
template <cluster_version_t W>
struct btree_sindex_block_magic_t { static const block_magic_t value; };

template <>
const block_magic_t
btree_sindex_block_magic_t<cluster_version_t::v1_13_is_latest>::value
    = { { 's', 'i', 'n', 'd' } };

cluster_version_t sindex_block_version(const btree_sindex_block_t *data) {
    if (data->magic
        == btree_sindex_block_magic_t<cluster_version_t::v1_13_is_latest>::value) {
        return cluster_version_t::v1_13_is_latest;
    } else {
        crash("Unexpected magic in btree_sindex_block_t.");
    }
}

void get_secondary_indexes_internal(
        buf_lock_t *sindex_block,
        std::map<sindex_name_t, secondary_index_t> *sindexes_out) {
    buf_read_t read(sindex_block);
    const btree_sindex_block_t *data
        = static_cast<const btree_sindex_block_t *>(read.get_data_read());

    blob_t sindex_blob(sindex_block->cache()->max_block_size(),
                       const_cast<char *>(data->sindex_blob),
                       btree_sindex_block_t::SINDEX_BLOB_MAXREFLEN);
    deserialize_for_version_from_blob(sindex_block_version(data),
                                      buf_parent_t(sindex_block), &sindex_blob, sindexes_out);
}

void set_secondary_indexes_internal(
        buf_lock_t *sindex_block,
        const std::map<sindex_name_t, secondary_index_t> &sindexes) {
    buf_write_t write(sindex_block);
    btree_sindex_block_t *data
        = static_cast<btree_sindex_block_t *>(write.get_data_write());

    blob_t sindex_blob(sindex_block->cache()->max_block_size(),
                       data->sindex_blob,
                       btree_sindex_block_t::SINDEX_BLOB_MAXREFLEN);
    serialize_for_version_onto_blob(sindex_block_version(data),
                                    buf_parent_t(sindex_block), &sindex_blob, sindexes);
}

void initialize_secondary_indexes(buf_lock_t *sindex_block) {
    buf_write_t write(sindex_block);
    btree_sindex_block_t *data
        = static_cast<btree_sindex_block_t *>(write.get_data_write());
    data->magic = btree_sindex_block_magic_t<cluster_version_t::ONLY_VERSION>::value;
    memset(data->sindex_blob, 0, btree_sindex_block_t::SINDEX_BLOB_MAXREFLEN);

    set_secondary_indexes_internal(sindex_block,
                                   std::map<sindex_name_t, secondary_index_t>());
}

bool get_secondary_index(buf_lock_t *sindex_block, const sindex_name_t &name,
                         secondary_index_t *sindex_out) {
    std::map<sindex_name_t, secondary_index_t> sindex_map;

    get_secondary_indexes_internal(sindex_block, &sindex_map);

    auto it = sindex_map.find(name);
    if (it != sindex_map.end()) {
        *sindex_out = it->second;
        return true;
    } else {
        return false;
    }
}

bool get_secondary_index(buf_lock_t *sindex_block, uuid_u id,
                         secondary_index_t *sindex_out) {
    std::map<sindex_name_t, secondary_index_t> sindex_map;

    get_secondary_indexes_internal(sindex_block, &sindex_map);
    for (auto it = sindex_map.begin(); it != sindex_map.end(); ++it) {
        if (it->second.id == id) {
            *sindex_out = it->second;
            return true;
        }
    }
{'fraction_non_alphanumeric': 0.09778597785977859, 'fraction_numerical': 0.005904059040590406, 'mean_word_length': 3.760538641686183, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3083665', 'n_tokens_mistral': 2923, 'n_tokens_neox': 2842, 'n_words': 402}