Stxxl  1.4.0
include/stxxl/bits/io/fileperblock_file.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  include/stxxl/bits/io/fileperblock_file.h
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2008, 2009 Johannes Singler <singler@ira.uka.de>
00007  *
00008  *  Distributed under the Boost Software License, Version 1.0.
00009  *  (See accompanying file LICENSE_1_0.txt or copy at
00010  *  http://www.boost.org/LICENSE_1_0.txt)
00011  **************************************************************************/
00012 
00013 #ifndef STXXL_FILEPERBLOCK_FILE_HEADER
00014 #define STXXL_FILEPERBLOCK_FILE_HEADER
00015 
00016 #include <string>
00017 #include <stxxl/bits/io/disk_queued_file.h>
00018 
00019 
00020 __STXXL_BEGIN_NAMESPACE
00021 
00022 //! \addtogroup fileimpl
00023 //! \{
00024 
00025 //! \brief Implementation of file based on other files, dynamically allocate one file per block.
00026 //! Allows for dynamic disk space consumption.
00027 template <class base_file_type>
00028 class fileperblock_file : public disk_queued_file
00029 {
00030 private:
00031     std::string filename_prefix;
00032     int mode;
00033     unsigned_type current_size;
00034     bool lock_file_created;
00035     base_file_type lock_file;
00036 
00037 protected:
00038     //! \brief Constructs a file name for a given block.
00039     std::string filename_for_block(unsigned_type offset);
00040 
00041 public:
00042     //! \brief constructs file object
00043     //! \param filename_prefix  filename prefix, numbering will be appended to it
00044     //! \param mode open mode, see \c file::open_modes
00045     //! \param disk disk(file) identifier
00046     fileperblock_file(
00047         const std::string & filename_prefix,
00048         int mode,
00049         int queue_id = DEFAULT_QUEUE,
00050         int allocator_id = NO_ALLOCATOR);
00051 
00052     virtual ~fileperblock_file();
00053 
00054     virtual void serve(const request * req) throw (io_error);
00055 
00056     //! \brief Changes the size of the file
00057     //! \param new_size value of the new file size
00058     virtual void set_size(offset_type new_size) { current_size = new_size; }
00059 
00060     //! \brief Returns size of the file
00061     //! \return file size in length
00062     virtual offset_type size() { return current_size; }
00063 
00064     virtual void lock();
00065 
00066     //! \brief Frees the specified region.
00067     //! Actually deletes the corresponding file if the whole thing is deleted.
00068     virtual void discard(offset_type offset, offset_type length);
00069 
00070     //! Rename the file corresponding to the offset such that it is out of reach for deleting.
00071     virtual void export_files(offset_type offset, offset_type length, std::string filename);
00072 
00073     const char * io_type() const;
00074 };
00075 
00076 //! \}
00077 
00078 __STXXL_END_NAMESPACE
00079 
00080 #endif // !STXXL_FILEPERBLOCK_FILE_HEADER
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines