Stxxl  1.4.0
include/stxxl/bits/io/wbtl_file.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  include/stxxl/bits/io/wbtl_file.h
00003  *
00004  *  a write-buffered-translation-layer pseudo file
00005  *
00006  *  Part of the STXXL. See http://stxxl.sourceforge.net
00007  *
00008  *  Copyright (C) 2008-2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00009  *  Copyright (C) 2009 Johannes Singler <singler@ira.uka.de>
00010  *
00011  *  Distributed under the Boost Software License, Version 1.0.
00012  *  (See accompanying file LICENSE_1_0.txt or copy at
00013  *  http://www.boost.org/LICENSE_1_0.txt)
00014  **************************************************************************/
00015 
00016 #ifndef STXXL_WBTL_FILE_HEADER
00017 #define STXXL_WBTL_FILE_HEADER
00018 
00019 #ifndef STXXL_HAVE_WBTL_FILE
00020 #define STXXL_HAVE_WBTL_FILE 1
00021 #endif
00022 
00023 #if STXXL_HAVE_WBTL_FILE
00024 
00025 #include <map>
00026 
00027 #include <stxxl/bits/io/disk_queued_file.h>
00028 
00029 
00030 __STXXL_BEGIN_NAMESPACE
00031 
00032 //! \addtogroup fileimpl
00033 //! \{
00034 
00035 //! \brief Implementation of file based on buffered writes and
00036 //!        block remapping via a translation layer.
00037 class wbtl_file : public disk_queued_file
00038 {
00039     typedef std::pair<offset_type, offset_type> place;
00040     typedef std::map<offset_type, offset_type> sortseq;
00041     typedef std::map<offset_type, place> place_map;
00042 
00043     // the physical disk used as backend
00044     file * storage;
00045     offset_type sz;
00046     size_type write_block_size;
00047 
00048     mutex mapping_mutex;
00049     // logical to physical address translation
00050     sortseq address_mapping;
00051     // physical to (logical address, size) translation
00052     place_map reverse_mapping;
00053     // list of free (physical) regions
00054     sortseq free_space;
00055     offset_type free_bytes;
00056 
00057     // the write buffers:
00058     // write_buffer[curbuf] is the current write buffer
00059     // write_buffer[1-curbuf] is the previous write buffer
00060     // buffer_address if the start offset on the backend file
00061     // curpos is the next writing position in write_buffer[curbuf]
00062     mutex buffer_mutex;
00063     char * write_buffer[2];
00064     offset_type buffer_address[2];
00065     int curbuf;
00066     size_type curpos;
00067     request_ptr backend_request;
00068 
00069     struct FirstFit : public std::binary_function<place, offset_type, bool>
00070     {
00071         bool operator () (
00072             const place & entry,
00073             const offset_type size) const
00074         {
00075             return (entry.second >= size);
00076         }
00077     };
00078 
00079 public:
00080     //! \brief constructs file object
00081     //! \param backend_file file object used as storage backend, will be deleted in ~wbtl_file()
00082     //! \param disk disk(file) identifier
00083     wbtl_file(
00084         file * backend_file,
00085         size_type write_buffer_size,
00086         int write_buffers = 2,
00087         int queue_id = DEFAULT_QUEUE,
00088         int allocator_id = NO_ALLOCATOR);
00089     ~wbtl_file();
00090     offset_type size();
00091     void set_size(offset_type newsize);
00092     void lock();
00093     void serve(const request * req) throw (io_error);
00094     void discard(offset_type offset, offset_type size);
00095     const char * io_type() const;
00096 
00097 private:
00098     void _add_free_region(offset_type offset, offset_type size);
00099 
00100 protected:
00101     void sread(void * buffer, offset_type offset, size_type bytes);
00102     void swrite(void * buffer, offset_type offset, size_type bytes);
00103     offset_type get_next_write_block();
00104     void check_corruption(offset_type region_pos, offset_type region_size,
00105                           sortseq::iterator pred, sortseq::iterator succ);
00106 };
00107 
00108 //! \}
00109 
00110 __STXXL_END_NAMESPACE
00111 
00112 #endif  // #if STXXL_HAVE_WBTL_FILE
00113 
00114 #endif  // !STXXL_WBTL_FILE_HEADER
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines