Thrill  0.1
dyn_block_reader.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/data/dyn_block_reader.hpp
3  *
4  * Dynamized instantiation of BlockReader which can polymorphically read from
5  * different block sources using the same object type.
6  *
7  * Part of Project Thrill - http://project-thrill.org
8  *
9  * Copyright (C) 2015 Timo Bingmann <[email protected]>
10  *
11  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
12  ******************************************************************************/
13 
14 #pragma once
15 #ifndef THRILL_DATA_DYN_BLOCK_READER_HEADER
16 #define THRILL_DATA_DYN_BLOCK_READER_HEADER
17 
19 
20 namespace thrill {
21 namespace data {
22 
23 //! \addtogroup data_layer
24 //! \{
25 
26 /*!
27  * This is a pure virtual base which will be used to fetch Blocks for the
28  * BlockReader from different sources.
29  */
31 {
32 public:
34 
35  virtual PinnedBlock NextBlock() = 0;
36 
37  //! set number of blocks to prefetch
38  virtual void Prefetch(size_t size) = 0;
39 };
40 
41 /*!
42  * This is the actual BlockSource used to instantiate BlockReader. The
43  * BlockSource holds a shared pointer to the polymorphic block source, which is
44  * derived from the above virtual base class.
45  *
46  * Think of this class being embedded into the BlockReader and delivering Blocks
47  * via the virtual function class from whatever is attached.
48  */
50 {
51 public:
53 
54  explicit DynBlockSource(
56  : block_source_ptr_(std::move(block_source_ptr)) { }
57 
59  return block_source_ptr_->NextBlock();
60  }
61 
62  void Prefetch(size_t size) {
63  return block_source_ptr_->Prefetch(size);
64  }
65 
66 private:
68 };
69 
70 //! Instantiation of BlockReader for reading from the polymorphic source.
72 
73 /*!
74  * Adapter class to wrap any existing BlockSource concept class into a
75  * DynBlockSourceInterface.
76  */
77 template <typename BlockSource>
79 {
80 public:
81  explicit DynBlockSourceAdapter(BlockSource&& block_source)
82  : block_source_(std::move(block_source)) { }
83 
84  //! non-copyable: delete copy-constructor
86  //! non-copyable: delete assignment operator
88  //! move-constructor: default
90  //! move-assignment operator: default
92 
94  return block_source_.NextBlock();
95  }
96 
97  void Prefetch(size_t size) final {
98  return block_source_.Prefetch(size);
99  }
100 
101 private:
102  BlockSource block_source_;
103 };
104 
105 /*!
106  * Method to construct a DynBlockSource from a non-polymorphic BlockSource. The
107  * variadic parameters are passed to the constructor of the existing
108  * BlockSource.
109  */
110 template <typename BlockSource, typename... Params>
112  return DynBlockSource(
114  BlockSource(std::forward<Params>(params) ...)));
115 }
116 
117 /*!
118  * Method to construct a DynBlockReader from a non-polymorphic BlockSource. The
119  * variadic parameters are passed to the constructor of the existing
120  * BlockSource.
121  */
122 template <typename BlockSource, typename... Params>
124  return DynBlockReader(
125  ConstructDynBlockSource<BlockSource>(std::forward<Params>(params) ...));
126 }
127 
128 //! \}
129 
130 } // namespace data
131 } // namespace thrill
132 
133 #endif // !THRILL_DATA_DYN_BLOCK_READER_HEADER
134 
135 /******************************************************************************/
tlx::CountingPtr< DynBlockSourceInterface > block_source_ptr_
DynBlockSource(tlx::CountingPtr< DynBlockSourceInterface > &&block_source_ptr)
ReferenceCounter & operator=(const ReferenceCounter &) noexcept
assignment operator, leaves pointers unchanged
STL namespace.
virtual PinnedBlock NextBlock()=0
DynBlockSourceAdapter(BlockSource &&block_source)
virtual void Prefetch(size_t size)=0
set number of blocks to prefetch
Adapter class to wrap any existing BlockSource concept class into a DynBlockSourceInterface.
void Prefetch(size_t size) final
set number of blocks to prefetch
This is a pure virtual base which will be used to fetch Blocks for the BlockReader from different sou...
BlockReader takes Block objects from BlockSource and allows reading of a) serializable Items or b) ar...
CountingPtr< Type > make_counting(Args &&... args)
method analogous to std::make_shared and std::make_unique.
High-performance smart pointer used as a wrapping reference counting pointer.
DynBlockSource ConstructDynBlockSource(Params &&... params)
Method to construct a DynBlockSource from a non-polymorphic BlockSource.
A pinned / pin-counted derivative of a Block.
Definition: block.hpp:157
list params
Definition: gen_data.py:30
BlockReader< DynBlockSource > DynBlockReader
Instantiation of BlockReader for reading from the polymorphic source.
Provides reference counting abilities for use with CountingPtr.
This is the actual BlockSource used to instantiate BlockReader.
DynBlockReader ConstructDynBlockReader(Params &&... params)
Method to construct a DynBlockReader from a non-polymorphic BlockSource.