<commit_before>/***************************************************************************
 *  io/fileperblock_file.cpp
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2008 <NAME> <<EMAIL>>
 *
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE_1_0.txt or copy at
 *  http://www.boost.org/LICENSE_1_0.txt)
 **************************************************************************/

#include <sstream>
#include <iomanip>
#include <stxxl/bits/io/fileperblock_file.h>
#include <stxxl/bits/io/syscall_file.h>
#include <stxxl/bits/io/mmap_file.h>
#include <stxxl/bits/io/boostfd_file.h>
#include <stxxl/bits/io/wincall_file.h>
#include <stxxl/bits/io/request_impl_basic.h>
#include <stxxl/bits/common/aligned_alloc.h>

__STXXL_BEGIN_NAMESPACE

template<class base_file_type>
fileperblock_file<base_file_type>::fileperblock_file(
    const std::string & filename_prefix,
    int mode,
    int disk)
        : file_request_basic(disk), filename_prefix(filename_prefix), mode(mode), disk(disk),
          lock_file_created(false), lock_file(filename_prefix + "_fpb_lock", mode, disk)
{
}

template<class base_file_type>
fileperblock_file<base_file_type>::~fileperblock_file()
{
    if(lock_file_created)
        ::remove((filename_prefix + "_fpb_lock").c_str());
}

template<class base_file_type>
std::string fileperblock_file<base_file_type>::filename_for_block(unsigned_type offset)
{
    std::ostringstream name;
    //enough for 1 billion blocks
    name << filename_prefix << "_fpb_" << std::setw(20) << std::setfill('0') << offset;
    return name.str();
}

template<class base_file_type>
void fileperblock_file<base_file_type>::serve(const request * req) throw(io_error)
{
    assert(req->get_file() == this);

    base_file_type base_file(filename_for_block(req->get_offset()), mode, disk);
    base_file.set_size(req->get_size());

    request_impl_basic derived(req->get_on_complete(), &base_file, req->get_buffer(), 0, req->get_size(), req->get_type());

    base_file.serve(&derived);

    derived.completed();
}

template<class base_file_type>
void fileperblock_file<base_file_type>::lock()
{
    if(!lock_file_created)
    {
        //create lock file and fill it with one page, an empty file cannot be locked
        const int page_size = 4096;
        void* one_page = aligned_alloc<4096>(page_size);
        lock_file.set_size(page_size);
        request_ptr r = lock_file.awrite(one_page, 0, page_size, default_completion_handler());
        r->wait();
        aligned_dealloc<4096>(one_page);
        lock_file_created = true;
    }
    lock_file.lock();
}

template<class base_file_type>
void fileperblock_file<base_file_type>::delete_region(offset_type offset, unsigned_type length)
{
    UNUSED(length);
    ::remove(filename_for_block(offset).c_str());
    STXXL_VERBOSE0("delete_region " << offset << " + " << length)
}

template<class base_file_type>
void fileperblock_file<base_file_type>::export_files(offset_type offset, offset_type length, std::string filename)
{
    std::string original(filename_for_block(offset));
    filename.insert(0, original.substr(0, original.find_last_of("/") + 1));
    ::remove(filename.c_str());
    ::rename(original.c_str(), filename.c_str());
#ifndef BOOST_MSVC
    //TODO: implement on Windows
    ::truncate(filename.c_str(), length);
#endif
}

template<class base_file_type>
const char * fileperblock_file<base_file_type>::io_type() const
{
    return "fileperblock";
}

////////////////////////////////////////////////////////////////////////////

template class fileperblock_file<syscall_file>;

#ifndef BOOST_MSVC

// mmap call does not exist in Windows
template class fileperblock_file<mmap_file>;

#else

template class fileperblock_file<boostfd_file>;
template class fileperblock_file<wincall_file>;

#endif

__STXXL_END_NAMESPACE<commit_msg>no boostfd files without boost<commit_after>/***************************************************************************
 *  io/fileperblock_file.cpp
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2008 <NAME> <<EMAIL>>
 *
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE_1_0.txt or copy at
 *  http://www.boost.org/LICENSE_1_0.txt)
 **************************************************************************/

#include <sstream>
#include <iomanip>
#include <stxxl/bits/io/fileperblock_file.h>
#include <stxxl/bits/io/syscall_file.h>
#include <stxxl/bits/io/mmap_file.h>
#include <stxxl/bits/io/boostfd_file.h>
#include <stxxl/bits/io/wincall_file.h>
#include <stxxl/bits/io/request_impl_basic.h>
#include <stxxl/bits/common/aligned_alloc.h>

__STXXL_BEGIN_NAMESPACE

template<class base_file_type>
fileperblock_file<base_file_type>::fileperblock_file(
    const std::string & filename_prefix,
    int mode,
    int disk)
        : file_request_basic(disk), filename_prefix(filename_prefix), mode(mode), disk(disk),
          lock_file_created(false), lock_file(filename_prefix + "_fpb_lock", mode, disk)
{
}

template<class base_file_type>
fileperblock_file<base_file_type>::~fileperblock_file()
{
    if(lock_file_created)
        ::remove((filename_prefix + "_fpb_lock").c_str());
}

template<class base_file_type>
std::string fileperblock_file<base_file_type>::filename_for_block(unsigned_type offset)
{
    std::ostringstream name;
    //enough for 1 billion blocks
    name << filename_prefix << "_fpb_" << std::setw(20) << std::setfill('0') << offset;
    return name.str();
}

template<class base_file_type>
void fileperblock_file<base_file_type>::serve(const request * req) throw(io_error)
{
    assert(req->get_file() == this);

    base_file_type base_file(filename_for_block(req->get_offset()), mode, disk);
    base_file.set_size(req->get_size());

    request_impl_basic derived(req->get_on_complete(), &base_file, req->get_buffer(), 0, req->get_size(), req->get_type());

    base_file.serve(&derived);

    derived.completed();
}

template<class base_file_type>
void fileperblock_file<base_file_type>::lock()
{
    if(!lock_file_created)
    {
        //create lock file and fill it with one page, an empty file cannot be locked
        const int page_size = 4096;
        void* one_page = aligned_alloc<4096>(page_size);
        lock_file.set_size(page_size);
        request_ptr r = lock_file.awrite(one_page, 0, page_size, default_completion_handler());
        r->wait();
        aligned_dealloc<4096>(one_page);
        lock_file_created = true;
    }
    lock_file.lock();
}

template<class base_file_type>
void fileperblock_file<base_file_type>::delete_region(offset_type offset, unsigned_type length)
{
    UNUSED(length);
    ::remove(filename_for_block(offset).c_str());
    STXXL_VERBOSE0("delete_region " << offset << " + " << length)
}

template<class base_file_type>
void fileperblock_file<base_file_type>::export_files(offset_type offset, offset_type length, std::string filename)
{
    std::string original(filename_for_block(offset));
    filename.insert(0, original.substr(0, original.find_last_of("/") + 1));
    ::remove(filename.c_str());
    ::rename(original.c_str(), filename.c_str());
#ifndef BOOST_MSVC
    //TODO: implement on Windows
    ::truncate(filename.c_str(), length);
#endif
}

template<class base_file_type>
const char * fileperblock_file<base_file_type>::io_type() const
{
    return "fileperblock";
}

////////////////////////////////////////////////////////////////////////////

template class fileperblock_file<syscall_file>;

#ifndef BOOST_MSVC

// mmap call does not exist in Windows
template class fileperblock_file<mmap_file>;

#else

template class fileperblock_file<wincall_file>;

#endif

#ifdef STXXL_BOOST_CONFIG
template class fileperblock_file<boostfd_file>;
#endif

__STXXL_END_NAMESPACE
{'fraction_non_alphanumeric': 0.17972644765435256, 'fraction_numerical': 0.00818100472964336, 'mean_word_length': 4.765659543109801, 'pattern_counts': {'":': 0, '<': 103, '<?xml version=': 0, '>': 87, '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': '4519426', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2567, 'n_words': 460}