Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bid.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * foxxll/mng/bid.hpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
5  *
6  * Copyright (C) 2002-2004 Roman Dementiev <[email protected]>
7  * Copyright (C) 2009, 2010 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2009 Johannes Singler <[email protected]>
9  * Copyright (C) 2013 Timo Bingmann <[email protected]>
10  * Copyright (C) 2018 Manuel Penschuck <[email protected]>
11  *
12  * Distributed under the Boost Software License, Version 1.0.
13  * (See accompanying file LICENSE_1_0.txt or copy at
14  * http://www.boost.org/LICENSE_1_0.txt)
15  **************************************************************************/
16 
17 #ifndef FOXXLL_MNG_BID_HEADER
18 #define FOXXLL_MNG_BID_HEADER
19 
20 #include <cstring>
21 #include <iomanip>
22 #include <ostream>
23 #include <sstream>
24 
25 #include <foxxll/common/utils.hpp>
26 #include <foxxll/io/file.hpp>
27 #include <foxxll/io/request.hpp>
28 #include <tlx/simple_vector.hpp>
29 
30 namespace foxxll {
31 
32 //! \addtogroup foxxll_mnglayer
33 //! \{
34 
35 //! Block identifier class.
36 //!
37 //! Stores block identity, given by file and offset within the file
38 template <size_t Size>
39 class BID
40 {
41 public:
42  //! Block size
43  static constexpr size_t size = Size;
44  //! Blocks size, given by the parameter
45  static constexpr size_t t_size = Size;
46 
47  //! pointer to the file of the block
48  file* storage = nullptr;
49  //! offset within the file of the block (uint64_t)
51 
52  BID() = default;
53 
54  BID(file* s, const external_size_type& o) : storage(s), offset(o) { }
55 
56  //! construction from another block size
57  template <size_t BlockSize>
58  explicit BID(const BID<BlockSize>& obj)
59  : storage(obj.storage), offset(obj.offset) { }
60 
61  //! assignment from another block size
62  template <size_t BlockSize>
64  {
65  storage = obj.storage;
66  offset = obj.offset;
67  return *this;
68  }
69 
70  bool valid() const
71  {
72  return storage != nullptr;
73  }
74 
75  bool is_managed() const
76  {
78  }
79 
80  //! Writes data to the disk(s).
81  request_ptr write(void* data, size_t data_size,
82  completion_handler on_complete = completion_handler())
83  {
84  return storage->awrite(data, offset, data_size, on_complete);
85  }
86 
87  //! Reads data from the disk(s).
88  request_ptr read(void* data, size_t data_size,
89  completion_handler on_complete = completion_handler())
90  {
91  return storage->aread(data, offset, data_size, on_complete);
92  }
93 
94  bool operator == (const BID<Size>& b) const
95  {
96  return storage == b.storage && offset == b.offset;
97  }
98 
99  bool operator != (const BID<Size>& b) const
100  {
101  return !operator == (b);
102  }
103 };
104 
105 /*!
106  * Specialization of block identifier class (BID) for variable size block size.
107  *
108  * Stores block identity, given by file, offset within the file, and size of the
109  * block
110  */
111 template <>
112 class BID<0>
113 {
114 public:
115  //! pointer to the file of the block
116  file* storage = nullptr;
117  //! offset within the file of the block (uint64_t)
119  //! size of the block in bytes
120  size_t size = 0;
121 
122  //! Blocks size, given by the parameter
123  static constexpr size_t t_size = 0;
124 
125  BID() = default;
126 
127  BID(file* f, const external_size_type& o, size_t s)
128  : storage(f), offset(o), size(s) { }
129 
130  bool valid() const
131  {
132  return (storage != nullptr);
133  }
134 
135  bool is_managed() const
136  {
138  }
139 
140  //! Writes data to the disk(s).
141  request_ptr write(void* data, size_t data_size,
142  completion_handler on_complete = completion_handler())
143  {
144  return storage->awrite(data, offset, data_size, on_complete);
145  }
146 
147  //! Reads data from the disk(s).
148  request_ptr read(void* data, size_t data_size,
149  completion_handler on_complete = completion_handler())
150  {
151  return storage->aread(data, offset, data_size, on_complete);
152  }
153 
154  bool operator == (const BID<0>& b) const
155  {
156  return storage == b.storage && offset == b.offset && size == b.size;
157  }
158 
159  bool operator != (const BID<0>& b) const
160  {
161  return !operator == (b);
162  }
163 };
164 
165 /**
166  * Prints a BID to an ostream using the following format
167  * \verbatim
168  * [0x12345678|0]0x00100000/0x00010000
169  * [file ptr|file id]offset/size
170  * \endverbatim
171  *
172  * \note
173  * Can be used to replace the obsolete FMT_BID macro
174  */
175 template <size_t BlockSize>
176 std::ostream& operator << (std::ostream& s, const BID<BlockSize>& bid)
177 {
178  std::stringstream ss;
179 
180  ss << "[" << static_cast<void*>(bid.storage) << "|";
181 
182  if (bid.storage) {
183  ss << bid.storage->get_allocator_id();
184  }
185  else {
186  ss << "?";
187  }
188 
189  ss << "]0x" << std::hex << std::setfill('0') << std::setw(8) << bid.offset
190  << "/0x" << std::setw(8) << bid.size << std::dec;
191 
192  s << ss.str();
193 
194  return s;
195 }
196 
197 template <size_t BlockSize>
199 
200 //! \}
201 
202 } // namespace foxxll
203 
204 #endif // !FOXXLL_MNG_BID_HEADER
205 
206 /**************************************************************************/
request_ptr write(void *data, size_t data_size, completion_handler on_complete=completion_handler())
Writes data to the disk(s).
Definition: bid.hpp:81
request_ptr read(void *data, size_t data_size, completion_handler on_complete=completion_handler())
Reads data from the disk(s).
Definition: bid.hpp:148
BID(file *s, const external_size_type &o)
Definition: bid.hpp:54
virtual int get_allocator_id() const =0
Returns the file's parallel disk block allocator number.
external_size_type offset
offset within the file of the block (uint64_t)
Definition: bid.hpp:118
Specialization of block identifier class (BID) for variable size block size.
Definition: bid.hpp:112
virtual request_ptr awrite(void *buffer, offset_type pos, size_type bytes, const completion_handler &on_complete=completion_handler())=0
Simpler non-growing vector without initialization.
request_ptr read(void *data, size_t data_size, completion_handler on_complete=completion_handler())
Reads data from the disk(s).
Definition: bid.hpp:88
bool operator!=(const BID< Size > &b) const
Definition: bid.hpp:99
external_size_type offset
offset within the file of the block (uint64_t)
Definition: bid.hpp:50
bool valid() const
Definition: bid.hpp:70
BID(file *f, const external_size_type &o, size_t s)
Definition: bid.hpp:127
request_ptr write(void *data, size_t data_size, completion_handler on_complete=completion_handler())
Writes data to the disk(s).
Definition: bid.hpp:141
virtual request_ptr aread(void *buffer, offset_type pos, size_type bytes, const completion_handler &on_complete=completion_handler())=0
tlx::delegate< void(request *r, bool success)> completion_handler
completion handler
Definition: request.hpp:46
BID()=default
file * storage
pointer to the file of the block
Definition: bid.hpp:116
High-performance smart pointer used as a wrapping reference counting pointer.
static constexpr size_t t_size
Blocks size, given by the parameter.
Definition: bid.hpp:45
BID & operator=(const BID< BlockSize > &obj)
assignment from another block size
Definition: bid.hpp:63
bool is_managed() const
Definition: bid.hpp:75
static const int NO_ALLOCATOR
Definition: file.hpp:91
bool operator==(const BID< Size > &b) const
Definition: bid.hpp:94
BID(const BID< BlockSize > &obj)
construction from another block size
Definition: bid.hpp:58
size_t size
size of the block in bytes
Definition: bid.hpp:120
file * storage
pointer to the file of the block
Definition: bid.hpp:48
bool is_managed() const
Definition: bid.hpp:135
bool valid() const
Definition: bid.hpp:130
uint64_t external_size_type
Definition: types.hpp:27
static constexpr size_t size
Block size.
Definition: bid.hpp:43