Stxxl  1.3.2
wbtl_file.h
1 /***************************************************************************
2  * include/stxxl/bits/io/wbtl_file.h
3  *
4  * a write-buffered-translation-layer pseudo file
5  *
6  * Part of the STXXL. See http://stxxl.sourceforge.net
7  *
8  * Copyright (C) 2008-2009 Andreas Beckmann <[email protected]>
9  * Copyright (C) 2009 Johannes Singler <[email protected]>
10  *
11  * Distributed under the Boost Software License, Version 1.0.
12  * (See accompanying file LICENSE_1_0.txt or copy at
13  * http://www.boost.org/LICENSE_1_0.txt)
14  **************************************************************************/
15 
16 #ifndef STXXL_WBTL_FILE_HEADER
17 #define STXXL_WBTL_FILE_HEADER
18 
19 #ifndef STXXL_HAVE_WBTL_FILE
20 #define STXXL_HAVE_WBTL_FILE 1
21 #endif
22 
23 #if STXXL_HAVE_WBTL_FILE
24 
25 #include <map>
26 
27 #include <stxxl/bits/io/disk_queued_file.h>
28 
29 
30 __STXXL_BEGIN_NAMESPACE
31 
34 
38 {
39  typedef std::pair<offset_type, offset_type> place;
40  typedef std::map<offset_type, offset_type> sortseq;
41  typedef std::map<offset_type, place> place_map;
42 
43  // the physical disk used as backend
44  file * storage;
45  offset_type sz;
46  size_type write_block_size;
47 
48  mutex mapping_mutex;
49  // logical to physical address translation
50  sortseq address_mapping;
51  // physical to (logical address, size) translation
52  place_map reverse_mapping;
53  // list of free (physical) regions
54  sortseq free_space;
55  offset_type free_bytes;
56 
57  // the write buffers:
58  // write_buffer[curbuf] is the current write buffer
59  // write_buffer[1-curbuf] is the previous write buffer
60  // buffer_address if the start offset on the backend file
61  // curpos is the next writing position in write_buffer[curbuf]
62  mutex buffer_mutex;
63  char * write_buffer[2];
64  offset_type buffer_address[2];
65  int curbuf;
66  size_type curpos;
67  request_ptr backend_request;
68 
69  struct FirstFit : public std::binary_function<place, offset_type, bool>
70  {
71  bool operator () (
72  const place & entry,
73  const offset_type size) const
74  {
75  return (entry.second >= size);
76  }
77  };
78 
79 public:
83  wbtl_file(
84  file * backend_file,
85  size_type write_buffer_size,
86  int write_buffers = 2,
87  int queue_id = DEFAULT_QUEUE,
88  int allocator_id = NO_ALLOCATOR);
89  ~wbtl_file();
90  offset_type size();
91  void set_size(offset_type newsize);
92  void lock();
93  void serve(const request * req) throw (io_error);
94  void discard(offset_type offset, offset_type size);
95  const char * io_type() const;
96 
97 private:
98  void _add_free_region(offset_type offset, offset_type size);
99 
100 protected:
101  void sread(void * buffer, offset_type offset, size_type bytes);
102  void swrite(void * buffer, offset_type offset, size_type bytes);
103  offset_type get_next_write_block();
104  void check_corruption(offset_type region_pos, offset_type region_size,
105  sortseq::iterator pred, sortseq::iterator succ);
106 };
107 
109 
110 __STXXL_END_NAMESPACE
111 
112 #endif // #if STXXL_HAVE_WBTL_FILE
113 
114 #endif // !STXXL_WBTL_FILE_HEADER
Defines interface of file.
Definition: file.h:90
void lock()
Locks file for reading and writing (acquires a lock in the file system)
Request with basic properties like file and offset.
Definition: request.h:39
offset_type size()
Returns size of the file.
wbtl_file(file *backend_file, size_type write_buffer_size, int write_buffers=2, int queue_id=DEFAULT_QUEUE, int allocator_id=NO_ALLOCATOR)
constructs file object
Implemented as reference counting smart pointer.
Definition: request_ptr.h:34
Implementation of file based on buffered writes and block remapping via a translation layer...
Definition: wbtl_file.h:37
const char * io_type() const
Identifies the type of I/O implementation.
Implementation of some file methods based on serving_request.
Definition: disk_queued_file.h:26