<commit_before>// Copyright 2010-2014 RethinkDB, all rights reserved.
#include <functional>
#include <vector>

#include "errors.hpp"
#include <boost/ptr_container/ptr_vector.hpp>

#include "arch/io/disk/conflict_resolving.hpp"
#include "arch/io/disk/accounting.hpp"
#include "arch/runtime/thread_pool.hpp"
#include "containers/intrusive_list.hpp"
#include "containers/scoped.hpp"
#include "unittest/gtest.hpp"

namespace unittest {

/* We need for multiple test_driver_t objects to share a file
   descriptor in order to test the conflict resolution logic, but
   it doesn't matter what that file descriptor is. */
static const int IRRELEVANT_DEFAULT_FD = 0;

struct test_driver_t {
    typedef conflict_resolving_diskmgr_t::action_t action_t;

    // We avoid deallocating actions during the test to make sure that each action
    // has a unique pointer value.
    boost::ptr_vector<action_t> allocated_actions;

    std::set<accounting_diskmgr_action_t *> running_actions;
    std::vector<char> data;

    conflict_resolving_diskmgr_t conflict_resolver;

    // These work because all actions are part of allocated_actions -- they have
    // unique pointer values.
    std::set<accounting_diskmgr_action_t *> actions_that_have_begun;
    std::set<accounting_diskmgr_action_t *> actions_that_are_done;

    int old_thread_id;
    test_driver_t() : conflict_resolver(&get_global_perfmon_collection()) {
        /* Fake thread-context to make perfmons work. */
        old_thread_id = linux_thread_pool_t::get_thread_id();
        linux_thread_pool_t::set_thread_id(0);

        conflict_resolver.submit_fun = std::bind(
                &test_driver_t::submit_from_conflict_resolving_diskmgr, this, ph::_1);
        conflict_resolver.done_fun = std::bind(
                &test_driver_t::done_from_conflict_resolving_diskmgr, this, ph::_1);
    }
    ~test_driver_t() {
        linux_thread_pool_t::set_thread_id(old_thread_id);
    }

    void submit(action_t *a) {
        conflict_resolver.submit(a);
    }

    action_t *make_action() {
        action_t *ret = new action_t;
        allocated_actions.push_back(ret);
        return ret;
    }

    bool action_has_begun(accounting_diskmgr_action_t *action) const {
        return actions_that_have_begun.find(action) != actions_that_have_begun.end();
    }

    bool action_is_done(accounting_diskmgr_action_t *action) const {
        return actions_that_are_done.find(action) != actions_that_are_done.end();
    }

    void submit_from_conflict_resolving_diskmgr(accounting_diskmgr_action_t *a) {

        rassert(!action_has_begun(a));
        rassert(!action_is_done(a));
        actions_that_have_begun.insert(a);

        /* The conflict_resolving_diskmgr_t should not have sent us two potentially
        conflicting actions */
        for (auto it = running_actions.begin(); it != running_actions.end(); ++it) {
            accounting_diskmgr_action_t *const p = *it;
            if (!(a->get_is_read() && p->get_is_read())) {
                ASSERT_TRUE(a->get_offset() >= static_cast<int64_t>(p->get_offset() + p->get_count())
                            || p->get_offset() >= static_cast<int64_t>(a->get_offset() + a->get_count()));
            }<commit_msg>Made disk_conflict_resolution.cc not use ptr_vector.<commit_after>// Copyright 2010-2014 RethinkDB, all rights reserved.
#include <functional>
#include <vector>

#include "arch/io/disk/conflict_resolving.hpp"
#include "arch/io/disk/accounting.hpp"
#include "arch/runtime/thread_pool.hpp"
#include "containers/intrusive_list.hpp"
#include "containers/scoped.hpp"
#include "unittest/gtest.hpp"

namespace unittest {

/* We need for multiple test_driver_t objects to share a file
   descriptor in order to test the conflict resolution logic, but
   it doesn't matter what that file descriptor is. */
static const int IRRELEVANT_DEFAULT_FD = 0;

struct test_driver_t {
    typedef conflict_resolving_diskmgr_t::action_t action_t;

    // We avoid deallocating actions during the test to make sure that each action
    // has a unique pointer value.
    std::vector<scoped_ptr_t<action_t> > allocated_actions;

    std::set<accounting_diskmgr_action_t *> running_actions;
    std::vector<char> data;

    conflict_resolving_diskmgr_t conflict_resolver;

    // These work because all actions are part of allocated_actions -- they have
    // unique pointer values.
    std::set<accounting_diskmgr_action_t *> actions_that_have_begun;
    std::set<accounting_diskmgr_action_t *> actions_that_are_done;

    int old_thread_id;
    test_driver_t() : conflict_resolver(&get_global_perfmon_collection()) {
        /* Fake thread-context to make perfmons work. */
        old_thread_id = linux_thread_pool_t::get_thread_id();
        linux_thread_pool_t::set_thread_id(0);

        conflict_resolver.submit_fun = std::bind(
                &test_driver_t::submit_from_conflict_resolving_diskmgr, this, ph::_1);
        conflict_resolver.done_fun = std::bind(
                &test_driver_t::done_from_conflict_resolving_diskmgr, this, ph::_1);
    }
    ~test_driver_t() {
        linux_thread_pool_t::set_thread_id(old_thread_id);
    }

    void submit(action_t *a) {
        conflict_resolver.submit(a);
    }

    action_t *make_action() {
        allocated_actions.push_back(make_scoped<action_t>());
        return allocated_actions.back().get();
    }

    bool action_has_begun(accounting_diskmgr_action_t *action) const {
        return actions_that_have_begun.find(action) != actions_that_have_begun.end();
    }

    bool action_is_done(accounting_diskmgr_action_t *action) const {
        return actions_that_are_done.find(action) != actions_that_are_done.end();
    }

    void submit_from_conflict_resolving_diskmgr(accounting_diskmgr_action_t *a) {

        rassert(!action_has_begun(a));
        rassert(!action_is_done(a));
        actions_that_have_begun.insert(a);

        /* The conflict_resolving_diskmgr_t should not have sent us two potentially
        conflicting actions */
        for (auto it = running_actions.begin(); it != running_actions.end(); ++it) {
            accounting_diskmgr_action_t *const p = *it;
            if (!(a->get_is_read() && p->get_is_read())) {
                ASSERT_TRUE(a->get_offset() >= static_cast<int64_t>(p->get_offset() + p->get_count())
                            || p->get_offset() >= static_cast<int64_t>(a->get_offset() + a->get_count()));
            }
{'fraction_non_alphanumeric': 0.1039788622940628, 'fraction_numerical': 0.004973577867578489, 'mean_word_length': 3.683406113537118, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 44, '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': '1070689', 'n_tokens_mistral': 2148, 'n_tokens_neox': 2088, 'n_words': 396}